net.sf.sdedit.ui.components
Class MenuBar

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JMenuBar
                  extended by net.sf.sdedit.ui.components.MenuBar
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, MenuElement

public class MenuBar
extends JMenuBar

This is a slightly advanced JMenuBar subclass that allows to structure a menu by means of category names, so a user is not required to build JMenus. Furthermore, MenuBar provides a convenient way to define mnemonics for menu entries (by using the '&'-notation as known from Qt).

When an Action or JMenuItem is added to the MenuBar, a title string must be specified. If the string has a prefix that starts with '[' and ends with ']', the substring between these square brackets represents the accelerator key for the action or item. The appropriate key for the string is found as described here: javax.swing.Keystroke#getKeyStroke(java.lang.String).

If a title string contains an '&', the following character is used as the mnemonic for the entry or action. This also applies to categories/sub-menus.

Author:
Markus Strauch
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JMenuBar
JMenuBar.AccessibleJMenuBar
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
MenuBar()
          Creates a new empty MenuBar.
 
Method Summary
 JMenuItem addAction(String category, Action action)
          Adds an action in the given category.
 JMenuItem addAction(String category, Action action, int pos)
          Adds an Action to a (sub-)menu of this MenuBar.
 void addItem(String category, JMenuItem item)
          Adds an item to the given category.
 void addMenu(String category, ImageIcon icon)
          Adds a new (sub-)menu to this MenuBar.
 Action getActionByName(String name)
          Returns an action that has formerly been added by addAction(String, Action) or addAction(String, Action, int).
static
<T extends JMenuItem>
T
makeMenuItem(String title, Class<T> itemType)
          Creates an instance of a subclass of JMenuItem with the given title (possibly specifying an accelerator key or a mnemonic as described here: MenuBar).
 void removeAction(String category, Action action)
          Removes an action from a category.
 void updateUI()
           
 
Methods inherited from class javax.swing.JMenuBar
add, addNotify, getAccessibleContext, getComponent, getComponentAtIndex, getComponentIndex, getHelpMenu, getMargin, getMenu, getMenuCount, getSelectionModel, getSubElements, getUI, getUIClassID, isBorderPainted, isSelected, menuSelectionChanged, paintBorder, paramString, processKeyBinding, processKeyEvent, processMouseEvent, removeNotify, setBorderPainted, setHelpMenu, setMargin, setSelected, setSelectionModel, setUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MenuBar

public MenuBar()
Creates a new empty MenuBar.

Method Detail

getActionByName

public Action getActionByName(String name)
Returns an action that has formerly been added by addAction(String, Action) or addAction(String, Action, int).

Parameters:
name - the name of the action
Returns:
the action with the name, or null

makeMenuItem

public static <T extends JMenuItem> T makeMenuItem(String title,
                                                   Class<T> itemType)
Creates an instance of a subclass of JMenuItem with the given title (possibly specifying an accelerator key or a mnemonic as described here: MenuBar). The item is not added to any menu.

Type Parameters:
T - the type of the JMenuItem to be created
Parameters:
title - the title of the item
itemType - the type of the JMenuItem, represented as a Class object.
Returns:
an instance of a subclass of JMenuItem, bearing the given title

removeAction

public void removeAction(String category,
                         Action action)
Removes an action from a category.

Parameters:
category - the name of the category, containing all '&'-characters
action - the action to be removed from the category

addAction

public JMenuItem addAction(String category,
                           Action action,
                           int pos)
Adds an Action to a (sub-)menu of this MenuBar. The Action must have a name (as set via Action.putValue(Action.NAME,name)), which may specify a mnemonic and accelerator key as described in the class comment: MenuBar().

Example: Let a be an action to be added to the submenu "foo" of "bar". This can be done so: addAction("foo.bar",a,0). If "foo" and "bar" should have "f" resp. "b" as mnemonics, you would call addAction("&foo.&bar",a,0)

Parameters:
category - the string describing the category, where names belonging to adjacent category levels are separated by '.'. Category names may include '&' for mnemonics.
action - the action to be added
pos - the position of the action in the (sub-)menu
Returns:
the JMenuItem instance associated to the action
Throws:
IllegalArgumentException - if an illegal (sub-)category is specified

addMenu

public void addMenu(String category,
                    ImageIcon icon)
Adds a new (sub-)menu to this MenuBar. Menus can be arbitrarily nested. When a menu for a category on the n n-th (n >= 1) level is added, the category string must contain n substrings, separated by '.', describing all higher categories in descending order and finally the n-th category. All submenus for the categories on the levels 1, ..., n-1 must already be present.

Parameters:
category - the category of the (sub-)menu (represented by a string with '.''s as described above)
icon - an icon for the (sub-)menu or null

addAction

public JMenuItem addAction(String category,
                           Action action)
Adds an action in the given category. See also addAction(String, Action, int).

Parameters:
category - the category of the action
action - the action to be added
Returns:
the JMenuItem associated to the action
Throws:
IllegalArgumentException - if the category is part of a non-existing higher level category

addItem

public void addItem(String category,
                    JMenuItem item)
Adds an item to the given category. See also addAction(String, Action, int).

Parameters:
category - the category of the action
item - the item to be added
Throws:
IllegalArgumentException - if the category is part of a non-existing higher level category

updateUI

public void updateUI()
Overrides:
updateUI in class JMenuBar
See Also:
JMenuBar.updateUI()


Copyright © 2011. All Rights Reserved.