diff options
Diffstat (limited to 'libjava/classpath/javax/swing/plaf/basic/BasicSplitPaneUI.java')
-rw-r--r-- | libjava/classpath/javax/swing/plaf/basic/BasicSplitPaneUI.java | 372 |
1 files changed, 156 insertions, 216 deletions
diff --git a/libjava/classpath/javax/swing/plaf/basic/BasicSplitPaneUI.java b/libjava/classpath/javax/swing/plaf/basic/BasicSplitPaneUI.java index 2d595597424..b7cc425482d 100644 --- a/libjava/classpath/javax/swing/plaf/basic/BasicSplitPaneUI.java +++ b/libjava/classpath/javax/swing/plaf/basic/BasicSplitPaneUI.java @@ -63,6 +63,7 @@ import javax.swing.JSlider; import javax.swing.JSplitPane; import javax.swing.KeyStroke; import javax.swing.LookAndFeel; +import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.plaf.ActionMapUIResource; @@ -107,13 +108,34 @@ public class BasicSplitPaneUI extends SplitPaneUI protected int[] sizes = new int[3]; /** + * This is used to determine if we are vertical or horizontal layout. + * In the JDK, the BasicVerticalLayoutManager seems to have no more + * methods implemented (as of JDK5), so we keep this state here. + */ + private int axis; + + /** * Creates a new instance. This is package private because the reference * implementation has no public constructor either. Still, we need to * call it from BasicVerticalLayoutManager. */ BasicHorizontalLayoutManager() { - // Nothing to do here. + this(SwingConstants.HORIZONTAL); + } + + /** + * Creates a new instance for a specified axis. This is provided for + * compatibility, since the BasicVerticalLayoutManager seems to have + * no more implementation in the RI, according to the specs. So + * we handle all the axis specific stuff here. + * + * @param a the axis, either SwingConstants#HORIZONTAL, + * or SwingConstants#VERTICAL + */ + BasicHorizontalLayoutManager(int a) + { + axis = a; } /** @@ -167,7 +189,12 @@ public class BasicSplitPaneUI extends SplitPaneUI */ protected int getAvailableSize(Dimension containerSize, Insets insets) { - return containerSize.width - insets.left - insets.right; + int size; + if (axis == SwingConstants.HORIZONTAL) + size = containerSize.width - insets.left - insets.right; + else + size = containerSize.height - insets.top - insets.bottom; + return size; } /** @@ -180,9 +207,15 @@ public class BasicSplitPaneUI extends SplitPaneUI */ protected int getInitialLocation(Insets insets) { + int loc = 0; if (insets != null) - return insets.left; - return 0; + { + if (axis == SwingConstants.HORIZONTAL) + loc = insets.left; + else + loc = insets.top; + } + return loc; } /** @@ -195,7 +228,7 @@ public class BasicSplitPaneUI extends SplitPaneUI */ public float getLayoutAlignmentX(Container target) { - return target.getAlignmentX(); + return 0.0f; } /** @@ -208,7 +241,7 @@ public class BasicSplitPaneUI extends SplitPaneUI */ public float getLayoutAlignmentY(Container target) { - return target.getAlignmentY(); + return 0.0f; } /** @@ -220,10 +253,19 @@ public class BasicSplitPaneUI extends SplitPaneUI */ protected int getPreferredSizeOfComponent(Component c) { + int size = 0; Dimension dims = c.getPreferredSize(); - if (dims != null) - return dims.width; - return 0; + if (axis == SwingConstants.HORIZONTAL) + { + if (dims != null) + size = dims.width; + } + else + { + if (dims != null) + size = dims.height; + } + return size; } /** @@ -235,7 +277,12 @@ public class BasicSplitPaneUI extends SplitPaneUI */ protected int getSizeOfComponent(Component c) { - return c.getWidth(); + int size; + if (axis == SwingConstants.HORIZONTAL) + size = c.getHeight(); + else + size = c.getWidth(); + return size; } /** @@ -273,8 +320,17 @@ public class BasicSplitPaneUI extends SplitPaneUI Dimension dims = split.getSize(); int loc = getInitialLocation(insets); int available = getAvailableSize(dims, insets); - sizes[0] = getDividerLocation(split) - loc; + sizes[0] = split.getDividerLocation(); sizes[1] = available - sizes[0] - sizes[2]; + + // According to a Mauve test we only honour the minimum + // size of the components, when the dividerLocation hasn't + // been excplicitly set. + if (! dividerLocationSet) + { + sizes[0] = Math.max(sizes[0], minimumSizeOfComponent(0)); + sizes[1] = Math.max(sizes[1], minimumSizeOfComponent(1)); + } // The size of the divider won't change. // Layout component#1. @@ -313,27 +369,30 @@ public class BasicSplitPaneUI extends SplitPaneUI */ public Dimension minimumLayoutSize(Container target) { + Dimension dim = new Dimension(); if (target instanceof JSplitPane) { - JSplitPane split = (JSplitPane) target; - Insets insets = target.getInsets(); - - int height = 0; - int width = 0; + int primary = 0; + int secondary = 0; for (int i = 0; i < components.length; i++) { - if (components[i] == null) - continue; - Dimension dims = components[i].getMinimumSize(); - if (dims != null) + if (components[i] != null) { - width += dims.width; - height = Math.max(height, dims.height); + Dimension dims = components[i].getMinimumSize(); + primary += axis == SwingConstants.HORIZONTAL ? dims.width + : dims.height; + int sec = axis == SwingConstants.HORIZONTAL ? dims.height + : dims.width; + secondary = Math.max(sec, secondary); } } - return new Dimension(width, height); + int width = axis == SwingConstants.HORIZONTAL ? primary : secondary; + int height = axis == SwingConstants.VERTICAL ? secondary : primary; + + Insets i = splitPane.getInsets(); + dim.setSize(width + i.left + i.right, height + i.top + i.bottom); } - return null; + return dim; } /** @@ -347,28 +406,30 @@ public class BasicSplitPaneUI extends SplitPaneUI */ public Dimension preferredLayoutSize(Container target) { + Dimension dim = new Dimension(); if (target instanceof JSplitPane) { - JSplitPane split = (JSplitPane) target; - Insets insets = target.getInsets(); - - int height = 0; - int width = 0; + int primary = 0; + int secondary = 0; for (int i = 0; i < components.length; i++) { - if (components[i] == null) - continue; - Dimension dims = components[i].getPreferredSize(); - if (dims != null) + if (components[i] != null) { - width += dims.width; - if (!(components[i] instanceof BasicSplitPaneDivider)) - height = Math.max(height, dims.height); + Dimension dims = components[i].getPreferredSize(); + primary += axis == SwingConstants.HORIZONTAL ? dims.width + : dims.height; + int sec = axis == SwingConstants.HORIZONTAL ? dims.height + : dims.width; + secondary = Math.max(sec, secondary); } } - return new Dimension(width, height); + int width = axis == SwingConstants.HORIZONTAL ? primary : secondary; + int height = axis == SwingConstants.VERTICAL ? secondary : primary; + + Insets i = splitPane.getInsets(); + dim.setSize(width + i.left + i.right, height + i.top + i.bottom); } - return null; + return dim; } /** @@ -406,8 +467,6 @@ public class BasicSplitPaneUI extends SplitPaneUI { for (int i = 0; i < components.length; i++) resetSizeAt(i); - setDividerLocation(splitPane, - getInitialLocation(splitPane.getInsets()) + sizes[0]); } /** @@ -425,11 +484,23 @@ public class BasicSplitPaneUI extends SplitPaneUI protected void setComponentToSize(Component c, int size, int location, Insets insets, Dimension containerSize) { - int w = size; - int h = containerSize.height - insets.top - insets.bottom; - int x = location; - int y = insets.top; - c.setBounds(x, y, w, h); + if (insets != null) + { + if (axis == SwingConstants.HORIZONTAL) + c.setBounds(location, insets.top, size, + containerSize.height - insets.top - insets.bottom); + else + c.setBounds(insets.left, location, + containerSize.width - insets.left - insets.right, + size); + } + else + { + if (axis == SwingConstants.HORIZONTAL) + c.setBounds(location, 0, size, containerSize.height); + else + c.setBounds(0, location, containerSize.width, size); + } } /** @@ -462,7 +533,6 @@ public class BasicSplitPaneUI extends SplitPaneUI resetSizeAt(1); } components[2] = divider; - resetSizeAt(2); } /** @@ -485,10 +555,13 @@ public class BasicSplitPaneUI extends SplitPaneUI int minimumSizeOfComponent(int index) { Dimension dims = components[index].getMinimumSize(); + int size = 0; if (dims != null) - return dims.width; - else - return 0; + if (axis == SwingConstants.HORIZONTAL) + size = dims.width; + else + size = dims.height; + return size; } } //end BasicHorizontalLayoutManager @@ -504,163 +577,11 @@ public class BasicSplitPaneUI extends SplitPaneUI extends BasicHorizontalLayoutManager { /** - * This method returns the height of the container minus the top and - * bottom inset. - * - * @param containerSize The size of the container. - * @param insets The insets of the container. - * - * @return The height minus top and bottom inset. - */ - protected int getAvailableSize(Dimension containerSize, Insets insets) - { - return containerSize.height - insets.top - insets.bottom; - } - - /** - * This method returns the top inset. - * - * @param insets The Insets to use. - * - * @return The top inset. - */ - protected int getInitialLocation(Insets insets) - { - return insets.top; - } - - /** - * This method returns the preferred height of the component. - * - * @param c The component to measure. - * - * @return The preferred height of the component. - */ - protected int getPreferredSizeOfComponent(Component c) - { - Dimension dims = c.getPreferredSize(); - if (dims != null) - return dims.height; - return 0; - } - - /** - * This method returns the current height of the component. - * - * @param c The component to measure. - * - * @return The current height of the component. - */ - protected int getSizeOfComponent(Component c) - { - return c.getHeight(); - } - - /** - * This method returns the minimum layout size. The minimum height is the - * sum of all the components' minimum heights. The minimum width is the - * maximum of all the components' minimum widths. - * - * @param container The container to measure. - * - * @return The minimum size. + * Creates a new instance. */ - public Dimension minimumLayoutSize(Container container) + public BasicVerticalLayoutManager() { - if (container instanceof JSplitPane) - { - JSplitPane split = (JSplitPane) container; - Insets insets = container.getInsets(); - - int height = 0; - int width = 0; - for (int i = 0; i < components.length; i++) - { - if (components[i] == null) - continue; - Dimension dims = components[i].getMinimumSize(); - if (dims != null) - { - height += dims.height; - width = Math.max(width, dims.width); - } - } - return new Dimension(width, height); - } - return null; - } - - /** - * This method returns the preferred layout size. The preferred height is - * the sum of all the components' preferred heights. The preferred width - * is the maximum of all the components' preferred widths. - * - * @param container The container to measure. - * - * @return The preferred size. - */ - public Dimension preferredLayoutSize(Container container) - { - if (container instanceof JSplitPane) - { - JSplitPane split = (JSplitPane) container; - Insets insets = container.getInsets(); - - int height = 0; - int width = 0; - for (int i = 0; i < components.length; i++) - { - if (components[i] == null) - continue; - Dimension dims = components[i].getPreferredSize(); - if (dims != null) - { - height += dims.height; - width = Math.max(width, dims.width); - } - } - return new Dimension(width, height); - } - return null; - } - - /** - * This method sets the bounds of the given component. The y coordinate is - * the location given. The x coordinate is the left inset. The height is - * the size given. The width is the container size minus the left and - * right inset. - * - * @param c The component to set bounds for. - * @param size The height. - * @param location The y coordinate. - * @param insets The insets to use. - * @param containerSize The container's size. - */ - protected void setComponentToSize(Component c, int size, int location, - Insets insets, Dimension containerSize) - { - int y = location; - int x = insets.left; - int h = size; - int w = containerSize.width - insets.left - insets.right; - c.setBounds(x, y, w, h); - } - - /** - * This method returns the minimum height of the component at the given - * index. - * - * @param index The index of the component to check. - * - * @return The minimum height of the given component. - */ - int minimumSizeOfComponent(int index) - { - Dimension dims = components[index].getMinimumSize(); - if (dims != null) - return dims.height; - else - return 0; + super(SwingConstants.VERTICAL); } } @@ -941,7 +862,13 @@ public class BasicSplitPaneUI extends SplitPaneUI /** The JSplitPane that this UI draws. */ protected JSplitPane splitPane; - private int dividerLocation; + /** + * True, when setDividerLocation() has been called at least + * once on the JSplitPane, false otherwise. + * + * This is package private to avoid a synthetic accessor method. + */ + boolean dividerLocationSet; /** * Creates a new BasicSplitPaneUI object. @@ -973,6 +900,7 @@ public class BasicSplitPaneUI extends SplitPaneUI if (c instanceof JSplitPane) { splitPane = (JSplitPane) c; + dividerLocationSet = false; installDefaults(); installListeners(); installKeyboardActions(); @@ -990,6 +918,7 @@ public class BasicSplitPaneUI extends SplitPaneUI uninstallListeners(); uninstallDefaults(); + dividerLocationSet = false; splitPane = null; } @@ -1007,8 +936,10 @@ public class BasicSplitPaneUI extends SplitPaneUI nonContinuousLayoutDivider = createDefaultNonContinuousLayoutDivider(); splitPane.add(divider, JSplitPane.DIVIDER); - // There is no need to add the nonContinuousLayoutDivider - splitPane.setDividerSize(UIManager.getInt("SplitPane.dividerSize")); + // There is no need to add the nonContinuousLayoutDivider. + dividerSize = UIManager.getInt("SplitPane.dividerSize"); + splitPane.setDividerSize(dividerSize); + divider.setDividerSize(dividerSize); splitPane.setOpaque(true); } @@ -1136,8 +1067,10 @@ public class BasicSplitPaneUI extends SplitPaneUI new AbstractAction("negativeIncrement") { public void actionPerformed(ActionEvent event) { - setDividerLocation(splitPane, Math.max(dividerLocation - - KEYBOARD_DIVIDER_MOVE_OFFSET, 0)); + int oldLoc = splitPane.getDividerLocation(); + int newLoc = + Math.max(oldLoc - KEYBOARD_DIVIDER_MOVE_OFFSET, 0); + splitPane.setDividerLocation(newLoc); } } ); @@ -1145,8 +1078,10 @@ public class BasicSplitPaneUI extends SplitPaneUI new AbstractAction("positiveIncrement") { public void actionPerformed(ActionEvent event) { - setDividerLocation(splitPane, dividerLocation - + KEYBOARD_DIVIDER_MOVE_OFFSET); + int oldLoc = splitPane.getDividerLocation(); + int newLoc = + Math.max(oldLoc + KEYBOARD_DIVIDER_MOVE_OFFSET, 0); + splitPane.setDividerLocation(newLoc); } } ); @@ -1436,7 +1371,7 @@ public class BasicSplitPaneUI extends SplitPaneUI */ public void setDividerLocation(JSplitPane jc, int location) { - dividerLocation = location; + dividerLocationSet = true; splitPane.revalidate(); splitPane.repaint(); } @@ -1450,7 +1385,12 @@ public class BasicSplitPaneUI extends SplitPaneUI */ public int getDividerLocation(JSplitPane jc) { - return dividerLocation; + int loc; + if (jc.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) + loc = divider.getX(); + else + loc = divider.getY(); + return loc; } /** @@ -1523,7 +1463,7 @@ public class BasicSplitPaneUI extends SplitPaneUI */ public Dimension getPreferredSize(JComponent jc) { - return layoutManager.preferredLayoutSize((Container) jc); + return layoutManager.preferredLayoutSize(jc); } /** @@ -1535,7 +1475,7 @@ public class BasicSplitPaneUI extends SplitPaneUI */ public Dimension getMinimumSize(JComponent jc) { - return layoutManager.minimumLayoutSize((Container) jc); + return layoutManager.minimumLayoutSize(jc); } /** @@ -1547,7 +1487,7 @@ public class BasicSplitPaneUI extends SplitPaneUI */ public Dimension getMaximumSize(JComponent jc) { - return layoutManager.maximumLayoutSize((Container) jc); + return layoutManager.maximumLayoutSize(jc); } /** |