| 1 18 19 package org.objectweb.jac.ide.diagrams; 20 21 import CH.ifa.draw.framework.Drawing; 22 import CH.ifa.draw.framework.DrawingEditor; 23 import CH.ifa.draw.framework.DrawingView; 24 import CH.ifa.draw.framework.Figure; 25 import CH.ifa.draw.framework.FigureEnumeration; 26 import CH.ifa.draw.framework.Tool; 27 import CH.ifa.draw.framework.ViewChangeListener; 28 import CH.ifa.draw.standard.ChangeAttributeCommand; 29 import CH.ifa.draw.standard.ChopBoxConnector; 30 import CH.ifa.draw.standard.StandardDrawing; 31 import CH.ifa.draw.util.ColorMap; 32 import CH.ifa.draw.util.CommandChoice; 33 import CH.ifa.draw.util.Filler; 34 import CH.ifa.draw.util.PaletteButton; 35 import CH.ifa.draw.util.PaletteListener; 36 import CH.ifa.draw.util.StandardVersionControlStrategy; 37 import CH.ifa.draw.util.VersionControlStrategy; 38 import CH.ifa.draw.util.VersionManagement; 39 import CH.ifa.draw.util.VersionRequester; 40 import java.awt.BorderLayout ; 41 import java.awt.Component ; 42 import java.awt.Point ; 43 import java.awt.datatransfer.Transferable ; 44 import java.awt.dnd.DnDConstants ; 45 import java.awt.dnd.DropTarget ; 46 import java.awt.dnd.DropTargetDragEvent ; 47 import java.awt.dnd.DropTargetDropEvent ; 48 import java.awt.dnd.DropTargetEvent ; 49 import java.awt.dnd.DropTargetListener ; 50 import java.awt.event.ActionEvent ; 51 import java.awt.event.ActionListener ; 52 import java.awt.event.KeyAdapter ; 53 import java.awt.event.KeyEvent ; 54 import java.awt.event.MouseAdapter ; 55 import java.awt.event.MouseEvent ; 56 import java.util.Iterator ; 57 import java.util.List ; 58 import java.util.Vector ; 59 import javax.swing.JComboBox ; 60 import javax.swing.JLabel ; 61 import javax.swing.JPanel ; 62 import javax.swing.JScrollPane ; 63 import org.objectweb.jac.aspects.gui.CollectionUpdate; 64 import org.objectweb.jac.aspects.gui.DisplayContext; 65 import org.objectweb.jac.aspects.gui.EventHandler; 66 import org.objectweb.jac.aspects.gui.GuiAC; 67 import org.objectweb.jac.aspects.gui.Transfer; 68 import org.objectweb.jac.aspects.gui.Utils; 69 import org.objectweb.jac.aspects.gui.ViewFactory; 70 import org.objectweb.jac.aspects.gui.swing.AbstractView; 71 import org.objectweb.jac.aspects.gui.swing.SwingEvents; 72 import org.objectweb.jac.core.rtti.ClassRepository; 73 import org.objectweb.jac.core.rtti.CollectionItem; 74 import org.objectweb.jac.ide.Aspect; 75 import org.objectweb.jac.ide.Class; 76 import org.objectweb.jac.ide.Diagram; 77 import org.objectweb.jac.ide.InheritanceLink; 78 import org.objectweb.jac.ide.Link; 79 import org.objectweb.jac.ide.ModelElement; 80 import org.objectweb.jac.ide.Package; 81 import org.objectweb.jac.ide.RelationLink; 82 import org.objectweb.jac.ide.RelationRole; 83 import org.objectweb.jac.ide.TypedElement; 84 import org.objectweb.jac.util.Log; 85 import org.objectweb.jac.util.Strings; 86 87 public class DiagramView extends AbstractView 88 implements DrawingEditor, PaletteListener, VersionRequester, 89 CollectionUpdate, DropTargetListener  90 { 91 static public boolean init = false; 92 93 protected Diagram diagram; 94 95 public DiagramView(ViewFactory factory, DisplayContext context, 96 Object diagram) { 97 super(factory,context); 98 new DropTarget (this, DnDConstants.ACTION_COPY_OR_MOVE, this); this.diagram = (Diagram)diagram; 102 init(); 103 Utils.registerCollection(diagram,"figures",this); 104 } 105 106 public void close(boolean validate) { 107 Log.trace("diagram","CLOSING DIAGRAM "+this); 108 super.close(validate); 109 FigureEnumeration figures = fDrawing.figures(); 110 while (figures.hasMoreElements()) { 111 Figure f = figures.nextFigure(); 112 if (f instanceof ModelElementFigure) 113 ((ModelElementFigure)f).close(); 114 } 115 Utils.unregisterCollection(diagram,"figures",this); 116 } 117 118 public Object getSubstance() { 119 return diagram; 120 } 121 122 public final Diagram getDiagram() { 123 return diagram; 124 } 125 126 131 public void addClass(Class cl, Point location) { 132 Diagram diagram = (Diagram)getSubstance(); 133 if (diagram.contains(cl)) 134 return; 135 ClassFigure cf = null; 136 org.objectweb.jac.ide.ClassFigure figure = new org.objectweb.jac.ide.ClassFigure(cl); 137 Log.trace("diagram","diagram = "+diagram); 138 diagram.addFigure(figure); 139 Log.trace("diagram","creating new figure "+figure+","+ 140 diagram.getContainer()); 141 cf = new ClassFigure(figure,diagram.getContainer(), 142 view()); 143 (view().add(cf)).displayBox(location,location); 144 } 145 146 151 public void importRelations(Class cl) { 152 Iterator it = diagram.getMissingRelations(cl).iterator(); 153 while (it.hasNext()) { 154 Link relation = (Link)it.next(); 155 try { 156 if (relation instanceof RelationLink) 157 importRelation((RelationLink)relation); 158 else if (relation instanceof InheritanceLink) 159 importInheritance((InheritanceLink)relation); 160 } catch (Exception e) { 161 Log.warning(e.getMessage()); 162 } 163 } 164 } 165 166 171 public void importRelation(RelationLink relation) throws Exception { 172 ClassFigure endFig = findClass((Class )relation.getEnd()); 173 if (endFig==null) { 174 throw new Exception ("Cannot import relation "+GuiAC.toString(relation)+ 175 " since "+GuiAC.toString(relation.getEnd())+ 176 " is not on the diagram"); 177 } 178 ClassFigure startFig = findClass((Class )relation.getStart()); 179 if (startFig==null) { 180 throw new Exception ("Cannot import relation "+GuiAC.toString(relation)+ 181 " since "+GuiAC.toString(relation.getStart())+ 182 " is not on the diagram"); 183 } 184 185 org.objectweb.jac.ide.LinkFigure linkFig = new org.objectweb.jac.ide.LinkFigure(relation); 186 diagram.addFigure(linkFig); 187 188 RelationLinkFigure relf = new RelationLinkFigure(); 189 relf.setLinkFigure(linkFig); 190 191 relf.startPoint(startFig.center()); 192 relf.endPoint(endFig.center()); 193 relf.connectStart(startFig.connectorAt(startFig.center())); 194 relf.connectEnd(endFig.connectorAt(endFig.center())); 195 196 relf.updateConnection(); 197 198 view().add(relf); 199 if (relation.getName()!=null && !relation.getName().equals("")) 200 view().add(relf.createName()); 201 if (relation.getEndRole()!=null && !relation.getEndRole().equals("")) 202 view().add(relf.createEndRole()); 203 if (relation.getStartRole()!=null && !relation.getStartRole().equals("")) 204 view().add(relf.createStartRole()); 205 String startCardinality = relation.startRole().getCardinality(); 206 if (startCardinality!=null && !startCardinality.equals("")) 207 view().add(relf.createStartCardinality()); 208 String endCardinality = relation.endRole().getCardinality(); 209 if (endCardinality!=null && !endCardinality.equals("")) 210 view().add(relf.createEndCardinality()); 211 212 Utils.registerObject(relation,relf); 213 } 214 215 219 public void importInheritance(InheritanceLink inheritance) throws Exception { 220 ClassFigure endFig = findClass((Class )inheritance.getEnd()); 221 ClassFigure startFig = findClass((Class )inheritance.getStart()); 222 223 org.objectweb.jac.ide.LinkFigure linkFig = new org.objectweb.jac.ide.LinkFigure(inheritance); 224 diagram.addFigure(linkFig); 225 226 InheritanceLinkFigure relf = new InheritanceLinkFigure(); 227 relf.setLinkFigure(linkFig); 228 229 relf.startPoint(startFig.center()); 230 relf.endPoint(endFig.center()); 231 relf.connectStart(startFig.connectorAt(startFig.center())); 232 relf.connectEnd(endFig.connectorAt(endFig.center())); 233 234 relf.updateConnection(); 235 236 view().add(relf); 237 238 Utils.registerObject(inheritance,relf); 239 } 240 241 249 public void createRelation(Class source, Class target, 250 RelationLinkFigure linkFigure, 251 boolean isAggregation) { 252 Log.trace("figures","creating a new relation link between "+ 253 source+" and "+target); 254 255 RelationLink rel = new RelationLink(source,target); 256 rel.setAggregation(isAggregation); 257 Log.trace("diagram","1. end="+rel.getEnd()+"===> substance="+target); 258 org.objectweb.jac.ide.LinkFigure linkFig = new org.objectweb.jac.ide.LinkFigure(rel); 259 linkFigure.setLinkFigure(linkFig); 260 view().add(linkFigure.createName()); 261 view().add(linkFigure.createEndRole()); 262 view().add(linkFigure.createStartRole()); 263 view().add(linkFigure.createStartCardinality()); 264 view().add(linkFigure.createEndCardinality()); 265 266 Log.trace("diagram","2. end="+rel.getEnd()); 267 268 if (source==target) { 269 Point c = linkFigure.endFigure().center(); 270 linkFig.addPoint(1,new Point (c.x+100,c.y)); 271 linkFig.addPoint(1,new Point (c.x+100,c.y+100)); 272 linkFig.addPoint(1,new Point (c.x,c.y+100)); 273 } 274 275 diagram.addFigure(linkFig); 276 277 Utils.registerObject(rel,linkFigure); 278 Log.trace("diagram","3. end="+rel.getEnd()); 279 } 280 281 public void drop(DropTargetDropEvent e) { 283 try { 284 Transferable tr = e.getTransferable(); 285 List transfered = Transfer.getTransferedWrappees(tr); 286 Object o = transfered.get(0); 287 Point location = e.getLocation(); 288 Point offset = scrollPane.getViewport().getViewPosition(); 289 location.translate((int)offset.getX(),(int)offset.getY()); 290 Log.trace("gui.dnd","drop event: "+o); 291 if (o==null) 292 return; 293 if (o instanceof Class ) { 294 addClass((Class )o,location); 295 } else if (o instanceof RelationRole) { 296 importRelation((RelationLink)((RelationRole)o).getLink()); 297 } 298 299 } catch(Exception ex) { 300 ex.printStackTrace(); 301 } 302 } 303 public void dragEnter(DropTargetDragEvent e) { } 304 public void dragExit(DropTargetEvent e) { } 305 public void dragOver(DropTargetDragEvent e) { } 306 public void dropActionChanged(DropTargetDragEvent e) { } 307 309 public void figureSelectionChanged(DrawingView view) { 310 if (view.selection().size() > 0) { 311 Figure f = (Figure)view.selection().get(0); 312 313 Log.trace("figures","figure "+f+" selected"); 314 315 if (f instanceof Selectable) { 316 ((Selectable)f).onSelect(getContext()); 317 } 318 319 if (f instanceof ClassFigure) { 320 CollectionItem coll = ClassRepository.get().getClass(Package .class) 321 .getCollection("classes"); 322 EventHandler.get().onSelection( 323 getContext(),coll,((ClassFigure)f).getSubstance(),null,null); 324 325 } else if (f instanceof InstanceFigure) { 326 CollectionItem coll = ClassRepository.get().getClass(Package .class) 327 .getCollection("instances"); 328 EventHandler.get().onSelection( 329 getContext(),coll,((InstanceFigure)f).getSubstance(),null,null); 330 331 } else if (f instanceof GenericObjectFigure) { 332 333 Log.trace("figures","generic object"); 334 EventHandler.get().onSelection( 335 getContext(), 336 ((GenericObjectFigure)f).getCollection(), 337 ((GenericObjectFigure)f).getSubstance(),null,null); 338 339 } else if (f instanceof RelationLinkFigure || 340 f instanceof AttachedTextFigure) { 341 342 RelationLink rel = (RelationLink) 343 ((ModelElementFigure)f).getSubstance(); 344 Class cl = (Class )rel.getStart(); 345 CollectionItem coll = ClassRepository.get().getClass(Package .class) 346 .getCollection("classes"); 347 EventHandler.get().onSelection(getContext(),coll,cl,null,null); 348 coll = ClassRepository.get().getClass(Class .class) 349 .getCollection("relationRoles"); 350 EventHandler.get().onSelection(getContext(),coll, 351 rel.getStartRole(),null,null); 352 354 } else if (f instanceof PointcutLinkFigure) { 355 356 org.objectweb.jac.ide.PointcutLink rel = (org.objectweb.jac.ide.PointcutLink) 357 ((PointcutLinkFigure)f).getSubstance(); 358 org.objectweb.jac.ide.Aspect aspect = (org.objectweb.jac.ide.Aspect)rel.getStart(); 359 CollectionItem coll = ClassRepository.get().getClass(Package .class) 360 .getCollection("aspects"); 361 EventHandler.get().onSelection(getContext(),coll,aspect,null,null); 362 coll = ClassRepository.get().getClass(Aspect.class) 363 .getCollection("pointcutLinks"); 364 EventHandler.get().onSelection(getContext(),coll,rel,null,null); 365 } 367 } 368 } 370 371 private transient Drawing fDrawing; 372 private transient Tool fTool; 373 374 private transient DrawingView fView; 375 private transient ToolButton fSelectedToolButton; 376 377 static String fgUntitled = "untitled"; 378 379 ToolPalette classPalette; 380 ToolPalette aspectPalette; 381 ToolPalette instancePalette; 382 ToolPalette groupPalette; 383 ToolPalette currentPalette; 384 385 390 public ClassFigure findClass(Class cl) { 391 return (ClassFigure)findFigure(view().drawing(),cl); 392 } 393 394 400 public ClassFigure findClass(Drawing drawing, Class cl) { 401 return (ClassFigure)findFigure(drawing,cl); 402 } 403 404 410 public ModelElementFigure findFigure(Drawing drawing, ModelElement element) { 411 FigureEnumeration figs = drawing.figures(); 412 while(figs.hasMoreElements()) { 413 Figure fig = figs.nextFigure(); 414 if( (fig instanceof ModelElementFigure) && 415 ((ModelElementFigure)fig).getSubstance() == element) { 416 return (ModelElementFigure)fig; 417 } 418 } 419 return null; 420 } 421 422 425 public Figure findElement(TypedElement te) { 426 FigureEnumeration figs = view().drawing().figures(); 427 while(figs.hasMoreElements()) { 428 Figure fig = figs.nextFigure(); 429 if( (fig instanceof ModelElementFigure) && 430 ((ModelElementFigure)fig).getSubstance() == te) { 431 return fig; 432 } 433 } 434 return null; 435 } 436 437 440 public Figure findElement(ModelElement element) { 441 FigureEnumeration figs = view().drawing().figures(); 442 while(figs.hasMoreElements()) { 443 Figure fig = figs.nextFigure(); 444 if( (fig instanceof ModelElementFigure) && 445 ((ModelElementFigure)fig).getSubstance() == element) { 446 return fig; 447 } 448 } 449 return null; 450 } 451 452 JScrollPane scrollPane; 453 456 public void init() { 457 460 getVersionControlStrategy().assertCompatibleVersion(); 461 462 setLayout(new BorderLayout ()); 463 464 fView = createDrawingView(); 465 466 try { 467 buildClassBar(); 468 } catch (Exception e) { 469 e.printStackTrace(); 470 } 471 472 scrollPane = new JScrollPane ((Component )view()); 473 add("Center", scrollPane); 474 JPanel buttonPalette = createButtonPanel(); 475 createButtons(buttonPalette); 476 add("North", buttonPalette); 477 setToolBar(buildClassBar()); 478 479 initDrawing(); 480 load(); 484 } 486 487 public void addViewChangeListener(ViewChangeListener vsl) { 488 } 489 490 public void removeViewChangeListener(ViewChangeListener vsl) { 491 } 492 493 494 497 protected JComboBox createColorChoice(String attribute) { 498 CommandChoice choice = new CommandChoice(); 499 for (int i = 0; i < ColorMap.size(); i++) 500 choice.addItem( 501 new ChangeAttributeCommand( 502 ColorMap.name(i), 503 attribute, 504 ColorMap.color(i), 505 this 506 ) 507 ); 508 return choice; 509 } 510 511 514 protected JPanel createButtonPanel() { 515 JPanel panel = new JPanel (); 516 panel.setLayout(new PaletteLayout(2, new Point (2,2), false)); 517 return panel; 518 } 519 520 523 void setToolBar(ToolPalette newPalette) { 524 if (currentPalette != null) { 525 remove(currentPalette); 526 } 527 add(BorderLayout.WEST, newPalette); 528 currentPalette = newPalette; 529 validate(); 530 } 531 532 ToolPalette buildClassBar() { 533 Log.trace("diagram","BUILDING CLASS BAR"); 534 classPalette = new ToolPalette(); 535 536 classPalette.setDefaultToolButton( 537 classPalette.addToolButton(this, "icon_selection", "Selection Tool", 538 createSelectionTool())); 539 540 classPalette.addToolButton(this, "icon_text", "Text Tool", 541 new TextTool(this, new TextFigure())); 542 classPalette.addToolButton(this, "icon_class", "Create a new class", 543 new NewClassFigureCreationTool(this,getContext())); 544 classPalette.addToolButton(this, "icon_import_class", "Add an existing class", 545 new ClassFigureCreationTool(this,getContext())); 546 try { 547 classPalette.addToolButton(this, "icon_see_relation", "Show existing relation", 548 new RelationLinkShowTool(this,getContext())); 549 } catch (ClassNotFoundException e) { 550 e.printStackTrace(); 551 } 552 classPalette.addToolButton(this, "icon_relation", "New relation link", 553 new RelationLinkCreationTool(this)); 554 classPalette.addToolButton(this, "icon_add_point", "Add a point", 555 new ConnectionTool(this, new RelationLinkFigure())); 556 classPalette.addToolButton(this, "icon_inheritance", "Inheritance link", 557 new InheritanceLinkCreationTool(this)); 558 classPalette.addToolButton(this, "icon_field", "Create new attribute", 559 new FieldCreationTool(this,getContext())); 560 classPalette.addToolButton(this, "icon_method", "Create new method", 561 new MethodCreationTool(this,getContext())); 562 return classPalette; 563 } 564 565 ToolPalette buildAspectBar() { 566 Log.trace("diagram","BUILDING ASPECT BAR"); 567 568 aspectPalette = new ToolPalette(); 569 570 aspectPalette.setDefaultToolButton( 571 aspectPalette.addToolButton(this, "icon_selection", "Selection Tool", 572 createSelectionTool())); 573 574 aspectPalette.addToolButton(this, "icon_text", "Text Tool", 575 new TextTool(this, new TextFigure())); 576 aspectPalette.addToolButton(this, "icon_aspect", "Create a new aspect", 577 new NewAspectFigureCreationTool(this,getContext())); 578 aspectPalette.addToolButton(this, "icon_import_aspect", "Add an existing aspect", 579 new AspectFigureCreationTool(this,getContext())); 580 aspectPalette.addToolButton(this, "icon_pointcut", "New pointcut link", 581 new PointcutLinkCreationTool(this, new PointcutLinkFigure())); 582 aspectPalette.addToolButton(this, "icon_see_pointcut", "Show existing pointcut", 583 new PointcutLinkShowTool(this,getContext())); 584 aspectPalette.addToolButton(this, "icon_add_point", "Add a point", 585 new ConnectionTool(this, new RelationLinkFigure())); 586 aspectPalette.addToolButton(this, "icon_field", "Create new attribute", 587 new FieldCreationTool(this,getContext())); 588 aspectPalette.addToolButton(this, "icon_method", "Create new configuration method", 589 new MethodCreationTool(this,getContext())); 590 aspectPalette.addToolButton(this, "icon_aspect_method","Create new aspect method", 591 new MethodCreationTool(this,getContext())); 592 return aspectPalette; 593 } 594 595 ToolPalette buildInstanceBar() { 596 Log.trace("diagram","BUILDING INSTANCE BAR"); 597 598 instancePalette = new ToolPalette(); 599 600 instancePalette.setDefaultToolButton( 601 instancePalette.addToolButton(this, "icon_selection", "Selection Tool", 602 createSelectionTool())); 603 instancePalette.addToolButton(this, "icon_text", "Text Tool", 604 new TextTool(this, new TextFigure())); 605 instancePalette.addToolButton(this, "icon_instance", "Create a new instance", 606 new NewInstanceFigureCreationTool(this,getContext())); 607 instancePalette.addToolButton(this, "icon_import_instance", "Add an existing instance", 608 new InstanceFigureCreationTool(this,getContext())); 609 return instancePalette; 610 } 611 612 ToolPalette buildGroupBar() { 613 Log.trace("diagram","BUILDING GROUP BAR"); 614 615 groupPalette = new ToolPalette(); 616 617 groupPalette.setDefaultToolButton( 618 groupPalette.addToolButton(this,"icon_selection", "Selection Tool", 619 createSelectionTool())); 620 groupPalette.addToolButton(this,"icon_text", "Text Tool", 621 new TextTool(this, new TextFigure())); 622 groupPalette.addToolButton(this, "icon_group", "Create a new group", 623 new NewInstanceFigureCreationTool(this,getContext())); 624 groupPalette.addToolButton(this, "icon_import_group", "Add an existing group", 625 new GroupFigureShowTool(this,getContext())); 626 return groupPalette; 627 } 628 629 634 protected void createButtons(JPanel panel) { 635 panel.add(new Filler(24,20)); 636 JLabel title = new JLabel (GuiAC.toString(diagram)+" diagram "); 637 panel.add(title); 638 639 panel.add(new Filler(6,20)); 640 641 JComboBox combo = new JComboBox (new Object [] { 642 "class mode", 643 "aspect mode", 644 "instance mode", 645 "group mode"} 646 ); 647 648 combo.addActionListener( 649 new ActionListener () { 650 public void actionPerformed(ActionEvent event) { 651 JComboBox cb = (JComboBox )event.getSource(); 652 String command = (String )cb.getSelectedItem(); 653 if (command.equals("class mode")) { 654 setToolBar(buildClassBar()); 655 } else if (command.equals("aspect mode")) { 656 setToolBar(buildAspectBar()); 657 } else if (command.equals("instance mode")) { 658 setToolBar(buildInstanceBar()); 659 } else |