KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
22 import java.awt.Container JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import javax.swing.AbstractButton JavaDoc;
28 import javax.swing.BoxLayout JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.JMenuBar JavaDoc;
31 import javax.swing.JMenuItem JavaDoc;
32 import org.apache.log4j.Logger;
33 import org.objectweb.jac.aspects.gui.Border;
34 import org.objectweb.jac.aspects.gui.Callback;
35 import org.objectweb.jac.aspects.gui.DisplayContext;
36 import org.objectweb.jac.aspects.gui.EventHandler;
37 import org.objectweb.jac.aspects.gui.GuiAC;
38 import org.objectweb.jac.aspects.gui.InvokeEvent;
39 import org.objectweb.jac.aspects.gui.Length;
40 import org.objectweb.jac.aspects.gui.Menu;
41 import org.objectweb.jac.aspects.gui.MenuView;
42 import org.objectweb.jac.aspects.gui.ResourceManager;
43 import org.objectweb.jac.aspects.gui.View;
44 import org.objectweb.jac.aspects.gui.ViewFactory;
45 import org.objectweb.jac.aspects.gui.ViewIdentity;
46 import org.objectweb.jac.aspects.session.SessionAC;
47 import org.objectweb.jac.core.Collaboration;
48 import org.objectweb.jac.core.rtti.FieldItem;
49 import org.objectweb.jac.core.rtti.MethodItem;
50 import org.objectweb.jac.util.Strings;
51
52 public class MenuBar extends JMenuBar JavaDoc implements MenuView, ActionListener JavaDoc {
53     static Logger logger = Logger.getLogger("gui.menu");
54     static Logger loggerContext = Logger.getLogger("display-context");
55     static Logger loggerEvents = Logger.getLogger("gui.events");
56
57     String JavaDoc label;
58     DisplayContext context;
59     ViewFactory factory;
60
61     Object JavaDoc[] parameters;
62     String JavaDoc type;
63
64     // label -> AbstractMethodItem
65
HashMap JavaDoc actions = new HashMap JavaDoc();
66
67
68     // style used to change display (css for web)
69
String JavaDoc style;
70
71     public void setStyle(String JavaDoc style) {
72         this.style = style;
73     }
74
75     public String JavaDoc getStyle() {
76         return style;
77     }
78
79     Border viewBorder;
80    
81     /**
82      * Get the value of viewBorder.
83      * @return value of viewBorder.
84      */

85     public Border getViewBorder() {
86         return viewBorder;
87     }
88    
89     /**
90      * Set the value of viewBorder.
91      * @param v Value to assign to viewBorder.
92      */

93     public void setViewBorder(Border v) {
94         this.viewBorder = v;
95     }
96    
97     public MenuBar(ViewFactory factory, DisplayContext context) {
98         logger.debug("new MenuBar");
99         this.factory = factory;
100         this.context = context;
101     }
102
103     public MenuBar() {
104         logger.debug("new MenuBar");
105     }
106
107     // View interface
108

109     String JavaDoc description;
110    
111     /**
112      * Get the value of description.
113      * @return value of description.
114      */

115     public String JavaDoc getDescription() {
116         return description;
117     }
118    
119     /**
120      * Set the value of description.
121      * @param v Value to assign to description.
122      */

123     public void setDescription(String JavaDoc v) {
124         this.description = v;
125     }
126    
127     View parentView;
128    
129     /**
130      * Get the value of parentView.
131      * @return value of parentView.
132      */

133     public View getParentView() {
134         return parentView;
135     }
136    
137     /**
138      * Set the value of parentView.
139      * @param v Value to assign to parentView.
140      */

141     public void setParentView(View v) {
142         this.parentView = v;
143     }
144
145     public View getRootView() {
146         if (parentView==null)
147             return this;
148         return parentView.getRootView();
149     }
150
151     public boolean isDescendantOf(View ancestor) {
152         if (this==ancestor)
153             return true;
154         else if (parentView==null)
155             return false;
156         else
157             return parentView.isDescendantOf(ancestor);
158     }
159
160     MethodItem message;
161    
162     /**
163      * Get the value of message.
164      * @return value of message.
165      */

166     public MethodItem getMessage() {
167         return message;
168     }
169    
170     /**
171      * Set the value of message.
172      * @param v Value to assign to message.
173      */

174     public void setMessage(MethodItem v) {
175         this.message = v;
176     }
177
178     public void setContext(DisplayContext context) {
179         loggerContext.debug("setContext on "+this);
180         this.context = context;
181     }
182
183     public DisplayContext getContext() {
184         return context;
185     }
186
187     public void setFactory(ViewFactory factory) {
188         this.factory = factory;
189     }
190
191     public ViewFactory getFactory() {
192         return factory;
193     }
194
195     public void setLabel(String JavaDoc label) {
196         this.label = label;
197     }
198
199     public String JavaDoc getLabel() {
200         return label;
201     }
202
203     public void setSize(Length width, Length height) {
204         if (width!=null || height!=null)
205             logger.warn("MenuBar does not support setSize");
206     }
207
208     public void setType(String JavaDoc type) {
209         this.type = type;
210     }
211
212     public String JavaDoc getType() {
213         return type;
214     }
215
216     public void setParameters(Object JavaDoc[] parameters) {
217         this.parameters = parameters;
218     }
219    
220     public Object JavaDoc[] getParameters() {
221         return parameters;
222     }
223
224
225     public void close(boolean validate) {
226         closed = true;
227     }
228
229     boolean closed = false;
230
231     public boolean isClosed() {
232         return closed;
233     }
234
235     public void setFocus(FieldItem field, Object JavaDoc option) {
236     }
237
238
239     public boolean equalsView(ViewIdentity view) {
240         return
241             ( ( type!=null &&
242                 type.equals(view.getType()) )
243               || (type==null && view.getType()==null ) )
244             && ( ( parameters!=null &&
245                    Arrays.equals(parameters,view.getParameters()) )
246                  || (parameters==null && view.getParameters()==null) );
247     }
248
249     public boolean equalsView(String JavaDoc type, Object JavaDoc[] parameters) {
250         return this.type.equals(type)
251             && Arrays.equals(this.parameters,parameters);
252     }
253
254     // MenuView interface
255

256     public void addSubMenu(String JavaDoc label, String JavaDoc icon, MenuView submenu) {
257         ((AbstractButton JavaDoc)submenu).setText(label);
258         ((AbstractButton JavaDoc)submenu).setMnemonic(getMnemonic(this,label));
259         add((JComponent JavaDoc)submenu);
260     }
261
262     public void addAction(String JavaDoc label, String JavaDoc icon, Callback callback) {
263         JMenuItem JavaDoc item = new JMenuItem JavaDoc(label,ResourceManager.getIcon(icon));
264         item.setActionCommand(label);
265         item.addActionListener(this);
266         item.setMnemonic(getMnemonic(this,label));
267         actions.put(label,callback);
268         add(item);
269     }
270
271     /*
272      * Workaround for SwingWT bug where JComponent does not extend
273      * Container.
274      */

275     public static char getMnemonic(Component JavaDoc component, String JavaDoc label) {
276         if (component instanceof Container JavaDoc) {
277             return getMnemonic((Container JavaDoc)component,label);
278         } else {
279             return label.length()>0 ? label.charAt(0) : (char)-1;
280         }
281     }
282
283     /**
284      * Returns a mnemonic (keyboard shortcut) not already used
285      *
286      * @param container check for used mnemonic in this container
287      * @param label the label to get a mnemonic for
288      * @return a mnemonic for the label, 0 if none could be
289      * computed. The mnemonic is a character contained in the label
290      */

291     public static char getMnemonic(Container JavaDoc container, String JavaDoc label) {
292         logger.debug("getMnemonic for \""+label+"\" in "+Strings.hex(container));
293         StringBuffer JavaDoc usedMnemonics = new StringBuffer JavaDoc();
294         for (int i=0; i<container.getComponentCount(); i++) {
295             Component JavaDoc component = container.getComponent(i);
296             if (component instanceof AbstractButton JavaDoc) {
297                 AbstractButton JavaDoc button = (AbstractButton JavaDoc)component;
298                 logger.debug(" Mnemonic of "+Strings.hex(component)+
299                              ": "+(char)button.getMnemonic());
300                 if (button.getMnemonic()!=0)
301                     usedMnemonics.append(Character.toLowerCase((char)button.getMnemonic()));
302             } else {
303                 logger.debug(" Skipping "+Strings.hex(component));
304             }
305         }
306         logger.debug(" usedMnemonics="+usedMnemonics);
307         for (int i=0; i<label.length(); i++) {
308             char c = Character.toLowerCase(label.charAt(i));
309             if (usedMnemonics.indexOf(""+c)==-1) {
310                 logger.debug(" -> "+c);
311                 return label.charAt(i);
312             }
313         }
314
315         return 0;
316     }
317    
318     String JavaDoc position = Menu.TOP;
319     /**
320      * Get the value of position.
321      * @return value of position.
322      */

323     public String JavaDoc getPosition() {
324         return position;
325     }
326
327     /**
328      * Set the value of position.
329      * @param v Value to assign to position. If null, use the default
330      * position.
331      */

332     public void setPosition(String JavaDoc v) {
333         logger.debug("setPosition("+v+")");
334         this.position = v;
335         if (position==null)
336             position = Menu.TOP;
337         if (position.equals(Menu.TOP)||position.equals(Menu.BOTTOM)) {
338             setLayout(new BoxLayout JavaDoc(this,BoxLayout.X_AXIS));
339         } else {
340             //getLayout(
341
setLayout(new BoxLayout JavaDoc(this,BoxLayout.Y_AXIS));
342         }
343     }
344
345     public void addSeparator() {
346     }
347
348     public String JavaDoc toString() {
349         return Integer.toString(hashCode());
350     }
351
352     // implementation of java.awt.event.ActionListener interface
353

354     public void actionPerformed(ActionEvent JavaDoc event)
355     {
356         loggerEvents.debug("MenuBar.actionPerformed("+event.getActionCommand()+")");
357         Collaboration.get().addAttribute(
358             SessionAC.SESSION_ID, GuiAC.getLocalSessionID());
359         Callback callback = (Callback)actions.get(event.getActionCommand());
360         EventHandler.get().onInvoke(
361             context,
362             new InvokeEvent(this,null,callback.getMethod(),callback.getParameters()),
363             true,
364             null,null);
365     }
366
367 }
368
369
Popular Tags