1 package org.splat.wapp;
3 * Base abstract class defining the minimal services for implementing menus.<br/>
4 * A menu is an ordered list of named menu-items. Each menu-item is made of:
6 * <li>A non localized label (a key into a locale file)</li>
7 * <li>Optionally, an icon (an image file name)</li>
8 * <li>A command to be executed when selecting this menu-item</li>
10 * On the contrary of PopupMenus, Menus are not contextual, that is, their items are always active, and can keep the user
12 * After its creation, a menu is in Unselected state - the getSelection() method returns null. The user selection is then
13 * set (and changed) by the name of its corresponding menu-item, thus making getSelection() returning this name.<br/>
15 * The commands of menu-items are in an application-dependent format. They can be undefined, for example in a demonstration
16 * version of the application, making the corresponding menu-items disabled.<br/>
18 * Menus are visible by the application as Lists (from java.util) for allowing their display.
22 * @author Daniel Brunier-Coulin
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Vector;
30 public abstract class Menu {
32 private String myname;
33 private String myscope;
34 protected Vector<MenuItem> menu; // For making the menu visible as a list of MenuItems
35 protected HashMap<String,Integer> indices; // Indices of MenuItem objects into this menu
36 protected HashMap<String,String> disabled; // Actions of disabled item
37 protected String selection = null;
39 // ==============================================================================================================================
41 // ==============================================================================================================================
43 protected Menu (String name) {
44 // ----------------------------
47 menu = new Vector<MenuItem>();
48 indices = new HashMap<String,Integer>();
49 disabled = new HashMap<String,String>();
51 protected Menu (String name, String scope) {
52 // ------------------------------------------
54 myscope = "/" + scope;
55 menu = new Vector<MenuItem>();
56 indices = new HashMap<String,Integer>();
57 disabled = new HashMap<String,String>();
60 // ==============================================================================================================================
62 // ==============================================================================================================================
64 * Adds an item to this menu.
65 * @param name the name of the item added to this menu
66 * @param item the added item
68 public void addItem (String name, MenuItem item) {
69 // ------------------------------------------------
70 indices.put(name, menu.size());
74 * Returns the list of items of this menu.
75 * @return the list of items of this menu.
77 public List<MenuItem> asList () {
78 // -------------------------------
82 * Disables the menu item of given name.
83 * @param name the name of the item
85 public void disables (String name) {
86 // ----------------------------------
87 String action = disabled.get(name);
88 MenuItem item = menu.get(indices.get(name));
90 if (action != null) return; // Item already disabled
91 disabled.put(name, item.getAction()); // Saves the current action for latter enabling
95 * Enables the menu item of given name, if previously disabled.
96 * @param name the name of the item
98 public void enables (String name) {
99 // ---------------------------------
100 String action = disabled.get(name);
101 MenuItem item = menu.get(indices.get(name));
103 if (action == null) return; // Item not previously disabled
104 disabled.remove(name);
108 * Returns the name of this menu.
109 * @return the name of this menu.
111 public String getName () {
112 // ------------------------
116 * Returns the namespace of this menu.
117 * @return the namespace of this menu.
119 public String getNamespace () {
120 // -----------------------------
124 * Returns the currently selected item of this menu previously set by selects().
125 * @return the currently selected item of this menu.
127 public MenuItem getSelectedItem () {
128 // ----------------------------------
129 return menu.get(indices.get(selection));
132 * Returns the name of the currently selected item of this menu previously set by selects().
133 * If no item selected, returns null.
134 * @return the name of the currently selected item of this menu.
136 public String getSelection () {
137 // -----------------------------
138 return selection; // May be null
141 * Sets the given menu-item as selected.
142 * @param name the name of the selected menu-item.
144 public void selects (String name) {
145 // ---------------------------------
146 Integer newdex = indices.get(name);
148 if (selection != null) menu.get(indices.get(selection)).unselect();
149 if (newdex == null) return;
150 menu.get(newdex).select();