1 19 20 package org.objectweb.jac.aspects.gui.swing; 21 22 23 import java.awt.BorderLayout ; 24 import java.awt.Component ; 25 import java.awt.Dimension ; 26 import java.awt.datatransfer.*; 27 import java.awt.dnd.*; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.awt.event.KeyEvent ; 31 import java.awt.event.KeyListener ; 32 import java.awt.event.MouseEvent ; 33 import java.awt.event.MouseListener ; 34 import java.util.Arrays ; 35 import java.util.Enumeration ; 36 import java.util.List ; 37 import javax.swing.Icon ; 38 import javax.swing.ImageIcon ; 39 import javax.swing.JButton ; 40 import javax.swing.JPanel ; 41 import javax.swing.JScrollPane ; 42 import javax.swing.JTree ; 43 import javax.swing.ToolTipManager ; 44 import javax.swing.event.TreeExpansionEvent ; 45 import javax.swing.event.TreeExpansionListener ; 46 import javax.swing.event.TreeSelectionEvent ; 47 import javax.swing.event.TreeSelectionListener ; 48 import javax.swing.tree.DefaultTreeCellRenderer ; 49 import javax.swing.tree.TreeNode ; 50 import javax.swing.tree.TreePath ; 51 import org.apache.log4j.Logger; 52 import org.objectweb.jac.aspects.gui.*; 53 import org.objectweb.jac.aspects.gui.Transfer; 54 import org.objectweb.jac.core.Collaboration; 55 import org.objectweb.jac.core.Wrappee; 56 import org.objectweb.jac.core.rtti.CollectionItem; 57 import org.objectweb.jac.core.rtti.FieldItem; 58 import org.objectweb.jac.core.rtti.MethodItem; 59 60 65 66 public class Tree extends AbstractView 67 implements View, TreeSelectionListener , MouseListener , 68 TreeExpansionListener , TreeListener, KeyListener , 69 DragGestureListener, DragSourceListener, DropTargetListener 70 { 71 static Logger logger = Logger.getLogger("gui.treeview"); 72 73 JTree tree; 74 TreeModel model; 75 boolean showRelations = true; 77 78 JButton viewButton = null; 79 JButton newButton = null; 80 JButton removeButton = null; 81 82 RootNode rootNode = null; 83 84 90 public Tree(ViewFactory factory, DisplayContext context, 91 String pathDef, boolean showRelations ) { 92 super(factory,context); 93 94 this.showRelations = showRelations; 95 96 setLayout(new BorderLayout ()); 97 98 tree = new JTree (); 99 100 DragSource dragSource = DragSource.getDefaultDragSource(); 101 dragSource.createDefaultDragGestureRecognizer( 104 tree, DnDConstants.ACTION_COPY_OR_MOVE, this); 108 JScrollPane upperCont = new JScrollPane (tree); 109 tree.addTreeExpansionListener(this); 110 JPanel downCont = new JPanel (); 111 112 tree.putClientProperty("JTree.lineStyle", "Angled"); 113 ToolTipManager.sharedInstance().registerComponent(tree); 114 tree.setRootVisible(false); 115 tree.addTreeSelectionListener( this ); 116 117 rootNode = new RootNode(); 118 model = new TreeModel(rootNode, pathDef, showRelations); 119 tree.setModel(model); 120 tree.setCellRenderer(new TreeNodeRenderer()); 121 tree.addMouseListener(this); 122 tree.addKeyListener(this); 123 model.addTreeListener(this); 124 125 viewButton = createButton("view_icon","View",new openHandler()); 127 downCont.add(viewButton); 128 129 130 newButton = createButton("new_icon","Add",new addHandler()); 131 downCont.add(newButton); 132 133 removeButton = createButton( 134 "remove_icon","Remove", 135 new ActionListener () { 136 public void actionPerformed(ActionEvent event) { 137 doDelete(false); 138 } 139 } 140 ); 141 142 downCont.add(removeButton); 143 144 add(upperCont, BorderLayout.CENTER); 145 add(downCont, BorderLayout.SOUTH); 146 147 new DropTarget(tree, DnDConstants.ACTION_COPY_OR_MOVE, this); 151 expandRoot(); 152 } 153 154 public void dragGestureRecognized(DragGestureEvent e) { 156 loggerDnd.debug("drag gesture detected"); 157 159 TreePath tp = tree.getPathForLocation( 160 (int)e.getDragOrigin().getX(), (int)e.getDragOrigin().getY() ); 161 if (tp != null) { 162 AbstractNode node=(AbstractNode) tp.getLastPathComponent(); 163 Object o = node.getUserObject(); 164 if(o instanceof Wrappee) { 165 node=(AbstractNode)node.getParent(); 166 Object parent = node.getUserObject(); 167 if(parent != null && (parent instanceof FieldItem)) { 168 node=(AbstractNode)node.getParent(); 169 parent = node.getUserObject(); 170 } 171 Wrappee[] toTransfer; 172 if(parent instanceof Wrappee) { 173 toTransfer=new Wrappee[] {(Wrappee)o,(Wrappee)parent}; 174 } else { 175 toTransfer=new Wrappee[] {(Wrappee)o,null}; 176 } 177 loggerDnd.debug("to transfer: "+Arrays.asList(toTransfer)); 178 e.startDrag( 179 null, Transfer.getJACTransfer(toTransfer), 181 this); } 183 } 184 } 185 public void dragDropEnd(DragSourceDropEvent e) {} 186 public void dragEnter(DragSourceDragEvent e) {} 187 public void dragExit(DragSourceEvent e) {} 188 public void dragOver(DragSourceDragEvent e) {} 189 public void dropActionChanged(DragSourceDragEvent e) {} 190 191 public void drop(DropTargetDropEvent e) { 192 try { 193 loggerDnd.debug("drop event"); 194 Transferable tr = e.getTransferable(); 195 TreePath tp = tree.getPathForLocation( 196 (int)e.getLocation().getX(), (int)e.getLocation().getY() ); 197 if(tp!=null) { 198 List transfered=Transfer.getTransferedWrappees(tr); 199 Object droppedObject=transfered.get(0); 200 Object source=transfered.get(1); 201 Object target=((AbstractNode) tp.getLastPathComponent()).getUserObject(); 202 loggerDnd.debug("target="+target+", droppedObject="+ 203 droppedObject+", source="+source); 204 if(droppedObject==null || target==null || 205 (!(target instanceof Wrappee)) ) return; 206 EventHandler.get().onDropObject(getContext(),target,droppedObject,source,false); 207 } 208 } catch(Exception ex) { 209 ex.printStackTrace(); 210 } 211 } 212 public void dragEnter(DropTargetDragEvent e) {} 213 public void dragExit(DropTargetEvent e) {} 214 public void dragOver(DropTargetDragEvent e) {} 215 public void dropActionChanged(DropTargetDragEvent e) {} 216 217 219 224 225 public void expandRoot() 226 { 227 Object root = model.getRoot(); 228 int rootCount = model.getChildCount(root); 229 for (int i=0; i<rootCount; i++) 230 { 231 Object child = model.getChild(root, i); 232 TreePath path = new TreePath (model.getPathToRoot((TreeNode ) child)); 233 logger.debug("Expanding path "+path); 234 tree.expandPath(path); 235 } 236 } 237 238 245 JButton createButton(String iconName, String text, ActionListener listener) { 246 ImageIcon icon = ResourceManager.getIconResource(iconName); 247 JButton button; 248 if (icon==null) 249 button = new JButton (text); 250 else { 251 button = new JButton (icon); 252 button.setToolTipText(text); 253 } 254 button.addActionListener(listener); 255 button.setEnabled(false); 256 return button; 257 } 258 259 261 public void close(boolean validate) { 262 model.unregisterEvents(); 263 } 264 265 void selectObject(AbstractNode root,Object toSelect) { 266 Enumeration children=root.children(); 267 while(children.hasMoreElements()) { 268 AbstractNode node = (AbstractNode)children.nextElement(); 269 if( node.getUserObject()==toSelect ) { 270 AbstractNode parent = (AbstractNode)node.getParent(); 271 while (parent!=null) { 272 tree.expandPath(new TreePath (parent.getPath())); 273 parent = (AbstractNode)parent.getParent(); 274 } 275 tree.expandPath(new TreePath (node.getPath())); 276 tree.addSelectionPath(new TreePath (node.getPath())); 277 } 278 selectObject(node,toSelect); 279 } 280 } 281 282 public void treeCollapsed(TreeExpansionEvent event) { 283 loggerEvents.debug("treeCollapsed "+event.getPath()); 284 } 285 286 public void treeExpanded(TreeExpansionEvent event) { 287 loggerEvents.debug("treeExpanded "+event.getPath()); 288 Object node = (AbstractNode)event.getPath().getLastPathComponent(); 289 if (node instanceof ObjectNode) { 290 ((ObjectNode)node).updateChildren(); 291 } 292 } 293 294 297 public void valueChanged(TreeSelectionEvent event) { 298 loggerEvents.debug("valueChanged "+event.getSource().getClass().getName()); 299 AbstractNode node = (AbstractNode) 300 tree.getLastSelectedPathComponent(); 301 if (node == null) { 302 viewButton.setEnabled(false); 303 removeButton.setEnabled(false); 304 } else { 305 Object selected = node.getUserObject(); 306 if (selected instanceof CollectionItem) { 307 loggerEvents.debug("selected the collection "+ 308 ((CollectionItem)selected).getName()); 309 viewButton.setEnabled(false); 310 newButton.setEnabled(true); 311 removeButton.setEnabled(doDelete(true)); 312 } else { 313 loggerEvents.debug("selected a wrappee "+selected); 314 viewButton.setEnabled(selected!=null); 315 newButton.setEnabled(false); 316 removeButton.setEnabled(doDelete(true)); 317 EventHandler.get().onNodeSelection(context,node,false); 318 } 319 } 320 } 321 322 324 325 public void mouseClicked(MouseEvent me){} 326 327 331 332 public void mousePressed(MouseEvent e) { 333 maybeShowPopup(e); 334 } 335 336 340 341 public void mouseReleased(MouseEvent e) { 342 maybeShowPopup(e); 343 } 344 345 346 public void mouseExited(MouseEvent me){} 347 348 public void mouseEntered(MouseEvent me){} 349 350 private void maybeShowPopup(MouseEvent e) { 351 loggerEvents.debug("maybeShowPopup: "+e.getClass().getName()); 352 TreePath tp = tree.getPathForLocation( e.getX(), e.getY() ); 353 if (tp != null) { 354 Object o = ((AbstractNode) tp.getLastPathComponent()).getUserObject(); 355 if (e.isPopupTrigger()) { 356 if (o instanceof Wrappee) 358 SwingEvents.showObjectMenu(context, o, e); 359 } 360 } 361 } 362 363 365 public void keyTyped(KeyEvent event) {} 366 367 public void keyPressed(KeyEvent event) { 368 System.out.println("keyPressed "+event); 369 } 370 371 public void keyReleased(KeyEvent event) { 372 if (event.getKeyCode()==KeyEvent.VK_DELETE) { 373 doDelete(false); 374 } 375 } 376 377 public void setSelection(TreePath selection) { 378 tree.setSelectionPath(selection); 379 } 380 381 384 class openHandler implements ActionListener { 385 public void actionPerformed(ActionEvent event) { 386 loggerEvents.debug("action performed: OPEN"); 387 setContext(); 388 TreePath anchorPath = tree.getSelectionPath(); 390 if (anchorPath!=null) { 391 AbstractNode node = (AbstractNode)anchorPath.getLastPathComponent(); 392 AbstractNode parentNode = (AbstractNode)node.getParent(); 393 Collaboration.get().addAttribute( 394 "Session.sid", 395 "Swing"+org.objectweb.jac.core.dist.Distd.getLocalContainerName() ); 396 397 if (node instanceof ObjectNode) { 398 ObjectNode objectNode = (ObjectNode)node; 399 EventHandler.get().onSelection( 400 context,objectNode.getRelation(),objectNode.getUserObject(), 401 null,null,true); 402 } 403 } 404 } 405 } 406 407 410 class addHandler implements ActionListener { 411 public void actionPerformed(ActionEvent event) { 412 loggerEvents.debug("action performed: ADD"); 413 setContext(); 414 AbstractNode node = (AbstractNode)tree 415 .getAnchorSelectionPath().getLastPathComponent(); 416 AbstractNode parentNode = (AbstractNode)node.getParent(); 417 Collaboration.get().addAttribute( 418 "Session.sid", "Swing"+org.objectweb.jac.core.dist.Distd.getLocalContainerName() ); 419 420 Object o = node.getUserObject(); 421 if( o instanceof CollectionItem ) { 422 if( parentNode != null ) { 423 try { 424 MethodItem[] addingMethods = ((CollectionItem)o).getAddingMethods(); 425 if( addingMethods != null && addingMethods.length > 0 ) { 426 logger.debug("invoking "+addingMethods[0]+" on "+ 427 parentNode.getUserObject()); 428 if (((CollectionItem)o).getAttribute(GuiAC.AUTO_CREATE)!=null) { 429 logger.debug("auto creation asked"); 430 Collaboration.get().addAttribute( 431 GuiAC.AUTO_CREATE, addingMethods[0].getParameterTypes()[0]); 432 } 433 Collaboration.get().addAttribute( 434 GuiAC.ASK_FOR_PARAMETERS, addingMethods[0]); 435 Collaboration.get().addAttribute( 436 GuiAC.DISPLAY_CONTEXT, context); 437 addingMethods[0].invoke( 438 parentNode.getUserObject(), 439 new Object [addingMethods[0].getParameterTypes().length]); 440 } 441 } catch( Exception e ) { 442 e.printStackTrace(); 443 } 444 } 445 } 446 } 447 } 448 449 457 protected boolean doDelete(boolean softRun) { 458 setContext(); 459 460 TreePath [] selectionPaths = tree.getSelectionPaths(); 461 if (selectionPaths==null) 462 return false; 463 boolean result = true; 464 for (int i=0; i<selectionPaths.length; i++) { 465 loggerEvents.debug("selectionPath = "+selectionPaths[i]); 466 AbstractNode node = 467 (AbstractNode)selectionPaths[i].getLastPathComponent(); 468 AbstractNode parentNode = 469 (AbstractNode)node.getParent(); 470 Object selectedObject = node.getUserObject(); 471 if (selectedObject instanceof CollectionItem) { 472 CollectionItem collection = (CollectionItem)selectedObject; 473 if (parentNode != null && 476 GuiAC.isRemovable(parentNode.getUserObject(),collection)) { 477 if (!softRun) { 478 EventHandler.get().onRemoveFromCollection( 479 context, 480 new RemoveEvent( 481 this, 482 parentNode.getUserObject(), 483 (CollectionItem)selectedObject, 484 null), 485 false); 486 } else { 487 result = false; 488 } 489 } 490 } else if (node instanceof ObjectNode && 491 ((ObjectNode)node).getRelation() instanceof CollectionItem) { 492 loggerEvents.debug("node = "+node+"; parentNode="+parentNode); 493 FieldItem coll = ((ObjectNode)node).getRelation(); 494 if (coll instanceof CollectionItem && 495 GuiAC.isRemovable(((ObjectNode)node).getSubstance(), 496 (CollectionItem)coll)) 497 { 498 if (!softRun) { 499 Collaboration.get().addAttribute(GuiAC.REMOVED_NODE,node); 500 try { 501 EventHandler.get().onRemoveFromCollection( 502 context, 503 new RemoveEvent( 504 this, 505 ((ObjectNode)node).getSubstance(), 506 (CollectionItem)coll, 507 selectedObject), 508 false); 509 } finally { 510 Collaboration.get().removeAttribute(GuiAC.REMOVED_NODE); 511 } 512 } 513 } else { 514 result = false; 515 } 516 } else { 517 result = false; 518 } 519 } 520 return result; 521 } 522 523 static class TreeNodeRenderer extends DefaultTreeCellRenderer { 524 525 public TreeNodeRenderer() {} 526 527 public Component getTreeCellRendererComponent(JTree tree, 528 Object value, 529 boolean sel, 530 boolean expanded, 531 boolean leaf, 532 int row, 533 boolean hasFocus) { 534 535 this.hasFocus = hasFocus; 536 AbstractNode node = (AbstractNode)value; 537 setText(node.getText()); 538 setToolTipText(node.getToolTip()); 539 540 if(sel) 541 setForeground(getTextSelectionColor()); 542 else 543 setForeground(getTextNonSelectionColor()); 544 if (!tree.isEnabled()) { 546 setEnabled(false); 547 if (leaf) { 548 setDisabledIcon(getLeafIcon()); 549 } else if (expanded) { 550 setDisabledIcon(getOpenIcon()); 551 } else { 552 setDisabledIcon(getClosedIcon()); 553 } 554 } else { 555 setEnabled(true); 556 setIcon(ResourceManager.getIcon(node.getIcon())); 557 } 558 setComponentOrientation(tree.getComponentOrientation()); 559 selected = sel; 560 561 return this; 562 } 563 564 public Dimension getPreferredSize() { 565 Dimension dim = super.getPreferredSize(); 566 Icon icon = getIcon(); 567 if (icon!=null) { 568 if (icon.getIconHeight()+2>dim.getHeight()) 569 dim.setSize((int)dim.getWidth(), getIcon().getIconHeight()+2); 570 } 571 return dim; 572 } 573 574 } 575 } 576 | Popular Tags |