示例#1
0
  /**
   * This method serves to help clients sort blocks within a page in some manner.
   *
   * @param page
   * @param topLevelBlocks
   * @requires page != null && topLevelBlocks != null
   * @modifies the location of all topLevelBlocks
   * @effects sort the topLevelBlocks and move them to an order location on the page
   */
  protected static void sortBlockStacks(Page page, Collection<RenderableBlock> topLevelBlocks) {
    blocksToArrange.clear();
    positioningBounds.setBounds(
        BUFFER_BETWEEN_BLOCKS, BUFFER_BETWEEN_BLOCKS, 0, BUFFER_BETWEEN_BLOCKS);
    // created an ordered list of blocks based on x-coordinate position
    blocksToArrange.addAll(topLevelBlocks);

    // Naively places blocks from top to bottom, left to right.
    for (RenderableBlock block : blocksToArrange) {
      Rectangle bounds = block.getStackBounds();
      if (positioningBounds.height + bounds.height > page.getJComponent().getHeight()) {
        // need to go to next column
        positioningBounds.x = positioningBounds.x + positioningBounds.width + BUFFER_BETWEEN_BLOCKS;
        positioningBounds.width = 0;
        positioningBounds.height = BUFFER_BETWEEN_BLOCKS;
      }
      block.setLocation(positioningBounds.x, positioningBounds.height);

      // sets the x and y position for when workspace is unzoomed
      block.setUnzoomedX(block.calculateUnzoomedX(positioningBounds.x));
      block.setUnzoomedY(block.calculateUnzoomedY(positioningBounds.height));
      block.moveConnectedBlocks();

      // update positioning bounds
      positioningBounds.width = Math.max(positioningBounds.width, bounds.width);
      positioningBounds.height = positioningBounds.height + bounds.height + BUFFER_BETWEEN_BLOCKS;

      if (positioningBounds.x + positioningBounds.width > page.getJComponent().getWidth()) {
        // resize page to the difference
        page.addPixelWidth(
            positioningBounds.x + positioningBounds.width - page.getJComponent().getWidth());
      }
    }
  }