Ejemplo n.º 1
0
  /**
   * Returns the component that corresponds to the given constraint location based on the target
   * <code>Container</code>'s component orientation. Components added with the relative constraints
   * <code>PAGE_START</code>, <code>PAGE_END</code>, <code>LINE_START</code>, and <code>LINE_END
   * </code> take precedence over components added with the explicit constraints <code>NORTH</code>,
   * <code>SOUTH</code>, <code>WEST</code>, and <code>EAST</code>. The <code>Container</code>'s
   * component orientation is used to determine the location of components added with <code>
   * LINE_START</code> and <code>LINE_END</code>.
   *
   * @param constraints the desired absolute position, one of <code>CENTER</code>, <code>NORTH
   *     </code>, <code>SOUTH</code>, <code>EAST</code>, <code>WEST</code>
   * @param target the {@code Container} used to obtain the constraint location based on the target
   *     {@code Container}'s component orientation.
   * @return the component at the given location, or <code>null</code> if the location is empty
   * @exception IllegalArgumentException if the constraint object is not one of the five specified
   *     constants
   * @exception NullPointerException if the target parameter is null
   * @see #addLayoutComponent(java.awt.Component, java.lang.Object)
   * @since 1.5
   */
  public Component getLayoutComponent(Container target, Object constraints) {
    boolean ltr = target.getComponentOrientation().isLeftToRight();
    Component result = null;

    if (NORTH.equals(constraints)) {
      result = (firstLine != null) ? firstLine : north;
    } else if (SOUTH.equals(constraints)) {
      result = (lastLine != null) ? lastLine : south;
    } else if (WEST.equals(constraints)) {
      result = ltr ? firstItem : lastItem;
      if (result == null) {
        result = west;
      }
    } else if (EAST.equals(constraints)) {
      result = ltr ? lastItem : firstItem;
      if (result == null) {
        result = east;
      }
    } else if (CENTER.equals(constraints)) {
      result = center;
    } else {
      throw new IllegalArgumentException(
          "cannot get component: invalid constraint: " + constraints);
    }

    return result;
  }
Ejemplo n.º 2
0
  /**
   * Constructs a <code>BoxLayout</code> object.
   *
   * @param container The container that needs to be laid out.
   * @param way The orientation of the components.
   * @exception AWTError If way has an invalid value.
   */
  public BoxLayout(Container container, int way) {
    int width = 0;
    int height = 0;
    ComponentOrientation orientation = container.getComponentOrientation();

    this.container = container;
    this.way = way;

    switch (way) {
      case X_AXIS:
        width = 1;
        break;
      case Y_AXIS:
        height = 1;
        break;
      case LINE_AXIS:
        if (orientation.isHorizontal()) height = 1;
        else width = 1;
        break;
      case PAGE_AXIS:
        if (!orientation.isHorizontal()) height = 1;
        else width = 1;
        break;
      default:
        throw new AWTError("Invalid value for way");
    }

    grid = new GridLayout(width, height);
  }
Ejemplo n.º 3
0
  /**
   * Constructs an instance of <code>AbstractFormBuilder</code> for the given FormLayout and layout
   * container.
   *
   * @param layout the {@link FormLayout} to use
   * @param container the layout container
   * @throws NullPointerException if the layout or container is null
   */
  public AbstractFormBuilder(FormLayout layout, Container container) {
    if (layout == null) throw new NullPointerException("The layout must not be null.");

    if (container == null) throw new NullPointerException("The layout container must not be null.");

    this.container = container;
    this.layout = layout;

    container.setLayout(layout);
    currentCellConstraints = new CellConstraints();
    ComponentOrientation orientation = container.getComponentOrientation();
    leftToRight = orientation.isLeftToRight() || !orientation.isHorizontal();
  }
Ejemplo n.º 4
0
 private int getOrientation(Container container) {
   if (!useOrientation) {
     return SwingConstants.CENTER;
   }
   if (container.getComponentOrientation().isLeftToRight()) {
     return orientation;
   }
   switch (orientation) {
     case SwingConstants.LEFT:
       return SwingConstants.RIGHT;
     case SwingConstants.RIGHT:
       return SwingConstants.LEFT;
     case SwingConstants.CENTER:
       return SwingConstants.CENTER;
   }
   return SwingConstants.LEFT;
 }
Ejemplo n.º 5
0
 public void layoutContainer(Container parent) {
   Insets insets = parent.getInsets();
   if (parent.getComponentOrientation().isLeftToRight()) {
     int x = insets.left;
     for (Component component : parent.getComponents()) {
       Dimension ps = component.getPreferredSize();
       component.setBounds(
           x, insets.top, ps.width, parent.getHeight() - insets.top - insets.bottom);
       x += ps.width - overlap;
     }
   } else {
     int x = parent.getWidth() - insets.right;
     for (Component component : parent.getComponents()) {
       Dimension ps = component.getPreferredSize();
       component.setBounds(
           x - ps.width, insets.top, ps.width, parent.getHeight() - insets.top - insets.bottom);
       x += overlap - ps.width;
     }
   }
 }
Ejemplo n.º 6
0
  /**
   * Lays out the container argument using this border layout.
   *
   * <p>This method actually reshapes the components in the specified container in order to satisfy
   * the constraints of this <code>BorderLayout</code> object. The <code>NORTH</code> and <code>
   * SOUTH</code> components, if any, are placed at the top and bottom of the container,
   * respectively. The <code>WEST</code> and <code>EAST</code> components are then placed on the
   * left and right, respectively. Finally, the <code>CENTER</code> object is placed in any
   * remaining space in the middle.
   *
   * <p>Most applications do not call this method directly. This method is called when a container
   * calls its <code>doLayout</code> method.
   *
   * @param target the container in which to do the layout.
   * @see java.awt.Container
   * @see java.awt.Container#doLayout()
   */
  public void layoutContainer(Container target) {
    synchronized (target.getTreeLock()) {
      Insets insets = target.getInsets();
      int top = insets.top;
      int bottom = target.getHeight() - insets.bottom;
      int left = insets.left;
      int right = target.getWidth() - insets.right;

      boolean ltr = target.getComponentOrientation().isLeftToRight();
      Component c = null;

      if ((c = getChild(NORTH, ltr)) != null) {
        c.setSize(right - left, c.getHeight());
        Dimension d = c.getPreferredSize();
        c.setBounds(left, top, right - left, d.height);
        top += d.height + vgap;
      }
      if ((c = getChild(SOUTH, ltr)) != null) {
        c.setSize(right - left, c.getHeight());
        Dimension d = c.getPreferredSize();
        c.setBounds(left, bottom - d.height, right - left, d.height);
        bottom -= d.height + vgap;
      }
      if ((c = getChild(EAST, ltr)) != null) {
        c.setSize(c.getWidth(), bottom - top);
        Dimension d = c.getPreferredSize();
        c.setBounds(right - d.width, top, d.width, bottom - top);
        right -= d.width + hgap;
      }
      if ((c = getChild(WEST, ltr)) != null) {
        c.setSize(c.getWidth(), bottom - top);
        Dimension d = c.getPreferredSize();
        c.setBounds(left, top, d.width, bottom - top);
        left += d.width + hgap;
      }
      if ((c = getChild(CENTER, ltr)) != null) {
        c.setBounds(left, top, right - left, bottom - top);
      }
    }
  }
Ejemplo n.º 7
0
  /**
   * Determines the preferred size of the <code>target</code> container using this layout manager,
   * based on the components in the container.
   *
   * <p>Most applications do not call this method directly. This method is called when a container
   * calls its <code>getPreferredSize</code> method.
   *
   * @param target the container in which to do the layout.
   * @return the preferred dimensions to lay out the subcomponents of the specified container.
   * @see java.awt.Container
   * @see java.awt.BorderLayout#minimumLayoutSize
   * @see java.awt.Container#getPreferredSize()
   */
  public Dimension preferredLayoutSize(Container target) {
    synchronized (target.getTreeLock()) {
      Dimension dim = new Dimension(0, 0);

      boolean ltr = target.getComponentOrientation().isLeftToRight();
      Component c = null;

      if ((c = getChild(EAST, ltr)) != null) {
        Dimension d = c.getPreferredSize();
        dim.width += d.width + hgap;
        dim.height = Math.max(d.height, dim.height);
      }
      if ((c = getChild(WEST, ltr)) != null) {
        Dimension d = c.getPreferredSize();
        dim.width += d.width + hgap;
        dim.height = Math.max(d.height, dim.height);
      }
      if ((c = getChild(CENTER, ltr)) != null) {
        Dimension d = c.getPreferredSize();
        dim.width += d.width;
        dim.height = Math.max(d.height, dim.height);
      }
      if ((c = getChild(NORTH, ltr)) != null) {
        Dimension d = c.getPreferredSize();
        dim.width = Math.max(d.width, dim.width);
        dim.height += d.height + vgap;
      }
      if ((c = getChild(SOUTH, ltr)) != null) {
        Dimension d = c.getPreferredSize();
        dim.width = Math.max(d.width, dim.width);
        dim.height += d.height + vgap;
      }

      Insets insets = target.getInsets();
      dim.width += insets.left + insets.right;
      dim.height += insets.top + insets.bottom;

      return dim;
    }
  }
  /*--------------------------------------------------------------------------*/
  public void layoutContainer(Container target) {
    synchronized (target.getTreeLock()) {
      Insets insets = target.getInsets();
      int maxWidth = target.getSize().width - (insets.left + insets.right + hgap * 2);
      int nMembers = target.getComponentCount();
      int x = 0;
      int y = insets.top + vgap;
      int rowh = 0;
      int start = 0;

      boolean ltr = target.getComponentOrientation().isLeftToRight();

      for (int i = 0; i < nMembers; i++) {
        Component m = target.getComponent(i);

        if (m.isVisible()) {
          Dimension d = m.getPreferredSize();
          m.setSize(d.width, d.height);

          if ((x == 0) || ((x + d.width) <= maxWidth)) {
            if (x > 0) {
              x += hgap;
            }
            x += d.width;
            rowh = Math.max(rowh, d.height);
          } else {
            moveComponents(target, insets.left, y, maxWidth - x, rowh, start, i, ltr);
            x = d.width;
            y += vgap + rowh;
            rowh = d.height;
            start = i;
          }
        }
      }

      moveComponents(target, insets.left, y, maxWidth - x, rowh, start, nMembers, ltr);
    }
  }
Ejemplo n.º 9
0
    public void layoutContainer(Container container) {
      Component[] children = container.getComponents();

      if (children != null && children.length > 0) {
        int numChildren = children.length;
        Insets insets = container.getInsets();
        int maxWidth = 0;
        int maxHeight = 0;
        int totalButtonWidth = 0;
        int x = 0;
        int xOffset = 0;
        boolean ltr = container.getComponentOrientation().isLeftToRight();
        boolean reverse = (ltr) ? reverseButtons : !reverseButtons;

        for (int counter = 0; counter < numChildren; counter++) {
          Dimension pref = children[counter].getPreferredSize();
          maxWidth = Math.max(maxWidth, pref.width);
          maxHeight = Math.max(maxHeight, pref.height);
          totalButtonWidth += pref.width;
        }
        if (getSyncAllWidths()) {
          totalButtonWidth = maxWidth * numChildren;
        }
        totalButtonWidth += (numChildren - 1) * padding;

        switch (getOrientation(container)) {
          case SwingConstants.LEFT:
            x = insets.left;
            break;
          case SwingConstants.RIGHT:
            x = container.getWidth() - insets.right - totalButtonWidth;
            break;
          case SwingConstants.CENTER:
            if (getCentersChildren() || numChildren < 2) {
              x = (container.getWidth() - totalButtonWidth) / 2;
            } else {
              x = insets.left;
              if (getSyncAllWidths()) {
                xOffset =
                    (container.getWidth() - insets.left - insets.right - totalButtonWidth)
                            / (numChildren - 1)
                        + maxWidth;
              } else {
                xOffset =
                    (container.getWidth() - insets.left - insets.right - totalButtonWidth)
                        / (numChildren - 1);
              }
            }
            break;
        }

        for (int counter = 0; counter < numChildren; counter++) {
          int index = (reverse) ? numChildren - counter - 1 : counter;
          Dimension pref = children[index].getPreferredSize();

          if (getSyncAllWidths()) {
            children[index].setBounds(x, insets.top, maxWidth, maxHeight);
          } else {
            children[index].setBounds(x, insets.top, pref.width, pref.height);
          }
          if (xOffset != 0) {
            x += xOffset;
          } else {
            x += children[index].getWidth() + padding;
          }
        }
      }
    }
Ejemplo n.º 10
0
  /**
   * This method is obsolete and supplied for backwards compatability only; new code should call
   * {@link #arrangeGrid(Container) arrangeGrid} instead.
   */
  protected void ArrangeGrid(Container parent) {
    Component comp;
    int compindex;
    GridBagConstraints constraints;
    Insets insets = parent.getInsets();
    Component components[] = parent.getComponents();
    Dimension d;
    Rectangle r = new Rectangle();
    int i, diffw, diffh;
    double weight;
    GridBagLayoutInfo info;

    rightToLeft = !parent.getComponentOrientation().isLeftToRight();

    /*
     * If the parent has no slaves anymore, then don't do anything
     * at all:  just leave the parent's size as-is.
     */
    if (components.length == 0
        && (columnWidths == null || columnWidths.length == 0)
        && (rowHeights == null || rowHeights.length == 0)) {
      return;
    }

    /*
     * Pass #1: scan all the slaves to figure out the total amount
     * of space needed.
     */

    info = getLayoutInfo(parent, PREFERREDSIZE);
    d = getMinSize(parent, info);

    if (parent.width < d.width || parent.height < d.height) {
      info = getLayoutInfo(parent, MINSIZE);
      d = getMinSize(parent, info);
    }

    layoutInfo = info;
    r.width = d.width;
    r.height = d.height;

    /*
     * DEBUG
     *
     * DumpLayoutInfo(info);
     * for (compindex = 0 ; compindex < components.length ; compindex++) {
     * comp = components[compindex];
     * if (!comp.isVisible())
     *	continue;
     * constraints = lookupConstraints(comp);
     * DumpConstraints(constraints);
     * }
     * System.out.println("minSize " + r.width + " " + r.height);
     */

    /*
     * If the current dimensions of the window don't match the desired
     * dimensions, then adjust the minWidth and minHeight arrays
     * according to the weights.
     */

    diffw = parent.width - r.width;
    if (diffw != 0) {
      weight = 0.0;
      for (i = 0; i < info.width; i++) weight += info.weightX[i];
      if (weight > 0.0) {
        for (i = 0; i < info.width; i++) {
          int dx = (int) ((((double) diffw) * info.weightX[i]) / weight);
          info.minWidth[i] += dx;
          r.width += dx;
          if (info.minWidth[i] < 0) {
            r.width -= info.minWidth[i];
            info.minWidth[i] = 0;
          }
        }
      }
      diffw = parent.width - r.width;
    } else {
      diffw = 0;
    }

    diffh = parent.height - r.height;
    if (diffh != 0) {
      weight = 0.0;
      for (i = 0; i < info.height; i++) weight += info.weightY[i];
      if (weight > 0.0) {
        for (i = 0; i < info.height; i++) {
          int dy = (int) ((((double) diffh) * info.weightY[i]) / weight);
          info.minHeight[i] += dy;
          r.height += dy;
          if (info.minHeight[i] < 0) {
            r.height -= info.minHeight[i];
            info.minHeight[i] = 0;
          }
        }
      }
      diffh = parent.height - r.height;
    } else {
      diffh = 0;
    }

    /*
     * DEBUG
     *
     * System.out.println("Re-adjusted:");
     * DumpLayoutInfo(info);
     */

    /*
     * Now do the actual layout of the slaves using the layout information
     * that has been collected.
     */

    info.startx = diffw / 2 + insets.left;
    info.starty = diffh / 2 + insets.top;

    for (compindex = 0; compindex < components.length; compindex++) {
      comp = components[compindex];
      if (!comp.isVisible()) continue;
      constraints = lookupConstraints(comp);

      if (!rightToLeft) {
        r.x = info.startx;
        for (i = 0; i < constraints.tempX; i++) r.x += info.minWidth[i];
      } else {
        r.x = parent.width - (diffw / 2 + insets.right);
        for (i = 0; i < constraints.tempX; i++) r.x -= info.minWidth[i];
      }

      r.y = info.starty;
      for (i = 0; i < constraints.tempY; i++) r.y += info.minHeight[i];

      r.width = 0;
      for (i = constraints.tempX; i < (constraints.tempX + constraints.tempWidth); i++) {
        r.width += info.minWidth[i];
      }

      r.height = 0;
      for (i = constraints.tempY; i < (constraints.tempY + constraints.tempHeight); i++) {
        r.height += info.minHeight[i];
      }

      adjustForGravity(constraints, r);

      /* fix for 4408108 - components were being created outside of the container */
      if (r.x < 0) {
        r.width -= r.x;
        r.x = 0;
      }

      if (r.y < 0) {
        r.height -= r.y;
        r.y = 0;
      }

      /*
       * If the window is too small to be interesting then
       * unmap it.  Otherwise configure it and then make sure
       * it's mapped.
       */

      if ((r.width <= 0) || (r.height <= 0)) {
        comp.setBounds(0, 0, 0, 0);
      } else {
        if (comp.x != r.x || comp.y != r.y || comp.width != r.width || comp.height != r.height) {
          comp.setBounds(r.x, r.y, r.width, r.height);
        }
      }
    }
  }
Ejemplo n.º 11
0
    @Override
    public void layoutContainer(Container target) {
      synchronized (target.getTreeLock()) {
        Insets insets = target.getInsets();
        int maxwidth = target.getWidth() - (insets.left + insets.right + getHgap() * 2);
        int nmembers = target.getComponentCount();
        int x = 0, y = insets.top + getVgap();
        int rowh = 0, start = 0;

        boolean ltr = target.getComponentOrientation().isLeftToRight();
        SizeRequirements[] xChildren = new SizeRequirements[nmembers];
        SizeRequirements[] yChildren = new SizeRequirements[nmembers];
        for (int i = 0; i < nmembers; i++) {
          Component c = target.getComponent(i);
          if (!c.isVisible()) {
            xChildren[i] = new SizeRequirements(0, 0, 0, c.getAlignmentX());
            yChildren[i] = new SizeRequirements(0, 0, 0, c.getAlignmentY());
          } else {
            Dimension min = c.getMinimumSize();
            Dimension typ = c.getPreferredSize();
            Dimension max = c.getMaximumSize();
            xChildren[i] = new SizeRequirements(min.width, typ.width, max.width, c.getAlignmentX());
            yChildren[i] =
                new SizeRequirements(min.height, typ.height, max.height, c.getAlignmentY());

            if ((x == 0) || ((x + typ.width) <= maxwidth)) {
              if (x > 0) {
                x += getHgap();
              }
              x += typ.width;
              rowh = Math.max(rowh, typ.height);
            } else {
              layoutComponents(
                  target,
                  insets.left + getHgap(),
                  y,
                  maxwidth,
                  rowh,
                  xChildren,
                  yChildren,
                  start,
                  i,
                  ltr);

              x = typ.width;
              y += getVgap() + rowh;
              rowh = typ.height;
              start = i;
            }
          }
        }
        layoutComponents(
            target,
            insets.left + getHgap(),
            y,
            maxwidth,
            rowh,
            xChildren,
            yChildren,
            start,
            nmembers,
            ltr);
      }
    }
Ejemplo n.º 12
0
  public void layoutContainer(Container container) {
    Component[] children = container.getComponents();

    if (children != null && children.length > 0) {
      int numChildren = children.length;
      Dimension[] sizes = new Dimension[numChildren];
      int counter;
      int yLocation = container.getInsets().top;

      if (syncAllWidths) {
        int maxWidth = getMinimumButtonWidth();

        for (counter = 0; counter < numChildren; counter++) {
          sizes[counter] = children[counter].getPreferredSize();
          maxWidth = Math.max(maxWidth, sizes[counter].width);
        }

        int xLocation;
        int xOffset;

        if (getCentersChildren()) {
          xLocation =
              (container.getSize().width - (maxWidth * numChildren + (numChildren - 1) * padding))
                  / 2;
          xOffset = padding + maxWidth;
        } else {
          if (numChildren > 1) {
            xLocation = 0;
            xOffset =
                (container.getSize().width - (maxWidth * numChildren)) / (numChildren - 1)
                    + maxWidth;
          } else {
            xLocation = (container.getSize().width - maxWidth) / 2;
            xOffset = 0;
          }
        }
        boolean ltr = container.getComponentOrientation().isLeftToRight();
        for (counter = 0; counter < numChildren; counter++) {
          int index = (ltr) ? counter : numChildren - counter - 1;
          children[index].setBounds(xLocation, yLocation, maxWidth, sizes[index].height);
          xLocation += xOffset;
        }
      } else {
        int totalWidth = 0;

        for (counter = 0; counter < numChildren; counter++) {
          sizes[counter] = children[counter].getPreferredSize();
          totalWidth += sizes[counter].width;
        }
        totalWidth += ((numChildren - 1) * padding);

        boolean cc = getCentersChildren();
        int xOffset;
        int xLocation;

        if (cc) {
          xLocation = (container.getSize().width - totalWidth) / 2;
          xOffset = padding;
        } else {
          if (numChildren > 1) {
            xOffset = (container.getSize().width - totalWidth) / (numChildren - 1);
            xLocation = 0;
          } else {
            xLocation = (container.getSize().width - totalWidth) / 2;
            xOffset = 0;
          }
        }

        boolean ltr = container.getComponentOrientation().isLeftToRight();
        for (counter = 0; counter < numChildren; counter++) {
          int index = (ltr) ? counter : numChildren - counter - 1;
          children[index].setBounds(xLocation, yLocation, sizes[index].width, sizes[index].height);
          xLocation += xOffset + sizes[index].width;
        }
      }
    }
  }