Salome HOME
Update copyrights 2014.
[tools/siman.git] / Workspace / Siman / src / org / splat / wapp / ContextualMenu.java
1 package org.splat.wapp;
2
3 /**
4  * Abstract class implementing the base services of contextual menu such as popup-menus and tool-bars.
5  * 
6  * @see Menu
7  * @author Daniel Brunier-Coulin
8  */
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 public class ContextualMenu {
16
17         protected transient int _width;
18         protected transient int _height;
19         protected transient Map<String, Object> _context;
20         protected List<ContextualItem> _items; // Instead of HashMap for being able to see ContextualMenu objects as Lists
21         protected transient Map<String, Integer> _indices; // Indices of ContextualItem objects into items
22
23         private enum Side {
24                 Client, Server
25         }; // Side of action execution
26
27         protected static class ContextualItem {
28                 private transient String _myname;
29                 private transient ContextualMenu _owner;
30                 private transient Side _execute;
31                 protected transient int _width = 0;
32                 protected transient int _height = 0;
33                 protected String _action = null;
34                 protected transient String _myicon = null;
35                 protected transient String _mylabel = null; // Either menu item name or button tooltip
36                 protected String _confirm = null; // Either menu item confirmation message or button argument
37
38                 public String getAction() {
39                         return _action; // Null if not enabled
40                 }
41
42                 public int getHeight() {
43                         return _height;
44                 }
45
46                 public String getIcon() {
47                         return _myicon;
48                 }
49
50                 public int getWidth() {
51                         return _width;
52                 }
53
54                 public boolean isSeparator() {
55                         return (_mylabel == null && _myicon == null);
56                 }
57
58                 public void setAction(final String url) { // Enables the item
59                         _action = url;
60                         if (url.endsWith(")")) {
61                                 _execute = Side.Client; // JavaScript function
62                         } else {
63                                 _execute = Side.Server; // URL
64                         }
65                 }
66
67                 public Boolean isClientSide() {
68                         Boolean res = null;
69                         if (_action != null) {
70                                 res = (_execute == Side.Client);
71                         }
72                         return res;
73                 }
74
75                 public boolean isEnabled() {
76                         return _owner.isEnabled(_myname); // Delegation to overridden function
77                 }
78         }
79
80         // ==============================================================================================================================
81         // Construction
82         // ==============================================================================================================================
83
84         protected ContextualMenu() {
85                 _context = new HashMap<String, Object>();
86                 _items = new ArrayList<ContextualItem>();
87                 _indices = new HashMap<String, Integer>();
88         }
89
90         protected void addItem(final String name, final ContextualItem item) {
91                 _indices.put(name, _items.size());
92                 _items.add(item);
93                 item._myname = name; // Just for delegating isEnabled
94                 item._owner = this;
95         }
96
97         // ==============================================================================================================================
98         // Member functions
99         // ==============================================================================================================================
100
101         public int getHeight() {
102                 return _height;
103         }
104
105         public int getWidth() {
106                 return _width;
107         }
108
109         public boolean isEmpty() {
110                 return _items.isEmpty();
111         }
112
113         public boolean isEnabled(final String name) {
114                 return (_items.get(_indices.get(name)).getAction() != null);
115         }
116
117         public void resetContext(final String name) {
118                 this._context.remove(name);
119         }
120
121         public void setContext(final String name, final Object context) {
122                 this._context.put(name, context);
123         }
124
125         /**
126          * Get the items.
127          * @return the items
128          */
129         public List<ContextualItem> getItems() {
130                 return _items;
131         }
132
133         /**
134          * Set the items.
135          * @param items the items to set
136          */
137         public void setItems(final List<ContextualItem> items) {
138                 _items = items;
139         }
140 }