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)
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
.JButton
;
54 import javax
.swing
.plaf
.ToolBarUI
;
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
71 protected class AccessibleJToolBar
extends AccessibleJComponent
74 private static final long serialVersionUID
= -5516888265903814215L;
77 * Constructor AccessibleJToolBar
79 protected AccessibleJToolBar()
84 * getAccessibleStateSet
86 * @return AccessibleStateSet
88 public AccessibleStateSet
getAccessibleStateSet()
96 * @return AccessibleRole
98 public AccessibleRole
getAccessibleRole()
100 return AccessibleRole
.TOOL_BAR
;
105 * This is the private JToolBar layout manager.
107 private class DefaultToolBarLayout
implements LayoutManager
110 * This method is called when a new component is added to the container.
112 * @param name The name of the component added.
113 * @param comp The component that was added.
115 public void addLayoutComponent(String name
, Component comp
)
121 * This method is called to lay out the given container to position and
122 * size the child components.
124 * @param c The container to lay out.
126 * @throws Error DOCUMENT ME!
128 public void layoutContainer(Container c
)
130 if (! (c
instanceof JToolBar
))
131 throw new Error("DefaultToolBarLayout can only be used on JToolBars.");
132 Insets insets
= getInsets();
133 Insets margin
= getMargin();
137 insets
.left
+= margin
.left
;
138 insets
.top
+= margin
.top
;
139 insets
.bottom
+= margin
.bottom
;
140 insets
.right
+= margin
.right
;
142 Component
[] components
= c
.getComponents();
143 Dimension tdims
= c
.getSize();
147 if (getOrientation() == SwingUtilities
.HORIZONTAL
)
149 start
+= insets
.left
;
150 for (int i
= 0; i
< components
.length
; i
++)
152 if (components
[i
] != null && components
[i
].isVisible())
154 pref
= components
[i
].getPreferredSize();
157 middle
= (tdims
.height
- pref
.height
) / 2;
158 components
[i
].setBounds(start
, middle
, pref
.width
,
168 for (int i
= 0; i
< components
.length
; i
++)
170 if (components
[i
] != null && components
[i
].isVisible())
172 pref
= components
[i
].getPreferredSize();
175 middle
= (tdims
.width
- pref
.width
) / 2;
176 components
[i
].setBounds(middle
, start
, pref
.width
,
178 start
+= pref
.height
;
186 * This method returns the minimum size of the given container given the
189 * @param parent The container to measure.
191 * @return The minimum size of the given container.
193 public Dimension
minimumLayoutSize(Container parent
)
195 return preferredLayoutSize(parent
);
199 * This method returns the preferred size of the given container given the
202 * @param parent The container to measure.
204 * @return The preferred size of the given container.
206 public Dimension
preferredLayoutSize(Container parent
)
208 int orientation
= getOrientation();
209 Component
[] components
= getComponents();
218 if (orientation
== SwingConstants
.HORIZONTAL
)
220 for (int i
= 0; i
< components
.length
; i
++)
222 dims
= components
[i
].getPreferredSize();
225 if (dims
.height
> limit
)
235 for (int i
= 0; i
< components
.length
; i
++)
237 dims
= components
[i
].getPreferredSize();
240 if (dims
.width
> limit
)
242 total
+= dims
.height
;
249 Insets insets
= getInsets();
250 w
+= insets
.left
+ insets
.right
;
251 h
+= insets
.top
+ insets
.bottom
;
253 Insets margin
= getMargin();
256 w
+= margin
.left
+ margin
.right
;
257 h
+= margin
.top
+ margin
.bottom
;
260 return new Dimension(w
, h
);
264 * This method is called when the given component is removed from the
267 * @param comp The component removed.
269 public void removeLayoutComponent(Component comp
)
276 * This is an extension of JSeparator used in toolbars. Unlike JSeparator,
277 * nothing is painted for this Separator, it is only blank space that
278 * separates components.
280 public static class Separator
extends JSeparator
283 private static final long serialVersionUID
= -1656745644823105219L;
286 * Creates a new Separator object.
294 * Creates a new Separator object with the given size.
296 * @param size The size of the separator.
298 public Separator(Dimension size
)
300 setPreferredSize(size
);
304 * This method returns the String ID of the UI class of Separator.
306 * @return The UI class' String ID.
308 public String
getUIClassID()
310 return "ToolBarSeparatorUI";
314 * This method returns the preferred size of the Separator.
316 * @return The preferred size of the Separator.
318 public Dimension
getPreferredSize()
320 return super.getPreferredSize();
321 } // getPreferredSize()
324 * This method returns the maximum size of the Separator.
326 * @return The maximum size of the Separator.
328 public Dimension
getMaximumSize()
330 return super.getPreferredSize();
331 } // getMaximumSize()
334 * This method returns the minimum size of the Separator.
336 * @return The minimum size of the Separator.
338 public Dimension
getMinimumSize()
340 return super.getPreferredSize();
341 } // getMinimumSize()
344 * This method returns the size of the Separator.
346 * @return The size of the Separator.
348 public Dimension
getSeparatorSize()
350 return super.getPreferredSize();
351 } // getSeparatorSize()
354 * This method sets the size of the Separator.
356 * @param size The new size of the Separator.
358 public void setSeparatorSize(Dimension size
)
360 setPreferredSize(size
);
361 } // setSeparatorSize()
365 private static final long serialVersionUID
= -1269915519555129643L;
367 /** Whether the JToolBar paints its border. */
368 private transient boolean paintBorder
= true;
370 /** The extra insets around the JToolBar. */
371 private transient Insets margin
;
373 /** Whether the JToolBar can float (and be dragged around). */
374 private transient boolean floatable
= true;
376 /** Whether the buttons will have rollover borders. */
377 private transient boolean rollover
;
379 /** The orientation of the JToolBar. */
380 private int orientation
= HORIZONTAL
;
383 * This method creates a new JToolBar object with horizontal orientation
388 this(null, HORIZONTAL
);
392 * This method creates a new JToolBar with the given orientation and no
395 * @param orientation JToolBar orientation (HORIZONTAL or VERTICAL)
397 public JToolBar(int orientation
)
399 this(null, orientation
);
403 * This method creates a new JToolBar object with the given name and
404 * horizontal orientation.
406 * @param name Name assigned to undocked tool bar.
408 public JToolBar(String name
)
410 this(name
, HORIZONTAL
);
414 * This method creates a new JToolBar object with the given name and
417 * @param name Name assigned to undocked tool bar.
418 * @param orientation JToolBar orientation (HORIZONTAL or VERTICAL)
420 public JToolBar(String name
, int orientation
)
423 setOrientation(orientation
);
424 setLayout(new DefaultToolBarLayout());
430 * This method adds a new JButton that performs the given Action to the
433 * @param action The Action to add to the JToolBar.
435 * @return The JButton that wraps the Action.
437 public JButton
add(Action action
)
439 JButton b
= createActionComponent(action
);
445 * This method paints the border if the borderPainted property is true.
447 * @param graphics The graphics object to paint with.
449 protected void paintBorder(Graphics graphics
)
451 if (paintBorder
&& isFloatable())
452 super.paintBorder(graphics
);
456 * This method returns the UI class used to paint this JToolBar.
458 * @return The UI class for this JToolBar.
460 public ToolBarUI
getUI()
462 return (ToolBarUI
) ui
;
466 * This method sets the UI used with the JToolBar.
468 * @param ui The UI used with the JToolBar.
470 public void setUI(ToolBarUI ui
)
476 * This method resets the UI used to the Look and Feel defaults.
478 public void updateUI()
480 setUI((ToolBarUI
) UIManager
.getUI(this));
486 * This method returns the String identifier for the UI class to the used
489 * @return The String identifier for the UI class.
491 public String
getUIClassID()
497 * This method sets the rollover property for the JToolBar. In rollover
498 * mode, JButtons inside the JToolBar will only display their borders when
499 * the mouse is moving over them.
501 * @param b The new rollover property.
503 public void setRollover(boolean b
)
508 firePropertyChange("rollover", ! rollover
, rollover
);
515 * This method returns the rollover property.
517 * @return The rollover property.
519 public boolean isRollover()
525 * This method returns the index of the given component.
527 * @param component The component to find.
529 * @return The index of the given component.
531 public int getComponentIndex(Component component
)
533 Component
[] components
= getComponents();
534 if (components
== null)
537 for (int i
= 0; i
< components
.length
; i
++)
538 if (components
[i
] == component
)
542 } // getComponentIndex()
545 * This method returns the component at the given index.
547 * @param index The index of the component.
549 * @return The component at the given index.
551 public Component
getComponentAtIndex(int index
)
553 return getComponent(index
);
554 } // getComponentAtIndex()
557 * This method returns the margin property.
559 * @return The margin property.
561 public Insets
getMargin()
567 * This method sets the margin property. The margin property determines the
568 * extra space between the children components of the JToolBar and the
571 * @param margin The margin property.
573 public void setMargin(Insets margin
)
575 if ((this.margin
!= null && margin
== null)
576 || (this.margin
== null && margin
!= null)
577 || (margin
!= null && this.margin
!= null
578 && (margin
.left
!= this.margin
.left
579 || margin
.right
!= this.margin
.right
|| margin
.top
!= this.margin
.top
580 || margin
.bottom
!= this.margin
.bottom
)))
582 Insets oldMargin
= this.margin
;
583 this.margin
= margin
;
584 firePropertyChange("margin", oldMargin
, this.margin
);
591 * This method returns the borderPainted property.
593 * @return The borderPainted property.
595 public boolean isBorderPainted()
598 } // isBorderPainted()
601 * This method sets the borderPainted property. If set to false, the border
602 * will not be painted.
604 * @param painted Whether the border will be painted.
606 public void setBorderPainted(boolean painted
)
608 if (painted
!= paintBorder
)
610 paintBorder
= painted
;
611 firePropertyChange("borderPainted", ! paintBorder
,
615 } // setBorderPainted()
618 * This method returns the floatable property.
620 * @return The floatable property.
622 public boolean isFloatable()
628 * This method sets the floatable property. If set to false, the JToolBar
631 * @param floatable Whether the JToolBar can be dragged.
633 public void setFloatable(boolean floatable
)
635 if (floatable
!= this.floatable
)
637 this.floatable
= floatable
;
638 firePropertyChange("floatable", ! floatable
, floatable
);
643 * This method returns the orientation of the JToolBar.
645 * @return The orientation of the JToolBar.
647 public int getOrientation()
650 } // getOrientation()
653 * This method sets the layout manager to be used with the JToolBar.
655 * @param mgr The Layout Manager used with the JToolBar.
657 public void setLayout(LayoutManager mgr
)
659 super.setLayout(mgr
);
665 * This method sets the orientation property for JToolBar.
667 * @param orientation The new orientation for JToolBar.
669 * @throws IllegalArgumentException If the orientation is not HORIZONTAL or
672 public void setOrientation(int orientation
)
674 if (orientation
!= HORIZONTAL
&& orientation
!= VERTICAL
)
675 throw new IllegalArgumentException(orientation
676 + " is not a legal orientation");
677 if (orientation
!= this.orientation
)
679 int oldOrientation
= this.orientation
;
680 this.orientation
= orientation
;
681 firePropertyChange("orientation", oldOrientation
, this.orientation
);
685 } // setOrientation()
688 * This method adds a Separator of default size to the JToolBar.
690 public void addSeparator()
692 add(new Separator());
696 * This method adds a Separator with the given size to the JToolBar.
698 * @param size The size of the Separator.
700 public void addSeparator(Dimension size
)
702 add(new Separator(size
));
706 * This method is used to create JButtons which can be added to the JToolBar
707 * for the given action.
709 * @param action The action to create a JButton for.
711 * @return The JButton created from the action.
713 protected JButton
createActionComponent(Action action
)
715 return new JButton(action
);
716 } // createActionComponent()
719 * This method creates a pre-configured PropertyChangeListener which updates
720 * the control as changes are made to the Action. However, this is no
721 * longer the recommended way of adding Actions to Containers. As such,
722 * this method returns null.
724 * @param button The JButton to configure a PropertyChangeListener for.
728 protected PropertyChangeListener
createActionChangeListener(JButton button
)
730 // XXX: As specified, this returns null. But seems kind of strange, usually deprecated methods don't just return null, verify!
732 } // createActionChangeListener()
735 * This method overrides Container's addImpl method. If a JButton is added,
738 * @param component The Component to add.
739 * @param constraints The Constraints placed on the component.
740 * @param index The index to place the Component at.
742 protected void addImpl(Component component
, Object constraints
, int index
)
744 // XXX: Sun says disable button but test cases show otherwise.
745 super.addImpl(component
, constraints
, index
);
747 // if we added a Swing Button then adjust this a little
748 if (component
instanceof AbstractButton
)
750 AbstractButton b
= (AbstractButton
) component
;
751 b
.setRolloverEnabled(rollover
);
758 * This method returns a String description of the JToolBar.
760 * @return A String description of the JToolBar.
762 protected String
paramString()
768 * getAccessibleContext
770 * @return AccessibleContext
772 public AccessibleContext
getAccessibleContext()
774 if (accessibleContext
== null)
775 accessibleContext
= new AccessibleJToolBar();
777 return accessibleContext
;