1 18 19 package org.objectweb.jac.aspects.gui.swing; 20 21 import java.awt.Component ; 22 import java.awt.Container ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.util.Arrays ; 26 import java.util.HashMap ; 27 import javax.swing.AbstractButton ; 28 import javax.swing.BoxLayout ; 29 import javax.swing.JComponent ; 30 import javax.swing.JMenuBar ; 31 import javax.swing.JMenuItem ; 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 implements MenuView, ActionListener { 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 label; 58 DisplayContext context; 59 ViewFactory factory; 60 61 Object [] parameters; 62 String type; 63 64 HashMap actions = new HashMap (); 66 67 68 String style; 70 71 public void setStyle(String style) { 72 this.style = style; 73 } 74 75 public String getStyle() { 76 return style; 77 } 78 79 Border viewBorder; 80 81 85 public Border getViewBorder() { 86 return viewBorder; 87 } 88 89 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 109 String description; 110 111 115 public String getDescription() { 116 return description; 117 } 118 119 123 public void setDescription(String v) { 124 this.description = v; 125 } 126 127 View parentView; 128 129 133 public View getParentView() { 134 return parentView; 135 } 136 137 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 166 public MethodItem getMessage() { 167 return message; 168 } 169 170 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 label) { 196 this.label = label; 197 } 198 199 public String 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 type) { 209 this.type = type; 210 } 211 212 public String getType() { 213 return type; 214 } 215 216 public void setParameters(Object [] parameters) { 217 this.parameters = parameters; 218 } 219 220 public Object [] 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 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 type, Object [] parameters) { 250 return this.type.equals(type) 251 && Arrays.equals(this.parameters,parameters); 252 } 253 254 256 public void addSubMenu(String label, String icon, MenuView submenu) { 257 ((AbstractButton )submenu).setText(label); 258 ((AbstractButton )submenu).setMnemonic(getMnemonic(this,label)); 259 add((JComponent )submenu); 260 } 261 262 public void addAction(String label, String icon, Callback callback) { 263 JMenuItem item = new JMenuItem (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 275 public static char getMnemonic(Component component, String label) { 276 if (component instanceof Container ) { 277 return getMnemonic((Container )component,label); 278 } else { 279 return label.length()>0 ? label.charAt(0) : (char)-1; 280 } 281 } 282 283 291 public static char getMnemonic(Container container, String label) { 292 logger.debug("getMnemonic for \""+label+"\" in "+Strings.hex(container)); 293 StringBuffer usedMnemonics = new StringBuffer (); 294 for (int i=0; i<container.getComponentCount(); i++) { 295 Component component = container.getComponent(i); 296 if (component instanceof AbstractButton ) { 297 AbstractButton button = (AbstractButton )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 position = Menu.TOP; 319 323 public String getPosition() { 324 return position; 325 } 326 327 332 public void setPosition(String 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 (this,BoxLayout.X_AXIS)); 339 } else { 340 setLayout(new BoxLayout (this,BoxLayout.Y_AXIS)); 342 } 343 } 344 345 public void addSeparator() { 346 } 347 348 public String toString() { 349 return Integer.toString(hashCode()); 350 } 351 352 354 public void actionPerformed(ActionEvent 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 |