1 package org.splat.wapp;
3 * Abstract class implementing the base services of contextual menu such as popup-menus and tool-bars.
6 * @author Daniel Brunier-Coulin
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Vector;
14 public abstract class ContextualMenu {
18 protected HashMap<String,Object> context;
19 protected List<ContextualItem> items; // Instead of HashMap for being able to see ContextualMenu objects as Lists
20 protected HashMap<String,Integer> indices; // Indices of ContextualItem objects into items
22 private enum Side { Client, Server }; // Side of action execution
23 protected static class ContextualItem {
24 // -------------------------------------
25 private String myname;
26 private ContextualMenu owner;
28 protected int width = 0;
29 protected int height = 0;
30 protected String myurl = null;
31 protected String myicon = null;
32 protected String mylabel = null; // Either menu item name or button tooltip
33 protected String confirm = null; // Either menu item confirmation message or button argument
35 public String getAction () {
36 return myurl; // Null if not enabled
38 public int getHeight () {
41 public String getIcon () {
44 public int getWidth () {
47 public boolean isSeparator () {
48 return (mylabel == null && myicon == null);
50 public void setAction (String url) { // Enables the item
52 if (url.endsWith(")")) execute = Side.Client; // JavaScript function
53 else execute = Side.Server; // URL
55 public Boolean isClientSide () {
56 if (myurl != null) return (execute == Side.Client);
59 public boolean isEnabled () {
60 return owner.isEnabled(myname); // Delegation to overridden function
64 // ==============================================================================================================================
66 // ==============================================================================================================================
68 protected ContextualMenu () {
69 // ---------------------------
70 context = new HashMap<String,Object>();
71 items = new Vector<ContextualItem>();
72 indices = new HashMap<String,Integer>();
74 protected void addItem (String name, ContextualItem item) {
75 // ---------------------------------------------------------
76 indices.put(name, items.size());
78 item.myname = name; // Just for delegating isEnabled
82 // ==============================================================================================================================
84 // ==============================================================================================================================
86 public int getHeight () {
87 // -----------------------
91 public int getWidth () {
92 // ----------------------
96 public boolean isEmpty () {
97 // -------------------------
98 return (items.size() == 0);
101 public boolean isEnabled (String name) {
102 // --------------------------------------
103 return (items.get(indices.get(name)).getAction() != null);
106 public void resetContext (String name) {
107 // --------------------------------------
108 this.context.remove(name);
111 public void setContext (String name, Object context) {
112 // ----------------------------------------------------
113 this.context.put(name, context);