1 package org.splat.wapp;
4 * Abstract class implementing the base services of contextual menu such as popup-menus and tool-bars.
7 * @author Daniel Brunier-Coulin
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
15 public class ContextualMenu {
17 protected transient int _width;
18 protected transient int _height;
19 protected transient Map<String, Object> _context;
20 protected transient 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
25 }; // Side of action execution
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
38 public String getAction() {
39 return _action; // Null if not enabled
42 public int getHeight() {
46 public String getIcon() {
50 public int getWidth() {
54 public boolean isSeparator() {
55 return (_mylabel == null && _myicon == null);
58 public void setAction(final String url) { // Enables the item
60 if (url.endsWith(")")) {
61 _execute = Side.Client; // JavaScript function
63 _execute = Side.Server; // URL
67 public Boolean isClientSide() {
69 if (_action != null) {
70 res = (_execute == Side.Client);
75 public boolean isEnabled() {
76 return _owner.isEnabled(_myname); // Delegation to overridden function
80 // ==============================================================================================================================
82 // ==============================================================================================================================
84 protected ContextualMenu() {
85 _context = new HashMap<String, Object>();
86 _items = new ArrayList<ContextualItem>();
87 _indices = new HashMap<String, Integer>();
90 protected void addItem(final String name, final ContextualItem item) {
91 _indices.put(name, _items.size());
93 item._myname = name; // Just for delegating isEnabled
97 // ==============================================================================================================================
99 // ==============================================================================================================================
101 public int getHeight() {
105 public int getWidth() {
109 public boolean isEmpty() {
110 return _items.isEmpty();
113 public boolean isEnabled(final String name) {
114 return (_items.get(_indices.get(name)).getAction() != null);
117 public void resetContext(final String name) {
118 this._context.remove(name);
121 public void setContext(final String name, final Object context) {
122 this._context.put(name, context);