Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / swing / JSplitPane.java
blob70feefab26e96511282c3e43459578a2dc044b89
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 super.addImpl(comp, constraints, index);
348 invalidate();
349 layout();
353 * DOCUMENT ME!
355 * @return DOCUMENT ME!
357 public AccessibleContext getAccessibleContext()
359 if (accessibleContext == null)
360 accessibleContext = new AccessibleJSplitPane();
362 return accessibleContext;
366 * This method returns the bottom component.
368 * @return The bottom component.
370 public Component getBottomComponent()
372 return rightComponent;
376 * This method returns the location of the divider. This method is passed to
377 * the UI.
379 * @return The location of the divider.
381 public int getDividerLocation()
383 if (ui != null)
384 return ((SplitPaneUI) ui).getDividerLocation(this);
385 else
386 return -1;
390 * This method returns the size of the divider.
392 * @return The size of the divider.
394 public int getDividerSize()
396 return dividerSize;
400 * This method returns the last divider location.
402 * @return The last divider location.
404 public int getLastDividerLocation()
406 return lastDividerLocation;
410 * This method returns the left component.
412 * @return The left component.
414 public Component getLeftComponent()
416 return leftComponent;
420 * This method returns the maximum divider location. This method is passed
421 * to the UI.
423 * @return DOCUMENT ME!
425 public int getMaximumDividerLocation()
427 if (ui != null)
428 return ((SplitPaneUI) ui).getMaximumDividerLocation(this);
429 else
430 return -1;
434 * This method returns the minimum divider location. This method is passed
435 * to the UI.
437 * @return The minimum divider location.
439 public int getMinimumDividerLocation()
441 if (ui != null)
442 return ((SplitPaneUI) ui).getMinimumDividerLocation(this);
443 else
444 return -1;
448 * This method returns the orientation that the JSplitPane is using.
450 * @return The current orientation.
452 public int getOrientation()
454 return orientation;
458 * This method returns the current resize weight.
460 * @return The current resize weight.
462 public double getResizeWeight()
464 return resizeWeight;
468 * This method returns the right component.
470 * @return The right component.
472 public Component getRightComponent()
474 return rightComponent;
478 * This method returns the top component.
480 * @return The top component.
482 public Component getTopComponent()
484 return leftComponent;
488 * This method returns the UI.
490 * @return The UI.
492 public SplitPaneUI getUI()
494 return (SplitPaneUI) ui;
498 * This method returns true if the JSplitPane is using a continuousLayout.
500 * @return True if using a continuousLayout.
502 public boolean isContinuousLayout()
504 return continuousLayout;
508 * This method returns true if the divider has one touch expandable buttons.
510 * @return True if one touch expandable is used.
512 public boolean isOneTouchExpandable()
514 return oneTouchExpandable;
518 * This method returns true.
520 * @return true.
522 public boolean isValidateRoot()
524 return true;
528 * This method overrides JComponent's paintChildren so the UI can be
529 * messaged when the children have finished painting.
531 * @param g The Graphics object to paint with.
533 protected void paintChildren(Graphics g)
535 super.paintChildren(g);
536 if (ui != null)
537 ((SplitPaneUI) ui).finishedPaintingChildren(this, g);
541 * This method returns a String that describes this JSplitPane. The string
542 * is primarily used for debugging purposes.
544 * @return A String used for debugging purposes.
546 protected String paramString()
548 return "JSplitPane";
552 * This method removes the given component from the JSplitPane.
554 * @param component The Component to remove.
556 public void remove(Component component)
558 if (component == leftComponent)
559 leftComponent = null;
560 else if (component == rightComponent)
561 rightComponent = null;
562 super.remove(component);
566 * This method removes the component at the given index.
568 * @param index The index of the component to remove.
570 public void remove(int index)
572 Component component = getComponent(index);
573 if (component == leftComponent)
574 leftComponent = null;
575 else if (component == rightComponent)
576 rightComponent = null;
577 super.remove(index);
581 * This method removes all components from the JSplitPane.
583 public void removeAll()
585 leftComponent = null;
586 rightComponent = null;
587 super.removeAll();
591 * This method resets all children of the JSplitPane to their preferred
592 * sizes.
594 public void resetToPreferredSizes()
596 if (ui != null)
597 ((SplitPaneUI) ui).resetToPreferredSizes(this);
601 * This method sets the bottom component.
603 * @param comp The Component to be placed at the bottom.
605 public void setBottomComponent(Component comp)
607 if (comp != null)
608 add(comp, BOTTOM);
609 else
610 add(new JButton("right button"), BOTTOM);
614 * This method sets the layout mode for the JSplitPane.
616 * @param newContinuousLayout Whether the JSplitPane is in continuousLayout
617 * mode.
619 public void setContinuousLayout(boolean newContinuousLayout)
621 if (newContinuousLayout != continuousLayout)
623 boolean oldValue = continuousLayout;
624 continuousLayout = newContinuousLayout;
625 firePropertyChange(CONTINUOUS_LAYOUT_PROPERTY, oldValue,
626 continuousLayout);
631 * This method sets the location of the divider. A value of 0 sets the
632 * divider to the farthest left. A value of 1 sets the divider to the
633 * farthest right.
635 * @param proportionalLocation A double that describes the location of the
636 * divider.
638 * @throws IllegalArgumentException DOCUMENT ME!
640 public void setDividerLocation(double proportionalLocation)
642 if (proportionalLocation > 1 || proportionalLocation < 0)
643 throw new IllegalArgumentException
644 ("proportion has to be between 0 and 1.");
646 int max = (orientation == HORIZONTAL_SPLIT) ? getWidth() : getHeight();
647 setDividerLocation((int) (proportionalLocation * max));
651 * This method sets the location of the divider.
653 * @param location The location of the divider.
655 public void setDividerLocation(int location)
657 if (ui != null && location != getDividerLocation())
659 int oldLocation = getDividerLocation();
660 ((SplitPaneUI) ui).setDividerLocation(this, location);
661 firePropertyChange(DIVIDER_LOCATION_PROPERTY, oldLocation, location);
666 * This method sets the size of the divider.
668 * @param newSize The size of the divider.
670 public void setDividerSize(int newSize)
672 if (newSize != dividerSize)
674 int oldSize = dividerSize;
675 dividerSize = newSize;
676 firePropertyChange(DIVIDER_SIZE_PROPERTY, oldSize, dividerSize);
680 // This doesn't appear to do anything when set from user side.
681 // so it probably is only used from the UI side to change the
682 // lastDividerLocation var.
685 * This method sets the last location of the divider.
687 * @param newLastLocation The last location of the divider.
689 public void setLastDividerLocation(int newLastLocation)
691 if (newLastLocation != lastDividerLocation)
693 int oldValue = lastDividerLocation;
694 lastDividerLocation = newLastLocation;
695 firePropertyChange(LAST_DIVIDER_LOCATION_PROPERTY, oldValue,
696 lastDividerLocation);
701 * This method sets the left component.
703 * @param comp The left component.
705 public void setLeftComponent(Component comp)
707 if (comp != null)
708 add(comp, LEFT);
709 else
710 remove (leftComponent);
714 * This method sets whether the divider has one touch expandable buttons.
715 * The one touch expandable buttons can expand the size of either component
716 * to the maximum allowed size.
718 * @param newValue Whether the divider will have one touch expandable
719 * buttons.
721 public void setOneTouchExpandable(boolean newValue)
723 if (newValue != oneTouchExpandable)
725 boolean oldValue = oneTouchExpandable;
726 oneTouchExpandable = newValue;
727 firePropertyChange(ONE_TOUCH_EXPANDABLE_PROPERTY, oldValue,
728 oneTouchExpandable);
733 * This method sets the orientation of the JSplitPane.
735 * @param orientation The orientation of the JSplitPane.
737 * @throws IllegalArgumentException DOCUMENT ME!
739 public void setOrientation(int orientation)
741 if (orientation != HORIZONTAL_SPLIT && orientation != VERTICAL_SPLIT)
742 throw new IllegalArgumentException
743 ("orientation must be one of VERTICAL_SPLIT, HORIZONTAL_SPLIT");
744 if (orientation != this.orientation)
746 int oldOrientation = this.orientation;
747 this.orientation = orientation;
748 firePropertyChange(ORIENTATION_PROPERTY, oldOrientation,
749 this.orientation);
754 * This method determines how extra space will be distributed among the left
755 * and right components. A value of 0 will allocate all extra space to the
756 * right component. A value of 1 indicates that all extra space will go to
757 * the left component. A value in between 1 and 0 will split the space
758 * accordingly.
760 * @param value The resize weight.
762 public void setResizeWeight(double value)
764 resizeWeight = value;
768 * This method sets the right component.
770 * @param comp The right component.
772 public void setRightComponent(Component comp)
774 if (comp != null)
775 add(comp, RIGHT);
776 else
777 remove (rightComponent);
781 * This method sets the top component.
783 * @param comp The top component.
785 public void setTopComponent(Component comp)
787 if (comp != null)
788 add(comp, TOP);
789 else
790 add(new JButton("left button"), TOP);
794 * This method sets the UI used by the JSplitPane.
796 * @param ui The UI to use.
798 public void setUI(SplitPaneUI ui)
800 super.setUI(ui);
804 * This method resets the UI to the one specified by the current Look and
805 * Feel.
807 public void updateUI()
809 setUI((SplitPaneUI) UIManager.getUI(this));
810 invalidate();
811 repaint();
815 * This method returns a string identifier to determine which UI class it
816 * needs.
818 * @return A string that identifies it's UI class.
820 public String getUIClassID()
822 return "SplitPaneUI";