]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/wapp/Menu.java
Salome HOME
Update copyrights 2014.
[tools/siman.git] / Workspace / Siman / src / org / splat / wapp / Menu.java
1 package org.splat.wapp;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Vector;
7
8 /**
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:
11  * <ul>
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>
15  * </ul>
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.
21  * 
22  * @see MenuItem
23  * @see ContextualMenu
24  * @author Daniel Brunier-Coulin
25  */
26 public class Menu {
27
28         private transient final String _myname;
29         private transient final String _myscope;
30         /**
31          * For making the menu visible as a list of MenuItems.
32          */
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)
35         /**
36          * Indices of MenuItem objects into this menu.
37          */
38         protected transient Map<String, Integer> _indices;
39         /**
40          * Actions of disabled item.
41          */
42         protected transient Map<String, String> _disabled;
43         protected transient String _selection = null;
44
45         // ==============================================================================================================================
46         // Constructors
47         // ==============================================================================================================================
48
49         protected Menu(final String name) {
50                 _myname = name;
51                 _myscope = "/";
52                 _menu = new Vector<MenuItem>();
53                 _indices = new HashMap<String, Integer>();
54                 _disabled = new HashMap<String, String>();
55         }
56
57         protected Menu(final String name, final String scope) {
58                 _myname = name;
59                 _myscope = "/" + scope;
60                 _menu = new Vector<MenuItem>();
61                 _indices = new HashMap<String, Integer>();
62                 _disabled = new HashMap<String, String>();
63         }
64
65         /**
66          * Initialize the menu.
67          */
68         protected void init() {
69                 _menu.clear();
70                 _indices.clear();
71                 _disabled.clear();
72         }
73
74         // ==============================================================================================================================
75         // Member functions
76         // ==============================================================================================================================
77         /**
78          * Adds an item to this menu.
79          * 
80          * @param name
81          *            the name of the item added to this menu
82          * @param item
83          *            the added item
84          */
85         public void addItem(final String name, final MenuItem item) {
86                 _indices.put(name, _menu.size());
87                 _menu.add(item);
88         }
89
90         /**
91          * Returns the list of items of this menu.
92          * 
93          * @return the list of items of this menu.
94          */
95         public List<MenuItem> asList() {
96                 return _menu;
97         }
98
99         /**
100          * Disables the menu item of given name.
101          * 
102          * @param name
103          *            the name of the item
104          */
105         public void disables(final String name) {
106                 String action = _disabled.get(name);
107                 MenuItem item = _menu.get(_indices.get(name));
108
109                 if (action == null) { // Item not previously disabled
110                         _disabled.put(name, item.getAction()); // Saves the current action for latter enabling
111                         item.action(null);
112                 }
113         }
114
115         /**
116          * Enables the menu item of given name, if previously disabled.
117          * 
118          * @param name
119          *            the name of the item
120          */
121         public void enables(final String name) {
122                 String action = _disabled.get(name);
123                 MenuItem item = _menu.get(_indices.get(name));
124
125                 if (action != null) { // Item already disabled
126                         _disabled.remove(name);
127                         item.action(action);
128                 }
129         }
130
131         /**
132          * Returns the name of this menu.
133          * 
134          * @return the name of this menu.
135          */
136         public String getName() {
137                 return _myname;
138         }
139
140         /**
141          * Returns the namespace of this menu.
142          * 
143          * @return the namespace of this menu.
144          */
145         public String getNamespace() {
146                 return _myscope;
147         }
148
149         /**
150          * Returns the currently selected item of this menu previously set by selects().
151          * 
152          * @return the currently selected item of this menu.
153          */
154         public MenuItem getSelectedItem() {
155                 return _menu.get(_indices.get(_selection));
156         }
157
158         /**
159          * Returns the name of the currently selected item of this menu previously set by selects(). If no item selected, returns null.
160          * 
161          * @return the name of the currently selected item of this menu.
162          */
163         public String getSelection() {
164                 return _selection; // May be null
165         }
166
167         /**
168          * Sets the given menu-item as selected.
169          * 
170          * @param name
171          *            the name of the selected menu-item.
172          */
173         public void selects(final String name) {
174                 Integer newdex = _indices.get(name);
175
176                 if ((_selection != null) && _indices.containsKey(_selection)) {
177                         _menu.get(_indices.get(_selection)).unselect();
178                 }
179                 if (newdex != null) {
180                         _menu.get(newdex).select();
181                         _selection = name;
182                 }
183         }
184
185         /**
186          * Get default selection.
187          * 
188          * @return the first item name or "none" if the menu is empty
189          */
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) {
195                                         res = item;
196                                         break;
197                                 }
198                         }
199                 }
200                 return res;
201         }
202 }