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., 59 Temple Place, Suite 330, 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
.Graphics
;
43 import java
.awt
.Insets
;
44 import java
.awt
.event
.KeyEvent
;
45 import java
.awt
.event
.MouseEvent
;
47 import javax
.accessibility
.Accessible
;
48 import javax
.accessibility
.AccessibleContext
;
49 import javax
.swing
.plaf
.MenuBarUI
;
52 * JMenuBar is a container for menu's. For a menu bar to be seen on the
53 * screen, at least one menu should be added to it. Just like adding
54 * components to container, one can use add() to add menu's to the menu bar.
55 * Menu's will be displayed in the menu bar in the order they were added.
56 * The JMenuBar uses selectionModel to keep track of selected menu index.
57 * JMenuBar's selectionModel will fire ChangeEvents to its registered
58 * listeners when the selected index changes.
60 public class JMenuBar
extends JComponent
implements Accessible
, MenuElement
62 private static final long serialVersionUID
= -8191026883931977036L;
64 /** JMenuBar's model. It keeps track of selected menu's index */
65 private transient SingleSelectionModel selectionModel
;
67 /* borderPainted property indicating if the menuBar's border will be painted*/
68 private boolean borderPainted
;
70 /* margin between menu bar's border and its menues*/
71 private Insets margin
;
74 * Creates a new JMenuBar object.
78 selectionModel
= new DefaultSingleSelectionModel();
84 * Adds menu to the menu bar
86 * @param c menu to add
88 * @return reference to the added menu
90 public JMenu
add(JMenu c
)
92 c
.setAlignmentX(Component
.LEFT_ALIGNMENT
);
98 * This method overrides addNotify() in the Container to register
99 * this menu bar with the current keyboard manager.
101 public void addNotify()
103 // FIXME: Should register this menu bar with the keyboard manager
107 public AccessibleContext
getAccessibleContext()
113 * Returns reference to this menu bar
115 * @return reference to this menu bar
117 public Component
getComponent()
123 * Returns component at the specified index.
125 * @param i index of the component to get
127 * @return component at the specified index. Null is returned if
128 * component at the specified index doesn't exist.
129 * @deprecated Replaced by getComponent(int)
131 public Component
getComponentAtIndex(int i
)
133 return getComponent(i
);
137 * Returns index of the specified component
139 * @param c Component to search for
141 * @return index of the specified component. -1 is returned if
142 * specified component doesnt' exist in the menu bar.
144 public int getComponentIndex(Component c
)
146 Component
[] comps
= getComponents();
150 for (int i
= 0; i
< comps
.length
; i
++)
152 if (comps
[i
].equals(c
))
165 * @return DOCUMENT ME!
167 public JMenu
getHelpMenu()
173 * Returns margin betweeen menu bar's border and its menues
175 * @return margin between menu bar's border and its menues
177 public Insets
getMargin()
180 return new Insets(0, 0, 0, 0);
186 * Return menu at the specified index. If component at the
187 * specified index is not a menu, then null is returned.
189 * @param index index to look for the menu
191 * @return menu at specified index, or null if menu doesn't exist
192 * at the specified index.
194 public JMenu
getMenu(int index
)
196 if (getComponentAtIndex(index
) instanceof JMenu
)
197 return (JMenu
) getComponentAtIndex(index
);
203 * Returns number of menu's in this menu bar
205 * @return number of menu's in this menu bar
207 public int getMenuCount()
209 return getComponentCount();
213 * Returns selection model for this menu bar. SelectionModel
214 * keeps track of the selected menu in the menu bar. Whenever
215 * selected property of selectionModel changes, the ChangeEvent
216 * will be fired its ChangeListeners.
218 * @return selection model for this menu bar.
220 public SingleSelectionModel
getSelectionModel()
222 return selectionModel
;
226 * Method of MenuElement interface. It returns subcomponents
227 * of the menu bar, which are all the menues that it contains.
229 * @return MenuElement[] array containing menues in this menu bar
231 public MenuElement
[] getSubElements()
233 MenuElement
[] subElements
= new MenuElement
[getComponentCount()];
235 for (int i
= 0; i
< getComponentCount(); i
++)
236 subElements
[i
] = (MenuElement
) getMenu(i
);
242 * Set the "UI" property of the menu bar, which is a look and feel class
243 * responsible for handling the menuBar's input events and painting it.
245 * @return The current "UI" property
247 public MenuBarUI
getUI()
249 return (MenuBarUI
) ui
;
253 * This method returns a name to identify which look and feel class will be
254 * the UI delegate for the menu bar.
256 * @return The Look and Feel classID. "MenuItemUI"
258 public String
getUIClassID()
264 * Returns true if menu bar paints its border and false otherwise
266 * @return true if menu bar paints its border and false otherwise
268 public boolean isBorderPainted()
270 return borderPainted
;
274 * Returns true if some menu in menu bar is selected.
276 * @return true if some menu in menu bar is selected and false otherwise
278 public boolean isSelected()
280 return selectionModel
.isSelected();
284 * This method does nothing by default. This method is need for the
285 * MenuElement interface to be implemented.
287 * @param isIncluded true if menuBar is included in the selection
288 * and false otherwise
290 public void menuSelectionChanged(boolean isIncluded
)
292 // Do nothing - needed for implementation of MenuElement interface
296 * Paints border of the menu bar, if its borderPainted property is set to
299 * @param g The graphics context with which to paint the border
301 protected void paintBorder(Graphics g
)
304 getBorder().paintBorder(this, g
, 0, 0, getSize(null).width
,
305 getSize(null).height
);
309 * A string that describes this JMenuBar. Normally only used
312 * @return A string describing this JMenuBar
314 protected String
paramString()
316 StringBuffer sb
= new StringBuffer();
317 sb
.append(super.paramString());
318 sb
.append(",margin=");
319 if (getMargin() != null)
320 sb
.append(getMargin());
321 sb
.append(",paintBorder=").append(isBorderPainted());
322 return sb
.toString();
326 * Process key events forwarded from MenuSelectionManager. This method
327 * doesn't do anything. It is here to conform to the MenuElement interface.
329 * @param event event forwarded from MenuSelectionManager
330 * @param path path to the menu element from which event was generated
331 * @param manager MenuSelectionManager for the current menu hierarchy
334 public void processKeyEvent(KeyEvent e
, MenuElement
[] path
,
335 MenuSelectionManager manager
)
337 // Do nothing - needed for implementation of MenuElement interface
341 * Process mouse events forwarded from MenuSelectionManager. This method
342 * doesn't do anything. It is here to conform to the MenuElement interface.
344 * @param event event forwarded from MenuSelectionManager
345 * @param path path to the menu element from which event was generated
346 * @param manager MenuSelectionManager for the current menu hierarchy
349 public void processMouseEvent(MouseEvent event
, MenuElement
[] path
,
350 MenuSelectionManager manager
)
352 // Do nothing - needed for implementation of MenuElement interface
356 * This method overrides removeNotify() in the Container to
357 * unregister this menu bar from the current keyboard manager.
359 public void removeNotify()
361 // Must unregister this menu bar with the current keyboard manager.
362 super.removeNotify();
366 * Sets painting status of the border. If 'b' is true then menu bar's
367 * border will be painted, and it will not be painted otherwise.
369 * @param b indicates if menu bar's border should be painted.
371 public void setBorderPainted(boolean b
)
373 if (b
!= borderPainted
)
375 boolean old
= borderPainted
;
377 firePropertyChange("borderPainted", old
, b
);
384 * Sets help menu for this menu bar
386 * @param menu help menu
388 public void setHelpMenu(JMenu menu
)
393 * Sets the menu bar's "margin" bound property, which represents
394 * distance between the menubar's border and its menus.
395 * icon. When marging property is modified, PropertyChangeEvent will
396 * be fired to menuBar's PropertyChangeListener's.
398 * @param m distance between the menubar's border and its menus.
401 public void setMargin(Insets m
)
405 Insets oldMargin
= margin
;
407 firePropertyChange("margin", oldMargin
, margin
);
412 * Changes menu bar's selection to the specified menu.
413 * This method updates selected index of menu bar's selection model,
414 * which results in a model firing change event.
416 * @param sel menu to select
418 public void setSelected(Component sel
)
420 int index
= getComponentIndex(sel
);
421 selectionModel
.setSelectedIndex(index
);
425 * Sets menuBar's selection model to the one specified
427 * @param model SingleSelectionModel that needs to be set for this menu bar
429 public void setSelectionModel(SingleSelectionModel model
)
431 if (selectionModel
!= model
)
433 SingleSelectionModel oldModel
= selectionModel
;
434 selectionModel
= model
;
435 firePropertyChange("model", oldModel
, selectionModel
);
440 * Set the "UI" property of the menu bar, which is a look and feel class
441 * responsible for handling menuBar's input events and painting it.
443 * @param ui The new "UI" property
445 public void setUI(MenuBarUI ui
)
451 * Set the "UI" property to a class constructed, via the {@link
452 * UIManager}, from the current look and feel.
454 public void updateUI()
456 setUI((MenuBarUI
) UIManager
.getUI(this));