diff options
author | Mark Wielaard <mark@klomp.org> | 2004-07-31 22:25:17 +0000 |
---|---|---|
committer | Mark Wielaard <mark@klomp.org> | 2004-07-31 22:25:17 +0000 |
commit | 49ec9e962962f56fe22e4f80554542cb88a9923a (patch) | |
tree | de5909615b54c1500735d9b3f3f60caebad729f0 /javax | |
parent | 2afbcd1a8b6d81ae638c75bd9656b14d5fae1005 (diff) | |
download | classpath-49ec9e962962f56fe22e4f80554542cb88a9923a.tar.gz |
2004-07-31 Roman Kennke <roman@ontographics.com>
* javax/swing/Box.java:
(createGlue): Implemented
(createHorizontalGlue): Implemented
(createHorizontalStrut): Implemented
(createVerticalGlue): Implemented
(createVerticalStrut): Implemented
2004-07-31 Roman Kennke <roman@ontographics.com>
* javax/swing/BoxLayout.java: Reimplement.
Diffstat (limited to 'javax')
-rw-r--r-- | javax/swing/Box.java | 107 | ||||
-rw-r--r-- | javax/swing/BoxLayout.java | 229 |
2 files changed, 263 insertions, 73 deletions
diff --git a/javax/swing/Box.java b/javax/swing/Box.java index 4e3387478..0cef534f8 100644 --- a/javax/swing/Box.java +++ b/javax/swing/Box.java @@ -47,7 +47,11 @@ import java.awt.Dimension; import java.awt.AWTError; /** - * Needs some work I guess.... + * A component that uses a {@link BoxLayout} as Layout Manager. + * + * In addition to that, this class provides a set of static methods for + * creating some filler components ('struts' and 'glue') for use in + * containers that are laid out using BoxLayout. * * @author Ronald Veldema (rveldema@cs.vu.nl) */ @@ -69,7 +73,10 @@ public class Box extends JComponent implements Accessible return null; } } - + + /** + * A component that servers as a filler in BoxLayout controlled containers. + */ public static class Filler extends JComponent implements Accessible { private static final long serialVersionUID = -1204263191910183998L; @@ -93,11 +100,25 @@ public class Box extends JComponent implements Accessible private transient Dimension min, pref, max; + /** + * Creates a new instance of Filler. + * + * @param min the minimum size of the filler. + * @param pref the preferred size of the filler. + * @param max the maximum size of the filler. + */ public Filler(Dimension min, Dimension pref, Dimension max) { changeShape(min, pref, max); } + /** + * Changes the dimensions of this Filler. + * + * @param min the new minimum size of the filler. + * @param pref the new preferred size of the filler. + * @param max the new maximum size of the filler. + */ public void changeShape(Dimension min, Dimension pref, Dimension max) { this.min = min; @@ -113,30 +134,66 @@ public class Box extends JComponent implements Accessible return accessibleContext; } + /** + * Returns the maximum size of this Filler. + * + * @return the maximum size of this Filler. + */ public Dimension getMaximumSize() { return max; } + /** + * Returns the minimum size of this Filler. + * + * @return the minimum size of this Filler. + */ public Dimension getMinimumSize() { return min; } + /** + * Returns the preferred size of this Filler. + * + * @return the preferred size of this Filler. + */ public Dimension getPreferredSize() { return pref; } } + /** + * Creates a new Box component, that lays out its children according + * to the <code>axis</code> parameter. + * + * @param axis the orientation of the BoxLayout. + * + * @see BoxLayout#X_AXIS + * @see BoxLayout#Y_AXIS + * @see BoxLayout#LINE_AXIS + * @see BoxLayout#PAGE_AXIS + */ public Box(int axis) { setLayout(new BoxLayout(this, axis)); } + /** + * Creates a filler component which acts as glue between components. + * It does not take space unless some extra space is available. If extra + * space is available, this component can expand in both X and Y directions. + * + * @return a glue-like filler component. + */ public static Component createGlue() { - return null; + Filler glue = new Filler(new Dimension(0,0), new Dimension(0,0), + new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE) + ); + return glue; } public static Box createHorizontalBox() @@ -144,14 +201,32 @@ public class Box extends JComponent implements Accessible return null; } + /** + * Creates a filler component which acts as glue between components. + * It does not take space unless some extra space is available. If extra + * space is available, this component can expand in the X direction. + * + * @return a glue-like filler component. + */ public static Component createHorizontalGlue() { - return null; + return createGlue(); } + /** + * Creates a filler component which acts as strut between components. + * It will fill exactly the specified horizontal size. + * + * @param width the width of this strut in pixels. + * + * @return a strut-like filler component. + */ public static Component createHorizontalStrut(int width) { - return null; + Filler strut = new Filler(new Dimension(width, 0), + new Dimension(width, 0), + new Dimension(width, Integer.MAX_VALUE)); + return strut; } public static Component createRigidArea(Dimension d) @@ -164,14 +239,32 @@ public class Box extends JComponent implements Accessible return null; } + /** + * Creates a filler component which acts as glue between components. + * It does not take space unless some extra space is available. If extra + * space is available, this component can expand in the Y direction. + * + * @return a glue-like filler component. + */ public static Component createVerticalGlue() { - return null; + return createGlue(); } + /** + * Creates a filler component which acts as strut between components. + * It will fill exactly the specified vertical size. + * + * @param height the height of this strut in pixels. + * + * @return a strut-like filler component. + */ public static Component createVerticalStrut(int height) { - return null; + Filler strut = new Filler(new Dimension(0, height), + new Dimension(0, height), + new Dimension(Integer.MAX_VALUE, height)); + return strut; } public void setLayout(LayoutManager l) diff --git a/javax/swing/BoxLayout.java b/javax/swing/BoxLayout.java index 0dd97ac60..7b12602a9 100644 --- a/javax/swing/BoxLayout.java +++ b/javax/swing/BoxLayout.java @@ -42,15 +42,11 @@ import java.awt.Component; import java.awt.ComponentOrientation; import java.awt.Container; import java.awt.Dimension; -import java.awt.GridLayout; import java.awt.LayoutManager2; import java.io.Serializable; - /** * A layout for swing components. - * This implementation delegates its methods to - * java.awt.GridLayout to do its work. * * @author Ronald Veldema (rveldema@cs.vu.nl) */ @@ -87,11 +83,6 @@ public class BoxLayout implements LayoutManager2, Serializable private Container container; /* - * Internal layout. - */ - private GridLayout grid; - - /* * Current type of component layouting. Defaults to X_AXIS. */ private int way = X_AXIS; @@ -108,75 +99,41 @@ public class BoxLayout implements LayoutManager2, Serializable { 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); } /** - * Adds a component to the layout. + * Adds a component to the layout. Not used in BoxLayout. * * @param name The name of the component to add. * @param component the component to add to the layout. */ public void addLayoutComponent(String name, Component component) { - if (way == X_AXIS - || (way == LINE_AXIS - && component.getComponentOrientation().isHorizontal()) - || (way == PAGE_AXIS - && !component.getComponentOrientation().isHorizontal())) - grid.setColumns(grid.getColumns() + 1); - else - grid.setRows(grid.getRows() + 1); } /** - * Removes a component from the layout. + * Removes a component from the layout. Not used in BoxLayout. * * @param component The component to remove from the layout. */ public void removeLayoutComponent(Component component) { - grid.removeLayoutComponent(component); - - if (way == X_AXIS - || (way == LINE_AXIS - && component.getComponentOrientation().isHorizontal()) - || (way == PAGE_AXIS - && !component.getComponentOrientation().isHorizontal())) - grid.setColumns(grid.getColumns() - 1); - else - grid.setRows(grid.getRows() - 1); } + private boolean isHorizontalIn(Container parent) + { + ComponentOrientation orientation = parent.getComponentOrientation(); + return this.way == X_AXIS + || (this.way == LINE_AXIS + && orientation.isHorizontal()) + || (this.way == PAGE_AXIS + && (!orientation.isHorizontal())); + } + + + /** * Returns the preferred size of the layout. * @@ -188,8 +145,38 @@ public class BoxLayout implements LayoutManager2, Serializable { if (parent != container) throw new AWTError("invalid parent"); + + int x = 0; + int y = 0; + + Component[] children = parent.getComponents(); + + if (isHorizontalIn(parent)) + { + // sum up preferred widths of components, find maximum of preferred + // heights + for (int index = 0; index < children.length; index++) + { + Component comp = children[index]; + Dimension sz = comp.getPreferredSize(); + x += sz.width; + y = Math.max(y, sz.height); + } + } + else + { + // sum up preferred heights of components, find maximum of + // preferred widths + for (int index = 0; index < children.length; index++) + { + Component comp = children[index]; + Dimension sz = comp.getPreferredSize(); + y += sz.height; + x = Math.max(x, sz.width); + } + } - return grid.preferredLayoutSize(parent); + return new Dimension(x, y); } /** @@ -203,8 +190,38 @@ public class BoxLayout implements LayoutManager2, Serializable { if (parent != container) throw new AWTError("invalid parent"); + + int x = 0; + int y = 0; + + Component[] children = parent.getComponents(); + + if (isHorizontalIn(parent)) + { + // sum up preferred widths of components, find maximum of preferred + // heights + for (int index = 0; index < children.length; index++) + { + Component comp = children[index]; + Dimension sz = comp.getMinimumSize(); + x += sz.width; + y = Math.max(y, sz.height); + } + } + else + { + // sum up preferred heights of components, find maximum of + // preferred widths + for (int index = 0; index < children.length; index++) + { + Component comp = children[index]; + Dimension sz = comp.getMinimumSize(); + y += sz.height; + x = Math.max(x, sz.width); + } + } - return grid.minimumLayoutSize(parent); + return new Dimension(x, y); } /** @@ -216,19 +233,69 @@ public class BoxLayout implements LayoutManager2, Serializable { if (parent != container) throw new AWTError("invalid parent"); - - grid.layoutContainer(parent); - } + Dimension size = parent.getSize(); + + Component[] children = parent.getComponents(); + + if (isHorizontalIn(parent)) + { + int x = 0; + for (int index = 0; index < children.length; index++) + { + Component comp = children[index]; + Dimension sz = comp.getPreferredSize(); + int width = sz.width; + int height = sz.height; + int cy = 0; + if (height > size.height) + { + height = size.height; + } + else + { + cy = (int) ((size.height - height) * comp.getAlignmentY()); + } + + comp.setSize(width, height); + comp.setLocation(x, cy); + x = x + width; + } + } + else + { + int y = 0; + for (int index = 0; index < children.length; index++) + { + Component comp = children[index]; + Dimension sz = comp.getPreferredSize(); + int width = sz.width; + int height = sz.height; + int cx = 0; + if (width > size.width) + { + width = size.width; + } + else + { + cx = (int) ((size.width - width) * comp.getAlignmentX()); + } + + comp.setSize(width, height); + comp.setLocation(cx, y); + y = y + height; + } + } + } + /** - * Adds a component to the layout. + * Adds a component to the layout. Not used in BoxLayout * * @param child The component to add to the layout. * @param constraints The constraints for the component in the layout. */ public void addLayoutComponent(Component child, Object constraints) { - addLayoutComponent("", child); } /** @@ -284,7 +351,37 @@ public class BoxLayout implements LayoutManager2, Serializable { if (parent != container) throw new AWTError("invalid parent"); - - return preferredLayoutSize(parent); + + int x = 0; + int y = 0; + + Component[] children = parent.getComponents(); + + if (isHorizontalIn(parent)) + { + + // sum up preferred widths of components, find maximum of preferred + // heights + for (int index = 0; index < children.length; index++) + { + Component comp = children[index]; + Dimension sz = comp.getMaximumSize(); + x += sz.width; + y = Math.max(y, sz.height); + } + } + else + { + // sum up preferred heights of components, find maximum of + // preferred widths + for (int index = 0; index < children.length; index++) + { + Component comp = children[index]; + Dimension sz = comp.getMaximumSize(); + y += sz.height; + x = Math.max(x, sz.width); + } + } + return new Dimension(x, y); } } |