From 1e8a3c5859414e154b43cfe938ff32f802c9fd6c Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Sat, 4 Mar 2006 20:57:24 +0000 Subject: * javax/swing/SpringLayout.java (Constraints): New constructor. * javax/swing/Spring.java (width): New method. (height): Likewise. (scale): Likewise. --- ChangeLog | 7 +++ javax/swing/Spring.java | 136 ++++++++++++++++++++++++++++++++++++++++++ javax/swing/SpringLayout.java | 19 ++++++ 3 files changed, 162 insertions(+) diff --git a/ChangeLog b/ChangeLog index e1a00ebc9..85fc818b4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2006-03-04 Tom Tromey + + * javax/swing/SpringLayout.java (Constraints): New constructor. + * javax/swing/Spring.java (width): New method. + (height): Likewise. + (scale): Likewise. + 2006-03-04 Mark Wielaard * gnu/java/net/protocol/http/HTTPConnection.java (Pool.get): Remove diff --git a/javax/swing/Spring.java b/javax/swing/Spring.java index 8f7105d49..b9890c714 100644 --- a/javax/swing/Spring.java +++ b/javax/swing/Spring.java @@ -37,6 +37,9 @@ exception statement from your version. */ package javax.swing; +import java.awt.Component; +import java.awt.Dimension; + /** * Calculates the space between component edges, that are layed out by * {@link SpringLayout}. @@ -167,6 +170,139 @@ public abstract class Spring return new AddSpring(s1, s2); } + /** + * Return a new Spring which computes its values by scaling + * the values of another spring by a constant factor. If the + * factor is negative, the minimum and maximum values of + * the argument spring will be interchanged. + * @param spring the spring to track + * @param factor the factor by which to scale + * @return a new multiplicative Spring + * @since 1.5 + */ + public static Spring scale(final Spring spring, final float factor) + { + if (spring == null) + throw new NullPointerException("spring argument is null"); + return new Spring() + { + public int getMaximumValue() + { + return (int) ((factor < 0 ? spring.getMinimumValue() + : spring.getMaximumValue()) + * factor); + } + + public int getMinimumValue() + { + return (int) ((factor < 0 ? spring.getMaximumValue() + : spring.getMinimumValue()) + * factor); + } + + public int getPreferredValue() + { + return (int) (spring.getPreferredValue() * factor); + } + + public int getValue() + { + return (int) (spring.getValue() * factor); + } + + public void setValue(int value) + { + spring.setValue((int) (value / factor)); + } + }; + } + + /** + * Return a new Spring which takes its values from the specified + * Component. In particular, the maximum value is taken from + * the maximumSize, the minimum value is taken from the minimumSize, + * the preferred value is taken from the preferredSize, and the + * value is taken from the component's current size. These values + * change as the component changes size. + * @param component the component + * @return a new Spring which tracks the component's width + * @since 1.5 + */ + public static Spring width(final Component component) + { + return new Spring() + { + public int getMaximumValue() + { + return component.getMaximumSize().width; + } + + public int getMinimumValue() + { + return component.getMinimumSize().width; + } + + public int getPreferredValue() + { + return component.getPreferredSize().width; + } + + public int getValue() + { + return component.getSize().width; + } + + public void setValue(int value) + { + Dimension d = component.getSize(); + component.setSize(value, d.height); + } + }; + } + + /** + * Return a new Spring which takes its values from the specified + * Component. In particular, the maximum value is taken from + * the maximumSize, the minimum value is taken from the minimumSize, + * the preferred value is taken from the preferredSize, and the + * value is taken from the component's current size. These values + * change as the component changes size. + * @param component the component + * @return a new Spring which tracks the component's height + * @since 1.5 + */ + public static Spring height(final Component component) + { + return new Spring() + { + public int getMaximumValue() + { + return component.getMaximumSize().height; + } + + public int getMinimumValue() + { + return component.getMinimumSize().height; + } + + public int getPreferredValue() + { + return component.getPreferredSize().height; + } + + public int getValue() + { + return component.getSize().height; + } + + public void setValue(int value) + { + Dimension d = component.getSize(); + component.setSize(d.width, value); + } + }; + } + /** * A simple Spring, that holds constant values for min, pref and max. * diff --git a/javax/swing/SpringLayout.java b/javax/swing/SpringLayout.java index 0f353e17b..8d46a736a 100644 --- a/javax/swing/SpringLayout.java +++ b/javax/swing/SpringLayout.java @@ -149,6 +149,25 @@ public class SpringLayout implements LayoutManager2 east = south = null; } + /** + * Create a new Constraints object which tracks the indicated + * component. The x and y positions for this Constraints object + * are constant Springs created with the component's location at + * the time this constructor is called. The width and height + * of this Constraints are Springs created using + * {@link Spring#width(Component)} and {@link Spring#height(Component)}, + * respectively. + * @param component the component to track + * @since 1.5 + */ + public Constraints(Component component) + { + this(Spring.constant(component.getX()), + Spring.constant(component.getY()), + Spring.width(component), + Spring.height(component)); + } + /** * Returns the constraint for the edge with the edgeName. * This is expected to be one of -- cgit v1.2.1