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>();
60 * Initialize the menu.
62 protected void init() {
67 // ==============================================================================================================================
69 // ==============================================================================================================================
71 * Adds an item to this menu.
72 * @param name the name of the item added to this menu
73 * @param item the added item
75 public void addItem (String name, MenuItem item) {
76 // ------------------------------------------------
77 indices.put(name, menu.size());
81 * Returns the list of items of this menu.
82 * @return the list of items of this menu.
84 public List<MenuItem> asList () {
85 // -------------------------------
89 * Disables the menu item of given name.
90 * @param name the name of the item
92 public void disables (String name) {
93 // ----------------------------------
94 String action = disabled.get(name);
95 MenuItem item = menu.get(indices.get(name));
97 if (action != null) return; // Item already disabled
98 disabled.put(name, item.getAction()); // Saves the current action for latter enabling
102 * Enables the menu item of given name, if previously disabled.
103 * @param name the name of the item
105 public void enables (String name) {
106 // ---------------------------------
107 String action = disabled.get(name);
108 MenuItem item = menu.get(indices.get(name));
110 if (action == null) return; // Item not previously disabled
111 disabled.remove(name);
115 * Returns the name of this menu.
116 * @return the name of this menu.
118 public String getName () {
119 // ------------------------
123 * Returns the namespace of this menu.
124 * @return the namespace of this menu.
126 public String getNamespace () {
127 // -----------------------------
131 * Returns the currently selected item of this menu previously set by selects().
132 * @return the currently selected item of this menu.
134 public MenuItem getSelectedItem () {
135 // ----------------------------------
136 return menu.get(indices.get(selection));
139 * Returns the name of the currently selected item of this menu previously set by selects().
140 * If no item selected, returns null.
141 * @return the name of the currently selected item of this menu.
143 public String getSelection () {
144 // -----------------------------
145 return selection; // May be null
148 * Sets the given menu-item as selected.
149 * @param name the name of the selected menu-item.
151 public void selects (String name) {
152 // ---------------------------------
153 Integer newdex = indices.get(name);
155 if (selection != null) menu.get(indices.get(selection)).unselect();
156 if (newdex == null) return;
157 menu.get(newdex).select();