Salome HOME
Update copyrights 2014.
[tools/siman.git] / Workspace / Siman / src / org / splat / wapp / TabBar.java
1 package org.splat.wapp;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9
10 /**
11  * Tab bar class.
12  */
13 public class TabBar implements Serializable {
14
15         /**
16          * Serial version ID.
17          */
18         private static final long serialVersionUID = 1851786085512439549L;
19
20         /**
21          * For making the menu visible as a list of MenuItems.
22          */
23         protected transient List<Item>          _menu;
24         /**
25          * Indices of MenuItem objects into this menu.
26          */
27         protected transient Map<String,Integer> _indices;
28         /**
29          * Actions of disabled item.
30          */
31         protected transient Map<String,String>  _disabled;
32     /**
33      * Selected item id.
34      */
35     protected transient String              _selection;
36
37 //  ==============================================================================================================================
38 //  Constructor
39 //  ==============================================================================================================================
40
41     /**
42      * Empty tab bar constuctor.
43      */
44     public TabBar () {
45       _menu      = new ArrayList<Item>();
46       _indices   = new HashMap<String,Integer>();
47       _disabled  = new HashMap<String,String>();
48       _selection = null;
49     }
50
51 //  ==============================================================================================================================
52 //  Member functions
53 //  ==============================================================================================================================
54 /**
55  * Adds an item to this tab-bar.
56  * @param name the name of the item added to this bar.
57  * @param url the added item url
58  */
59     public void addItem (final String name, final String url) {
60       _indices.put(name, _menu.size());
61       _menu.add( new Item(name, url) );
62     }
63
64 /**
65  * Returns the list of items of this tab-bar.
66  * @return the list of items of this bar.
67  */
68     public List<Item> asList () {
69       return _menu;
70     }
71
72 /**
73  * Disables the item of given name.
74  * @param name the name of the item.
75  */
76     public void disables (final String name) {
77       String action = _disabled.get(name);
78       Item   item   = _menu.get(_indices.get(name));
79
80       if (action != null) {
81                 return;             // Item already disabled
82         }
83       _disabled.put(name, item.getAction());   // Saves the current action for latter enabling
84       item.action(null);
85     }
86
87 /**
88  * Enables the item of given name, if previously disabled.
89  * @param name the name of the item.
90  */
91     public void enables (final String name) {
92       String action = _disabled.get(name);
93       Item   item   = _menu.get(_indices.get(name));
94
95       if (action == null) {
96                 return;             // Item not previously disabled
97         }
98       _disabled.remove(name);
99       item.action(action);
100     }
101
102 /**
103  * Returns the name of the currently selected item of this menu previously set by selects().
104  * If no item selected, returns null.
105  * @return the name of the currently selected item of this menu.
106  */
107     public String getSelection () {
108       return _selection;        // May be null
109     }
110
111 /**
112  * Sets the given menu-item as selected.
113  * @param name the name of the selected menu-item.
114  */
115     public void selects (final String name) {
116       Integer  newdex = _indices.get(name);
117
118       if (_selection != null) {
119                 _menu.get(_indices.get(_selection)).unselect();
120         }
121       if (newdex    == null) {
122                 return;
123         }
124       _menu.get(newdex).select();
125       _selection = name;
126     }
127 }