KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SMenuBar


1 /*
2  * $Id: SMenuBar.java,v 1.10 2005/05/13 09:02:49 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import org.wings.plaf.MenuBarCG;
17
18 import javax.swing.*;
19 import java.awt.*;
20 import java.util.ArrayList JavaDoc;
21
22 /**
23  * An implementation of a MenuBar. You add SMenu objects to the
24  * menu bar to construct a menu. When the user selects a SMenu
25  * object, its associated {@link org.wings.SMenu} is displayed, allowing the
26  * user to select one of the {@link org.wings.SMenuItem}s on it.
27  * <p/>
28  * Component are rendered in the order of the container. If a component is right
29  * aligned, every following components are also right aligned. So you have to
30  * sort the components in the order you want and have to take care that te
31  * components are sorted by their horizontal alignment
32  *
33  * @author <a HREF="mailto:andre@lison.de">Andre Lison</a>
34  * @author <a HREF="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
35  * @see SMenu
36  * @see SMenuItem
37  */

38 public class SMenuBar extends SContainer {
39     /*
40      * Model for the selected subcontrol
41      */

42     private transient SingleSelectionModel selectionModel;
43
44     private boolean paintBorder = true;
45     private Insets margin = null;
46
47     /**
48      * Creates a new menu bar.
49      */

50     public SMenuBar() {
51         super();
52         setSelectionModel(new DefaultSingleSelectionModel());
53     }
54
55     /**
56      * Returns the model object that handles single selections.
57      *
58      * @return the SingleSelectionModel in use
59      * @see SingleSelectionModel
60      */

61     public SingleSelectionModel getSelectionModel() {
62         return selectionModel;
63     }
64
65     /**
66      * Set the model object to handle single selections.
67      *
68      * @param model the SingleSelectionModel to use
69      * @beaninfo bound: true
70      * description: The selection model, recording which child is selected.
71      * @see SingleSelectionModel
72      */

73     public void setSelectionModel(SingleSelectionModel model) {
74         SingleSelectionModel oldValue = selectionModel;
75         this.selectionModel = model;
76     }
77
78
79     /**
80      * Appends the specified menu to the end of the menu bar.
81      *
82      * @param c the SMenu component to add
83      */

84     public SMenuItem add(SMenuItem c) {
85         super.add(c);
86         return c;
87     }
88
89     /**
90      * Gets the menu at the specified position in the menu bar.
91      *
92      * @param index an int giving the position in the menu bar, where
93      * 0 is the first position
94      * @return the SMenu at that position
95      */

96     public SMenuItem getMenuItem(int index) {
97         SComponent c = (SComponent) super.getComponent(index);
98         if (c instanceof SMenuItem)
99             return (SMenuItem) c;
100         return null;
101     }
102
103     /**
104      * Returns the number of items in the menu bar.
105      *
106      * @return the number of items in the menu bar
107      */

108     public int getMenuCount() {
109         return getComponentCount();
110     }
111
112     /**
113      * Returns the index of the specified component.
114      *
115      * @param c the <code>SComponent</code> to find
116      * @return an integer giving the component's position, where 0 is first;
117      * or -1 if it can't be found
118      */

119     public int getComponentIndex(SComponent c) {
120         int ncomponents = this.getComponentCount();
121         for (int i = 0; i < ncomponents; i++) {
122             SComponent comp = getComponent(i);
123             if (comp == c)
124                 return i;
125         }
126         return -1;
127     }
128
129     /**
130      * Sets the currently selected component, producing a
131      * a change to the selection model.
132      *
133      * @param sel the SComponent to select
134      */

135     public void setSelected(SComponent sel) {
136         SingleSelectionModel model = getSelectionModel();
137         int index = getComponentIndex(sel);
138         model.setSelectedIndex(index);
139     }
140
141     /**
142      * Returns true if the MenuBar currently has a component selected
143      *
144      * @return true if a selection has been made, else false
145      */

146     public boolean isSelected() {
147         return selectionModel.isSelected();
148     }
149
150     /**
151      * Returns true if the Menubar's border should be painted.
152      *
153      * @return true if the border should be painted, else false
154      */

155     public boolean isBorderPainted() {
156         return paintBorder;
157     }
158
159     /**
160      * Sets whether the border should be painted.
161      *
162      * @param b if true and border property is not null, the border is painted.
163      * @beaninfo bound: true
164      * attribute: visualUpdate true
165      * description: Whether the border should be painted.
166      * @see #isBorderPainted
167      */

168     public void setBorderPainted(boolean b) {
169         boolean oldValue = paintBorder;
170         paintBorder = b;
171     }
172
173     /**
174      * Paint the menubar's border if BorderPainted property is true.
175      *
176      * @param g the Graphics context to use for painting
177      * @see SComponent#paint
178      * @see SComponent#setBorder
179      */

180     /*
181     protected void paintBorder(Graphics g) {
182         if (isBorderPainted()) {
183             super.paintBorder(g);
184         }
185     }
186     */

187
188     /**
189      * Sets the margin between the menubar's border and
190      * its menus. Setting to null will cause the menubar to
191      * use the default margins.
192      *
193      * @param m an Insets object containing the margin values
194      * @beaninfo bound: true
195      * attribute: visualUpdate true
196      * description: The space between the menubar's border and its contents
197      * @see Insets
198      */

199     public void setMargin(Insets m) {
200         Insets old = margin;
201         this.margin = m;
202         /*
203           if (old == null || !m.equals(old)) {
204                 revalidate();
205                 repaint();
206           }
207         */

208     }
209
210     /**
211      * Returns the margin between the menubar's border and
212      * its menus.
213      *
214      * @return an Insets object containing the margin values
215      * @see Insets
216      */

217     public Insets getMargin() {
218         if (margin == null) {
219             return new Insets(0, 0, 0, 0);
220         } else {
221             return margin;
222         }
223     }
224
225
226     /**
227      * Implemented to be a MenuElement -- does nothing.
228      */

229     public void menuSelectionChanged(boolean isIncluded) {
230     }
231
232     /**
233      * Returns a string representation of this SMenuBar. This method
234      * is intended to be used only for debugging purposes, and the
235      * content and format of the returned string may vary between
236      * implementations. The returned string may be empty but may not
237      * be <code>null</code>.
238      *
239      * @return a string representation of this SMenuBar.
240      */

241     public String JavaDoc paramString() {
242         String JavaDoc paintBorderString = (paintBorder ?
243                 "true" : "false");
244         String JavaDoc marginString = (margin != null ?
245                 margin.toString() : "");
246
247         return super.paramString() +
248                 ",margin=" + marginString +
249                 ",paintBorder=" + paintBorderString;
250     }
251
252     public void setCG(MenuBarCG cg) {
253         super.setCG(cg);
254     }
255     
256     /**
257      * Close all currently open menus.
258      */

259     public void closeAllMenus() {
260         /* for ( int i = 0; i < fComponents.size(); i++ ) {
261             if ( fComponents.get(i) instanceof SMenu ) {
262                 ((SMenu) fComponents.get(i)).setActive( false );
263             }
264             }*/

265     }
266
267     public ArrayList JavaDoc getMenus() {
268         ArrayList JavaDoc menus = new ArrayList JavaDoc();
269         if (isVisible()) {
270             SPopupMenu pmenu = getComponentPopupMenu();
271             if (pmenu != null) {
272                 menus.add(pmenu);
273             }
274             for (int i = 0; i < getComponentCount(); i++) {
275                 SMenu menu = (SMenu)getComponent(i);
276                 if (menus.contains(menu)) {
277                     remove(menu);
278                 }
279                 menus.add(menu);
280             }
281         }
282         return menus;
283     }
284 }
285
286
287
Popular Tags