KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > bridges > menu > MenuBridge


1 package rero.bridges.menu;
2
3 import java.awt.*;
4 import java.awt.event.*;
5
6 import javax.swing.*;
7 import javax.swing.event.*;
8
9 import java.util.*;
10
11 import sleep.engine.*;
12 import sleep.interfaces.*;
13 import sleep.runtime.*;
14
15 import rero.config.*;
16
17 public class MenuBridge implements Environment, Function, Loadable
18 {
19     protected static String JavaDoc WINDOW_MENU = "&Window";
20     protected static String JavaDoc HELP_MENU = "&Help";
21
22     //
23
// Static code for keeping track of menu parents (truth is only one popup menu will be showing at once)
24
//
25
protected static Stack ParentMenu = new Stack();
26
27     public static void SetParent(MenuBridgeParent m)
28     {
29         ParentMenu.push(m);
30     }
31
32     public static MenuBridgeParent GetParent()
33     {
34         return (MenuBridgeParent)ParentMenu.peek();
35     }
36
37     public static void FinishParent()
38     {
39         ParentMenu.pop();
40     }
41
42     protected LinkedHashMap menubarMenus;
43     protected HashMap menus;
44
45     public MenuBridge()
46     {
47         menubarMenus = new LinkedHashMap(10, .75f, false); // initial capacity of 10 items, load factor of .75, and most
48
// importantly access order (last param) of false meaning internal
49
// order is based on when element was inserted not on access.
50

51         menus = new HashMap();
52     }
53
54     /** returns true if the specified menu name is one of the top level menus we use in the client */
55     protected static boolean isTopLevel(String JavaDoc name)
56     {
57         name = name.toLowerCase();
58
59         if (name.length() > 2 && name.substring(0, 2).equals("__"))
60               return true;
61
62         return (name.equals("list") || name.equals("dcc") || name.equals("switchbar") || name.equals("nicklist") || name.equals("status") || name.equals("channel") || name.equals("query") || name.equals("input") || name.equals("chat") || name.equals("tab") || name.equals("background"));
63     }
64
65     protected static boolean isSpecialMenu(String JavaDoc name)
66     {
67         return (name.equals(WINDOW_MENU) || name.equals(HELP_MENU));
68     }
69
70     public void bindFunction(ScriptInstance si, String JavaDoc type, String JavaDoc description, Block code)
71     {
72        //
73
// Setup "menubar" menus (i.e. the menus at the top of the application)
74
//
75
if (type.equals("menubar"))
76        {
77           if (menubarMenus.containsKey(description))
78           {
79              ((ScriptedMenu)menubarMenus.get(description)).installCode(si, code);
80           }
81           else if (isSpecialMenu(description))
82           {
83              if (menus.containsKey(description))
84              {
85                 ((ScriptedMenu)menus.get(description)).installCode(si, code);
86              }
87              else
88              {
89                 menus.put(description, new ScriptedMenu(si, description, code));
90              }
91           }
92           else
93           {
94              menubarMenus.put(description, new ScriptedMenu(si, description, code));
95           }
96        }
97
98        if (type.equals("menu"))
99        {
100           //
101
// A top level menu is what we are in business for, consists of stuff like nicklist, status, channel, query etc.
102
//
103
if (isTopLevel(description.toUpperCase()))
104           {
105              if (menus.containsKey(description.toUpperCase()))
106              {
107                 ((ScriptedPopupMenu)menus.get(description.toUpperCase())).installCode(si, code);
108              }
109              else
110              {
111                 menus.put(description.toUpperCase(), new ScriptedPopupMenu(si, code));
112              }
113           }
114           else
115           {
116              GetParent().add(new ScriptedMenu(si, description, code));
117           }
118        }
119        
120        if (type.equals("item"))
121        {
122           GetParent().add(new ScriptedItem(si, description, code));
123        }
124     }
125
126     public Scalar evaluate(String JavaDoc function, ScriptInstance script, Stack locals)
127     {
128        if (function.equals("&addSeparator"))
129        {
130           GetParent().addSeparator();
131        }
132        else if (function.equals("&addItem") && locals.size() == 2)
133        {
134           String JavaDoc name = ((Scalar)locals.pop()).stringValue();
135           String JavaDoc func = ((Scalar)locals.pop()).stringValue();
136
137           GetParent().add(new SimpleItem(script, name, func));
138        }
139        else if (function.equals("&removeMenubarItem") && locals.size() == 1)
140        {
141           String JavaDoc name = ((Scalar)locals.pop()).stringValue();
142           menus.remove(name);
143        }
144
145        return null;
146     }
147
148     public boolean scriptLoaded(ScriptInstance si)
149     {
150        Hashtable env = si.getScriptEnvironment().getEnvironment();
151        env.put("item", this);
152        env.put("menu", this);
153        env.put("menubar", this);
154
155        env.put("&addItem", this);
156        env.put("&addSeparator", this);
157        env.put("&removeMenubarItem", this);
158
159        return true;
160     }
161  
162     public JPopupMenu getPopupMenu(String JavaDoc description, HashMap data)
163     {
164        if (!menus.containsKey(description.toUpperCase()))
165        {
166           return null;
167        }
168
169        ScriptedPopupMenu.SetMenuData(data);
170
171        JPopupMenu temp = (JPopupMenu)menus.get(description.toUpperCase());
172        return temp;
173     }
174
175     public JPopupMenu getPrimaryPopup(String JavaDoc description)
176     {
177        if (!menubarMenus.containsKey(description))
178        {
179           return null;
180        }
181
182        ScriptedPopupMenu.SetMenuData(new HashMap());
183
184        ScriptedMenu temp = (ScriptedMenu)menubarMenus.get(description);
185
186        return temp.getScriptedPopupMenu();
187     }
188
189     public void installMenubar(JMenuBar bar)
190     {
191        bar.removeAll();
192  
193        Iterator i = menubarMenus.values().iterator();
194        while (i.hasNext())
195        {
196           ScriptedMenu temp = (ScriptedMenu)i.next();
197           if (temp.isValidCode())
198           {
199              bar.add(temp);
200           }
201           else
202           {
203              i.remove();
204           }
205        }
206
207        if (menus.containsKey(WINDOW_MENU))
208        {
209           bar.add((JMenu)menus.get(WINDOW_MENU));
210        }
211
212        if (menus.containsKey(HELP_MENU))
213        {
214           bar.add((JMenu)menus.get(HELP_MENU));
215        }
216     }
217
218     public boolean scriptUnloaded(ScriptInstance si)
219     {
220        return true;
221     }
222 }
223
Popular Tags