2 Copyright (C) 2002, 2004, 2005, 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)
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
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
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. */
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.plaf.ToolBarUI;
56 * JToolBar is a component that provides a toolbar to Swing programs. Users
57 * can add buttons (or actions that will be represented by JButtons) as well
58 * as other components to the JToolBar. JToolBars can be dragged in and out
59 * of their parent components. If the JToolBar is dragged out of the parent,
60 * then it will be displayed in its own RootPaneContainer. For dragging to
61 * work properly, JToolBars need to be placed in a Container that has a
62 * BorderLayout. That parent Container cannot have components in the NORTH,
63 * EAST, SOUTH, or WEST components (that is not the JToolBar).
65 public class JToolBar extends JComponent implements SwingConstants, Accessible
68 * Provides the accessibility features for the <code>JToolBar</code>
71 protected class AccessibleJToolBar extends AccessibleJComponent
73 private static final long serialVersionUID = -5516888265903814215L;
76 * Creates a new <code>AccessibleJToolBar</code> instance.
78 protected AccessibleJToolBar()
80 // Nothing to do here.
84 * Returns a set containing the current state of the {@link JToolBar}
85 * component. The current implementation simply calls the superclass.
87 * @return The accessible state set.
89 public AccessibleStateSet getAccessibleStateSet()
91 // running tests against the reference implementation, I was unable
92 // to find any state information that is set specifically by the
94 return super.getAccessibleStateSet();
98 * Returns the accessible role for the <code>JToolBar</code> component.
100 * @return {@link AccessibleRole#TOOL_BAR}.
102 public AccessibleRole getAccessibleRole()
104 return AccessibleRole.TOOL_BAR;
109 * This is the private JToolBar layout manager.
111 private class DefaultToolBarLayout implements LayoutManager
114 * This method is called when a new component is added to the container.
116 * @param name The name of the component added.
117 * @param comp The component that was added.
119 public void addLayoutComponent(String name, Component comp)
125 * This method is called to lay out the given container to position and
126 * size the child components.
128 * @param c The container to lay out.
130 * @throws Error DOCUMENT ME!
132 public void layoutContainer(Container c)
134 if (! (c instanceof JToolBar))
135 throw new Error("DefaultToolBarLayout can only be used on JToolBars.");
136 Insets insets = getInsets();
137 Insets margin = getMargin();
141 insets.left += margin.left;
142 insets.top += margin.top;
143 insets.bottom += margin.bottom;
144 insets.right += margin.right;
146 Component[] components = c.getComponents();
147 Dimension tdims = c.getSize();
151 if (getOrientation() == SwingUtilities.HORIZONTAL)
153 start += insets.left;
154 for (int i = 0; i < components.length; i++)
156 if (components[i] != null && components[i].isVisible())
158 pref = components[i].getPreferredSize();
161 middle = (tdims.height - pref.height) / 2;
162 components[i].setBounds(start, middle, pref.width,
172 for (int i = 0; i < components.length; i++)
174 if (components[i] != null && components[i].isVisible())
176 pref = components[i].getPreferredSize();
179 middle = (tdims.width - pref.width) / 2;
180 components[i].setBounds(middle, start, pref.width,
182 start += pref.height;
190 * This method returns the minimum size of the given container given the
193 * @param parent The container to measure.
195 * @return The minimum size of the given container.
197 public Dimension minimumLayoutSize(Container parent)
199 return preferredLayoutSize(parent);
203 * This method returns the preferred size of the given container given the
206 * @param parent The container to measure.
208 * @return The preferred size of the given container.
210 public Dimension preferredLayoutSize(Container parent)
212 int orientation = getOrientation();
213 Component[] components = getComponents();
222 if (orientation == SwingConstants.HORIZONTAL)
224 for (int i = 0; i < components.length; i++)
226 dims = components[i].getPreferredSize();
229 if (dims.height > limit)
239 for (int i = 0; i < components.length; i++)
241 dims = components[i].getPreferredSize();
244 if (dims.width > limit)
246 total += dims.height;
253 Insets insets = getInsets();
254 w += insets.left + insets.right;
255 h += insets.top + insets.bottom;
257 Insets margin = getMargin();
260 w += margin.left + margin.right;
261 h += margin.top + margin.bottom;
264 return new Dimension(w, h);
268 * This method is called when the given component is removed from the
271 * @param comp The component removed.
273 public void removeLayoutComponent(Component comp)
280 * This is an extension of JSeparator used in toolbars. Unlike JSeparator,
281 * nothing is painted for this Separator, it is only blank space that
282 * separates components.
284 public static class Separator extends JSeparator
287 private static final long serialVersionUID = -1656745644823105219L;
290 * Creates a new Separator object.
298 * Creates a new Separator object with the given size.
300 * @param size The size of the separator.
302 public Separator(Dimension size)
304 setPreferredSize(size);
308 * This method returns the String ID of the UI class of Separator.
310 * @return The UI class' String ID.
312 public String getUIClassID()
314 return "ToolBarSeparatorUI";
318 * This method returns the preferred size of the Separator.
320 * @return The preferred size of the Separator.
322 public Dimension getPreferredSize()
324 return super.getPreferredSize();
325 } // getPreferredSize()
328 * This method returns the maximum size of the Separator.
330 * @return The maximum size of the Separator.
332 public Dimension getMaximumSize()
334 return super.getPreferredSize();
335 } // getMaximumSize()
338 * This method returns the minimum size of the Separator.
340 * @return The minimum size of the Separator.
342 public Dimension getMinimumSize()
344 return super.getPreferredSize();
345 } // getMinimumSize()
348 * This method returns the size of the Separator.
350 * @return The size of the Separator.
352 public Dimension getSeparatorSize()
354 return super.getPreferredSize();
355 } // getSeparatorSize()
358 * This method sets the size of the Separator.
360 * @param size The new size of the Separator.
362 public void setSeparatorSize(Dimension size)
364 setPreferredSize(size);
365 } // setSeparatorSize()
369 private static final long serialVersionUID = -1269915519555129643L;
371 /** Whether the JToolBar paints its border. */
372 private transient boolean paintBorder = true;
374 /** The extra insets around the JToolBar. */
375 private transient Insets margin;
377 /** Whether the JToolBar can float (and be dragged around). */
378 private transient boolean floatable = true;
380 /** Whether the buttons will have rollover borders. */
381 private transient boolean rollover;
383 /** The orientation of the JToolBar. */
384 private int orientation = HORIZONTAL;
387 * This method creates a new JToolBar object with horizontal orientation
392 this(null, HORIZONTAL);
396 * This method creates a new JToolBar with the given orientation and no
399 * @param orientation JToolBar orientation (HORIZONTAL or VERTICAL)
401 public JToolBar(int orientation)
403 this(null, orientation);
407 * This method creates a new JToolBar object with the given name and
408 * horizontal orientation.
410 * @param name Name assigned to undocked tool bar.
412 public JToolBar(String name)
414 this(name, HORIZONTAL);
418 * This method creates a new JToolBar object with the given name and
421 * @param name Name assigned to undocked tool bar.
422 * @param orientation JToolBar orientation (HORIZONTAL or VERTICAL)
424 public JToolBar(String name, int orientation)
427 setOrientation(orientation);
428 setLayout(new DefaultToolBarLayout());
435 * This method adds a new JButton that performs the given Action to the
438 * @param action The Action to add to the JToolBar.
440 * @return The JButton that wraps the Action.
442 public JButton add(Action action)
444 JButton b = createActionComponent(action);
450 * This method paints the border if the borderPainted property is true.
452 * @param graphics The graphics object to paint with.
454 protected void paintBorder(Graphics graphics)
456 if (paintBorder && isFloatable())
457 super.paintBorder(graphics);
461 * This method returns the UI class used to paint this JToolBar.
463 * @return The UI class for this JToolBar.
465 public ToolBarUI getUI()
467 return (ToolBarUI) ui;
471 * This method sets the UI used with the JToolBar.
473 * @param ui The UI used with the JToolBar.
475 public void setUI(ToolBarUI ui)
481 * This method resets the UI used to the Look and Feel defaults.
483 public void updateUI()
485 setUI((ToolBarUI) UIManager.getUI(this));
489 * This method returns the String identifier for the UI class to the used
492 * @return The String identifier for the UI class.
494 public String 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)
511 firePropertyChange("rollover", ! rollover, rollover);
518 * This method returns the rollover property.
520 * @return The rollover property.
522 public boolean isRollover()
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)
540 for (int i = 0; i < components.length; i++)
541 if (components[i] == component)
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()
570 * This method sets the margin property. The margin property determines the
571 * extra space between the children components of the JToolBar and the
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);
594 * This method returns the borderPainted property.
596 * @return The borderPainted property.
598 public boolean isBorderPainted()
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,
618 } // setBorderPainted()
621 * This method returns the floatable property.
623 * @return The floatable property.
625 public boolean isFloatable()
631 * This method sets the floatable property. If set to false, the JToolBar
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);
646 * This method returns the orientation of the JToolBar.
648 * @return The orientation of the JToolBar.
650 public int getOrientation()
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);
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
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);
688 } // setOrientation()
691 * This method adds a Separator of default size to the JToolBar.
693 public void addSeparator()
695 add(new Separator());
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));
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.
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!
735 } // createActionChangeListener()
738 * This method overrides Container's addImpl method. If a JButton is added,
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);
760 * Returns a string describing the attributes for the <code>JToolBar</code>
761 * component, for use in debugging. The return value is guaranteed to be
762 * non-<code>null</code>, but the format of the string may vary between
765 * @return A string describing the attributes of the <code>JToolBar</code>.
767 protected String paramString()
769 StringBuffer sb = new StringBuffer(super.paramString());
770 sb.append(",floatable=").append(floatable);
771 sb.append(",margin=");
774 sb.append(",orientation=");
775 if (orientation == HORIZONTAL)
776 sb.append("HORIZONTAL");
779 sb.append(",paintBorder=").append(paintBorder);
780 return sb.toString();
784 * Returns the object that provides accessibility features for this
785 * <code>JToolBar</code> component.
787 * @return The accessible context (an instance of {@link AccessibleJToolBar}).
789 public AccessibleContext getAccessibleContext()
791 if (accessibleContext == null)
792 accessibleContext = new AccessibleJToolBar();
794 return accessibleContext;