1 18 19 package org.objectweb.jac.aspects.gui.swing; 20 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.util.Arrays ; 24 import java.util.HashMap ; 25 import javax.swing.AbstractButton ; 26 import javax.swing.JComponent ; 27 import javax.swing.JMenu ; 28 import javax.swing.JMenuItem ; 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 implements MenuView, ActionListener { 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 label; 44 DisplayContext context; 45 ViewFactory factory; 46 47 Object [] parameters; 48 String type; 49 50 HashMap actions = new HashMap (); 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 69 public Border getViewBorder() { 70 return viewBorder; 71 } 72 73 77 public void setViewBorder(Border v) { 78 this.viewBorder = v; 79 } 80 81 String style; 83 84 public void setStyle(String style) { 85 this.style = style; 86 } 87 88 public String getStyle() { 89 return style; 90 } 91 92 93 95 public void addSubMenu(String label, String icon, MenuView submenu) { 96 logger.debug("addSubMenu("+label+","+icon+") on "+this); 97 if (icon==null) 98 icon = ResourceManager.getResource("blank_icon"); 99 AbstractButton button = (AbstractButton )submenu; 100 button.setText(label); 101 button.setMnemonic( 102 MenuBar.getMnemonic( 103 getPopupMenu(), 104 label)); 105 button.setIcon(ResourceManager.getIcon(icon)); 106 add((JComponent )submenu); 107 } 108 109 public void addAction(String label, String icon, Callback callback) { 110 logger.debug("addAction("+label+","+icon+","+callback+") on "+this); 111 JMenuItem item = new JMenuItem (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 position; 123 124 128 public String getPosition() { 129 return position; 130 } 131 132 136 public void setPosition(String v) { 137 this.position = v; 138 } 139 140 141 143 String description; 144 145 149 public String getDescription() { 150 return description; 151 } 152 153 157 public void setDescription(String v) { 158 this.description = v; 159 } 160 161 View parentView; 162 163 167 public View getParentView() { 168 return parentView; 169 } 170 171 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 200 public MethodItem getMessage() { 201 return message; 202 } 203 204 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 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 type) { 239 this.type = type; 240 } 241 242 public String 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 type, Object [] parameters) { 257 return this.type.equals(type) 258 && Arrays.equals(this.parameters,parameters); 259 } 260 261 public void setParameters(Object [] parameters) { 262 this.parameters = parameters; 263 } 264 265 public Object [] 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 option) { 281 } 282 283 285 public void actionPerformed(ActionEvent 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 toString() { 295 return Strings.hex(this); 296 } 297 298 } 299 300 | Popular Tags |