KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JToolBar


1 /*
2  * @(#)JToolBar.java 1.108 04/02/26
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing;
9
10 import java.awt.Color JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.ComponentOrientation JavaDoc;
13 import java.awt.Container JavaDoc;
14 import java.awt.Dimension JavaDoc;
15 import java.awt.Graphics JavaDoc;
16 import java.awt.Insets JavaDoc;
17 import java.awt.LayoutManager JavaDoc;
18 import java.awt.LayoutManager2 JavaDoc;
19 import java.awt.event.*;
20 import java.beans.*;
21
22 import javax.swing.border.Border JavaDoc;
23 import javax.swing.plaf.*;
24 import javax.accessibility.*;
25
26 import java.io.Serializable JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.Hashtable JavaDoc;
31
32
33 /**
34  * <code>JToolBar</code> provides a component that is useful for
35  * displaying commonly used <code>Action</code>s or controls.
36  * For examples and information on using tool bars see
37  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/toolbar.html">How to Use Tool Bars</a>,
38  * a section in <em>The Java Tutorial</em>.
39  *
40  * <p>
41  * With most look and feels,
42  * the user can drag out a tool bar into a separate window
43  * (unless the <code>floatable</code> property is set to <code>false</code>).
44  * For drag-out to work correctly, it is recommended that you add
45  * <code>JToolBar</code> instances to one of the four "sides" of a
46  * container whose layout manager is a <code>BorderLayout</code>,
47  * and do not add children to any of the other four "sides".
48  * <p>
49  * <strong>Warning:</strong>
50  * Serialized objects of this class will not be compatible with
51  * future Swing releases. The current serialization support is
52  * appropriate for short term storage or RMI between applications running
53  * the same version of Swing. As of 1.4, support for long term storage
54  * of all JavaBeans<sup><font size="-2">TM</font></sup>
55  * has been added to the <code>java.beans</code> package.
56  * Please see {@link java.beans.XMLEncoder}.
57  *
58  * @beaninfo
59  * attribute: isContainer true
60  * description: A component which displays commonly used controls or Actions.
61  *
62  * @version 1.108 02/26/04
63  * @author Georges Saab
64  * @author Jeff Shapiro
65  * @see Action
66  */

67 public class JToolBar extends JComponent JavaDoc implements SwingConstants JavaDoc, Accessible
68 {
69     /**
70      * @see #getUIClassID
71      * @see #readObject
72      */

73     private static final String JavaDoc uiClassID = "ToolBarUI";
74
75     private boolean paintBorder = true;
76     private Insets JavaDoc margin = null;
77     private boolean floatable = true;
78     private int orientation = HORIZONTAL;
79
80     /**
81      * Creates a new tool bar; orientation defaults to <code>HORIZONTAL</code>.
82      */

83     public JToolBar()
84     {
85         this( HORIZONTAL );
86     }
87
88     /**
89      * Creates a new tool bar with the specified <code>orientation</code>.
90      * The <code>orientation</code> must be either <code>HORIZONTAL</code>
91      * or <code>VERTICAL</code>.
92      *
93      * @param orientation the orientation desired
94      */

95     public JToolBar( int orientation )
96     {
97     this(null, orientation);
98     }
99
100     /**
101      * Creates a new tool bar with the specified <code>name</code>. The
102      * name is used as the title of the undocked tool bar. The default
103      * orientation is <code>HORIZONTAL</code>.
104      *
105      * @param name the name of the tool bar
106      * @since 1.3
107      */

108     public JToolBar( String JavaDoc name ) {
109     this(name, HORIZONTAL);
110     }
111
112     /**
113      * Creates a new tool bar with a specified <code>name</code> and
114      * <code>orientation</code>.
115      * All other constructors call this constructor.
116      * If <code>orientation</code> is an invalid value, an exception will
117      * be thrown.
118      *
119      * @param name the name of the tool bar
120      * @param orientation the initial orientation -- it must be
121      * either <code>HORIZONTAL</code> or <code>VERTICAL</code>
122      * @exception IllegalArgumentException if orientation is neither
123      * <code>HORIZONTAL</code> nor <code>VERTICAL</code>
124      * @since 1.3
125      */

126     public JToolBar( String JavaDoc name , int orientation) {
127     setName(name);
128         checkOrientation( orientation );
129
130     this.orientation = orientation;
131     DefaultToolBarLayout layout = new DefaultToolBarLayout( orientation );
132     setLayout( layout );
133
134         addPropertyChangeListener( layout );
135
136         updateUI();
137     }
138
139     /**
140      * Returns the tool bar's current UI.
141      * @see #setUI
142      */

143     public ToolBarUI getUI() {
144         return (ToolBarUI)ui;
145     }
146
147     /**
148      * Sets the L&F object that renders this component.
149      *
150      * @param ui the <code>ToolBarUI</code> L&F object
151      * @see UIDefaults#getUI
152      * @beaninfo
153      * bound: true
154      * hidden: true
155      * attribute: visualUpdate true
156      * description: The UI object that implements the Component's LookAndFeel.
157      */

158     public void setUI(ToolBarUI ui) {
159         super.setUI(ui);
160     }
161
162     /**
163      * Notification from the <code>UIFactory</code> that the L&F has changed.
164      * Called to replace the UI with the latest version from the
165      * <code>UIFactory</code>.
166      *
167      * @see JComponent#updateUI
168      */

169     public void updateUI() {
170         setUI((ToolBarUI)UIManager.getUI(this));
171         // GTKLookAndFeel installs a different LayoutManager, and sets it
172
// to null after changing the look and feel, so, install the default
173
// if the LayoutManager is null.
174
if (getLayout() == null) {
175             setLayout(new DefaultToolBarLayout(getOrientation()));
176         }
177         invalidate();
178     }
179
180
181
182     /**
183      * Returns the name of the L&F class that renders this component.
184      *
185      * @return the string "ToolBarUI"
186      * @see JComponent#getUIClassID
187      * @see UIDefaults#getUI
188      */

189     public String JavaDoc getUIClassID() {
190         return uiClassID;
191     }
192
193
194     /**
195      * Returns the index of the specified component.
196      * (Note: Separators occupy index positions.)
197      *
198      * @param c the <code>Component</code> to find
199      * @return an integer indicating the component's position,
200      * where 0 is first
201      */

202     public int getComponentIndex(Component JavaDoc c) {
203         int ncomponents = this.getComponentCount();
204         Component JavaDoc[] component = this.getComponents();
205         for (int i = 0 ; i < ncomponents ; i++) {
206             Component JavaDoc comp = component[i];
207             if (comp == c)
208                 return i;
209         }
210         return -1;
211     }
212
213     /**
214      * Returns the component at the specified index.
215      *
216      * @param i the component's position, where 0 is first
217      * @return the <code>Component</code> at that position,
218      * or <code>null</code> for an invalid index
219      *
220      */

221     public Component JavaDoc getComponentAtIndex(int i) {
222         int ncomponents = this.getComponentCount();
223         if ( i >= 0 && i < ncomponents) {
224             Component JavaDoc[] component = this.getComponents();
225             return component[i];
226         }
227         return null;
228     }
229
230      /**
231       * Sets the margin between the tool bar's border and
232       * its buttons. Setting to <code>null</code> causes the tool bar to
233       * use the default margins. The tool bar's default <code>Border</code>
234       * object uses this value to create the proper margin.
235       * However, if a non-default border is set on the tool bar,
236       * it is that <code>Border</code> object's responsibility to create the
237       * appropriate margin space (otherwise this property will
238       * effectively be ignored).
239       *
240       * @param m an <code>Insets</code> object that defines the space
241       * between the border and the buttons
242       * @see Insets
243       * @beaninfo
244       * description: The margin between the tool bar's border and contents
245       * bound: true
246       * expert: true
247       */

248      public void setMargin(Insets JavaDoc m)
249      {
250          Insets JavaDoc old = margin;
251          margin = m;
252          firePropertyChange("margin", old, m);
253      revalidate();
254      repaint();
255      }
256
257      /**
258       * Returns the margin between the tool bar's border and
259       * its buttons.
260       *
261       * @return an <code>Insets</code> object containing the margin values
262       * @see Insets
263       */

264      public Insets JavaDoc getMargin()
265      {
266          if(margin == null) {
267              return new Insets JavaDoc(0,0,0,0);
268          } else {
269              return margin;
270          }
271      }
272
273      /**
274       * Gets the <code>borderPainted</code> property.
275       *
276       * @return the value of the <code>borderPainted</code> property
277       * @see #setBorderPainted
278       */

279      public boolean isBorderPainted()
280      {
281          return paintBorder;
282      }
283
284
285      /**
286       * Sets the <code>borderPainted</code> property, which is
287       * <code>true</code> if the border should be painted.
288       * The default value for this property is <code>true</code>.
289       * Some look and feels might not implement painted borders;
290       * they will ignore this property.
291       *
292       * @param b if true, the border is painted
293       * @see #isBorderPainted
294       * @beaninfo
295       * description: Does the tool bar paint its borders?
296       * bound: true
297       * expert: true
298       */

299      public void setBorderPainted(boolean b)
300      {
301          if ( paintBorder != b )
302          {
303          boolean old = paintBorder;
304          paintBorder = b;
305          firePropertyChange("borderPainted", old, b);
306          revalidate();
307          repaint();
308      }
309      }
310
311      /**
312       * Paints the tool bar's border if the <code>borderPainted</code> property
313       * is <code>true</code>.
314       *
315       * @param g the <code>Graphics</code> context in which the painting
316       * is done
317       * @see JComponent#paint
318       * @see JComponent#setBorder
319       */

320      protected void paintBorder(Graphics JavaDoc g)
321      {
322          if (isBorderPainted())
323      {
324              super.paintBorder(g);
325          }
326      }
327
328     /**
329      * Gets the <code>floatable</code> property.
330      *
331      * @return the value of the <code>floatable</code> property
332      *
333      * @see #setFloatable
334      */

335     public boolean isFloatable()
336     {
337         return floatable;
338     }
339
340      /**
341       * Sets the <code>floatable</code> property,
342       * which must be <code>true</code> for the user to move the tool bar.
343       * Typically, a floatable tool bar can be
344       * dragged into a different position within the same container
345       * or out into its own window.
346       * The default value of this property is <code>true</code>.
347       * Some look and feels might not implement floatable tool bars;
348       * they will ignore this property.
349       *
350       * @param b if <code>true</code>, the tool bar can be moved;
351       * <code>false</code> otherwise
352       * @see #isFloatable
353       * @beaninfo
354       * description: Can the tool bar be made to float by the user?
355       * bound: true
356       * preferred: true
357       */

358     public void setFloatable( boolean b )
359     {
360         if ( floatable != b )
361     {
362             boolean old = floatable;
363         floatable = b;
364
365         firePropertyChange("floatable", old, b);
366         revalidate();
367         repaint();
368         }
369     }
370
371     /**
372      * Returns the current orientation of the tool bar. The value is either
373      * <code>HORIZONTAL</code> or <code>VERTICAL</code>.
374      *
375      * @return an integer representing the current orientation -- either
376      * <code>HORIZONTAL</code> or <code>VERTICAL</code>
377      * @see #setOrientation
378      */

379     public int getOrientation()
380     {
381         return this.orientation;
382     }
383
384     /**
385      * Sets the orientation of the tool bar. The orientation must have
386      * either the value <code>HORIZONTAL</code> or <code>VERTICAL</code>.
387      * If <code>orientation</code> is
388      * an invalid value, an exception will be thrown.
389      *
390      * @param o the new orientation -- either <code>HORIZONTAL</code> or
391      * </code>VERTICAL</code>
392      * @exception IllegalArgumentException if orientation is neither
393      * <code>HORIZONTAL</code> nor <code>VERTICAL</code>
394      * @see #getOrientation
395      * @beaninfo
396      * description: The current orientation of the tool bar
397      * bound: true
398      * preferred: true
399      */

400     public void setOrientation( int o )
401     {
402         checkOrientation( o );
403
404     if ( orientation != o )
405     {
406         int old = orientation;
407         orientation = o;
408
409         firePropertyChange("orientation", old, o);
410         revalidate();
411         repaint();
412     }
413     }
414
415     /**
416      * Sets the rollover state of this toolbar. If the rollover state is true
417      * then the border of the toolbar buttons will be drawn only when the
418      * mouse pointer hovers over them. The default value of this property
419      * is false.
420      * <p>
421      * The implementation of a look and feel may choose to ignore this
422      * property.
423      *
424      * @param rollover true for rollover toolbar buttons; otherwise false
425      * @since 1.4
426      * @beaninfo
427      * bound: true
428      * preferred: true
429      * attribute: visualUpdate true
430      * description: Will draw rollover button borders in the toolbar.
431      */

432     public void setRollover(boolean rollover) {
433     putClientProperty("JToolBar.isRollover",
434               rollover ? Boolean.TRUE : Boolean.FALSE);
435     }
436
437     /**
438      * Returns the rollover state.
439      *
440      * @return true if rollover toolbar buttons are to be drawn; otherwise false
441      * @see #setRollover(boolean)
442      * @since 1.4
443      */

444     public boolean isRollover() {
445     Boolean JavaDoc rollover = (Boolean JavaDoc)getClientProperty("JToolBar.isRollover");
446     if (rollover != null) {
447         return rollover.booleanValue();
448     }
449     return false;
450     }
451
452     private void checkOrientation( int orientation )
453     {
454         switch ( orientation )
455     {
456             case VERTICAL:
457             case HORIZONTAL:
458                 break;
459             default:
460                 throw new IllegalArgumentException JavaDoc( "orientation must be one of: VERTICAL, HORIZONTAL" );
461         }
462     }
463
464     /**
465      * Appends a separator of default size to the end of the tool bar.
466      * The default size is determined by the current look and feel.
467      */

468     public void addSeparator()
469     {
470     addSeparator(null);
471     }
472
473     /**
474      * Appends a separator of a specified size to the end
475      * of the tool bar.
476      *
477      * @param size the <code>Dimension</code> of the separator
478      */

479     public void addSeparator( Dimension JavaDoc size )
480     {
481         JToolBar.Separator JavaDoc s = new JToolBar.Separator JavaDoc( size );
482         add(s);
483     }
484
485     /**
486      * Adds a new <code>JButton</code> which dispatches the action.
487      *
488      * <p>
489      * As of 1.3, this is no longer the preferred method for adding
490      * <code>Action</code>s to a container. Instead it is recommended
491      * to configure a control with an action using
492      * using <code>setAction</code>, and then add that control directly
493      * to the <code>Container</code>.
494      *
495      * @param a the <code>Action</code> object to add as a new menu item
496      * @return the new button which dispatches the action
497      */

498     public JButton JavaDoc add(Action JavaDoc a) {
499     JButton JavaDoc b = createActionComponent(a);
500     b.setAction(a);
501         add(b);
502         return b;
503     }
504
505     /**
506      * Factory method which creates the <code>JButton</code> for
507      * <code>Action</code>s added to the <code>JToolBar</code>.
508      * The default name is empty if a <code>null</code> action is passed.
509      *
510      * <p>
511      * As of 1.3, this is no longer the preferred method for adding
512      * <code>Action</code>s to a <code>Container</code>.
513      * Instead it is recommended to configure a control with an action
514      * using <code>setAction</code>, and then add that control directly
515      * to the <code>Container</code>.
516      *
517      * @param a the <code>Action</code> for the button to be added
518      * @return the newly created button
519      * @see Action
520      */

521     protected JButton JavaDoc createActionComponent(Action JavaDoc a) {
522     String JavaDoc text = a!=null? (String JavaDoc)a.getValue(Action.NAME) : null;
523     Icon JavaDoc icon = a!=null? (Icon JavaDoc)a.getValue(Action.SMALL_ICON) : null;
524         boolean enabled = a!=null? a.isEnabled() : true;
525         String JavaDoc tooltip = a!=null?
526             (String JavaDoc)a.getValue(Action.SHORT_DESCRIPTION) : null;
527         JButton JavaDoc b = new JButton JavaDoc(text, icon) {
528         protected PropertyChangeListener createActionPropertyChangeListener(Action JavaDoc a) {
529         PropertyChangeListener pcl = createActionChangeListener(this);
530         if (pcl==null) {
531             pcl = super.createActionPropertyChangeListener(a);
532         }
533         return pcl;
534         }
535     };
536     if (icon !=null) {
537         b.putClientProperty("hideActionText", Boolean.TRUE);
538     }
539     b.setHorizontalTextPosition(JButton.CENTER);
540     b.setVerticalTextPosition(JButton.BOTTOM);
541     b.setEnabled(enabled);
542     b.setToolTipText(tooltip);
543     return b;
544     }
545
546     /**
547      * Returns a properly configured <code>PropertyChangeListener</code>
548      * which updates the control as changes to the <code>Action</code> occur,
549      * or <code>null</code> if the default
550      * property change listener for the control is desired.
551      *
552      * <p>
553      * As of 1.3, this is no longer the preferred method for adding
554      * <code>Action</code>s to a <code>Container</code>.
555      * Instead it is recommended to configure a control with an action
556      * using <code>setAction</code>, and then add that control directly
557      * to the <code>Container</code>.
558      * @return <code>null</code>
559      */

560     protected PropertyChangeListener createActionChangeListener(JButton JavaDoc b) {
561     return null;
562     }
563
564     /**
565      * If a <code>JButton</code> is being added, it is initially
566      * set to be disabled.
567      *
568      * @param comp the component to be enhanced
569      * @param constraints the constraints to be enforced on the component
570      * @param index the index of the component
571      *
572      */

573     protected void addImpl(Component JavaDoc comp, Object JavaDoc constraints, int index) {
574         if (comp instanceof Separator) {
575             if (getOrientation() == VERTICAL) {
576                 ( (Separator)comp ).setOrientation(JSeparator.HORIZONTAL);
577             } else {
578                 ( (Separator)comp ).setOrientation(JSeparator.VERTICAL);
579             }
580         }
581         super.addImpl(comp, constraints, index);
582         if (comp instanceof JButton JavaDoc) {
583             ((JButton JavaDoc)comp).setDefaultCapable(false);
584         }
585     }
586
587
588     /**
589      * A toolbar-specific separator. An object with dimension but
590      * no contents used to divide buttons on a tool bar into groups.
591      */

592     static public class Separator extends JSeparator JavaDoc
593     {
594         private Dimension JavaDoc separatorSize;
595
596         /**
597          * Creates a new toolbar separator with the default size
598          * as defined by the current look and feel.
599          */

600         public Separator()
601     {
602         this( null ); // let the UI define the default size
603
}
604
605         /**
606          * Creates a new toolbar separator with the specified size.
607      *
608          * @param size the <code>Dimension</code> of the separator
609          */

610         public Separator( Dimension JavaDoc size )
611     {
612         super( JSeparator.HORIZONTAL );
613             setSeparatorSize(size);
614         }
615
616         /**
617      * Returns the name of the L&F class that renders this component.
618      *
619      * @return the string "ToolBarSeparatorUI"
620      * @see JComponent#getUIClassID
621      * @see UIDefaults#getUI
622      */

623         public String JavaDoc getUIClassID()
624     {
625             return "ToolBarSeparatorUI";
626     }
627
628         /**
629          * Sets the size of the separator.
630          *
631          * @param size the new <code>Dimension</code> of the separator
632          */

633         public void setSeparatorSize( Dimension JavaDoc size )
634     {
635             if (size != null) {
636                 separatorSize = size;
637             } else {
638                 super.updateUI();
639             }
640         this.invalidate();
641     }
642
643         /**
644          * Returns the size of the separator
645          *
646          * @return the <code>Dimension</code> object containing the separator's
647          * size (This is a reference, NOT a copy!)
648          */

649         public Dimension JavaDoc getSeparatorSize()
650     {
651         return separatorSize;
652     }
653
654         /**
655          * Returns the minimum size for the separator.
656          *
657          * @return the <code>Dimension</code> object containing the separator's
658          * minimum size
659          */

660         public Dimension JavaDoc getMinimumSize()
661     {
662         if (separatorSize != null) {
663         return separatorSize.getSize();
664         } else {
665         return super.getMinimumSize();
666         }
667         }
668
669         /**
670          * Returns the maximum size for the separator.
671          *
672          * @return the <code>Dimension</code> object containing the separator's
673          * maximum size
674          */

675         public Dimension JavaDoc getMaximumSize()
676     {
677         if (separatorSize != null) {
678         return separatorSize.getSize();
679         } else {
680         return super.getMaximumSize();
681         }
682         }
683
684         /**
685          * Returns the preferred size for the separator.
686          *
687          * @return the <code>Dimension</code> object containing the separator's
688          * preferred size
689          */

690         public Dimension JavaDoc getPreferredSize()
691     {
692         if (separatorSize != null) {
693         return separatorSize.getSize();
694         } else {
695         return super.getPreferredSize();
696         }
697         }
698     }
699
700
701     /**
702      * See <code>readObject</code> and <code>writeObject</code> in
703      * <code>JComponent</code> for more
704      * information about serialization in Swing.
705      */

706     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
707         s.defaultWriteObject();
708         if (getUIClassID().equals(uiClassID)) {
709             byte count = JComponent.getWriteObjCounter(this);
710             JComponent.setWriteObjCounter(this, --count);
711             if (count == 0 && ui != null) {
712                 ui.installUI(this);
713             }
714         }
715     }
716
717
718     /**
719      * Returns a string representation of this <code>JToolBar</code>.
720      * This method
721      * is intended to be used only for debugging purposes, and the
722      * content and format of the returned string may vary between
723      * implementations. The returned string may be empty but may not
724      * be <code>null</code>.
725      *
726      * @return a string representation of this <code>JToolBar</code>.
727      */

728     protected String JavaDoc paramString() {
729         String JavaDoc paintBorderString = (paintBorder ?
730                     "true" : "false");
731         String JavaDoc marginString = (margin != null ?
732                    margin.toString() : "");
733         String JavaDoc floatableString = (floatable ?
734                   "true" : "false");
735         String JavaDoc orientationString = (orientation == HORIZONTAL ?
736                                     "HORIZONTAL" : "VERTICAL");
737
738         return super.paramString() +
739         ",floatable=" + floatableString +
740         ",margin=" + marginString +
741         ",orientation=" + orientationString +
742         ",paintBorder=" + paintBorderString;
743     }
744
745
746     private class DefaultToolBarLayout
747     implements LayoutManager2 JavaDoc, Serializable JavaDoc, PropertyChangeListener, UIResource {
748
749     LayoutManager JavaDoc lm;
750
751     DefaultToolBarLayout(int orientation) {
752         if (orientation == JToolBar.VERTICAL) {
753         lm = new BoxLayout JavaDoc(JToolBar.this, BoxLayout.PAGE_AXIS);
754         } else {
755         lm = new BoxLayout JavaDoc(JToolBar.this, BoxLayout.LINE_AXIS);
756         }
757     }
758
759     public void addLayoutComponent(String JavaDoc name, Component JavaDoc comp) {
760     }
761
762     public void addLayoutComponent(Component JavaDoc comp, Object JavaDoc constraints) {
763     }
764
765     public void removeLayoutComponent(Component JavaDoc comp) {
766     }
767
768     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc target) {
769         return lm.preferredLayoutSize(target);
770     }
771
772     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc target) {
773         return lm.minimumLayoutSize(target);
774     }
775
776     public Dimension JavaDoc maximumLayoutSize(Container JavaDoc target) {
777         if (lm instanceof LayoutManager2 JavaDoc) {
778         return ((LayoutManager2 JavaDoc)lm).maximumLayoutSize(target);
779         } else {
780         // Code copied from java.awt.Component.getMaximumSize()
781
// to avoid infinite recursion.
782
// See also java.awt.Container.getMaximumSize()
783
return new Dimension JavaDoc(Short.MAX_VALUE, Short.MAX_VALUE);
784         }
785     }
786
787     public void layoutContainer(Container JavaDoc target) {
788         lm.layoutContainer(target);
789     }
790
791     public float getLayoutAlignmentX(Container JavaDoc target) {
792         if (lm instanceof LayoutManager2 JavaDoc) {
793         return ((LayoutManager2 JavaDoc)lm).getLayoutAlignmentX(target);
794         } else {
795         // Code copied from java.awt.Component.getAlignmentX()
796
// to avoid infinite recursion.
797
// See also java.awt.Container.getAlignmentX()
798
return CENTER_ALIGNMENT;
799         }
800     }
801
802     public float getLayoutAlignmentY(Container JavaDoc target) {
803         if (lm instanceof LayoutManager2 JavaDoc) {
804         return ((LayoutManager2 JavaDoc)lm).getLayoutAlignmentY(target);
805         } else {
806         // Code copied from java.awt.Component.getAlignmentY()
807
// to avoid infinite recursion.
808
// See also java.awt.Container.getAlignmentY()
809
return CENTER_ALIGNMENT;
810         }
811     }
812
813     public void invalidateLayout(Container JavaDoc target) {
814         if (lm instanceof LayoutManager2 JavaDoc) {
815         ((LayoutManager2 JavaDoc)lm).invalidateLayout(target);
816         }
817     }
818
819         public void propertyChange(PropertyChangeEvent e) {
820             String JavaDoc name = e.getPropertyName();
821         if( name.equals("orientation") ) {
822         int o = ((Integer JavaDoc)e.getNewValue()).intValue();
823
824         if (o == JToolBar.VERTICAL)
825             lm = new BoxLayout JavaDoc(JToolBar.this, BoxLayout.PAGE_AXIS);
826         else {
827             lm = new BoxLayout JavaDoc(JToolBar.this, BoxLayout.LINE_AXIS);
828         }
829         }
830         }
831     }
832
833
834     public void setLayout(LayoutManager JavaDoc mgr) {
835     LayoutManager JavaDoc oldMgr = getLayout();
836     if (oldMgr instanceof PropertyChangeListener) {
837         removePropertyChangeListener((PropertyChangeListener)oldMgr);
838     }
839     super.setLayout(mgr);
840     }
841
842 /////////////////
843
// Accessibility support
844
////////////////
845

846     /**
847      * Gets the AccessibleContext associated with this JToolBar.
848      * For tool bars, the AccessibleContext takes the form of an
849      * AccessibleJToolBar.
850      * A new AccessibleJToolBar instance is created if necessary.
851      *
852      * @return an AccessibleJToolBar that serves as the
853      * AccessibleContext of this JToolBar
854      */

855     public AccessibleContext getAccessibleContext() {
856         if (accessibleContext == null) {
857             accessibleContext = new AccessibleJToolBar();
858         }
859         return accessibleContext;
860     }
861
862     /**
863      * This class implements accessibility support for the
864      * <code>JToolBar</code> class. It provides an implementation of the
865      * Java Accessibility API appropriate to toolbar user-interface elements.
866      */

867     protected class AccessibleJToolBar extends AccessibleJComponent {
868
869         /**
870          * Get the state of this object.
871          *
872          * @return an instance of AccessibleStateSet containing the current
873          * state set of the object
874          * @see AccessibleState
875          */

876         public AccessibleStateSet getAccessibleStateSet() {
877             AccessibleStateSet states = super.getAccessibleStateSet();
878             // FIXME: [[[WDW - need to add orientation from BoxLayout]]]
879
// FIXME: [[[WDW - need to do SELECTABLE if SelectionModel is added]]]
880
return states;
881         }
882
883         /**
884          * Get the role of this object.
885          *
886          * @return an instance of AccessibleRole describing the role of the object
887          */

888         public AccessibleRole getAccessibleRole() {
889             return AccessibleRole.TOOL_BAR;
890         }
891     } // inner class AccessibleJToolBar
892
}
893
Popular Tags