Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / JSplitPane.java
blob9cdad16a7cff59697e8a8b18d1f2715352cb0145
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 protected class AccessibleJSplitPane extends JComponent.AccessibleJComponent
63 implements AccessibleValue
65 private static final long serialVersionUID = -1788116871416305366L;
67 /**
68 * Creates a new AccessibleJSplitPane object.
70 * @param value0 DOCUMENT ME!
72 protected AccessibleJSplitPane()
76 /**
77 * DOCUMENT ME!
79 * @return DOCUMENT ME!
81 public AccessibleStateSet getAccessibleStateSet()
83 return null;
86 /**
87 * DOCUMENT ME!
89 * @return DOCUMENT ME!
91 public AccessibleRole getAccessibleRole()
93 return null;
96 /**
97 * DOCUMENT ME!
99 * @return DOCUMENT ME!
101 public AccessibleValue getAccessibleValue()
103 return null;
107 * DOCUMENT ME!
109 * @return DOCUMENT ME!
111 public Number getCurrentAccessibleValue()
113 return null;
117 * DOCUMENT ME!
119 * @param value0 DOCUMENT ME!
121 * @return DOCUMENT ME!
123 public boolean setCurrentAccessibleValue(Number value0)
125 return false;
129 * DOCUMENT ME!
131 * @return DOCUMENT ME!
133 public Number getMinimumAccessibleValue()
135 return null;
139 * DOCUMENT ME!
141 * @return DOCUMENT ME!
143 public Number getMaximumAccessibleValue()
145 return null;
149 private static final long serialVersionUID = -5634142046175988380L;
151 /** The constraints string used to add components to the bottom. */
152 public static final String BOTTOM = "bottom";
154 /** The property fired when the continuousLayout property changes. */
155 public static final String CONTINUOUS_LAYOUT_PROPERTY = "continuousLayout";
157 /** The property fired when the divider property changes. */
158 public static final String DIVIDER = "divider";
160 /** The property fired when the divider location property changes. */
161 public static final String DIVIDER_LOCATION_PROPERTY = "dividerLocation";
163 /** The property fired when the divider size property changes. */
164 public static final String DIVIDER_SIZE_PROPERTY = "dividerSize";
167 * The value of the orientation when the components are split horizontally.
169 public static final int HORIZONTAL_SPLIT = 1;
171 /** The property fired when the last divider location property changes. */
172 public static final String LAST_DIVIDER_LOCATION_PROPERTY = "lastDividerLocation";
174 /** The constraints string used to add components to the left. */
175 public static final String LEFT = "left";
177 /** The property fired when the one touch expandable property changes. */
178 public static final String ONE_TOUCH_EXPANDABLE_PROPERTY = "oneTouchExpandable";
180 /** The property fired when the orientation property changes. */
181 public static final String ORIENTATION_PROPERTY = "orientation";
183 /** The property fired when the resize weight property changes. */
184 public static final String RESIZE_WEIGHT_PROPERTY = "resizeWeight";
186 /** The constraints string used to add components to the right. */
187 public static final String RIGHT = "right";
189 /** The constraints string used to add components to the top. */
190 public static final String TOP = "top";
192 /** The value of the orientation when the components are split vertically. */
193 public static final int VERTICAL_SPLIT = 0;
195 /** Whether the JSplitPane uses continuous layout. */
196 protected boolean continuousLayout;
198 /** Whether the JSplitPane uses one touch expandable buttons. */
199 protected boolean oneTouchExpandable = false;
201 // This is the master dividerSize variable and sets the BasicSplitPaneDivider one accordingly
203 /** The size of the divider. */
204 protected int dividerSize = 10;
206 /** The last location of the divider given by the UI. */
207 protected int lastDividerLocation;
209 /** The orientation of the JSplitPane. */
210 protected int orientation;
212 /** The component on the top or left. */
213 protected Component leftComponent;
215 /** The component on the right or bottom. */
216 protected Component rightComponent;
218 /** Determines how extra space should be allocated. */
219 private transient double resizeWeight;
222 * Creates a new JSplitPane object with the given orientation, layout mode,
223 * and left and right components.
225 * @param newOrientation The orientation to use.
226 * @param newContinuousLayout The layout mode to use.
227 * @param newLeftComponent The left component.
228 * @param newRightComponent The right component.
230 * @throws IllegalArgumentException DOCUMENT ME!
232 public JSplitPane(int newOrientation, boolean newContinuousLayout,
233 Component newLeftComponent, Component newRightComponent)
235 if (newOrientation != HORIZONTAL_SPLIT && newOrientation != VERTICAL_SPLIT)
236 throw new IllegalArgumentException("orientation is invalid.");
237 orientation = newOrientation;
238 continuousLayout = newContinuousLayout;
239 setLeftComponent(newLeftComponent);
240 setRightComponent(newRightComponent);
242 updateUI();
246 * Creates a new JSplitPane object using nonContinuousLayout mode, the given
247 * orientation and left and right components.
249 * @param newOrientation The orientation to use.
250 * @param newLeftComponent The left component.
251 * @param newRightComponent The right component.
253 public JSplitPane(int newOrientation, Component newLeftComponent,
254 Component newRightComponent)
256 this(newOrientation, false, newLeftComponent, newRightComponent);
260 * Creates a new JSplitPane object with the given layout mode and
261 * orientation.
263 * @param newOrientation The orientation to use.
264 * @param newContinuousLayout The layout mode to use.
266 public JSplitPane(int newOrientation, boolean newContinuousLayout)
268 this(newOrientation, newContinuousLayout, null, null);
272 * Creates a new JSplitPane object using a nonContinuousLayout mode and the
273 * given orientation.
275 * @param newOrientation The orientation to use.
277 public JSplitPane(int newOrientation)
279 this(newOrientation, false, null, null);
283 * Creates a new JSplitPane object using HORIZONTAL_SPLIT and a
284 * nonContinuousLayout mode.
286 public JSplitPane()
288 this(HORIZONTAL_SPLIT, false, null, null);
292 * This method adds a component to the JSplitPane. The constraints object is
293 * a string that identifies where this component should go. If the
294 * constraints is not a known one, it will throw an
295 * IllegalArgumentException. The valid constraints are LEFT, TOP, RIGHT,
296 * BOTTOM and DIVIDER.
298 * @param comp The component to add.
299 * @param constraints The constraints string to use.
300 * @param index Where to place to component in the list of components.
302 * @throws IllegalArgumentException When the constraints is not a known identifier.
304 protected void addImpl(Component comp, Object constraints, int index)
306 int left = 0;
307 int right = 1;
308 int div = 2;
309 int place;
310 if (constraints == null)
312 if (leftComponent == null)
313 constraints = LEFT;
314 else if (rightComponent == null)
315 constraints = RIGHT;
318 if (constraints instanceof String)
320 String placement = (String) constraints;
322 if (placement.equals(BOTTOM) || placement.equals(RIGHT))
324 if (rightComponent != null)
325 remove(rightComponent);
326 rightComponent = comp;
328 else if (placement.equals(LEFT) || placement.equals(TOP))
330 if (leftComponent != null)
331 remove(leftComponent);
332 leftComponent = comp;
334 else if (placement.equals(DIVIDER))
335 constraints = null;
336 else
337 throw new IllegalArgumentException("Constraints is not a known identifier.");
339 super.addImpl(comp, constraints, index);
341 invalidate();
342 layout();
346 * DOCUMENT ME!
348 * @return DOCUMENT ME!
350 public AccessibleContext getAccessibleContext()
352 if (accessibleContext == null)
353 accessibleContext = new AccessibleJSplitPane();
355 return accessibleContext;
359 * This method returns the bottom component.
361 * @return The bottom component.
363 public Component getBottomComponent()
365 return rightComponent;
369 * This method returns the location of the divider. This method is passed to
370 * the UI.
372 * @return The location of the divider.
374 public int getDividerLocation()
376 if (ui != null)
377 return ((SplitPaneUI) ui).getDividerLocation(this);
378 else
379 return -1;
383 * This method returns the size of the divider.
385 * @return The size of the divider.
387 public int getDividerSize()
389 return dividerSize;
393 * This method returns the last divider location.
395 * @return The last divider location.
397 public int getLastDividerLocation()
399 return lastDividerLocation;
403 * This method returns the left component.
405 * @return The left component.
407 public Component getLeftComponent()
409 return leftComponent;
413 * This method returns the maximum divider location. This method is passed
414 * to the UI.
416 * @return DOCUMENT ME!
418 public int getMaximumDividerLocation()
420 if (ui != null)
421 return ((SplitPaneUI) ui).getMaximumDividerLocation(this);
422 else
423 return -1;
427 * This method returns the minimum divider location. This method is passed
428 * to the UI.
430 * @return The minimum divider location.
432 public int getMinimumDividerLocation()
434 if (ui != null)
435 return ((SplitPaneUI) ui).getMinimumDividerLocation(this);
436 else
437 return -1;
441 * This method returns the orientation that the JSplitPane is using.
443 * @return The current orientation.
445 public int getOrientation()
447 return orientation;
451 * This method returns the current resize weight.
453 * @return The current resize weight.
455 public double getResizeWeight()
457 return resizeWeight;
461 * This method returns the right component.
463 * @return The right component.
465 public Component getRightComponent()
467 return rightComponent;
471 * This method returns the top component.
473 * @return The top component.
475 public Component getTopComponent()
477 return leftComponent;
481 * This method returns the UI.
483 * @return The UI.
485 public SplitPaneUI getUI()
487 return (SplitPaneUI) ui;
491 * This method returns true if the JSplitPane is using a continuousLayout.
493 * @return True if using a continuousLayout.
495 public boolean isContinuousLayout()
497 return continuousLayout;
501 * This method returns true if the divider has one touch expandable buttons.
503 * @return True if one touch expandable is used.
505 public boolean isOneTouchExpandable()
507 return oneTouchExpandable;
511 * This method returns true.
513 * @return true.
515 public boolean isValidateRoot()
517 return true;
521 * This method overrides JComponent's paintChildren so the UI can be
522 * messaged when the children have finished painting.
524 * @param g The Graphics object to paint with.
526 protected void paintChildren(Graphics g)
528 super.paintChildren(g);
529 if (ui != null)
530 ((SplitPaneUI) ui).finishedPaintingChildren(this, g);
534 * This method returns a String that describes this JSplitPane. The string
535 * is primarily used for debugging purposes.
537 * @return A String used for debugging purposes.
539 protected String paramString()
541 return "JSplitPane";
545 * This method removes the given component from the JSplitPane.
547 * @param component The Component to remove.
549 public void remove(Component component)
551 if (component == leftComponent)
552 leftComponent = null;
553 else if (component == rightComponent)
554 rightComponent = null;
555 super.remove(component);
559 * This method removes the component at the given index.
561 * @param index The index of the component to remove.
563 public void remove(int index)
565 Component component = getComponent(index);
566 if (component == leftComponent)
567 leftComponent = null;
568 else if (component == rightComponent)
569 rightComponent = null;
570 super.remove(index);
574 * This method removes all components from the JSplitPane.
576 public void removeAll()
578 leftComponent = null;
579 rightComponent = null;
580 super.removeAll();
584 * This method resets all children of the JSplitPane to their preferred
585 * sizes.
587 public void resetToPreferredSizes()
589 if (ui != null)
590 ((SplitPaneUI) ui).resetToPreferredSizes(this);
594 * This method sets the bottom component.
596 * @param comp The Component to be placed at the bottom.
598 public void setBottomComponent(Component comp)
600 if (comp != null)
601 add(comp, BOTTOM);
602 else
603 add(new JButton("right button"), BOTTOM);
607 * This method sets the layout mode for the JSplitPane.
609 * @param newContinuousLayout Whether the JSplitPane is in continuousLayout
610 * mode.
612 public void setContinuousLayout(boolean newContinuousLayout)
614 if (newContinuousLayout != continuousLayout)
616 boolean oldValue = continuousLayout;
617 continuousLayout = newContinuousLayout;
618 firePropertyChange(CONTINUOUS_LAYOUT_PROPERTY, oldValue,
619 continuousLayout);
624 * This method sets the location of the divider. A value of 0 sets the
625 * divider to the farthest left. A value of 1 sets the divider to the
626 * farthest right.
628 * @param proportionalLocation A double that describes the location of the
629 * divider.
631 * @throws IllegalArgumentException DOCUMENT ME!
633 public void setDividerLocation(double proportionalLocation)
635 if (proportionalLocation > 1 || proportionalLocation < 0)
636 throw new IllegalArgumentException("proportion has to be between 0 and 1.");
638 int max = (orientation == HORIZONTAL_SPLIT) ? getWidth() : getHeight();
639 setDividerLocation((int) (proportionalLocation * max));
643 * This method sets the location of the divider.
645 * @param location The location of the divider.
647 public void setDividerLocation(int location)
649 if (ui != null && location != getDividerLocation())
651 int oldLocation = getDividerLocation();
652 ((SplitPaneUI) ui).setDividerLocation(this, location);
653 firePropertyChange(DIVIDER_LOCATION_PROPERTY, oldLocation, location);
658 * This method sets the size of the divider.
660 * @param newSize The size of the divider.
662 public void setDividerSize(int newSize)
664 if (newSize != dividerSize)
666 int oldSize = dividerSize;
667 dividerSize = newSize;
668 firePropertyChange(DIVIDER_SIZE_PROPERTY, oldSize, dividerSize);
672 // This doesn't appear to do anything when set from user side.
673 // so it probably is only used from the UI side to change the
674 // lastDividerLocation var.
677 * This method sets the last location of the divider.
679 * @param newLastLocation The last location of the divider.
681 public void setLastDividerLocation(int newLastLocation)
683 if (newLastLocation != lastDividerLocation)
685 int oldValue = lastDividerLocation;
686 lastDividerLocation = newLastLocation;
687 firePropertyChange(LAST_DIVIDER_LOCATION_PROPERTY, oldValue,
688 lastDividerLocation);
693 * This method sets the left component.
695 * @param comp The left component.
697 public void setLeftComponent(Component comp)
699 if (comp != null)
700 add(comp, LEFT);
701 else
702 add(new JButton("left button"), LEFT);
706 * This method sets whether the divider has one touch expandable buttons.
707 * The one touch expandable buttons can expand the size of either component
708 * to the maximum allowed size.
710 * @param newValue Whether the divider will have one touch expandable
711 * buttons.
713 public void setOneTouchExpandable(boolean newValue)
715 if (newValue != oneTouchExpandable)
717 boolean oldValue = oneTouchExpandable;
718 oneTouchExpandable = newValue;
719 firePropertyChange(ONE_TOUCH_EXPANDABLE_PROPERTY, oldValue,
720 oneTouchExpandable);
725 * This method sets the orientation of the JSplitPane.
727 * @param orientation The orientation of the JSplitPane.
729 * @throws IllegalArgumentException DOCUMENT ME!
731 public void setOrientation(int orientation)
733 if (orientation != HORIZONTAL_SPLIT && orientation != VERTICAL_SPLIT)
734 throw new IllegalArgumentException("orientation must be one of VERTICAL_SPLIT, HORIZONTAL_SPLIT");
735 if (orientation != this.orientation)
737 int oldOrientation = this.orientation;
738 this.orientation = orientation;
739 firePropertyChange(ORIENTATION_PROPERTY, oldOrientation,
740 this.orientation);
745 * This method determines how extra space will be distributed among the left
746 * and right components. A value of 0 will allocate all extra space to the
747 * right component. A value of 1 indicates that all extra space will go to
748 * the left component. A value in between 1 and 0 will split the space
749 * accordingly.
751 * @param value The resize weight.
753 public void setResizeWeight(double value)
755 resizeWeight = value;
759 * This method sets the right component.
761 * @param comp The right component.
763 public void setRightComponent(Component comp)
765 if (comp != null)
766 add(comp, RIGHT);
767 else
768 add(new JButton("right button"), RIGHT);
772 * This method sets the top component.
774 * @param comp The top component.
776 public void setTopComponent(Component comp)
778 if (comp != null)
779 add(comp, TOP);
780 else
781 add(new JButton("left button"), TOP);
785 * This method sets the UI used by the JSplitPane.
787 * @param ui The UI to use.
789 public void setUI(SplitPaneUI ui)
791 super.setUI(ui);
795 * This method resets the UI to the one specified by the current Look and
796 * Feel.
798 public void updateUI()
800 setUI((SplitPaneUI) UIManager.getUI(this));
801 invalidate();
802 repaint();
806 * This method returns a string identifier to determine which UI class it
807 * needs.
809 * @return A string that identifies it's UI class.
811 public String getUIClassID()
813 return "SplitPaneUI";