Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / JSplitPane.java
blobdc75dfe3184417938047810a09ce63dae13abba3
1 /* JSplitPane.java --
2 Copyright (C) 2004 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;
44 import javax.accessibility.Accessible;
45 import javax.accessibility.AccessibleContext;
46 import javax.accessibility.AccessibleRole;
47 import javax.accessibility.AccessibleStateSet;
48 import javax.accessibility.AccessibleValue;
49 import javax.swing.plaf.SplitPaneUI;
51 /**
52 * This class implements JSplitPane. It is used to divide two components. By
53 * dragging the SplitPane's divider, the user can resize the two components.
54 * Note that the divider cannot resize a component to smaller than it's
55 * minimum size.
57 public class JSplitPane extends JComponent implements Accessible
59 /**
60 * DOCUMENT ME!
62 // FIXME: This inner class is a complete stub and must be implemented
63 // properly.
64 protected class AccessibleJSplitPane extends JComponent.AccessibleJComponent
65 implements AccessibleValue
67 private static final long serialVersionUID = -1788116871416305366L;
69 /**
70 * Creates a new AccessibleJSplitPane object.
72 protected AccessibleJSplitPane()
74 // Nothing to do here.
77 /**
78 * DOCUMENT ME!
80 * @return DOCUMENT ME!
82 public AccessibleStateSet getAccessibleStateSet()
84 return null;
87 /**
88 * DOCUMENT ME!
90 * @return DOCUMENT ME!
92 public AccessibleRole getAccessibleRole()
94 return null;
97 /**
98 * DOCUMENT ME!
100 * @return DOCUMENT ME!
102 public AccessibleValue getAccessibleValue()
104 return null;
108 * DOCUMENT ME!
110 * @return DOCUMENT ME!
112 public Number getCurrentAccessibleValue()
114 return null;
118 * DOCUMENT ME!
120 * @param value0 DOCUMENT ME!
122 * @return DOCUMENT ME!
124 public boolean setCurrentAccessibleValue(Number value0)
126 return false;
130 * DOCUMENT ME!
132 * @return DOCUMENT ME!
134 public Number getMinimumAccessibleValue()
136 return null;
140 * DOCUMENT ME!
142 * @return DOCUMENT ME!
144 public Number getMaximumAccessibleValue()
146 return null;
150 private static final long serialVersionUID = -5634142046175988380L;
152 /** The constraints string used to add components to the bottom. */
153 public static final String BOTTOM = "bottom";
155 /** The property fired when the continuousLayout property changes. */
156 public static final String CONTINUOUS_LAYOUT_PROPERTY = "continuousLayout";
158 /** The property fired when the divider property changes. */
159 public static final String DIVIDER = "divider";
161 /** The property fired when the divider location property changes. */
162 public static final String DIVIDER_LOCATION_PROPERTY = "dividerLocation";
164 /** The property fired when the divider size property changes. */
165 public static final String DIVIDER_SIZE_PROPERTY = "dividerSize";
168 * The value of the orientation when the components are split horizontally.
170 public static final int HORIZONTAL_SPLIT = 1;
172 /** The property fired when the last divider location property changes. */
173 public static final String LAST_DIVIDER_LOCATION_PROPERTY =
174 "lastDividerLocation";
176 /** The constraints string used to add components to the left. */
177 public static final String LEFT = "left";
179 /** The property fired when the one touch expandable property changes. */
180 public static final String ONE_TOUCH_EXPANDABLE_PROPERTY =
181 "oneTouchExpandable";
183 /** The property fired when the orientation property changes. */
184 public static final String ORIENTATION_PROPERTY = "orientation";
186 /** The property fired when the resize weight property changes. */
187 public static final String RESIZE_WEIGHT_PROPERTY = "resizeWeight";
189 /** The constraints string used to add components to the right. */
190 public static final String RIGHT = "right";
192 /** The constraints string used to add components to the top. */
193 public static final String TOP = "top";
195 /** The value of the orientation when the components are split vertically. */
196 public static final int VERTICAL_SPLIT = 0;
198 /** Whether the JSplitPane uses continuous layout. */
199 protected boolean continuousLayout;
201 /** Whether the JSplitPane uses one touch expandable buttons. */
202 protected boolean oneTouchExpandable = false;
204 // This is the master dividerSize variable and sets the
205 // BasicSplitPaneDivider one accordingly
207 /** The size of the divider. */
208 protected int dividerSize = 10;
210 /** The last location of the divider given by the UI. */
211 protected int lastDividerLocation;
213 /** The orientation of the JSplitPane. */
214 protected int orientation;
216 /** The component on the top or left. */
217 protected Component leftComponent;
219 /** The component on the right or bottom. */
220 protected Component rightComponent;
222 /** Determines how extra space should be allocated. */
223 private transient double resizeWeight;
226 * Creates a new JSplitPane object with the given orientation, layout mode,
227 * and left and right components.
229 * @param newOrientation The orientation to use.
230 * @param newContinuousLayout The layout mode to use.
231 * @param newLeftComponent The left component.
232 * @param newRightComponent The right component.
234 * @throws IllegalArgumentException DOCUMENT ME!
236 public JSplitPane(int newOrientation, boolean newContinuousLayout,
237 Component newLeftComponent, Component newRightComponent)
239 if (newOrientation != HORIZONTAL_SPLIT && newOrientation != VERTICAL_SPLIT)
240 throw new IllegalArgumentException("orientation is invalid.");
241 orientation = newOrientation;
242 continuousLayout = newContinuousLayout;
243 setLeftComponent(newLeftComponent);
244 setRightComponent(newRightComponent);
246 updateUI();
250 * Creates a new JSplitPane object using nonContinuousLayout mode, the given
251 * orientation and left and right components.
253 * @param newOrientation The orientation to use.
254 * @param newLeftComponent The left component.
255 * @param newRightComponent The right component.
257 public JSplitPane(int newOrientation, Component newLeftComponent,
258 Component newRightComponent)
260 this(newOrientation, false, newLeftComponent, newRightComponent);
264 * Creates a new JSplitPane object with the given layout mode and
265 * orientation.
267 * @param newOrientation The orientation to use.
268 * @param newContinuousLayout The layout mode to use.
270 public JSplitPane(int newOrientation, boolean newContinuousLayout)
272 this(newOrientation, newContinuousLayout, null, null);
276 * Creates a new JSplitPane object using a nonContinuousLayout mode and the
277 * given orientation.
279 * @param newOrientation The orientation to use.
281 public JSplitPane(int newOrientation)
283 this(newOrientation, false, null, null);
287 * Creates a new JSplitPane object using HORIZONTAL_SPLIT and a
288 * nonContinuousLayout mode.
290 public JSplitPane()
292 this(HORIZONTAL_SPLIT, false, new JButton("left button"),
293 new JButton("right button"));
297 * This method adds a component to the JSplitPane. The constraints object is
298 * a string that identifies where this component should go. If the
299 * constraints is not a known one, it will throw an
300 * IllegalArgumentException. The valid constraints are LEFT, TOP, RIGHT,
301 * BOTTOM and DIVIDER.
303 * @param comp The component to add.
304 * @param constraints The constraints string to use.
305 * @param index Where to place to component in the list of components.
307 * @throws IllegalArgumentException When the constraints is not a known
308 * identifier.
310 protected void addImpl(Component comp, Object constraints, int index)
312 int left = 0;
313 int right = 1;
314 int div = 2;
315 int place;
316 if (constraints == null)
318 if (leftComponent == null)
319 constraints = LEFT;
320 else if (rightComponent == null)
321 constraints = RIGHT;
324 if (constraints instanceof String)
326 String placement = (String) constraints;
328 if (placement.equals(BOTTOM) || placement.equals(RIGHT))
330 if (rightComponent != null)
331 remove(rightComponent);
332 rightComponent = comp;
334 else if (placement.equals(LEFT) || placement.equals(TOP))
336 if (leftComponent != null)
337 remove(leftComponent);
338 leftComponent = comp;
340 else if (placement.equals(DIVIDER))
341 constraints = null;
342 else
343 throw new
344 IllegalArgumentException("Constraints is not a known identifier.");
346 // If no dividerLocation has been set, then we need to trigger an
347 // initial layout.
348 if (getDividerLocation() != -1)
349 resetToPreferredSizes();
351 super.addImpl(comp, constraints, index);
356 * DOCUMENT ME!
358 * @return DOCUMENT ME!
360 public AccessibleContext getAccessibleContext()
362 if (accessibleContext == null)
363 accessibleContext = new AccessibleJSplitPane();
365 return accessibleContext;
369 * This method returns the bottom component.
371 * @return The bottom component.
373 public Component getBottomComponent()
375 return rightComponent;
379 * This method returns the location of the divider. This method is passed to
380 * the UI.
382 * @return The location of the divider.
384 public int getDividerLocation()
386 if (ui != null)
387 return ((SplitPaneUI) ui).getDividerLocation(this);
388 else
389 return -1;
393 * This method returns the size of the divider.
395 * @return The size of the divider.
397 public int getDividerSize()
399 return dividerSize;
403 * This method returns the last divider location.
405 * @return The last divider location.
407 public int getLastDividerLocation()
409 return lastDividerLocation;
413 * This method returns the left component.
415 * @return The left component.
417 public Component getLeftComponent()
419 return leftComponent;
423 * This method returns the maximum divider location. This method is passed
424 * to the UI.
426 * @return DOCUMENT ME!
428 public int getMaximumDividerLocation()
430 if (ui != null)
431 return ((SplitPaneUI) ui).getMaximumDividerLocation(this);
432 else
433 return -1;
437 * This method returns the minimum divider location. This method is passed
438 * to the UI.
440 * @return The minimum divider location.
442 public int getMinimumDividerLocation()
444 if (ui != null)
445 return ((SplitPaneUI) ui).getMinimumDividerLocation(this);
446 else
447 return -1;
451 * This method returns the orientation that the JSplitPane is using.
453 * @return The current orientation.
455 public int getOrientation()
457 return orientation;
461 * This method returns the current resize weight.
463 * @return The current resize weight.
465 public double getResizeWeight()
467 return resizeWeight;
471 * This method returns the right component.
473 * @return The right component.
475 public Component getRightComponent()
477 return rightComponent;
481 * This method returns the top component.
483 * @return The top component.
485 public Component getTopComponent()
487 return leftComponent;
491 * This method returns the UI.
493 * @return The UI.
495 public SplitPaneUI getUI()
497 return (SplitPaneUI) ui;
501 * This method returns true if the JSplitPane is using a continuousLayout.
503 * @return True if using a continuousLayout.
505 public boolean isContinuousLayout()
507 return continuousLayout;
511 * This method returns true if the divider has one touch expandable buttons.
513 * @return True if one touch expandable is used.
515 public boolean isOneTouchExpandable()
517 return oneTouchExpandable;
521 * This method returns true.
523 * @return true.
525 public boolean isValidateRoot()
527 return true;
531 * This method overrides JComponent's paintChildren so the UI can be
532 * messaged when the children have finished painting.
534 * @param g The Graphics object to paint with.
536 protected void paintChildren(Graphics g)
538 super.paintChildren(g);
539 if (ui != null)
540 ((SplitPaneUI) ui).finishedPaintingChildren(this, g);
544 * This method returns a String that describes this JSplitPane. The string
545 * is primarily used for debugging purposes.
547 * @return A String used for debugging purposes.
549 protected String paramString()
551 return "JSplitPane";
555 * This method removes the given component from the JSplitPane.
557 * @param component The Component to remove.
559 public void remove(Component component)
561 if (component == leftComponent)
562 leftComponent = null;
563 else if (component == rightComponent)
564 rightComponent = null;
565 super.remove(component);
569 * This method removes the component at the given index.
571 * @param index The index of the component to remove.
573 public void remove(int index)
575 Component component = getComponent(index);
576 if (component == leftComponent)
577 leftComponent = null;
578 else if (component == rightComponent)
579 rightComponent = null;
580 super.remove(index);
584 * This method removes all components from the JSplitPane.
586 public void removeAll()
588 leftComponent = null;
589 rightComponent = null;
590 super.removeAll();
594 * This method resets all children of the JSplitPane to their preferred
595 * sizes.
597 public void resetToPreferredSizes()
599 if (ui != null)
600 ((SplitPaneUI) ui).resetToPreferredSizes(this);
604 * This method sets the bottom component.
606 * @param comp The Component to be placed at the bottom.
608 public void setBottomComponent(Component comp)
610 if (comp != null)
611 add(comp, BOTTOM);
612 else
613 add(new JButton("right button"), BOTTOM);
617 * This method sets the layout mode for the JSplitPane.
619 * @param newContinuousLayout Whether the JSplitPane is in continuousLayout
620 * mode.
622 public void setContinuousLayout(boolean newContinuousLayout)
624 if (newContinuousLayout != continuousLayout)
626 boolean oldValue = continuousLayout;
627 continuousLayout = newContinuousLayout;
628 firePropertyChange(CONTINUOUS_LAYOUT_PROPERTY, oldValue,
629 continuousLayout);
634 * This method sets the location of the divider. A value of 0 sets the
635 * divider to the farthest left. A value of 1 sets the divider to the
636 * farthest right.
638 * @param proportionalLocation A double that describes the location of the
639 * divider.
641 * @throws IllegalArgumentException DOCUMENT ME!
643 public void setDividerLocation(double proportionalLocation)
645 if (proportionalLocation > 1 || proportionalLocation < 0)
646 throw new IllegalArgumentException
647 ("proportion has to be between 0 and 1.");
649 int max = (orientation == HORIZONTAL_SPLIT) ? getWidth() : getHeight();
650 setDividerLocation((int) (proportionalLocation * max));
654 * This method sets the location of the divider.
656 * @param location The location of the divider.
658 public void setDividerLocation(int location)
660 if (ui != null && location != getDividerLocation())
662 int oldLocation = getDividerLocation();
663 ((SplitPaneUI) ui).setDividerLocation(this, location);
664 firePropertyChange(DIVIDER_LOCATION_PROPERTY, oldLocation, location);
669 * This method sets the size of the divider.
671 * @param newSize The size of the divider.
673 public void setDividerSize(int newSize)
675 if (newSize != dividerSize)
677 int oldSize = dividerSize;
678 dividerSize = newSize;
679 firePropertyChange(DIVIDER_SIZE_PROPERTY, oldSize, dividerSize);
683 // This doesn't appear to do anything when set from user side.
684 // so it probably is only used from the UI side to change the
685 // lastDividerLocation var.
688 * This method sets the last location of the divider.
690 * @param newLastLocation The last location of the divider.
692 public void setLastDividerLocation(int newLastLocation)
694 if (newLastLocation != lastDividerLocation)
696 int oldValue = lastDividerLocation;
697 lastDividerLocation = newLastLocation;
698 firePropertyChange(LAST_DIVIDER_LOCATION_PROPERTY, oldValue,
699 lastDividerLocation);
704 * This method sets the left component.
706 * @param comp The left component.
708 public void setLeftComponent(Component comp)
710 if (comp != null)
711 add(comp, LEFT);
712 else
713 remove (leftComponent);
717 * This method sets whether the divider has one touch expandable buttons.
718 * The one touch expandable buttons can expand the size of either component
719 * to the maximum allowed size.
721 * @param newValue Whether the divider will have one touch expandable
722 * buttons.
724 public void setOneTouchExpandable(boolean newValue)
726 if (newValue != oneTouchExpandable)
728 boolean oldValue = oneTouchExpandable;
729 oneTouchExpandable = newValue;
730 firePropertyChange(ONE_TOUCH_EXPANDABLE_PROPERTY, oldValue,
731 oneTouchExpandable);
736 * This method sets the orientation of the JSplitPane.
738 * @param orientation The orientation of the JSplitPane.
740 * @throws IllegalArgumentException DOCUMENT ME!
742 public void setOrientation(int orientation)
744 if (orientation != HORIZONTAL_SPLIT && orientation != VERTICAL_SPLIT)
745 throw new IllegalArgumentException
746 ("orientation must be one of VERTICAL_SPLIT, HORIZONTAL_SPLIT");
747 if (orientation != this.orientation)
749 int oldOrientation = this.orientation;
750 this.orientation = orientation;
751 firePropertyChange(ORIENTATION_PROPERTY, oldOrientation,
752 this.orientation);
757 * This method determines how extra space will be distributed among the left
758 * and right components. A value of 0 will allocate all extra space to the
759 * right component. A value of 1 indicates that all extra space will go to
760 * the left component. A value in between 1 and 0 will split the space
761 * accordingly.
763 * @param value The resize weight.
765 public void setResizeWeight(double value)
767 resizeWeight = value;
771 * This method sets the right component.
773 * @param comp The right component.
775 public void setRightComponent(Component comp)
777 if (comp != null)
778 add(comp, RIGHT);
779 else
780 remove (rightComponent);
784 * This method sets the top component.
786 * @param comp The top component.
788 public void setTopComponent(Component comp)
790 if (comp != null)
791 add(comp, TOP);
792 else
793 add(new JButton("left button"), TOP);
797 * This method sets the UI used by the JSplitPane.
799 * @param ui The UI to use.
801 public void setUI(SplitPaneUI ui)
803 super.setUI(ui);
807 * This method resets the UI to the one specified by the current Look and
808 * Feel.
810 public void updateUI()
812 setUI((SplitPaneUI) UIManager.getUI(this));
813 invalidate();
814 repaint();
818 * This method returns a string identifier to determine which UI class it
819 * needs.
821 * @return A string that identifies it's UI class.
823 public String getUIClassID()
825 return "SplitPaneUI";