1 package org.splat.wapp;
3 import java.util.HashMap;
5 import java.util.Vector;
8 * Base abstract class defining the minimal services for implementing menus.<br/>
9 * A menu is an ordered list of named menu-items. Each menu-item is made of:
11 * <li>A non localized label (a key into a locale file)</li>
12 * <li>Optionally, an icon (an image file name)</li>
13 * <li>A command to be executed when selecting this menu-item</li>
15 * On the contrary of PopupMenus, Menus are not contextual, that is, their items are always active, and can keep the user
17 * After its creation, a menu is in Unselected state - the getSelection() method returns null. The user selection is then
18 * set (and changed) by the name of its corresponding menu-item, thus making getSelection() returning this name.<br/>
20 * The commands of menu-items are in an application-dependent format. They can be undefined, for example in a demonstration
21 * version of the application, making the corresponding menu-items disabled.<br/>
23 * Menus are visible by the application as Lists (from java.util) for allowing their display.
27 * @author Daniel Brunier-Coulin
29 public abstract class Menu {
31 private String myname;
32 private String myscope;
33 protected Vector<MenuItem> menu; // For making the menu visible as a list of MenuItems
34 protected HashMap<String,Integer> indices; // Indices of MenuItem objects into this menu
35 protected HashMap<String,String> disabled; // Actions of disabled item
36 protected String selection = null;
38 // ==============================================================================================================================
40 // ==============================================================================================================================
42 protected Menu (String name) {
43 // ----------------------------
46 menu = new Vector<MenuItem>();
47 indices = new HashMap<String,Integer>();
48 disabled = new HashMap<String,String>();
50 protected Menu (String name, String scope) {
51 // ------------------------------------------
53 myscope = "/" + scope;
54 menu = new Vector<MenuItem>();
55 indices = new HashMap<String,Integer>();
56 disabled = new HashMap<String,String>();
59 // ==============================================================================================================================
61 // ==============================================================================================================================
63 * Adds an item to this menu.
64 * @param name the name of the item added to this menu
65 * @param item the added item
67 public void addItem (String name, MenuItem item) {
68 // ------------------------------------------------
69 indices.put(name, menu.size());
73 * Returns the list of items of this menu.
74 * @return the list of items of this menu.
76 public List<MenuItem> asList () {
77 // -------------------------------
81 * Disables the menu item of given name.
82 * @param name the name of the item
84 public void disables (String name) {
85 // ----------------------------------
86 String action = disabled.get(name);
87 MenuItem item = menu.get(indices.get(name));
89 if (action != null) return; // Item already disabled
90 disabled.put(name, item.getAction()); // Saves the current action for latter enabling
94 * Enables the menu item of given name, if previously disabled.
95 * @param name the name of the item
97 public void enables (String name) {
98 // ---------------------------------
99 String action = disabled.get(name);
100 MenuItem item = menu.get(indices.get(name));
102 if (action == null) return; // Item not previously disabled
103 disabled.remove(name);
107 * Returns the name of this menu.
108 * @return the name of this menu.
110 public String getName () {
111 // ------------------------
115 * Returns the namespace of this menu.
116 * @return the namespace of this menu.
118 public String getNamespace () {
119 // -----------------------------
123 * Returns the currently selected item of this menu previously set by selects().
124 * @return the currently selected item of this menu.
126 public MenuItem getSelectedItem () {
127 // ----------------------------------
128 return menu.get(indices.get(selection));
131 * Returns the name of the currently selected item of this menu previously set by selects().
132 * If no item selected, returns null.
133 * @return the name of the currently selected item of this menu.
135 public String getSelection () {
136 // -----------------------------
137 return selection; // May be null
140 * Sets the given menu-item as selected.
141 * @param name the name of the selected menu-item.
143 public void selects (String name) {
144 // ---------------------------------
145 Integer newdex = indices.get(name);
147 if (selection != null) menu.get(indices.get(selection)).unselect();
148 if (newdex == null) return;
149 menu.get(newdex).select();