1 17 18 package org.objectweb.jac.aspects.gui.swing; 19 20 import org.objectweb.jac.aspects.gui.Callback; 21 import org.objectweb.jac.aspects.gui.GuiAC; 22 import org.objectweb.jac.aspects.gui.MenuView; 23 import org.objectweb.jac.aspects.gui.MethodUpdate; 24 import org.objectweb.jac.aspects.gui.ResourceManager; 25 import org.objectweb.jac.aspects.gui.Utils; 26 import org.objectweb.jac.aspects.session.SessionAC; 27 import org.objectweb.jac.core.Collaboration; 28 import org.objectweb.jac.core.rtti.MethodItem; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.util.Hashtable ; 32 import java.util.Iterator ; 33 import javax.swing.JButton ; 34 import javax.swing.JToolBar ; 35 36 public class ToolBar extends AbstractCompositeView 37 implements MenuView, ActionListener , MethodUpdate 38 { 39 40 JToolBar toolbar; 41 42 Hashtable actions = new Hashtable (); 44 45 Hashtable buttons = new Hashtable (); 47 48 public ToolBar() { 49 toolbar = new JToolBar (); 50 add(toolbar); 51 } 52 53 55 public void addSubMenu(String label, String icon, MenuView submenu) { 56 } 58 59 public void addAction(String label, String icon, 60 Callback callback) { 61 JButton button = icon!=null? 62 new JButton (ResourceManager.getIcon(icon)) : 63 new JButton (label); 64 String actionCommand = callback.toString(); 65 button.setActionCommand(actionCommand); 66 button.addActionListener(this); 67 button.setMnemonic( 68 MenuBar.getMnemonic( 69 toolbar, 70 GuiAC.getMnemonics(callback.getMethod())+label)); 71 button.setToolTipText(GuiAC.getLabel(callback.getMethod())); 72 actions.put(actionCommand,callback); 73 buttons.put(actionCommand,button); 74 updateEnabled(button,callback); 75 toolbar.add(button); 76 MethodItem condition = (MethodItem)callback.getMethod().getAttribute(GuiAC.CONDITION); 77 if (condition!=null) 78 Utils.registerMethod(callback.getObject(),condition,this,actionCommand); 79 } 80 81 82 public void addSeparator() { 83 toolbar.addSeparator(); 84 } 85 86 String position; 87 88 92 public String getPosition() { 93 return position; 94 } 95 96 100 public void setPosition(String v) { 101 this.position = v; 102 } 103 104 public void close(boolean validate) { 105 super.close(validate); 106 Iterator it = actions.keySet().iterator(); 108 while (it.hasNext()) { 109 Callback callback = (Callback)it.next(); 110 MethodItem condition = (MethodItem)callback.getMethod().getAttribute(GuiAC.CONDITION); 111 if (condition!=null) 112 Utils.unregisterMethod(null,condition,this); 113 } 114 } 115 116 public String toString() { 117 return getClass().getName()+"@"+Integer.toString(hashCode()); 118 } 119 120 122 public void actionPerformed(ActionEvent event) { 123 Collaboration.get().addAttribute( 124 SessionAC.SESSION_ID, GuiAC.getLocalSessionID()); 125 Callback callback = (Callback)actions.get(event.getActionCommand()); 126 callback.invoke(context,this); 127 } 128 129 protected void updateEnabled(JButton button, Callback callback) { 130 button.setEnabled(GuiAC.isEnabled(callback.getMethod(), callback.getObject())); 131 } 132 133 public void methodUpdated(Object substance, MethodItem method, Object param) { 135 updateEnabled((JButton )buttons.get((String )param), 136 (Callback)actions.get((String )param)); 137 } 138 } 139 | Popular Tags |