1 18 19 package org.objectweb.jac.aspects.gui.swing; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Insets ; 23 import java.awt.Point ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.awt.event.KeyEvent ; 27 import java.awt.event.KeyListener ; 28 import java.awt.event.MouseEvent ; 29 import java.awt.event.MouseListener ; 30 import java.util.List ; 31 import javax.swing.DefaultListSelectionModel ; 32 import javax.swing.JButton ; 33 import javax.swing.JComponent ; 34 import javax.swing.JPanel ; 35 import javax.swing.JScrollPane ; 36 import javax.swing.ListSelectionModel ; 37 import javax.swing.event.ListSelectionEvent ; 38 import javax.swing.event.ListSelectionListener ; 39 import org.objectweb.jac.aspects.gui.*; 40 import org.objectweb.jac.aspects.session.SessionAC; 41 import org.objectweb.jac.core.Collaboration; 42 import org.objectweb.jac.core.Wrappee; 43 import org.objectweb.jac.core.rtti.ClassRepository; 44 import org.objectweb.jac.core.rtti.CollectionItem; 45 import org.objectweb.jac.core.rtti.FieldItem; 46 import org.objectweb.jac.core.rtti.MethodItem; 47 import org.objectweb.jac.core.rtti.NamingConventions; 48 import org.objectweb.jac.util.ExtArrays; 49 50 53 public abstract class AbstractCollection extends AbstractView 54 implements ListSelectionListener , ActionListener , 55 MouseListener , CollectionView, KeyListener 56 { 57 CollectionItem collection; 58 Object substance; 59 CollectionModel model; 60 protected org.objectweb.jac.aspects.gui.CollectionItemView itemView; 61 JComponent component; 62 63 JButton viewButton; 64 JButton removeButton; 65 JButton moveupButton; 66 JButton movedownButton; 67 68 public CollectionModel getCollectionModel() { 69 return model; 70 } 71 72 public AbstractCollection(ViewFactory factory, DisplayContext context, 73 CollectionItem collection, Object substance, 74 CollectionModel model, 75 org.objectweb.jac.aspects.gui.CollectionItemView itemView) { 76 super(factory,context); 77 this.collection = collection; 78 this.substance = substance; 79 this.model = model; 80 this.itemView = itemView; 81 82 setLayout(new BorderLayout ()); 83 84 component = getInnerComponent(model); 85 component.addKeyListener(this); 86 JScrollPane scrollPane = new JScrollPane (component); 87 component.addMouseListener(this); 88 add(scrollPane, BorderLayout.CENTER); 89 90 JPanel lowerCont = new JPanel (); 92 JButton b = null; 93 94 CustomizedGUI customized = null; 95 List targets = null; 96 if (context.getCustomizedView()!=null) { 97 customized = context.getCustomizedView().getCustomizedGUI(); 98 targets = customized.getFieldTargets(collection); 99 } 100 101 if (targets==null || targets.size()==0) { 102 viewButton = createButton("view_icon","View","open"); 103 viewButton.setEnabled(false); 104 lowerCont.add(viewButton); 105 } 106 107 if (GuiAC.isAddable(substance,collection)) { 108 b = createButton("new_icon",GuiAC.getLabelAdd(),"add"); 109 lowerCont.add(b); 110 } 111 112 if (GuiAC.isRemovable(substance,collection)) { 113 removeButton = createButton("remove_icon","Remove","remove"); 114 removeButton.setEnabled(false); 115 lowerCont.add(removeButton); 116 } 117 118 if (collection.isList() && !collection.isCalculated()) { 119 moveupButton = createButton("up_icon","Move up","moveup"); 120 moveupButton.setEnabled(false); 121 lowerCont.add(moveupButton); 122 123 movedownButton = createButton("down_icon","Move up","movedown"); 124 movedownButton.setEnabled(false); 125 lowerCont.add(movedownButton); 126 } 127 128 MethodItem[] directMeths = (MethodItem[]) 129 collection.getAttribute(GuiAC.DIRECT_COLLECTION_METHODS); 130 if (directMeths != null) { 131 for (int i=0; i<directMeths.length; i++) { 132 lowerCont.add(new DirectButton(directMeths[i])); 134 } 135 } 136 137 add(lowerCont,BorderLayout.SOUTH); 138 139 } 141 142 protected JButton createButton(String icon, String tooltip, String action) { 143 JButton button = new JButton (ResourceManager.getIconResource(icon)); 144 button.setToolTipText(tooltip); 145 button.setActionCommand(action); 146 button.addActionListener(this); 147 button.setMargin(new Insets (1,5,1,5)); 148 return button; 149 } 150 151 protected abstract JComponent getInnerComponent(Model model); 152 153 boolean isEditor; 154 public boolean isEditor() { 155 return isEditor; 156 } 157 public void setEditor(boolean isEditor) { 158 this.isEditor = isEditor; 159 } 160 161 public void setAutoUpdate(boolean autoUpdate) { 162 } 164 165 public void close(boolean validate) { 166 closed = true; 167 model.close(); 168 } 169 170 173 174 public void valueChanged(ListSelectionEvent event) { 175 loggerEvents.debug("valueChanged "+event); 176 177 ListSelectionModel lsm = (ListSelectionModel )event.getSource(); 178 if (lsm.isSelectionEmpty()) { 179 if (viewButton!=null) 180 viewButton.setEnabled(false); 181 if (removeButton!=null) 182 removeButton.setEnabled(false); 183 if (moveupButton!=null) 184 moveupButton.setEnabled(false); 185 if (movedownButton!=null) 186 movedownButton.setEnabled(false); 187 return; 188 } else if (event.getValueIsAdjusting()) { 189 return; 190 } else { 191 if (removeButton!=null) 192 removeButton.setEnabled(true); 193 if (viewButton!=null) 194 viewButton.setEnabled(selectionContainsAWrappee()); 195 int indice = getSelectedIndices()[0]; 196 CollectionPosition collpos = new CollectionPosition( 197 this, 198 collection, 199 indice, 200 substance); 201 EventHandler.get().onSelection(context, 202 collection, 203 getSelected()[0], 204 null, 205 collpos); 206 208 if (moveupButton!=null) 209 moveupButton.setEnabled(indice>0); 210 if (movedownButton!=null) 211 movedownButton.setEnabled(indice<model.getRowCount()-1); 212 } 213 } 214 215 218 boolean selectionContainsAWrappee() { 219 Object [] selection = getSelected(); 220 for (int i=0; i<selection.length; i++) { 221 if (selection[i] instanceof Wrappee) { 222 return true; 223 } 224 } 225 return false; 226 } 227 228 230 public void keyTyped(KeyEvent event) {} 231 232 public void keyPressed(KeyEvent event) {} 233 234 public void keyReleased(KeyEvent event) { 235 if (event.getKeyCode()==KeyEvent.VK_DELETE && 236 removeButton!=null 237 ) { 239 doDelete(); 240 } 241 } 242 243 void doDelete() { 244 Object [] selected = getSelected(); 245 MethodItem removeMethod = collection.getRemover(); 246 loggerEvents.debug("remover="+removeMethod); 247 if (removeMethod!=null) { 248 for (int i=0; i<selected.length; i++) { 250 EventHandler.get().onRemoveFromCollection( 251 context, 252 new RemoveEvent( 253 this, 254 substance, 255 collection, 256 selected[i]), 257 false); 258 } 259 } else { 260 287 } 288 onRemove(); 289 } 290 291 301 public void actionPerformed(ActionEvent event) { 302 setContext(); 303 loggerEvents.debug("action performed on collection : "+event.getActionCommand()+ 304 "; modifiers = "+event.getModifiers()); 305 if (event.getActionCommand().equals("open")) { 306 Object [] selected = getSelected(); 307 for (int i=0; i<selected.length; i++) { 308 try { 309 EventHandler.get().onSelection( 310 context,collection,selected[i],null,null,true); 311 } catch (Exception e) { 312 loggerEvents.error("failed to view "+selected[i],e); 313 } 314 } 315 } else if (event.getActionCommand().equals("add")) { 316 System.out.println(event. getModifiers()+"&"+ActionEvent.CTRL_MASK+"="+ 317 (event. getModifiers()& ActionEvent.CTRL_MASK)); 318 EventHandler.get().onAddToCollection( 319 context, 320 new AddEvent(this,substance,collection,null), 321 (event. getModifiers()& ActionEvent.CTRL_MASK) != 0); 322 } else if (event.getActionCommand().equals("remove")) { 323 doDelete(); 324 } else if (event.getActionCommand().equals("movedown")) { 325 Object selected = getSelected()[0]; 326 int index = getSelectedIndices()[0]; 327 if (index<model.getRowCount()-1) { 328 Integer i = new Integer (index); 329 collection.remove(substance,selected,i); 330 CollectionUpdate update = getCollectionUpdate(); 331 update.onRemove(substance, collection, null, i , null); 332 i = new Integer (index+1); 333 collection.add(substance,selected,i); 334 update.onAdd(substance, collection, null, i, null); 335 setSelected(index+1); 336 } 337 } else if (event.getActionCommand().equals("moveup")) { 338 Object selected = getSelected()[0]; 339 int index = getSelectedIndices()[0]; 340 if (index>0) { 341 Integer i = new Integer (index); 342 collection.remove(substance,selected,i); 343 CollectionUpdate update = getCollectionUpdate(); 344 update.onRemove(substance, collection, null, i, null); 345 i = new Integer (index-1); 346 collection.add(substance,selected,i); 347 update.onAdd(substance, collection, null, i, null); 348 setSelected(index-1); 349 } 350 } 351 } 352 353 class DirectButton extends JButton 355 implements ListSelectionListener , ActionListener 356 { 357 MethodItem method; 358 DirectButton(MethodItem method) { 359 super(GuiAC.getLabel(method)); 360 this.method = method; 361 setIcon(ResourceManager.getIcon(GuiAC.getIcon(method))); 362 addActionListener(this); 363 setEnabled(false); 364 getSelectionModel().addListSelectionListener(this); 365 } 366 public void valueChanged(ListSelectionEvent event) { 367 ListSelectionModel lsm = (ListSelectionModel )event.getSource(); 368 setEnabled(!lsm.isSelectionEmpty()); 369 } 370 public void actionPerformed(ActionEvent event) { 371 Collaboration.get().addAttribute( 372 SessionAC.SESSION_ID, GuiAC.getLocalSessionID()); 373 374 Object [] selected = getSelected(); 375 for (int i=0; i<selected.length; i++) { 376 try { 377 EventHandler.get().onInvoke( 378 context, 379 new InvokeEvent(AbstractCollection.this,selected[i],method)).join(); 380 } catch(InterruptedException e) { 381 context.getDisplay().showModal( 382 e, 383 "Error: "+e, 384 "An error occured during the invocation of "+ 385 method.getCompactFullName()+ 386 " on "+selected[i].getClass().getName()+ 387 " \""+GuiAC.toString(selected[i])+"\"", 388 context.getWindow(), 389 false,false,true); 390 } 391 } 392 } 393 } 394 395 protected abstract void onRemove(); 397 398 protected abstract CollectionUpdate getCollectionUpdate(); 399 400 404 405 protected Object [] getSelected() { 406 Object [] selected = null; 407 int[] indices = getSelectedIndices(); 408 if (indices != null) { 409 selected = new Object [indices.length]; 410 for (int i=0; i<indices.length; i++) 411 { 412 selected[i]= model.getObject(indices[i]); 413 } 414 } else { 415 selected = ExtArrays.emptyObjectArray; 416 } 417 return selected; 418 } 419 420 423 protected abstract int[] getSelectedIndices(); 424 425 protected abstract ListSelectionModel getSelectionModel(); 426 427 protected void setNoRefresh(boolean norefresh) { 428 if (norefresh==false) { 429 repaint(); 430 } 431 } 432 433 static class BetterListSelectionModel extends DefaultListSelectionModel { 434 JComponent list = null; 435 public BetterListSelectionModel( JComponent list ) { 436 super(); 437 this.list = list; 438 } 439 public JComponent getList() { 440 return list; 441 } 442 } 443 444 446 public void mouseClicked(MouseEvent me){ 447 loggerEvents.debug("mouseClicked"); 448 if (model.getRowCount()==1) { 449 loggerEvents.debug("rowCount==1 => forceRefresh"); 450 if (getSelectedIndices().length>0) { 451 CollectionPosition collpos = new CollectionPosition( 452 this, 453 collection, 454 getSelectedIndices()[0], 455 substance); 456 EventHandler.get().onSelection(context, 457 collection, 458 getSelected()[0], 459 null, 460 collpos); 461 } 462 } 463 } 464 465 public void mousePressed(MouseEvent e) { 466 maybeShowPopup(e); 467 } 468 public void mouseReleased(MouseEvent e) { 469 maybeShowPopup(e); 470 } 471 public void mouseExited(MouseEvent me){} 472 public void mouseEntered(MouseEvent me){} 473 void maybeShowPopup(MouseEvent event) { 474 loggerEvents.debug("maybeShowPopup"); 475 if (event.isPopupTrigger()) { 476 int index = locationToIndex(event.getPoint()); 477 if (index!=-1) { 478 Object o = model.getObject(index); 479 if (o instanceof Wrappee) 480 SwingEvents.showObjectMenu(context, o, event); 481 } 482 } 483 } 484 487 abstract int locationToIndex(Point location); 488 489 public void setField(FieldItem field) { 490 collection = (CollectionItem)field; 491 } 492 493 public void setSubstance(Object substance) { 494 this.substance = substance; 495 } 496 497 public Object getSubstance() { 498 return substance; 499 } 500 501 public FieldItem getField() { 502 return collection; 503 } 504 505 public void setValue(Object value) { 506 } 507 508 public void updateModel(Object substance) { 509 } 510 511 } 512 | Popular Tags |