KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2002-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, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18   USA */

19
20 package org.objectweb.jac.aspects.gui.swing;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import javax.swing.ImageIcon JavaDoc;
25 import javax.swing.JMenuItem JavaDoc;
26 import javax.swing.JPopupMenu JavaDoc;
27 import org.apache.log4j.Logger;
28 import org.objectweb.jac.aspects.gui.DisplayContext;
29 import org.objectweb.jac.aspects.gui.EventHandler;
30 import org.objectweb.jac.aspects.gui.GuiAC;
31 import org.objectweb.jac.aspects.gui.InvokeEvent;
32 import org.objectweb.jac.aspects.gui.ResourceManager;
33 import org.objectweb.jac.core.rtti.MethodItem;
34 import org.objectweb.jac.core.rtti.NamingConventions;
35
36 public class ObjectPopup extends JPopupMenu JavaDoc {
37     static Logger loggerEvents = Logger.getLogger("gui.events");
38
39     DisplayContext context;
40
41     public ObjectPopup(DisplayContext context) {
42         this.context = context;
43     }
44
45     public void addMethodItem(Object JavaDoc substance, MethodItem method, String JavaDoc icon) {
46         MenuItem JavaDoc item = new MenuItem JavaDoc(substance,method,ResourceManager.getIcon(icon));
47         item.addActionListener(item);
48         String JavaDoc label = item.getText();
49         if (method.isSetter())
50             label = method.getSetField().getName();
51         else if (method.isAdder())
52             label = method.getAddedCollection().getName();
53         item.setMnemonic(
54             MenuBar.getMnemonic(
55                 this,
56                 GuiAC.getMnemonics(method)+label));
57         add(item);
58         boolean enabled = GuiAC.isEnabled(method,substance);
59         if (!enabled) {
60             item.setEnabled(enabled);
61         }
62     }
63
64     public void addViewItem(Object JavaDoc substance, String JavaDoc label, ImageIcon JavaDoc icon) {
65         MenuItem JavaDoc item = new MenuItem JavaDoc(substance,label,icon);
66         item.setActionCommand("view");
67         item.addActionListener(item);
68         add(item);
69     }
70
71     class MenuItem extends JMenuItem JavaDoc implements ActionListener JavaDoc {
72         MethodItem method;
73         Object JavaDoc substance;
74         boolean toggle;
75         public MenuItem(Object JavaDoc substance, MethodItem method, ImageIcon JavaDoc icon) {
76             super(method.isSetter() && method.getSetField().getType()==boolean.class
77                   ? "Toggle "+NamingConventions.textForName(method.getSetField().getName())
78                   : NamingConventions.textForName(method.getName()),
79                   icon);
80             this.toggle = method.isSetter() && method.getSetField().getType()==boolean.class;
81             this.method = method;
82             this.substance = substance;
83         }
84         public MenuItem(Object JavaDoc substance, String JavaDoc label, ImageIcon JavaDoc icon) {
85             super(label,icon);
86             this.method = null;
87             this.substance = substance;
88         }
89         public void actionPerformed(ActionEvent JavaDoc event) {
90             loggerEvents.debug("ObjectPopup.actionPerformed from "+
91                                event.getSource().getClass().getName());
92             if ("view".equals(event.getActionCommand())) {
93                 EventHandler.get().onView(context,null,substance,null,null);
94             } else if (toggle) {
95                 if (method!=null)
96                     EventHandler.get().onInvoke(
97                         context,
98                         new InvokeEvent(
99                             null,
100                             substance,
101                             method,
102                             new Object JavaDoc[] {((Boolean JavaDoc)method.getSetField().getThroughAccessor(substance)).booleanValue()
103                                           ? Boolean.FALSE : Boolean.TRUE }),
104                         false,
105                         null, null);
106             } else {
107                 if (method!=null)
108                     EventHandler.get().onInvoke(
109                         context,
110                         new InvokeEvent(null, substance, method));
111             }
112         }
113     }
114 }
115
Popular Tags