KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > Menu


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>,
3                           Laurent Martelli <laurent@aopsys.com>
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

18
19 package org.objectweb.jac.aspects.gui.swing;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import javax.swing.AbstractButton JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.JMenu JavaDoc;
28 import javax.swing.JMenuItem JavaDoc;
29 import org.apache.log4j.Logger;
30 import org.objectweb.jac.aspects.gui.*;
31 import org.objectweb.jac.aspects.gui.InvokeEvent;
32 import org.objectweb.jac.aspects.session.SessionAC;
33 import org.objectweb.jac.core.Collaboration;
34 import org.objectweb.jac.core.rtti.FieldItem;
35 import org.objectweb.jac.core.rtti.MethodItem;
36 import org.objectweb.jac.util.Strings;
37
38 public class Menu extends JMenu JavaDoc implements MenuView, ActionListener JavaDoc {
39     static Logger logger = Logger.getLogger("gui.menu");
40     static Logger loggerContext = Logger.getLogger("display-context");
41     static Logger loggerEvents = Logger.getLogger("gui.events");
42    
43     String JavaDoc label;
44     DisplayContext context;
45     ViewFactory factory;
46
47     Object JavaDoc[] parameters;
48     String JavaDoc type;
49
50     // label -> AbstractMethodItem
51
HashMap JavaDoc actions = new HashMap JavaDoc();
52
53     public Menu(ViewFactory factory, DisplayContext context) {
54         logger.debug("new Menu");
55         this.factory = factory;
56         this.context = context;
57     }
58
59     public Menu() {
60         logger.debug("new Menu");
61     }
62
63     Border viewBorder;
64    
65     /**
66      * Get the value of viewBorder.
67      * @return value of viewBorder.
68      */

69     public Border getViewBorder() {
70         return viewBorder;
71     }
72    
73     /**
74      * Set the value of viewBorder.
75      * @param v Value to assign to viewBorder.
76      */

77     public void setViewBorder(Border v) {
78         this.viewBorder = v;
79     }
80    
81     // style used to change display (css for web)
82
String JavaDoc style;
83
84     public void setStyle(String JavaDoc style) {
85         this.style = style;
86     }
87
88     public String JavaDoc getStyle() {
89         return style;
90     }
91
92
93     // MenuView interface
94

95     public void addSubMenu(String JavaDoc label, String JavaDoc icon, MenuView submenu) {
96         logger.debug("addSubMenu("+label+","+icon+") on "+this);
97         if (icon==null)
98             icon = ResourceManager.getResource("blank_icon");
99         AbstractButton JavaDoc button = (AbstractButton JavaDoc)submenu;
100         button.setText(label);
101         button.setMnemonic(
102             MenuBar.getMnemonic(
103                 getPopupMenu(),
104                 label));
105         button.setIcon(ResourceManager.getIcon(icon));
106         add((JComponent JavaDoc)submenu);
107     }
108
109     public void addAction(String JavaDoc label, String JavaDoc icon, Callback callback) {
110         logger.debug("addAction("+label+","+icon+","+callback+") on "+this);
111         JMenuItem JavaDoc item = new JMenuItem JavaDoc(label,ResourceManager.getIcon(icon));
112         item.setActionCommand(label);
113         item.addActionListener(this);
114         item.setMnemonic(
115             MenuBar.getMnemonic(
116                 getPopupMenu(),
117                 GuiAC.getMnemonics(callback.getMethod())+label));
118         actions.put(label,callback);
119         add(item);
120     }
121
122     String JavaDoc position;
123    
124     /**
125      * Get the value of position.
126      * @return value of position.
127      */

128     public String JavaDoc getPosition() {
129         return position;
130     }
131    
132     /**
133      * Set the value of position.
134      * @param v Value to assign to position.
135      */

136     public void setPosition(String JavaDoc v) {
137         this.position = v;
138     }
139    
140
141     // View interface
142

143     String JavaDoc description;
144    
145     /**
146      * Get the value of description.
147      * @return value of description.
148      */

149     public String JavaDoc getDescription() {
150         return description;
151     }
152    
153     /**
154      * Set the value of description.
155      * @param v Value to assign to description.
156      */

157     public void setDescription(String JavaDoc v) {
158         this.description = v;
159     }
160    
161     View parentView;
162    
163     /**
164      * Get the value of parentView.
165      * @return value of parentView.
166      */

167     public View getParentView() {
168         return parentView;
169     }
170    
171     /**
172      * Set the value of parentView.
173      * @param v Value to assign to parentView.
174      */

175     public void setParentView(View v) {
176         this.parentView = v;
177     }
178
179     public View getRootView() {
180         if (parentView==null)
181             return this;
182         return parentView.getRootView();
183     }
184
185     public boolean isDescendantOf(View ancestor) {
186         if (this==ancestor)
187             return true;
188         else if (parentView==null)
189             return false;
190         else
191             return parentView.isDescendantOf(ancestor);
192     }
193
194     MethodItem message;
195
196     /**
197      * Get the value of message.
198      * @return value of message.
199      */

200     public MethodItem getMessage() {
201         return message;
202     }
203    
204     /**
205      * Set the value of message.
206      * @param v Value to assign to message.
207      */

208     public void setMessage(MethodItem v) {
209         this.message = v;
210     }
211
212     public void setContext(DisplayContext context) {
213         loggerContext.debug("setContext on "+this);
214         this.context = context;
215     }
216
217     public DisplayContext getContext() {
218         return context;
219     }
220
221     public void setFactory(ViewFactory factory) {
222         this.factory = factory;
223     }
224
225     public ViewFactory getFactory() {
226         return factory;
227     }
228
229     public void setLabel(String JavaDoc label) {
230         this.label = label;
231     }
232
233     public void setSize(Length width, Length height) {
234         if (width!=null || height!=null)
235             logger.warn("MenuBar does not support setSize");
236     }
237
238     public void setType(String JavaDoc type) {
239         this.type = type;
240     }
241
242     public String JavaDoc getType() {
243         return type;
244     }
245
246     public boolean equalsView(ViewIdentity view) {
247         return
248             ( ( type!=null &&
249                 type.equals(view.getType()) )
250               || (type==null && view.getType()==null ) )
251             && ( ( parameters!=null &&
252                    Arrays.equals(parameters,view.getParameters()) )
253                  || (parameters==null && view.getParameters()==null) );
254     }
255
256     public boolean equalsView(String JavaDoc type, Object JavaDoc[] parameters) {
257         return this.type.equals(type)
258             && Arrays.equals(this.parameters,parameters);
259     }
260
261     public void setParameters(Object JavaDoc[] parameters) {
262         this.parameters = parameters;
263     }
264    
265     public Object JavaDoc[] getParameters() {
266         return parameters;
267     }
268
269
270     public void close(boolean validate) {
271         closed = true;
272     }
273
274     boolean closed = false;
275
276     public boolean isClosed() {
277         return closed;
278     }
279
280     public void setFocus(FieldItem field, Object JavaDoc option) {
281     }
282
283     // implementation of java.awt.event.ActionListener interface
284

285     public void actionPerformed(ActionEvent JavaDoc event)
286     {
287         loggerEvents.debug("Menu.actionPerformed("+event.getActionCommand()+")");
288         Collaboration.get().addAttribute(
289             SessionAC.SESSION_ID, GuiAC.getLocalSessionID());
290         Callback callback = (Callback)actions.get(event.getActionCommand());
291         callback.invoke(context,this);
292     }
293
294     public String JavaDoc toString() {
295         return Strings.hex(this);
296     }
297
298 }
299
300
Popular Tags