de.matthiasmann.twl
Class Menu

java.lang.Object
  extended by de.matthiasmann.twl.MenuElement
      extended by de.matthiasmann.twl.Menu
All Implemented Interfaces:
java.lang.Iterable<MenuElement>

public class Menu
extends MenuElement
implements java.lang.Iterable<MenuElement>

A menu which can be displayed as menu bar or as popup menu.


Nested Class Summary
static interface Menu.Listener
           
 
Field Summary
static AnimationState.StateKey STATE_HAS_OPEN_MENUS
           
 
Constructor Summary
Menu()
          Creates a new menu without name.
Menu(java.lang.String name)
          Creates a new menu with the given name.
 
Method Summary
 Menu add(MenuElement e)
          Adds the given menu element at the end.
 Menu add(java.lang.String name, BooleanModel model)
          Adds a {code MenuCheckbox} element at the end.
 Menu add(java.lang.String name, java.lang.Runnable cb)
          Adds a {code MenuAction} element at the end.
 void addListener(Menu.Listener listener)
           
 Menu addSpacer()
          Adds a {code MenuSpacer} element at the end.
 void clear()
          Removes all menu elements
 Widget createMenuBar()
          Creates a menu bar with a DialogLayout as conatiner.
 void createMenuBar(Widget container)
          Creates a menu bar by adding all menu widgets to the specified container.
protected  MenuManager createMenuManager(Widget parent, boolean isMenuBar)
           
protected  Widget createMenuWidget(MenuManager mm, int level)
           
protected  Widget[] createWidgets(MenuManager mm, int level)
           
 MenuElement get(int index)
          Returns the menu element at the given index.
 Alignment getClassAlignment(java.lang.Class<? extends MenuElement> clazz)
          Retrieves the default alignment for the given menu element class.
 int getNumElements()
          Returns the number of menu elements in this menu.
 java.lang.String getPopupTheme()
          Returns the theme which is used when this menu is displayed as popup/sub menu.
 java.util.Iterator<MenuElement> iterator()
          Returns a mutable iterator which iterates over all menu elements
 MenuManager openPopupMenu(Widget parent)
          Creates a popup menu from this menu.
 MenuManager openPopupMenu(Widget parent, int x, int y)
          Creates a popup menu from this menu at the specified position.
 void removeListener(Menu.Listener listener)
           
 void setClassAlignment(java.lang.Class<? extends MenuElement> clazz, Alignment value)
          Sets the default alignment based on menu element subclasses.
 void setPopupTheme(java.lang.String popupTheme)
          Sets the theme which is used when this menun is displayed as popup/sub menu.
 
Methods inherited from class de.matthiasmann.twl.MenuElement
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, firePropertyChange, firePropertyChange, getAlignment, getName, getTheme, getTooltipContent, isEnabled, removePropertyChangeListener, removePropertyChangeListener, setAlignment, setEnabled, setName, setTheme, setTooltipContent, setWidgetTheme
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATE_HAS_OPEN_MENUS

public static final AnimationState.StateKey STATE_HAS_OPEN_MENUS
Constructor Detail

Menu

public Menu()
Creates a new menu without name. This constructor should be used for top level menus.

See Also:
createMenuBar(), createMenuBar(de.matthiasmann.twl.Widget), openPopupMenu(de.matthiasmann.twl.Widget), openPopupMenu(de.matthiasmann.twl.Widget, int, int)

Menu

public Menu(java.lang.String name)
Creates a new menu with the given name. This constructor should be used used for sub menus. The name is used for the button which opens this sub menu.

Parameters:
name - The name of the popup menu entry
See Also:
add(de.matthiasmann.twl.MenuElement)
Method Detail

addListener

public void addListener(Menu.Listener listener)

removeListener

public void removeListener(Menu.Listener listener)

getPopupTheme

public java.lang.String getPopupTheme()
Returns the theme which is used when this menu is displayed as popup/sub menu.

Returns:
the popup theme

setPopupTheme

public void setPopupTheme(java.lang.String popupTheme)
Sets the theme which is used when this menun is displayed as popup/sub menu.

Parameters:
popupTheme - the popup theme

setClassAlignment

public void setClassAlignment(java.lang.Class<? extends MenuElement> clazz,
                              Alignment value)
Sets the default alignment based on menu element subclasses.

By default all alignments are Alignment.FILL

Parameters:
clazz - the class for which a default alignment should be set
value - the alignment

getClassAlignment

public Alignment getClassAlignment(java.lang.Class<? extends MenuElement> clazz)
Retrieves the default alignment for the given menu element class.

By default all alignments are Alignment.FILL

Parameters:
clazz - the menu element class
Returns:
the alignment

iterator

public java.util.Iterator<MenuElement> iterator()
Returns a mutable iterator which iterates over all menu elements

Specified by:
iterator in interface java.lang.Iterable<MenuElement>
Returns:
a iterator

get

public MenuElement get(int index)
Returns the menu element at the given index.

Parameters:
index - the index. Must be < {code getNumElements}
Returns:
the menu element
Throws:
java.lang.IndexOutOfBoundsException - if index is invalid
See Also:
getNumElements()

getNumElements

public int getNumElements()
Returns the number of menu elements in this menu.

Returns:
the number of menu elements

clear

public void clear()
Removes all menu elements


add

public Menu add(MenuElement e)
Adds the given menu element at the end. It is possible to add the same menu element several times also in different menus.

Parameters:
e - the menu element
Returns:
this

add

public Menu add(java.lang.String name,
                java.lang.Runnable cb)
Adds a {code MenuAction} element at the end. It is equivalent to {code add(new MenuAction(name, cb)) }

Parameters:
name - the name of the menu action
cb - the callback when the menu action has been selected
Returns:
this

add

public Menu add(java.lang.String name,
                BooleanModel model)
Adds a {code MenuCheckbox} element at the end. It is equivalent to {code add(new MenuCheckbox(name, model)) }

Parameters:
name - the name of the menu checkbox
model - the boolean model which is displayed/modified by the menu checkbox
Returns:
this

addSpacer

public Menu addSpacer()
Adds a {code MenuSpacer} element at the end. It is equivalent to {code add(new MenuSpacer()) }

Returns:
this

createMenuBar

public void createMenuBar(Widget container)
Creates a menu bar by adding all menu widgets to the specified container.

Parameters:
container - the container for the menu widgets.
See Also:
createMenuBar()

createMenuBar

public Widget createMenuBar()
Creates a menu bar with a DialogLayout as conatiner. This is the preferred method to create a menu bar.

Returns:
the menu bar conatiner

openPopupMenu

public MenuManager openPopupMenu(Widget parent)
Creates a popup menu from this menu. The popup is positioned to the right of the parent widget.

Parameters:
parent - the parent widget for the popup.
Returns:
the MenuManager which manages this popup
See Also:
MenuManager.closePopup()

openPopupMenu

public MenuManager openPopupMenu(Widget parent,
                                 int x,
                                 int y)
Creates a popup menu from this menu at the specified position.

Parameters:
parent - the parent widget for the popup.
x - the absolute X coordinate for the popup
y - the absolute Y coordinate for the popup
Returns:
the MenuManager which manages this popup
See Also:
MenuManager.closePopup()

createMenuWidget

protected Widget createMenuWidget(MenuManager mm,
                                  int level)
Specified by:
createMenuWidget in class MenuElement

createMenuManager

protected MenuManager createMenuManager(Widget parent,
                                        boolean isMenuBar)

createWidgets

protected Widget[] createWidgets(MenuManager mm,
                                 int level)