Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / swing / JToolBar.java
bloba508ee6d8e7e887c10fd35ec8b16aa403a29125f
1 /* JToolBar.java --
2 Copyright (C) 2002, 2004, 2005 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.Container;
43 import java.awt.Dimension;
44 import java.awt.Graphics;
45 import java.awt.Insets;
46 import java.awt.LayoutManager;
47 import java.beans.PropertyChangeListener;
49 import javax.accessibility.Accessible;
50 import javax.accessibility.AccessibleContext;
51 import javax.accessibility.AccessibleRole;
52 import javax.accessibility.AccessibleStateSet;
53 import javax.swing.JButton;
54 import javax.swing.plaf.ToolBarUI;
56 /**
57 * JToolBar is a component that provides a toolbar to Swing programs. Users
58 * can add buttons (or actions that will be represented by JButtons) as well
59 * as other components to the JToolBar. JToolBars can be dragged in and out
60 * of their parent components. If the JToolBar is dragged out of the parent,
61 * then it will be displayed in its own RootPaneContainer. For dragging to
62 * work properly, JToolBars need to be placed in a Container that has a
63 * BorderLayout. That parent Container cannot have components in the NORTH,
64 * EAST, SOUTH, or WEST components (that is not the JToolBar).
66 public class JToolBar extends JComponent implements SwingConstants, Accessible
68 /**
69 * AccessibleJToolBar
71 // FIXME: This inner class is a complete stub and must be implemented
72 // properly.
73 protected class AccessibleJToolBar extends AccessibleJComponent
75 /** DOCUMENT ME! */
76 private static final long serialVersionUID = -5516888265903814215L;
78 /**
79 * Constructor AccessibleJToolBar
81 protected AccessibleJToolBar()
83 // Nothing to do here.
86 /**
87 * getAccessibleStateSet
89 * @return AccessibleStateSet
91 public AccessibleStateSet getAccessibleStateSet()
93 return null; // TODO
96 /**
97 * getAccessibleRole
99 * @return AccessibleRole
101 public AccessibleRole getAccessibleRole()
103 return AccessibleRole.TOOL_BAR;
108 * This is the private JToolBar layout manager.
110 private class DefaultToolBarLayout implements LayoutManager
113 * This method is called when a new component is added to the container.
115 * @param name The name of the component added.
116 * @param comp The component that was added.
118 public void addLayoutComponent(String name, Component comp)
120 // Do nothing.
124 * This method is called to lay out the given container to position and
125 * size the child components.
127 * @param c The container to lay out.
129 * @throws Error DOCUMENT ME!
131 public void layoutContainer(Container c)
133 if (! (c instanceof JToolBar))
134 throw new Error("DefaultToolBarLayout can only be used on JToolBars.");
135 Insets insets = getInsets();
136 Insets margin = getMargin();
137 int middle;
138 if (margin != null)
140 insets.left += margin.left;
141 insets.top += margin.top;
142 insets.bottom += margin.bottom;
143 insets.right += margin.right;
145 Component[] components = c.getComponents();
146 Dimension tdims = c.getSize();
147 int start = 0;
148 Dimension pref;
150 if (getOrientation() == SwingUtilities.HORIZONTAL)
152 start += insets.left;
153 for (int i = 0; i < components.length; i++)
155 if (components[i] != null && components[i].isVisible())
157 pref = components[i].getPreferredSize();
158 if (pref != null)
160 middle = (tdims.height - pref.height) / 2;
161 components[i].setBounds(start, middle, pref.width,
162 pref.height);
163 start += pref.width;
168 else
170 start += insets.top;
171 for (int i = 0; i < components.length; i++)
173 if (components[i] != null && components[i].isVisible())
175 pref = components[i].getPreferredSize();
176 if (pref != null)
178 middle = (tdims.width - pref.width) / 2;
179 components[i].setBounds(middle, start, pref.width,
180 pref.height);
181 start += pref.height;
189 * This method returns the minimum size of the given container given the
190 * child components.
192 * @param parent The container to measure.
194 * @return The minimum size of the given container.
196 public Dimension minimumLayoutSize(Container parent)
198 return preferredLayoutSize(parent);
202 * This method returns the preferred size of the given container given the
203 * child components.
205 * @param parent The container to measure.
207 * @return The preferred size of the given container.
209 public Dimension preferredLayoutSize(Container parent)
211 int orientation = getOrientation();
212 Component[] components = getComponents();
214 int limit = 0;
215 int total = 0;
216 Dimension dims;
218 int w = 0;
219 int h = 0;
221 if (orientation == SwingConstants.HORIZONTAL)
223 for (int i = 0; i < components.length; i++)
225 dims = components[i].getPreferredSize();
226 if (dims != null)
228 if (dims.height > limit)
229 limit = dims.height;
230 total += dims.width;
233 w = total;
234 h = limit;
236 else
238 for (int i = 0; i < components.length; i++)
240 dims = components[i].getPreferredSize();
241 if (dims != null)
243 if (dims.width > limit)
244 limit = dims.width;
245 total += dims.height;
248 w = limit;
249 h = total;
252 Insets insets = getInsets();
253 w += insets.left + insets.right;
254 h += insets.top + insets.bottom;
256 Insets margin = getMargin();
257 if (margin != null)
259 w += margin.left + margin.right;
260 h += margin.top + margin.bottom;
263 return new Dimension(w, h);
267 * This method is called when the given component is removed from the
268 * container.
270 * @param comp The component removed.
272 public void removeLayoutComponent(Component comp)
274 // Do nothing.
279 * This is an extension of JSeparator used in toolbars. Unlike JSeparator,
280 * nothing is painted for this Separator, it is only blank space that
281 * separates components.
283 public static class Separator extends JSeparator
285 /** DOCUMENT ME! */
286 private static final long serialVersionUID = -1656745644823105219L;
289 * Creates a new Separator object.
291 public Separator()
293 super();
294 } // Separator()
297 * Creates a new Separator object with the given size.
299 * @param size The size of the separator.
301 public Separator(Dimension size)
303 setPreferredSize(size);
304 } // Separator()
307 * This method returns the String ID of the UI class of Separator.
309 * @return The UI class' String ID.
311 public String getUIClassID()
313 return "ToolBarSeparatorUI";
314 } // getUIClassID()
317 * This method returns the preferred size of the Separator.
319 * @return The preferred size of the Separator.
321 public Dimension getPreferredSize()
323 return super.getPreferredSize();
324 } // getPreferredSize()
327 * This method returns the maximum size of the Separator.
329 * @return The maximum size of the Separator.
331 public Dimension getMaximumSize()
333 return super.getPreferredSize();
334 } // getMaximumSize()
337 * This method returns the minimum size of the Separator.
339 * @return The minimum size of the Separator.
341 public Dimension getMinimumSize()
343 return super.getPreferredSize();
344 } // getMinimumSize()
347 * This method returns the size of the Separator.
349 * @return The size of the Separator.
351 public Dimension getSeparatorSize()
353 return super.getPreferredSize();
354 } // getSeparatorSize()
357 * This method sets the size of the Separator.
359 * @param size The new size of the Separator.
361 public void setSeparatorSize(Dimension size)
363 setPreferredSize(size);
364 } // setSeparatorSize()
365 } // Separator
367 /** DOCUMENT ME! */
368 private static final long serialVersionUID = -1269915519555129643L;
370 /** Whether the JToolBar paints its border. */
371 private transient boolean paintBorder = true;
373 /** The extra insets around the JToolBar. */
374 private transient Insets margin;
376 /** Whether the JToolBar can float (and be dragged around). */
377 private transient boolean floatable = true;
379 /** Whether the buttons will have rollover borders. */
380 private transient boolean rollover;
382 /** The orientation of the JToolBar. */
383 private int orientation = HORIZONTAL;
386 * This method creates a new JToolBar object with horizontal orientation
387 * and no name.
389 public JToolBar()
391 this(null, HORIZONTAL);
392 } // JToolBar()
395 * This method creates a new JToolBar with the given orientation and no
396 * name.
398 * @param orientation JToolBar orientation (HORIZONTAL or VERTICAL)
400 public JToolBar(int orientation)
402 this(null, orientation);
403 } // JToolBar()
406 * This method creates a new JToolBar object with the given name and
407 * horizontal orientation.
409 * @param name Name assigned to undocked tool bar.
411 public JToolBar(String name)
413 this(name, HORIZONTAL);
414 } // JToolBar()
417 * This method creates a new JToolBar object with the given name and
418 * orientation.
420 * @param name Name assigned to undocked tool bar.
421 * @param orientation JToolBar orientation (HORIZONTAL or VERTICAL)
423 public JToolBar(String name, int orientation)
425 setName(name);
426 setOrientation(orientation);
427 setLayout(new DefaultToolBarLayout());
428 revalidate();
429 updateUI();
430 } // JToolBar()
433 * This method adds a new JButton that performs the given Action to the
434 * JToolBar.
436 * @param action The Action to add to the JToolBar.
438 * @return The JButton that wraps the Action.
440 public JButton add(Action action)
442 JButton b = createActionComponent(action);
443 add(b);
444 return b;
445 } // add()
448 * This method paints the border if the borderPainted property is true.
450 * @param graphics The graphics object to paint with.
452 protected void paintBorder(Graphics graphics)
454 if (paintBorder && isFloatable())
455 super.paintBorder(graphics);
456 } // paintBorder()
459 * This method returns the UI class used to paint this JToolBar.
461 * @return The UI class for this JToolBar.
463 public ToolBarUI getUI()
465 return (ToolBarUI) ui;
466 } // getUI()
469 * This method sets the UI used with the JToolBar.
471 * @param ui The UI used with the JToolBar.
473 public void setUI(ToolBarUI ui)
475 super.setUI(ui);
476 } // setUI()
479 * This method resets the UI used to the Look and Feel defaults.
481 public void updateUI()
483 setUI((ToolBarUI) UIManager.getUI(this));
484 revalidate();
485 repaint();
486 } // updateUI()
489 * This method returns the String identifier for the UI class to the used
490 * with the JToolBar.
492 * @return The String identifier for the UI class.
494 public String getUIClassID()
496 return "ToolBarUI";
497 } // getUIClassID()
500 * This method sets the rollover property for the JToolBar. In rollover
501 * mode, JButtons inside the JToolBar will only display their borders when
502 * the mouse is moving over them.
504 * @param b The new rollover property.
506 public void setRollover(boolean b)
508 if (b != rollover)
510 rollover = b;
511 firePropertyChange("rollover", ! rollover, rollover);
512 revalidate();
513 repaint();
518 * This method returns the rollover property.
520 * @return The rollover property.
522 public boolean isRollover()
524 return rollover;
528 * This method returns the index of the given component.
530 * @param component The component to find.
532 * @return The index of the given component.
534 public int getComponentIndex(Component component)
536 Component[] components = getComponents();
537 if (components == null)
538 return -1;
540 for (int i = 0; i < components.length; i++)
541 if (components[i] == component)
542 return i;
544 return -1;
545 } // getComponentIndex()
548 * This method returns the component at the given index.
550 * @param index The index of the component.
552 * @return The component at the given index.
554 public Component getComponentAtIndex(int index)
556 return getComponent(index);
557 } // getComponentAtIndex()
560 * This method returns the margin property.
562 * @return The margin property.
564 public Insets getMargin()
566 return margin;
567 } // getMargin()
570 * This method sets the margin property. The margin property determines the
571 * extra space between the children components of the JToolBar and the
572 * border.
574 * @param margin The margin property.
576 public void setMargin(Insets margin)
578 if ((this.margin != null && margin == null)
579 || (this.margin == null && margin != null)
580 || (margin != null && this.margin != null
581 && (margin.left != this.margin.left
582 || margin.right != this.margin.right || margin.top != this.margin.top
583 || margin.bottom != this.margin.bottom)))
585 Insets oldMargin = this.margin;
586 this.margin = margin;
587 firePropertyChange("margin", oldMargin, this.margin);
588 revalidate();
589 repaint();
591 } // setMargin()
594 * This method returns the borderPainted property.
596 * @return The borderPainted property.
598 public boolean isBorderPainted()
600 return paintBorder;
601 } // isBorderPainted()
604 * This method sets the borderPainted property. If set to false, the border
605 * will not be painted.
607 * @param painted Whether the border will be painted.
609 public void setBorderPainted(boolean painted)
611 if (painted != paintBorder)
613 paintBorder = painted;
614 firePropertyChange("borderPainted", ! paintBorder,
615 paintBorder);
616 repaint();
618 } // setBorderPainted()
621 * This method returns the floatable property.
623 * @return The floatable property.
625 public boolean isFloatable()
627 return floatable;
628 } // isFloatable()
631 * This method sets the floatable property. If set to false, the JToolBar
632 * cannot be dragged.
634 * @param floatable Whether the JToolBar can be dragged.
636 public void setFloatable(boolean floatable)
638 if (floatable != this.floatable)
640 this.floatable = floatable;
641 firePropertyChange("floatable", ! floatable, floatable);
643 } // setFloatable()
646 * This method returns the orientation of the JToolBar.
648 * @return The orientation of the JToolBar.
650 public int getOrientation()
652 return orientation;
653 } // getOrientation()
656 * This method sets the layout manager to be used with the JToolBar.
658 * @param mgr The Layout Manager used with the JToolBar.
660 public void setLayout(LayoutManager mgr)
662 super.setLayout(mgr);
663 revalidate();
664 repaint();
665 } // setLayout()
668 * This method sets the orientation property for JToolBar.
670 * @param orientation The new orientation for JToolBar.
672 * @throws IllegalArgumentException If the orientation is not HORIZONTAL or
673 * VERTICAL.
675 public void setOrientation(int orientation)
677 if (orientation != HORIZONTAL && orientation != VERTICAL)
678 throw new IllegalArgumentException(orientation
679 + " is not a legal orientation");
680 if (orientation != this.orientation)
682 int oldOrientation = this.orientation;
683 this.orientation = orientation;
684 firePropertyChange("orientation", oldOrientation, this.orientation);
685 revalidate();
686 repaint();
688 } // setOrientation()
691 * This method adds a Separator of default size to the JToolBar.
693 public void addSeparator()
695 add(new Separator());
696 } // addSeparator()
699 * This method adds a Separator with the given size to the JToolBar.
701 * @param size The size of the Separator.
703 public void addSeparator(Dimension size)
705 add(new Separator(size));
706 } // addSeparator()
709 * This method is used to create JButtons which can be added to the JToolBar
710 * for the given action.
712 * @param action The action to create a JButton for.
714 * @return The JButton created from the action.
716 protected JButton createActionComponent(Action action)
718 return new JButton(action);
719 } // createActionComponent()
722 * This method creates a pre-configured PropertyChangeListener which updates
723 * the control as changes are made to the Action. However, this is no
724 * longer the recommended way of adding Actions to Containers. As such,
725 * this method returns null.
727 * @param button The JButton to configure a PropertyChangeListener for.
729 * @return null.
731 protected PropertyChangeListener createActionChangeListener(JButton button)
733 // XXX: As specified, this returns null. But seems kind of strange, usually deprecated methods don't just return null, verify!
734 return null;
735 } // createActionChangeListener()
738 * This method overrides Container's addImpl method. If a JButton is added,
739 * it is disabled.
741 * @param component The Component to add.
742 * @param constraints The Constraints placed on the component.
743 * @param index The index to place the Component at.
745 protected void addImpl(Component component, Object constraints, int index)
747 // XXX: Sun says disable button but test cases show otherwise.
748 super.addImpl(component, constraints, index);
750 // if we added a Swing Button then adjust this a little
751 if (component instanceof AbstractButton)
753 AbstractButton b = (AbstractButton) component;
754 b.setRolloverEnabled(rollover);
757 } // addImpl()
760 * This method returns a String description of the JToolBar.
762 * @return A String description of the JToolBar.
764 protected String paramString()
766 return "JToolBar";
767 } // paramString()
770 * getAccessibleContext
772 * @return AccessibleContext
774 public AccessibleContext getAccessibleContext()
776 if (accessibleContext == null)
777 accessibleContext = new AccessibleJToolBar();
779 return accessibleContext;