1 package org.splat.wapp;
3 import java.util.HashMap;
6 import java.util.Vector;
9 * Base abstract class defining the minimal services for implementing menus.<br/> A menu is an ordered list of named menu-items. Each
10 * menu-item is made of:
12 * <li>A non localized label (a key into a locale file)</li>
13 * <li>Optionally, an icon (an image file name)</li>
14 * <li>A command to be executed when selecting this menu-item</li>
16 * On the contrary of PopupMenus, Menus are not contextual, that is, their items are always active, and can keep the user selection.<br/>
17 * After its creation, a menu is in Unselected state - the getSelection() method returns null. The user selection is then set (and changed)
18 * by the name of its corresponding menu-item, thus making getSelection() returning this name.<br/> <br/> The commands of menu-items are in
19 * an application-dependent format. They can be undefined, for example in a demonstration version of the application, making the
20 * corresponding menu-items disabled.<br/> <br/> Menus are visible by the application as Lists (from java.util) for allowing their display.
24 * @author Daniel Brunier-Coulin
28 private transient final String _myname;
29 private transient final String _myscope;
31 * For making the menu visible as a list of MenuItems.
33 protected transient Vector<MenuItem> _menu; // RKV: NOPMD: Because of usage of lastElement() method in void
34 // org.splat.wapp.SlidMenu.addSubItem(String name, String label, String icon, String url)
36 * Indices of MenuItem objects into this menu.
38 protected transient Map<String, Integer> _indices;
40 * Actions of disabled item.
42 protected transient Map<String, String> _disabled;
43 protected transient String _selection = null;
45 // ==============================================================================================================================
47 // ==============================================================================================================================
49 protected Menu(final String name) {
52 _menu = new Vector<MenuItem>();
53 _indices = new HashMap<String, Integer>();
54 _disabled = new HashMap<String, String>();
57 protected Menu(final String name, final String scope) {
59 _myscope = "/" + scope;
60 _menu = new Vector<MenuItem>();
61 _indices = new HashMap<String, Integer>();
62 _disabled = new HashMap<String, String>();
66 * Initialize the menu.
68 protected void init() {
74 // ==============================================================================================================================
76 // ==============================================================================================================================
78 * Adds an item to this menu.
81 * the name of the item added to this menu
85 public void addItem(final String name, final MenuItem item) {
86 _indices.put(name, _menu.size());
91 * Returns the list of items of this menu.
93 * @return the list of items of this menu.
95 public List<MenuItem> asList() {
100 * Disables the menu item of given name.
103 * the name of the item
105 public void disables(final String name) {
106 String action = _disabled.get(name);
107 MenuItem item = _menu.get(_indices.get(name));
109 if (action == null) { // Item not previously disabled
110 _disabled.put(name, item.getAction()); // Saves the current action for latter enabling
116 * Enables the menu item of given name, if previously disabled.
119 * the name of the item
121 public void enables(final String name) {
122 String action = _disabled.get(name);
123 MenuItem item = _menu.get(_indices.get(name));
125 if (action != null) { // Item already disabled
126 _disabled.remove(name);
132 * Returns the name of this menu.
134 * @return the name of this menu.
136 public String getName() {
141 * Returns the namespace of this menu.
143 * @return the namespace of this menu.
145 public String getNamespace() {
150 * Returns the currently selected item of this menu previously set by selects().
152 * @return the currently selected item of this menu.
154 public MenuItem getSelectedItem() {
155 return _menu.get(_indices.get(_selection));
159 * Returns the name of the currently selected item of this menu previously set by selects(). If no item selected, returns null.
161 * @return the name of the currently selected item of this menu.
163 public String getSelection() {
164 return _selection; // May be null
168 * Sets the given menu-item as selected.
171 * the name of the selected menu-item.
173 public void selects(final String name) {
174 Integer newdex = _indices.get(name);
176 if ((_selection != null) && _indices.containsKey(_selection)) {
177 _menu.get(_indices.get(_selection)).unselect();
179 if (newdex != null) {
180 _menu.get(newdex).select();
186 * Get default selection.
188 * @return the first item name or "none" if the menu is empty
190 public String getDefaultSelection() {
191 String res = Constants.NONE;
192 if ((!_indices.isEmpty()) && _indices.containsValue(0)) {
193 for (String item : _indices.keySet()) {
194 if (_indices.get(item) == 0) {