Salome HOME
SIMAN Eclipse workspace first version
[tools/siman.git] / Workspace / Siman / src / org / splat / wapp / ContextualMenu.java
1 package org.splat.wapp;
2 /**
3  * Abstract class implementing the base services of contextual menu such as popup-menus and tool-bars.
4  * 
5  * @see Menu
6  * @author Daniel Brunier-Coulin
7  */
8
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Vector;
12
13
14 public abstract class ContextualMenu {
15         
16     protected int                     width;
17     protected int                     height;
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
21
22     private          enum  Side { Client, Server };          // Side of action execution
23     protected static class ContextualItem {
24 //  -------------------------------------
25           private   String          myname;
26       private   ContextualMenu  owner;
27           private   Side            execute;
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
34
35       public String getAction () {
36         return myurl;                                        // Null if not enabled
37       }
38       public int getHeight () {
39         return height;
40       }
41       public String getIcon () {
42             return myicon;
43       }
44       public int getWidth () {
45         return width;
46       }
47       public boolean isSeparator () {
48         return (mylabel == null && myicon == null);
49       }
50       public void setAction (String url) {                   // Enables the item
51         myurl = url;
52         if (url.endsWith(")")) execute = Side.Client;        // JavaScript function
53         else                   execute = Side.Server;        // URL
54       }
55       public Boolean isClientSide () { 
56         if (myurl != null) return (execute == Side.Client);
57         return null;
58       }
59       public boolean isEnabled () {
60         return owner.isEnabled(myname);                      // Delegation to overridden function
61       }
62     }
63
64 //  ==============================================================================================================================
65 //  Construction
66 //  ==============================================================================================================================
67
68     protected ContextualMenu () {
69 //  ---------------------------
70       context = new HashMap<String,Object>();
71       items   = new Vector<ContextualItem>();
72       indices = new HashMap<String,Integer>();
73     }
74     protected void addItem (String name, ContextualItem item) {
75 //  ---------------------------------------------------------
76       indices.put(name, items.size());
77       items.add(item);
78       item.myname = name;                                    // Just for delegating isEnabled
79       item.owner  = this;
80     }
81
82 //  ==============================================================================================================================
83 //  Member functions
84 //  ==============================================================================================================================
85
86     public int getHeight () {
87 //  -----------------------
88       return height;
89     }
90
91     public int getWidth () {
92 //  ----------------------
93       return width;
94     }
95
96     public boolean isEmpty () {
97 //  -------------------------
98       return (items.size() == 0);
99     }
100
101     public boolean isEnabled (String name) {
102 //  --------------------------------------
103       return (items.get(indices.get(name)).getAction() != null);
104     }
105
106     public void resetContext (String name) {
107 //  --------------------------------------
108       this.context.remove(name);
109     }
110
111     public void setContext (String name, Object context) {
112 //  ----------------------------------------------------
113       this.context.put(name, context);
114     }
115 }