1 19 20 package org.objectweb.jac.aspects.gui.swing; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import javax.swing.ImageIcon ; 25 import javax.swing.JMenuItem ; 26 import javax.swing.JPopupMenu ; 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 { 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 substance, MethodItem method, String icon) { 46 MenuItem item = new MenuItem (substance,method,ResourceManager.getIcon(icon)); 47 item.addActionListener(item); 48 String 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 substance, String label, ImageIcon icon) { 65 MenuItem item = new MenuItem (substance,label,icon); 66 item.setActionCommand("view"); 67 item.addActionListener(item); 68 add(item); 69 } 70 71 class MenuItem extends JMenuItem implements ActionListener { 72 MethodItem method; 73 Object substance; 74 boolean toggle; 75 public MenuItem(Object substance, MethodItem method, ImageIcon 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 substance, String label, ImageIcon icon) { 85 super(label,icon); 86 this.method = null; 87 this.substance = substance; 88 } 89 public void actionPerformed(ActionEvent 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 [] {((Boolean )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 |