springlayout.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 833 行 · 第 1/2 页

JAVA
833
字号
   * This method is usually only called by a {@link java.awt.Container}s add   * method.   *   * @param component the component to be added.   * @param constraint the constraint to be set.   */  public void addLayoutComponent(Component component, Object constraint)  {    constraintsMap.put(component, constraint);  }  /**   * Adds a layout component and a constraint object to this layout.   * This method is usually only called by a {@link java.awt.Container}s add   * method. This method does nothing, since SpringLayout does not manage   * String-indexed components.   *   * @param name  the name.   * @param c the component to be added.   */  public void addLayoutComponent(String name, Component c)  {    // do nothing here.  }  /**   * The trick to SpringLayout is that the network of Springs needs to   * completely created before the positioning results are generated.   *   * Using the springs directly during network creation will set their values    * before the network is completed, Using Deferred Springs during creation of    * the network allows all the edges to be connected together and the network    * to be created without resolving the Springs until their results need to be    * known, at which point the network is complete and the spring addition and    * and substitution calculations will work on a complete and valid network.   *   * @author Caolan McNamara (caolanm@redhat.com)   */  private static class DeferredSpring extends Spring   {    private SpringLayout sl;    private String edgeName;    private Component c;    public String toString()    {      return "DeferredSpring of edge" + edgeName + " of " + "something";    }    public DeferredSpring(SpringLayout s, String edge, Component component)    {        sl = s;        edgeName = edge;        c = component;    }    private Spring resolveSpring()     {        return sl.getConstraints(c).getConstraint(edgeName);    }    public int getMaximumValue()     {        return resolveSpring().getMaximumValue();    }    public int getMinimumValue()     {        return resolveSpring().getMinimumValue();    }    public int getPreferredValue()     {        return resolveSpring().getPreferredValue();    }    public int getValue()     {        int nRet = resolveSpring().getValue();        if (nRet == Spring.UNSET)            nRet = getPreferredValue();        return nRet;    }    public void setValue(int size)     {        resolveSpring().setValue(size);    }  }  private static abstract class DeferredDimension extends Spring  {    private int value;    public DeferredDimension()    {      value = Spring.UNSET;    }    public void setValue(int val)    {      value = val;    }    public int getValue()    {      if (value == Spring.UNSET)          return getPreferredValue();      return value;    }  }  private static class DeferredWidth extends DeferredDimension  {    private Component c;    public DeferredWidth(Component component)    {        c = component;    }    public String toString()    {      return "DeferredWidth of " + "something";    }    //clip max to a value we can do meaningful calculation with    public int getMaximumValue()     {        int widget_width = c.getMaximumSize().width;        return Math.min(Short.MAX_VALUE, widget_width);    }    public int getMinimumValue()     {        return c.getMinimumSize().width;    }    public int getPreferredValue()     {        return c.getPreferredSize().width;    }  }  private static class DeferredHeight extends DeferredDimension  {    private Component c;    public String toString()    {        return "DeferredHeight of " + "something";    }    public DeferredHeight(Component component)    {        c = component;    }    //clip max to a value we can do meaningful calculations with it    public int getMaximumValue()     {        int widget_height = c.getMaximumSize().height;        return Math.min(Short.MAX_VALUE, widget_height);    }    public int getMinimumValue()     {        return c.getMinimumSize().height;    }    public int getPreferredValue()     {        return c.getPreferredSize().height;    }  }  /**   * Returns the constraint of the edge named by <code>edgeName</code>.   *   * @param c the component from which to get the constraint.   * @param edgeName the name of the edge, one of {@link #EAST},   *     {@link #WEST}, {@link #NORTH} or {@link #SOUTH}.   * @return the constraint of the edge <code>edgeName</code> of the   * component c.   */  public Spring getConstraint(String edgeName, Component c)  {    return new DeferredSpring(this, edgeName, c);  }  /**   * Returns the {@link Constraints} object associated with the specified   * component.   *   * @param c the component for which to determine the constraint.   * @return the {@link Constraints} object associated with the specified   *      component.   */  public SpringLayout.Constraints getConstraints(Component c)  {    Constraints constraints = (Constraints) constraintsMap.get(c);    if (constraints == null)    {      constraints = new Constraints();      constraints.setWidth(new DeferredWidth(c));      constraints.setHeight(new DeferredHeight(c));      constraints.setX(Spring.constant(0));      constraints.setY(Spring.constant(0));      constraintsMap.put(c, constraints);    }    return constraints;  }  /**   * Returns the X alignment of the Container <code>p</code>.   *    * @param p   *          the {@link java.awt.Container} for which to determine the X   *          alignment.   * @return always 0.0   */  public float getLayoutAlignmentX(Container p)  {    return 0.0F;  }  /**   * Returns the Y alignment of the Container <code>p</code>.   *   * @param p the {@link java.awt.Container} for which to determine the Y   *     alignment.   * @return always 0.0   */  public float getLayoutAlignmentY(Container p)  {    return 0.0F;  }  /**   * Recalculate a possibly cached layout.   */  public void invalidateLayout(Container p)  {    // nothing to do here yet  }  private Constraints initContainer(Container p)  {    Constraints c = getConstraints(p);    c.setX(Spring.constant(0));    c.setY(Spring.constant(0));    c.setWidth(null);    c.setHeight(null);    if (c.getEast() == null)      c.setEast(Spring.constant(0, 0, Integer.MAX_VALUE));    if (c.getSouth() == null)       c.setSouth(Spring.constant(0, 0, Integer.MAX_VALUE));    return c;  }  /**   * Lays out the container <code>p</code>.   *   * @param p the container to be laid out.   */  public void layoutContainer(Container p)  {    java.awt.Insets insets = p.getInsets();    Component[] components = p.getComponents();    Constraints cs = initContainer(p);    cs.dropCalcResult();    for (int index = 0 ; index < components.length; index++)    {        Component c = components[index];        getConstraints(c).dropCalcResult();    }    int offsetX = p.getInsets().left;    int offsetY = p.getInsets().right;    cs.getX().setValue(0);    cs.getY().setValue(0);    cs.getWidth().setValue(p.getWidth() - offsetX - insets.right);    cs.getHeight().setValue(p.getHeight() - offsetY - insets.bottom);    for (int index = 0; index < components.length; index++)    {      Component c = components[index];      Constraints constraints = getConstraints(c);            int x = constraints.getX().getValue();      int y = constraints.getY().getValue();      int width = constraints.getWidth().getValue();      int height = constraints.getHeight().getValue();            c.setBounds(x + offsetX, y + offsetY, width, height);    }  }  /**   * Calculates the maximum size of the layed out container. This   * respects the maximum sizes of all contained components.   *   * @param p the container to be laid out.   * @return the maximum size of the container.   */  public Dimension maximumLayoutSize(Container p)  {    java.awt.Insets insets = p.getInsets();    Constraints cs = initContainer(p);    int maxX = cs.getWidth().getMaximumValue() + insets.left + insets.right;    int maxY = cs.getHeight().getMaximumValue() + insets.top + insets.bottom;    return new Dimension(maxX, maxY);  }  /**   * Calculates the minimum size of the layed out container. This   * respects the minimum sizes of all contained components.   *   * @param p the container to be laid out.   * @return the minimum size of the container.   */  public Dimension minimumLayoutSize(Container p)  {    java.awt.Insets insets = p.getInsets();    Constraints cs = initContainer(p);    int maxX = cs.getWidth().getMinimumValue() + insets.left + insets.right;    int maxY = cs.getHeight().getMinimumValue() + insets.top + insets.bottom;    return new Dimension(maxX, maxY);  }  /**   * Calculates the preferred size of the layed out container. This   * respects the preferred sizes of all contained components.   *   * @param p the container to be laid out.   * @return the preferred size of the container.   */  public Dimension preferredLayoutSize(Container p)  {    java.awt.Insets insets = p.getInsets();    Constraints cs = initContainer(p);    int maxX = cs.getWidth().getPreferredValue() + insets.left + insets.right;    int maxY = cs.getHeight().getPreferredValue() + insets.top + insets.bottom;    return new Dimension(maxX, maxY);  }  /**   * Attaches the edge <code>e1</code> of component <code>c1</code> to   * the edge <code>e2</code> of component <code>c2</code> width the   * fixed strut <code>pad</code>.   *   * @param e1 the edge of component 1.   * @param c1 the component 1.   * @param pad the space between the components in pixels.   * @param e2 the edge of component 2.   * @param c2 the component 2.   */  public void putConstraint(String e1, Component c1, int pad, String e2,                             Component c2)  {    putConstraint(e1, c1, Spring.constant(pad), e2, c2);  }  /**   * Attaches the edge <code>e1</code> of component <code>c1</code> to   * the edge <code>e2</code> of component <code>c2</code> width the   * {@link Spring} <code>s</code>.   *   * @param e1 the edge of component 1.   * @param c1 the component 1.   * @param s the space between the components as a {@link Spring} object.   * @param e2 the edge of component 2.   * @param c2 the component 2.   */  public void putConstraint(String e1, Component c1, Spring s, String e2,                             Component c2)  {    Constraints constraints1 = getConstraints(c1);    Spring otherEdge = getConstraint(e2, c2);    constraints1.setConstraint(e1, Spring.sum(s, otherEdge));  }  /**   * Removes a layout component.   * @param c the layout component to remove.   */  public void removeLayoutComponent(Component c)  {    // do nothing here  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?