Source code: jac/aspects/gui/web/AbstractMenu.java
1 /*
2 Copyright (C) 2001 Laurent Martelli <laurent@aopsys.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 package jac.aspects.gui.web;
19
20 import jac.aspects.gui.*;
21 import jac.core.rtti.AbstractMethodItem;
22 import jac.util.Log;
23 import java.io.PrintWriter;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Vector;
27
28 public abstract class AbstractMenu extends AbstractView
29 implements MenuView, HTMLViewer, MenuListener
30 {
31
32 // key -> [ callback | Menu | null ]
33 HashMap map = new HashMap();
34 // item order
35 Vector keys = new Vector();
36
37 public AbstractMenu(ViewFactory factory, DisplayContext context) {
38 super(factory,context);
39 }
40
41 // MenuView interface
42
43 public void addSubMenu(String label, String icon, MenuView submenu) {
44 if (!map.containsKey(label)) {
45 keys.add(label);
46 map.put(label,submenu);
47 }
48 }
49
50 public void addAction(String label, String icon, Callback callback) {
51 if (!map.containsKey(label)) {
52 keys.add(label);
53 map.put(label,new MenuItem(label,icon,callback));
54 }
55 }
56
57 public void addSeparator() {
58 }
59
60 String position = jac.aspects.gui.Menu.LEFT;
61
62 /**
63 * Get the value of position.
64 * @return value of position.
65 */
66 public String getPosition() {
67 return position;
68 }
69
70 /**
71 * Set the value of position.
72 * @param v Value to assign to position.
73 */
74 public void setPosition(String v) {
75 this.position = v;
76 if (position==null)
77 position = jac.aspects.gui.Menu.LEFT;
78 }
79
80 // MenuListener interface
81
82 public void onMenuClick(String key) {
83 try {
84 Log.trace("gui.events","onMenuClick `"+key+"'");
85 MenuItem item = (MenuItem)map.get(key);
86 if (item!=null && item.callback!=null)
87 EventHandler.get().onInvoke(context,null,
88 item.callback.getMethod(),
89 item.callback.getParameters(),
90 null,null);
91 else
92 context.getDisplay().refresh();
93 } catch (Exception e) {
94 context.getDisplay().showError("Menu error","onMenuClick "+key+": "+
95 e.toString()+"<br><pre>"+map+"</pre>");
96 e.printStackTrace();
97 }
98 }
99 }