Merge from mainline.
[official-gcc.git] / libjava / classpath / javax / swing / JSplitPane.java
blobc115121985462d576fbad774a61897abd5c46eb4
1 /* JSplitPane.java --
2 Copyright (C) 2004, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing;
41 import java.awt.Component;
42 import java.awt.Graphics;
43 import java.beans.PropertyChangeEvent;
45 import javax.accessibility.Accessible;
46 import javax.accessibility.AccessibleContext;
47 import javax.accessibility.AccessibleRole;
48 import javax.accessibility.AccessibleState;
49 import javax.accessibility.AccessibleStateSet;
50 import javax.accessibility.AccessibleValue;
51 import javax.swing.plaf.SplitPaneUI;
53 /**
54 * This class implements JSplitPane. It is used to divide two components. By
55 * dragging the SplitPane's divider, the user can resize the two components.
56 * Note that the divider cannot resize a component to smaller than it's
57 * minimum size.
59 public class JSplitPane extends JComponent implements Accessible
62 /**
63 * Provides the accessibility features for the <code>JSplitPane</code>
64 * component.
66 protected class AccessibleJSplitPane extends JComponent.AccessibleJComponent
67 implements AccessibleValue
69 private static final long serialVersionUID = -1788116871416305366L;
71 /**
72 * Creates a new <code>AccessibleJSplitPane</code> instance.
74 protected AccessibleJSplitPane()
76 // Nothing to do here.
79 /**
80 * Returns a set containing the current state of the {@link JSplitPane}
81 * component.
83 * @return The accessible state set.
85 public AccessibleStateSet getAccessibleStateSet()
87 AccessibleStateSet result = super.getAccessibleStateSet();
88 if (getOrientation() == HORIZONTAL_SPLIT)
90 result.add(AccessibleState.HORIZONTAL);
92 else if (getOrientation() == VERTICAL_SPLIT)
94 result.add(AccessibleState.VERTICAL);
96 return result;
99 /**
100 * Returns the accessible role for the <code>JSplitPane</code> component.
102 * @return {@link AccessibleRole#SPLIT_PANE}.
104 public AccessibleRole getAccessibleRole()
106 return AccessibleRole.SPLIT_PANE;
110 * Returns an object that provides access to the current, minimum and
111 * maximum values for the {@link JSlider}. Since this class implements
112 * {@link AccessibleValue}, it returns itself.
114 * @return The accessible value.
116 public AccessibleValue getAccessibleValue()
118 return this;
122 * Returns the current divider location for the {@link JSplitPane}
123 * component, as an {@link Integer}.
125 * @return The current divider location.
127 public Number getCurrentAccessibleValue()
129 return new Integer(getDividerLocation());
133 * Sets the divider location for the {@link JSplitPane} component and sends
134 * a {@link PropertyChangeEvent} (with the property name
135 * {@link AccessibleContext#ACCESSIBLE_VALUE_PROPERTY}) to all registered
136 * listeners. If the supplied value is <code>null</code>, this method
137 * does nothing and returns <code>false</code>.
139 * @param value the new slider value (<code>null</code> permitted).
141 * @return <code>true</code> if the slider value is updated, and
142 * <code>false</code> otherwise.
144 public boolean setCurrentAccessibleValue(Number value)
146 if (value == null)
147 return false;
148 Number oldValue = getCurrentAccessibleValue();
149 setDividerLocation(value.intValue());
150 firePropertyChange(AccessibleContext.ACCESSIBLE_VALUE_PROPERTY, oldValue,
151 new Integer(value.intValue()));
152 return true;
156 * Returns the minimum divider location for the {@link JSplitPane}
157 * component, as an {@link Integer}.
159 * @return The minimum divider location.
161 public Number getMinimumAccessibleValue()
163 return new Integer(getMinimumDividerLocation());
167 * Returns the maximum divider location for the {@link JSplitPane}
168 * component, as an {@link Integer}.
170 * @return The maximum divider location.
172 public Number getMaximumAccessibleValue()
174 return new Integer(getMaximumDividerLocation());
178 private static final long serialVersionUID = -5634142046175988380L;
180 /** The constraints string used to add components to the bottom. */
181 public static final String BOTTOM = "bottom";
183 /** The property fired when the continuousLayout property changes. */
184 public static final String CONTINUOUS_LAYOUT_PROPERTY = "continuousLayout";
186 /** The property fired when the divider property changes. */
187 public static final String DIVIDER = "divider";
189 /** The property fired when the divider location property changes. */
190 public static final String DIVIDER_LOCATION_PROPERTY = "dividerLocation";
192 /** The property fired when the divider size property changes. */
193 public static final String DIVIDER_SIZE_PROPERTY = "dividerSize";
196 * The value of the orientation when the components are split horizontally.
198 public static final int HORIZONTAL_SPLIT = 1;
200 /** The property fired when the last divider location property changes. */
201 public static final String LAST_DIVIDER_LOCATION_PROPERTY =
202 "lastDividerLocation";
204 /** The constraints string used to add components to the left. */
205 public static final String LEFT = "left";
207 /** The property fired when the one touch expandable property changes. */
208 public static final String ONE_TOUCH_EXPANDABLE_PROPERTY =
209 "oneTouchExpandable";
211 /** The property fired when the orientation property changes. */
212 public static final String ORIENTATION_PROPERTY = "orientation";
214 /** The property fired when the resize weight property changes. */
215 public static final String RESIZE_WEIGHT_PROPERTY = "resizeWeight";
217 /** The constraints string used to add components to the right. */
218 public static final String RIGHT = "right";
220 /** The constraints string used to add components to the top. */
221 public static final String TOP = "top";
223 /** The value of the orientation when the components are split vertically. */
224 public static final int VERTICAL_SPLIT = 0;
226 /** Whether the JSplitPane uses continuous layout. */
227 protected boolean continuousLayout;
229 /** Whether the JSplitPane uses one touch expandable buttons. */
230 protected boolean oneTouchExpandable = false;
232 // This is the master dividerSize variable and sets the
233 // BasicSplitPaneDivider one accordingly
235 /** The size of the divider. */
236 protected int dividerSize = 10;
238 /** The last location of the divider given by the UI. */
239 protected int lastDividerLocation;
241 /** The orientation of the JSplitPane. */
242 protected int orientation;
244 /** The component on the top or left. */
245 protected Component leftComponent;
247 /** The component on the right or bottom. */
248 protected Component rightComponent;
250 /** Determines how extra space should be allocated. */
251 private transient double resizeWeight;
254 * Indicates if the dividerSize property has been set by a client program or
255 * by the UI.
257 * @see #setUIProperty(String, Object)
258 * @see LookAndFeel#installProperty(JComponent, String, Object)
260 private boolean clientDividerSizeSet = false;
263 * Indicates if the oneTouchExpandable property has been set by a client
264 * program or by the UI.
266 * @see #setUIProperty(String, Object)
267 * @see LookAndFeel#installProperty(JComponent, String, Object)
269 private boolean clientOneTouchExpandableSet = false;
272 * Creates a new JSplitPane object with the given orientation, layout mode,
273 * and left and right components.
275 * @param newOrientation The orientation to use.
276 * @param newContinuousLayout The layout mode to use.
277 * @param newLeftComponent The left component.
278 * @param newRightComponent The right component.
280 * @throws IllegalArgumentException DOCUMENT ME!
282 public JSplitPane(int newOrientation, boolean newContinuousLayout,
283 Component newLeftComponent, Component newRightComponent)
285 if (newOrientation != HORIZONTAL_SPLIT && newOrientation != VERTICAL_SPLIT)
286 throw new IllegalArgumentException("orientation is invalid.");
287 orientation = newOrientation;
288 continuousLayout = newContinuousLayout;
289 setLeftComponent(newLeftComponent);
290 setRightComponent(newRightComponent);
292 updateUI();
296 * Creates a new JSplitPane object using nonContinuousLayout mode, the given
297 * orientation and left and right components.
299 * @param newOrientation The orientation to use.
300 * @param newLeftComponent The left component.
301 * @param newRightComponent The right component.
303 public JSplitPane(int newOrientation, Component newLeftComponent,
304 Component newRightComponent)
306 this(newOrientation, false, newLeftComponent, newRightComponent);
310 * Creates a new JSplitPane object with the given layout mode and
311 * orientation.
313 * @param newOrientation The orientation to use.
314 * @param newContinuousLayout The layout mode to use.
316 public JSplitPane(int newOrientation, boolean newContinuousLayout)
318 this(newOrientation, newContinuousLayout, null, null);
322 * Creates a new JSplitPane object using a nonContinuousLayout mode and the
323 * given orientation.
325 * @param newOrientation The orientation to use.
327 public JSplitPane(int newOrientation)
329 this(newOrientation, false, null, null);
333 * Creates a new JSplitPane object using HORIZONTAL_SPLIT and a
334 * nonContinuousLayout mode.
336 public JSplitPane()
338 this(HORIZONTAL_SPLIT, false, new JButton("left button"),
339 new JButton("right button"));
343 * This method adds a component to the JSplitPane. The constraints object is
344 * a string that identifies where this component should go. If the
345 * constraints is not a known one, it will throw an
346 * IllegalArgumentException. The valid constraints are LEFT, TOP, RIGHT,
347 * BOTTOM and DIVIDER.
349 * @param comp The component to add.
350 * @param constraints The constraints string to use.
351 * @param index Where to place to component in the list of components.
353 * @throws IllegalArgumentException When the constraints is not a known
354 * identifier.
356 protected void addImpl(Component comp, Object constraints, int index)
358 int left = 0;
359 int right = 1;
360 int div = 2;
361 int place;
362 if (constraints == null)
364 if (leftComponent == null)
365 constraints = LEFT;
366 else if (rightComponent == null)
367 constraints = RIGHT;
370 if (constraints instanceof String)
372 String placement = (String) constraints;
374 if (placement.equals(BOTTOM) || placement.equals(RIGHT))
376 if (rightComponent != null)
377 remove(rightComponent);
378 rightComponent = comp;
380 else if (placement.equals(LEFT) || placement.equals(TOP))
382 if (leftComponent != null)
383 remove(leftComponent);
384 leftComponent = comp;
386 else if (placement.equals(DIVIDER))
387 constraints = null;
388 else
389 throw new
390 IllegalArgumentException("Constraints is not a known identifier.");
392 // If no dividerLocation has been set, then we need to trigger an
393 // initial layout.
394 if (getDividerLocation() != -1)
395 resetToPreferredSizes();
397 super.addImpl(comp, constraints, index);
402 * Returns the object that provides accessibility features for this
403 * <code>JSplitPane</code> component.
405 * @return The accessible context (an instance of
406 * {@link AccessibleJSplitPane}).
408 public AccessibleContext getAccessibleContext()
410 if (accessibleContext == null)
411 accessibleContext = new AccessibleJSplitPane();
413 return accessibleContext;
417 * This method returns the bottom component.
419 * @return The bottom component.
421 public Component getBottomComponent()
423 return rightComponent;
427 * This method returns the location of the divider. This method is passed to
428 * the UI.
430 * @return The location of the divider.
432 public int getDividerLocation()
434 if (ui != null)
435 return ((SplitPaneUI) ui).getDividerLocation(this);
436 else
437 return -1;
441 * This method returns the size of the divider.
443 * @return The size of the divider.
445 public int getDividerSize()
447 return dividerSize;
451 * This method returns the last divider location.
453 * @return The last divider location.
455 public int getLastDividerLocation()
457 return lastDividerLocation;
461 * This method returns the left component.
463 * @return The left component.
465 public Component getLeftComponent()
467 return leftComponent;
471 * This method returns the maximum divider location. This method is passed
472 * to the UI.
474 * @return DOCUMENT ME!
476 public int getMaximumDividerLocation()
478 if (ui != null)
479 return ((SplitPaneUI) ui).getMaximumDividerLocation(this);
480 else
481 return -1;
485 * This method returns the minimum divider location. This method is passed
486 * to the UI.
488 * @return The minimum divider location.
490 public int getMinimumDividerLocation()
492 if (ui != null)
493 return ((SplitPaneUI) ui).getMinimumDividerLocation(this);
494 else
495 return -1;
499 * This method returns the orientation that the JSplitPane is using.
501 * @return The current orientation.
503 public int getOrientation()
505 return orientation;
509 * This method returns the current resize weight.
511 * @return The current resize weight.
513 public double getResizeWeight()
515 return resizeWeight;
519 * This method returns the right component.
521 * @return The right component.
523 public Component getRightComponent()
525 return rightComponent;
529 * This method returns the top component.
531 * @return The top component.
533 public Component getTopComponent()
535 return leftComponent;
539 * This method returns the UI.
541 * @return The UI.
543 public SplitPaneUI getUI()
545 return (SplitPaneUI) ui;
549 * This method returns true if the JSplitPane is using a continuousLayout.
551 * @return True if using a continuousLayout.
553 public boolean isContinuousLayout()
555 return continuousLayout;
559 * This method returns true if the divider has one touch expandable buttons.
561 * @return True if one touch expandable is used.
563 public boolean isOneTouchExpandable()
565 return oneTouchExpandable;
569 * This method returns true.
571 * @return true.
573 public boolean isValidateRoot()
575 return true;
579 * This method overrides JComponent's paintChildren so the UI can be
580 * messaged when the children have finished painting.
582 * @param g The Graphics object to paint with.
584 protected void paintChildren(Graphics g)
586 super.paintChildren(g);
587 if (ui != null)
588 ((SplitPaneUI) ui).finishedPaintingChildren(this, g);
592 * Returns an implementation-dependent string describing the attributes of
593 * this <code>JSplitPane</code>.
595 * @return A string describing the attributes of this <code>JSplitPane</code>
596 * (never <code>null</code>).
598 protected String paramString()
600 // FIXME: the next line can be restored once PR27208 is fixed
601 String superParamStr = ""; //super.paramString();
602 StringBuffer sb = new StringBuffer();
603 sb.append(",continuousLayout=").append(isContinuousLayout());
604 sb.append(",dividerSize=").append(getDividerSize());
605 sb.append(",lastDividerLocation=").append(getLastDividerLocation());
606 sb.append(",oneTouchExpandable=").append(isOneTouchExpandable());
607 sb.append(",orientation=");
608 if (orientation == HORIZONTAL_SPLIT)
609 sb.append("HORIZONTAL_SPLIT");
610 else
611 sb.append("VERTICAL_SPLIT");
612 return superParamStr + sb.toString();
616 * This method removes the given component from the JSplitPane.
618 * @param component The Component to remove.
620 public void remove(Component component)
622 if (component == leftComponent)
623 leftComponent = null;
624 else if (component == rightComponent)
625 rightComponent = null;
626 super.remove(component);
630 * This method removes the component at the given index.
632 * @param index The index of the component to remove.
634 public void remove(int index)
636 Component component = getComponent(index);
637 if (component == leftComponent)
638 leftComponent = null;
639 else if (component == rightComponent)
640 rightComponent = null;
641 super.remove(index);
645 * This method removes all components from the JSplitPane.
647 public void removeAll()
649 leftComponent = null;
650 rightComponent = null;
651 super.removeAll();
655 * This method resets all children of the JSplitPane to their preferred
656 * sizes.
658 public void resetToPreferredSizes()
660 if (ui != null)
661 ((SplitPaneUI) ui).resetToPreferredSizes(this);
665 * This method sets the bottom component.
667 * @param comp The Component to be placed at the bottom.
669 public void setBottomComponent(Component comp)
671 if (comp != null)
672 add(comp, BOTTOM);
673 else
674 add(new JButton("right button"), BOTTOM);
678 * This method sets the layout mode for the JSplitPane.
680 * @param newContinuousLayout Whether the JSplitPane is in continuousLayout
681 * mode.
683 public void setContinuousLayout(boolean newContinuousLayout)
685 if (newContinuousLayout != continuousLayout)
687 boolean oldValue = continuousLayout;
688 continuousLayout = newContinuousLayout;
689 firePropertyChange(CONTINUOUS_LAYOUT_PROPERTY, oldValue,
690 continuousLayout);
695 * This method sets the location of the divider. A value of 0 sets the
696 * divider to the farthest left. A value of 1 sets the divider to the
697 * farthest right.
699 * @param proportionalLocation A double that describes the location of the
700 * divider.
702 * @throws IllegalArgumentException DOCUMENT ME!
704 public void setDividerLocation(double proportionalLocation)
706 if (proportionalLocation > 1 || proportionalLocation < 0)
707 throw new IllegalArgumentException
708 ("proportion has to be between 0 and 1.");
710 int max = (orientation == HORIZONTAL_SPLIT) ? getWidth() : getHeight();
711 setDividerLocation((int) (proportionalLocation * max));
715 * This method sets the location of the divider.
717 * @param location The location of the divider. The negative value forces to
718 * compute the new location from the preferred sizes of the split
719 * pane components.
721 public void setDividerLocation(int location)
723 if (ui != null && location != getDividerLocation())
725 int oldLocation = getDividerLocation();
726 if (location < 0)
727 ((SplitPaneUI) ui).resetToPreferredSizes(this);
728 else
729 ((SplitPaneUI) ui).setDividerLocation(this, location);
731 firePropertyChange(DIVIDER_LOCATION_PROPERTY, oldLocation,
732 getDividerLocation());
737 * This method sets the size of the divider.
739 * @param newSize The size of the divider.
741 public void setDividerSize(int newSize)
743 clientDividerSizeSet = true;
744 if (newSize != dividerSize)
746 int oldSize = dividerSize;
747 dividerSize = newSize;
748 firePropertyChange(DIVIDER_SIZE_PROPERTY, oldSize, dividerSize);
752 // This doesn't appear to do anything when set from user side.
753 // so it probably is only used from the UI side to change the
754 // lastDividerLocation var.
757 * This method sets the last location of the divider.
759 * @param newLastLocation The last location of the divider.
761 public void setLastDividerLocation(int newLastLocation)
763 if (newLastLocation != lastDividerLocation)
765 int oldValue = lastDividerLocation;
766 lastDividerLocation = newLastLocation;
767 firePropertyChange(LAST_DIVIDER_LOCATION_PROPERTY, oldValue,
768 lastDividerLocation);
773 * This method sets the left component.
775 * @param comp The left component.
777 public void setLeftComponent(Component comp)
779 if (comp != null)
780 add(comp, LEFT);
781 else
782 remove (leftComponent);
786 * This method sets whether the divider has one touch expandable buttons.
787 * The one touch expandable buttons can expand the size of either component
788 * to the maximum allowed size.
790 * @param newValue Whether the divider will have one touch expandable
791 * buttons.
793 public void setOneTouchExpandable(boolean newValue)
795 clientOneTouchExpandableSet = true;
796 if (newValue != oneTouchExpandable)
798 boolean oldValue = oneTouchExpandable;
799 oneTouchExpandable = newValue;
800 firePropertyChange(ONE_TOUCH_EXPANDABLE_PROPERTY, oldValue,
801 oneTouchExpandable);
806 * Sets the orientation for the <code>JSplitPane</code> and sends a
807 * {@link PropertyChangeEvent} (with the property name
808 * {@link #ORIENTATION_PROPERTY}) to all registered listeners.
810 * @param orientation the orientation (either {@link #HORIZONTAL_SPLIT}
811 * or {@link #VERTICAL_SPLIT}).
813 * @throws IllegalArgumentException if <code>orientation</code> is not one of
814 * the listed values.
816 public void setOrientation(int orientation)
818 if (orientation != HORIZONTAL_SPLIT && orientation != VERTICAL_SPLIT)
819 throw new IllegalArgumentException
820 ("orientation must be one of VERTICAL_SPLIT, HORIZONTAL_SPLIT");
821 if (orientation != this.orientation)
823 int oldOrientation = this.orientation;
824 this.orientation = orientation;
825 firePropertyChange(ORIENTATION_PROPERTY, oldOrientation,
826 this.orientation);
831 * This method determines how extra space will be distributed among the left
832 * and right components. A value of 0 will allocate all extra space to the
833 * right component. A value of 1 indicates that all extra space will go to
834 * the left component. A value in between 1 and 0 will split the space
835 * accordingly.
837 * @param value The resize weight.
839 public void setResizeWeight(double value)
841 if (value < 0.0 || value > 1.0)
842 throw new IllegalArgumentException("Value outside permitted range.");
843 if (this.resizeWeight != value)
845 double old = resizeWeight;
846 resizeWeight = value;
847 firePropertyChange(RESIZE_WEIGHT_PROPERTY, old, value);
852 * This method sets the right component.
854 * @param comp The right component.
856 public void setRightComponent(Component comp)
858 if (comp != null)
859 add(comp, RIGHT);
860 else
861 remove (rightComponent);
865 * This method sets the top component.
867 * @param comp The top component.
869 public void setTopComponent(Component comp)
871 if (comp != null)
872 add(comp, TOP);
873 else
874 add(new JButton("left button"), TOP);
878 * This method sets the UI used by the JSplitPane.
880 * @param ui The UI to use.
882 public void setUI(SplitPaneUI ui)
884 super.setUI(ui);
888 * This method resets the UI to the one specified by the current Look and
889 * Feel.
891 public void updateUI()
893 setUI((SplitPaneUI) UIManager.getUI(this));
897 * This method returns a string identifier to determine which UI class it
898 * needs.
900 * @return A string that identifies it's UI class.
902 public String getUIClassID()
904 return "SplitPaneUI";
908 * Helper method for
909 * {@link LookAndFeel#installProperty(JComponent, String, Object)}.
911 * @param propertyName the name of the property
912 * @param value the value of the property
914 * @throws IllegalArgumentException if the specified property cannot be set
915 * by this method
916 * @throws ClassCastException if the property value does not match the
917 * property type
918 * @throws NullPointerException if <code>c</code> or
919 * <code>propertyValue</code> is <code>null</code>
921 void setUIProperty(String propertyName, Object value)
923 if (propertyName.equals("dividerSize"))
925 if (! clientDividerSizeSet)
927 setDividerSize(((Integer) value).intValue());
928 clientDividerSizeSet = false;
931 else if (propertyName.equals("oneTouchExpandable"))
933 if (! clientOneTouchExpandableSet)
935 setOneTouchExpandable(((Boolean) value).booleanValue());
936 clientOneTouchExpandableSet = false;
939 else
941 super.setUIProperty(propertyName, value);