/**
   * Calculates and sets the bounds of all the items in the specified container, subject to the
   * given constraint. The <code>Graphics2D</code> can be used by some items (particularly items
   * containing text) to calculate sizing parameters.
   *
   * @param container the container whose items are being arranged.
   * @param g2 the graphics device.
   * @param constraint the size constraint.
   * @return The size of the container after arrangement of the contents.
   */
  @Override
  public Size2D arrange(BlockContainer container, Graphics2D g2, RectangleConstraint constraint) {

    LengthConstraintType w = constraint.getWidthConstraintType();
    LengthConstraintType h = constraint.getHeightConstraintType();
    if (w == LengthConstraintType.NONE) {
      if (h == LengthConstraintType.NONE) {
        return arrangeNN(container, g2);
      } else if (h == LengthConstraintType.FIXED) {
        throw new RuntimeException("Not implemented.");
      } else if (h == LengthConstraintType.RANGE) {
        throw new RuntimeException("Not implemented.");
      }
    } else if (w == LengthConstraintType.FIXED) {
      if (h == LengthConstraintType.NONE) {
        return arrangeFN(container, g2, constraint);
      } else if (h == LengthConstraintType.FIXED) {
        throw new RuntimeException("Not implemented.");
      } else if (h == LengthConstraintType.RANGE) {
        throw new RuntimeException("Not implemented.");
      }
    } else if (w == LengthConstraintType.RANGE) {
      if (h == LengthConstraintType.NONE) {
        return arrangeRN(container, g2, constraint);
      } else if (h == LengthConstraintType.FIXED) {
        return arrangeRF(container, g2, constraint);
      } else if (h == LengthConstraintType.RANGE) {
        return arrangeRR(container, g2, constraint);
      }
    }
    throw new IllegalArgumentException("Unknown LengthConstraintType.");
  }
 /**
  * Performs an arrangement with a fixed width and a range for the height.
  *
  * @param container the container.
  * @param g2 the graphics device.
  * @param constraint the constraint.
  * @return The container size after the arrangement.
  */
 protected Size2D arrangeFR(
     BlockContainer container, Graphics2D g2, RectangleConstraint constraint) {
   Size2D size1 = arrangeFN(container, g2, constraint.getWidth());
   if (constraint.getHeightRange().contains(size1.getHeight())) {
     return size1;
   } else {
     double h = constraint.getHeightRange().constrain(size1.getHeight());
     RectangleConstraint c2 = constraint.toFixedHeight(h);
     return arrange(container, g2, c2);
   }
 }
 /**
  * Arranges the block with a range constraint on the width, and no constraint on the height.
  *
  * @param container the container.
  * @param g2 the graphics device.
  * @param constraint the constraint.
  * @return The size following the arrangement.
  */
 protected Size2D arrangeRN(
     BlockContainer container, Graphics2D g2, RectangleConstraint constraint) {
   // first arrange without constraints, then see if the width fits
   // within the required range...if not, call arrangeFN() at max width
   Size2D s1 = arrangeNN(container, g2);
   if (constraint.getWidthRange().contains(s1.width)) {
     return s1;
   } else {
     RectangleConstraint c = constraint.toFixedWidth(constraint.getWidthRange().getUpperBound());
     return arrangeFN(container, g2, c);
   }
 }
  /**
   * Arranges the blocks in the container with a range constraint on the width and a fixed height.
   *
   * @param container the container.
   * @param g2 the graphics device.
   * @param constraint the constraint.
   * @return The size following the arrangement.
   */
  protected Size2D arrangeRF(
      BlockContainer container, Graphics2D g2, RectangleConstraint constraint) {

    Size2D s = arrangeNF(container, g2, constraint);
    if (constraint.getWidthRange().contains(s.width)) {
      return s;
    } else {
      RectangleConstraint c =
          constraint.toFixedWidth(constraint.getWidthRange().constrain(s.getWidth()));
      return arrangeFF(container, g2, c);
    }
  }
  /**
   * Arranges the blocks in the container with a fixed with and a range constraint on the height.
   *
   * @param container the container.
   * @param g2 the graphics device.
   * @param constraint the constraint.
   * @return The size following the arrangement.
   */
  protected Size2D arrangeFR(
      BlockContainer container, Graphics2D g2, RectangleConstraint constraint) {

    Size2D s = arrangeFN(container, g2, constraint);
    if (constraint.getHeightRange().contains(s.height)) {
      return s;
    } else {
      RectangleConstraint c =
          constraint.toFixedHeight(constraint.getHeightRange().constrain(s.getHeight()));
      return arrangeFF(container, g2, c);
    }
  }
  /**
   * Arranges the blocks with the overall width and height to fit within specified ranges.
   *
   * @param container the container.
   * @param g2 the graphics device.
   * @param constraint the constraint.
   * @return The size after the arrangement.
   */
  protected Size2D arrangeRR(
      BlockContainer container, Graphics2D g2, RectangleConstraint constraint) {

    // first arrange without constraints, and see if this fits within
    // the required ranges...
    Size2D s1 = arrangeNN(container, g2);
    if (constraint.getWidthRange().contains(s1.width)) {
      return s1; // TODO: we didn't check the height yet
    } else {
      RectangleConstraint c = constraint.toFixedWidth(constraint.getWidthRange().getUpperBound());
      return arrangeFR(container, g2, c);
    }
  }
  /**
   * Arranges the blocks in the container with a fixed width and no height constraint.
   *
   * @param container the container.
   * @param g2 the graphics device.
   * @param constraint the constraint.
   * @return The size.
   */
  protected Size2D arrangeFN(
      BlockContainer container, Graphics2D g2, RectangleConstraint constraint) {

    List blocks = container.getBlocks();
    Block b = (Block) blocks.get(0);
    Size2D s = b.arrange(g2, RectangleConstraint.NONE);
    double width = constraint.getWidth();
    Rectangle2D bounds = new Rectangle2D.Double((width - s.width) / 2.0, 0.0, s.width, s.height);
    b.setBounds(bounds);
    return new Size2D((width - s.width) / 2.0, s.height);
  }
  /**
   * Arranges the blocks in the container with a fixed width and no height constraint.
   *
   * @param container the container.
   * @param constraint the constraint.
   * @param g2 the graphics device.
   * @return The size.
   */
  protected Size2D arrangeFN(
      BlockContainer container, Graphics2D g2, RectangleConstraint constraint) {

    List blocks = container.getBlocks();
    double width = constraint.getWidth();

    double x = 0.0;
    double y = 0.0;
    double maxHeight = 0.0;
    List itemsInRow = new ArrayList();
    for (int i = 0; i < blocks.size(); i++) {
      Block block = (Block) blocks.get(i);
      Size2D size = block.arrange(g2, RectangleConstraint.NONE);
      if (x + size.width <= width) {
        itemsInRow.add(block);
        block.setBounds(new Rectangle2D.Double(x, y, size.width, size.height));
        x = x + size.width + this.horizontalGap;
        maxHeight = Math.max(maxHeight, size.height);
      } else {
        if (itemsInRow.isEmpty()) {
          // place in this row (truncated) anyway
          block.setBounds(
              new Rectangle2D.Double(x, y, Math.min(size.width, width - x), size.height));
          x = 0.0;
          y = y + size.height + this.verticalGap;
        } else {
          // start new row
          itemsInRow.clear();
          x = 0.0;
          y = y + maxHeight + this.verticalGap;
          maxHeight = size.height;
          block.setBounds(new Rectangle2D.Double(x, y, Math.min(size.width, width), size.height));
          x = size.width + this.horizontalGap;
          itemsInRow.add(block);
        }
      }
    }
    return new Size2D(constraint.getWidth(), y + maxHeight);
  }
示例#9
0
 /**
  * Returns a constraint for the content of this block that will result in the bounds of the block
  * matching the specified constraint.
  *
  * @param c the outer constraint (<code>null</code> not permitted).
  * @return The content constraint.
  */
 protected RectangleConstraint toContentConstraint(RectangleConstraint c) {
   ParamChecks.nullNotPermitted(c, "c");
   if (c.equals(RectangleConstraint.NONE)) {
     return c;
   }
   double w = c.getWidth();
   Range wr = c.getWidthRange();
   double h = c.getHeight();
   Range hr = c.getHeightRange();
   double ww = trimToContentWidth(w);
   double hh = trimToContentHeight(h);
   Range wwr = trimToContentWidth(wr);
   Range hhr = trimToContentHeight(hr);
   return new RectangleConstraint(
       ww, wwr, c.getWidthConstraintType(), hh, hhr, c.getHeightConstraintType());
 }
 /**
  * Arranges the items in the specified container, subject to the given constraint.
  *
  * @param container the container.
  * @param g2 the graphics device.
  * @param constraint the constraint.
  * @return The block size.
  */
 @Override
 public Size2D arrange(BlockContainer container, Graphics2D g2, RectangleConstraint constraint) {
   RectangleConstraint contentConstraint = container.toContentConstraint(constraint);
   Size2D contentSize = null;
   LengthConstraintType w = contentConstraint.getWidthConstraintType();
   LengthConstraintType h = contentConstraint.getHeightConstraintType();
   if (w == LengthConstraintType.NONE) {
     if (h == LengthConstraintType.NONE) {
       contentSize = arrangeNN(container, g2);
     } else if (h == LengthConstraintType.FIXED) {
       throw new RuntimeException("Not implemented.");
     } else if (h == LengthConstraintType.RANGE) {
       throw new RuntimeException("Not implemented.");
     }
   } else if (w == LengthConstraintType.FIXED) {
     if (h == LengthConstraintType.NONE) {
       contentSize = arrangeFN(container, g2, constraint.getWidth());
     } else if (h == LengthConstraintType.FIXED) {
       contentSize = arrangeFF(container, g2, constraint);
     } else if (h == LengthConstraintType.RANGE) {
       contentSize = arrangeFR(container, g2, constraint);
     }
   } else if (w == LengthConstraintType.RANGE) {
     if (h == LengthConstraintType.NONE) {
       throw new RuntimeException("Not implemented.");
     } else if (h == LengthConstraintType.FIXED) {
       throw new RuntimeException("Not implemented.");
     } else if (h == LengthConstraintType.RANGE) {
       contentSize =
           arrangeRR(container, constraint.getWidthRange(), constraint.getHeightRange(), g2);
     }
   }
   return new Size2D(
       container.calculateTotalWidth(contentSize.getWidth()),
       container.calculateTotalHeight(contentSize.getHeight()));
 }
示例#11
0
 /**
  * Arranges the contents of the block, within the given constraints, and returns the block size.
  *
  * @param g2 the graphics device.
  * @param constraint the constraint (<code>null</code> not permitted).
  * @return The block size (in Java2D units, never <code>null</code>).
  */
 public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
   Size2D base = new Size2D(getWidth(), getHeight());
   return constraint.calculateConstrainedSize(base);
 }
  /**
   * Arranges the items within a container.
   *
   * @param container the container.
   * @param constraint the constraint.
   * @param g2 the graphics device.
   * @return The container size after the arrangement.
   */
  protected Size2D arrangeFF(
      BlockContainer container, Graphics2D g2, RectangleConstraint constraint) {
    double[] w = new double[5];
    double[] h = new double[5];
    w[0] = constraint.getWidth();
    if (this.topBlock != null) {
      RectangleConstraint c1 =
          new RectangleConstraint(
              w[0],
              null,
              LengthConstraintType.FIXED,
              0.0,
              new Range(0.0, constraint.getHeight()),
              LengthConstraintType.RANGE);
      Size2D size = this.topBlock.arrange(g2, c1);
      h[0] = size.height;
    }
    w[1] = w[0];
    if (this.bottomBlock != null) {
      RectangleConstraint c2 =
          new RectangleConstraint(
              w[0],
              null,
              LengthConstraintType.FIXED,
              0.0,
              new Range(0.0, constraint.getHeight() - h[0]),
              LengthConstraintType.RANGE);
      Size2D size = this.bottomBlock.arrange(g2, c2);
      h[1] = size.height;
    }
    h[2] = constraint.getHeight() - h[1] - h[0];
    if (this.leftBlock != null) {
      RectangleConstraint c3 =
          new RectangleConstraint(
              0.0,
              new Range(0.0, constraint.getWidth()),
              LengthConstraintType.RANGE,
              h[2],
              null,
              LengthConstraintType.FIXED);
      Size2D size = this.leftBlock.arrange(g2, c3);
      w[2] = size.width;
    }
    h[3] = h[2];
    if (this.rightBlock != null) {
      RectangleConstraint c4 =
          new RectangleConstraint(
              0.0,
              new Range(0.0, Math.max(constraint.getWidth() - w[2], 0.0)),
              LengthConstraintType.RANGE,
              h[2],
              null,
              LengthConstraintType.FIXED);
      Size2D size = this.rightBlock.arrange(g2, c4);
      w[3] = size.width;
    }
    h[4] = h[2];
    w[4] = constraint.getWidth() - w[3] - w[2];
    RectangleConstraint c5 = new RectangleConstraint(w[4], h[4]);
    if (this.centerBlock != null) {
      this.centerBlock.arrange(g2, c5);
    }

    if (this.topBlock != null) {
      this.topBlock.setBounds(new Rectangle2D.Double(0.0, 0.0, w[0], h[0]));
    }
    if (this.bottomBlock != null) {
      this.bottomBlock.setBounds(new Rectangle2D.Double(0.0, h[0] + h[2], w[1], h[1]));
    }
    if (this.leftBlock != null) {
      this.leftBlock.setBounds(new Rectangle2D.Double(0.0, h[0], w[2], h[2]));
    }
    if (this.rightBlock != null) {
      this.rightBlock.setBounds(new Rectangle2D.Double(w[2] + w[4], h[0], w[3], h[3]));
    }
    if (this.centerBlock != null) {
      this.centerBlock.setBounds(new Rectangle2D.Double(w[2], h[0], w[4], h[4]));
    }
    return new Size2D(constraint.getWidth(), constraint.getHeight());
  }