View Javadoc

1   /*
2    * ToolGraph.java
3    *
4    * Created on 20. april 2005, 20:09
5    */
6   
7   package com.vikash.firsttool.Diagram;
8   
9   import com.vikash.firsttool.UI.ToolTransferable;
10  import java.awt.*;
11  import java.awt.event.*;
12  import java.awt.dnd.*;
13  import java.awt.datatransfer.*; 
14  import java.beans.*;
15  import java.awt.geom.*;
16  import java.util.List;
17  import java.util.LinkedList;
18  import java.util.Hashtable;
19  import java.util.Map;
20  import java.net.URL;
21  import java.io.Serializable;
22  
23  
24  import javax.swing.*;
25  import javax.swing.tree.*;
26  import javax.swing.border.LineBorder;
27  
28  
29  
30  
31  
32  import org.jgraph.JGraph;
33  import org.jgraph.graph.*;
34  
35  import com.vikash.firsttool.UI.ToolGraphLayoutCache;
36  import com.vikash.firsttool.UI.IconGraphCellView;
37  import com.vikash.firsttool.UI.EllipseCellView;
38  import com.vikash.firsttool.UI.RectangleCellView;
39  import com.vikash.firsttool.ProfileImpl.*;
40  /***
41   *
42   * @author  studajb
43   */
44  public class ToolGraph extends JGraph{
45      String diagramtype;
46      boolean copyFromTree;
47      DefaultGraphCell cellFromTree;
48      GraphModel model;
49      public ToolGraph(GraphModel model, String diagramtype) {
50          
51          super(model);
52          this.model=model;
53          this.diagramtype=diagramtype;
54          //ToolTipManager.sharedInstance().registerComponent(this);
55          setJumpToDefaultPort(true);
56          setBackground(Color.WHITE);
57          setGridVisible(true);
58          setGridEnabled(true);
59          setCloneable(true);
60          setInvokesStopCellEditing(true);
61          
62          setGraphLayoutCache(new ToolGraphLayoutCache(model,null,true,this));
63          
64          getGraphLayoutCache().setFactory(new DefaultCellViewFactory() {
65              
66              protected VertexView createVertexView(Object v) {
67                  if(v instanceof StakeholderGraphCell || v instanceof AssetGraphCell || v instanceof SWOTGraphCell ||
68                  v instanceof ThreatAgentGraphCell ){
69                      return new IconGraphCellView(v);
70                  }
71                  else if (v instanceof ThreatScenarioGraphCell || v instanceof UnwantedIncidentGraphCell || v instanceof TreatmentGraphCell )
72                      return new EllipseCellView(v);
73                  
74                  
75                  else if (v instanceof EntityGraphCell|| v instanceof RiskGraphCell ||v instanceof TreatmentEffectGraphCell)
76                      return new RectangleCellView(v);
77                  
78                  else {
79                      
80                      return  super.createVertexView(v);
81                  }
82              }
83              protected EdgeView createEdgeView(Edge cell) {
84                  
85  		return new EdgeView(cell){
86                    public CellHandle getHandle(GraphContext context){
87                        return new ToolEdgeHandle(this,context); 
88                    }
89                  };
90                  
91  	}
92  
93              
94              
95          });
96          ToolDropTargetListener listener=new ToolDropTargetListener(this);
97          DropTarget target=new DropTarget(this,listener);
98          this.setDropTarget(target);
99      }
100     
101     public void insertVertex(Point2D point,String selected) {
102         final DefaultGraphCell vertex;
103         int u = GraphConstants.PERMILLE;
104         Font DEFAULTFONT = UIManager.getDefaults().getFont("Label.font");
105         AttributeMap attributes= new AttributeMap();
106         Map attributemap = new Hashtable();
107         List Insert = new LinkedList();
108         // Assign cell according to the selected tooolbar button
109         if (selected=="Entity"){
110             
111             vertex= new EntityGraphCell();
112             GraphConstants.setBounds(attributes, new Rectangle2D.Double(point.getX(),point.getY(),150,80));
113             GraphConstants.setExtraLabels(attributes, new Object[] {selected});
114             GraphConstants.setBackground(attributes, Color.white);
115             GraphConstants.setBorderColor(attributes, Color.black);
116             GraphConstants.setFont(attributes,DEFAULTFONT);
117             GraphConstants.setOpaque(attributes, true);
118             
119             vertex.add(new DefaultPort("CENTER"));
120             attributemap.put(vertex, attributes);
121             Insert.add(vertex);
122             this.getGraphLayoutCache().insert(
123             Insert.toArray(), attributemap, null, null, null);
124             
125         }
126         
127         else if(selected=="UnwantedIncident" ||selected=="ThreatScenario" || selected=="Treatment" ) {
128             if(selected=="UnwantedIncident"){
129                 
130                 vertex=new UnwantedIncidentGraphCell();
131             }
132             else if(selected=="ThreatScenario" ){
133                 
134                 vertex=new ThreatScenarioGraphCell();
135             }
136             else
137                 vertex=new TreatmentGraphCell();
138             
139             GraphConstants.setBounds(attributes, new Rectangle2D.Double(point.getX(),point.getY(),150,80));
140             GraphConstants.setExtraLabels(attributes, new Object[] {selected});
141             GraphConstants.setBackground(attributes, Color.white);
142             GraphConstants.setBorderColor(attributes, Color.black);
143             GraphConstants.setFont(attributes,DEFAULTFONT);
144             GraphConstants.setOpaque(attributes, true);
145             
146             vertex.add(new DefaultPort("CENTER"));
147             attributemap.put(vertex, attributes);
148             Insert.add(vertex);
149             this.getGraphLayoutCache().insert(
150             Insert.toArray(), attributemap, null, null, null);
151         }
152         else if(selected=="TreatmentEffect"){
153             vertex= new TreatmentEffectGraphCell();
154             GraphConstants.setBounds(attributes, new Rectangle2D.Double(point.getX(),point.getY(),150,80));
155             GraphConstants.setResize(attributes, false);
156             GraphConstants.setExtraLabels(attributes, new Object[] {selected});
157             GraphConstants.setBackground(attributes, Color.white);
158             GraphConstants.setBorderColor(attributes, Color.black);
159             GraphConstants.setFont(attributes,DEFAULTFONT);
160             GraphConstants.setOpaque(attributes, true);
161             
162             vertex.add(new DefaultPort("CENTER"));
163             attributemap.put(vertex, attributes);
164             Insert.add(vertex);
165             this.getGraphLayoutCache().insert(
166             Insert.toArray(), attributemap, null, null, null);
167         }
168         else if(selected=="Risk"){
169             vertex= new RiskGraphCell();
170             GraphConstants.setBounds(attributes, new Rectangle2D.Double(point.getX(),point.getY(),150,80));
171             GraphConstants.setResize(attributes, false);
172             GraphConstants.setExtraLabels(attributes, new Object[] {selected});
173             GraphConstants.setBackground(attributes, Color.white);
174             GraphConstants.setBorderColor(attributes, Color.black);
175             GraphConstants.setFont(attributes,DEFAULTFONT);
176             GraphConstants.setOpaque(attributes, true);
177             
178             vertex.add(new DefaultPort("CENTER"));
179             attributemap.put(vertex, attributes);
180             Insert.add(vertex);
181             this.getGraphLayoutCache().insert(
182             Insert.toArray(), attributemap, null, null, null);
183         }
184         
185         /*else if(selected=="TreatmentEffect"){
186             RectangleGraphCell topvertex=new RectangleGraphCell();
187             ToolMap topvertexattributes= new ToolMap();
188             GraphConstants.setBounds(topvertexattributes, new Rectangle2D.Double(point.getX(),point.getY(),150,50));
189             GraphConstants.setBorderColor(topvertexattributes, Color.black);
190             GraphConstants.setOpaque(topvertexattributes, false);
191             //GraphConstants.setAutoSize(topvertexattributes, true);
192             GraphConstants.setSelectable(topvertexattributes,true);
193             //GraphConstants.setSizeable(topvertexattributes,false);
194             //GraphConstants.setMoveable(topvertexattributes,false);
195             GraphConstants.setExtraLabels(topvertexattributes, new Object[] {selected});
196             topvertex.add(new DefaultPort("CENTER"));
197             attributemap.put(topvertex, topvertexattributes);
198             Insert.add(topvertex);
199          
200          
201             RectangleGraphCell bottomvertex=new RectangleGraphCell();
202             ToolMap bottomvertexattributes= new ToolMap();
203             Rectangle2D toprect=GraphConstants.getBounds(topvertexattributes);
204             GraphConstants.setBounds(bottomvertexattributes, new Rectangle2D.Double(point.getX(),point.getY()+toprect.getHeight(),150,50));
205             GraphConstants.setBorderColor(bottomvertexattributes, Color.black);
206             GraphConstants.setOpaque(bottomvertexattributes, false);
207             //GraphConstants.setAutoSize(bottomvertexattributes, true);
208             GraphConstants.setSelectable(bottomvertexattributes,true);
209             //GraphConstants.setSizeable(bottomvertexattributes,false);
210             //GraphConstants.setMoveable(bottomvertexattributes,false);
211             GraphConstants.setExtraLabels(bottomvertexattributes, new Object[] {"RiskReduction"});
212             bottomvertex.add(new DefaultPort("CENTER"));
213             attributemap.put(bottomvertex, bottomvertexattributes);
214             Insert.add(bottomvertex);
215             this.getGraphLayoutCache().insert(Insert.toArray(),attributemap,null,null,null);
216          
217             DefaultGraphCell group=new DefaultGraphCell();
218             ToolMap attr=new ToolMap();
219             GraphConstants.setExtraLabels(attr, new Object[] {"some"});
220             GraphConstants.setSelectable(attr,true);
221             attributemap.put(group, attr);
222             Object[] cells=this.order(Insert.toArray());
223             this.getGraphLayoutCache().insertGroup(group,cells);
224         }
225         else if(selected=="Risk") {
226          
227             RectangleGraphCell topvertex=new RectangleGraphCell();
228             AttributeMap topvertexattributes= new AttributeMap();
229             GraphConstants.setBounds(topvertexattributes, new Rectangle2D.Double(point.getX(),point.getY(),250,50));
230             GraphConstants.setBorderColor(topvertexattributes, Color.black);
231             GraphConstants.setOpaque(topvertexattributes, true);
232             //GraphConstants.setAutoSize(topvertexattributes, true);
233             GraphConstants.setSelectable(topvertexattributes,true);
234             //GraphConstants.setSizeable(topvertexattributes,false);
235             //GraphConstants.setMoveable(topvertexattributes,false);
236             GraphConstants.setExtraLabels(topvertexattributes, new Object[] {selected});
237             topvertex.add(new DefaultPort("CENTER"));
238             attributemap.put(topvertex, topvertexattributes);
239             Insert.add(topvertex);
240          
241          
242             DefaultGraphCell bottomvertex=new DefaultGraphCell();
243             AttributeMap bottomvertexattributes= new AttributeMap();
244             Rectangle2D toprect=GraphConstants.getBounds(topvertexattributes);
245             GraphConstants.setBounds(bottomvertexattributes, new Rectangle2D.Double(point.getX(),point.getY()+toprect.getHeight(),250,180));
246             GraphConstants.setBorderColor(bottomvertexattributes, Color.black);
247             GraphConstants.setOpaque(bottomvertexattributes, true);
248             //GraphConstants.setAutoSize(bottomvertexattributes, true);
249             GraphConstants.setSelectable(bottomvertexattributes,true);
250             //GraphConstants.setSizeable(bottomvertexattributes,false);
251             //GraphConstants.setMoveable(bottomvertexattributes,false);
252             bottomvertex.add(new DefaultPort("CENTER"));
253             attributemap.put(bottomvertex, bottomvertexattributes);
254             Insert.add(bottomvertex);
255          
256             DefaultPort port1,port2,port3;
257          
258             RectangleGraphCell insidevertex1=new RectangleGraphCell();
259             AttributeMap insidevertex1attributes= new AttributeMap();
260             GraphConstants.setBounds(insidevertex1attributes, new Rectangle2D.Double(point.getX()+10,point.getY()+toprect.getHeight()+10,100,50));
261             GraphConstants.setBorderColor(insidevertex1attributes, Color.black);
262             GraphConstants.setOpaque(insidevertex1attributes, true);
263             //GraphConstants.setAutoSize(bottomvertexattributes, true);
264             GraphConstants.setSelectable(insidevertex1attributes,true);
265             //GraphConstants.setSizeable(insidevertex1attributes,false);
266             //GraphConstants.setMoveable(insidevertex1attributes,false);
267             GraphConstants.setExtraLabels(insidevertex1attributes, new Object[] {"Consequence"});
268             port1=new DefaultPort();
269             bottomvertex.add(port1);
270             port1.setParent(insidevertex1);
271             attributemap.put(insidevertex1, insidevertex1attributes);
272             Insert.add(insidevertex1);
273          
274             RectangleGraphCell insidevertex2=new RectangleGraphCell();
275             AttributeMap insidevertex2attributes= new AttributeMap();
276             GraphConstants.setBounds(insidevertex2attributes, new Rectangle2D.Double(point.getX()+10+120,point.getY()+toprect.getHeight()+10,100,50));
277             GraphConstants.setBorderColor(insidevertex2attributes, Color.black);
278             GraphConstants.setOpaque(insidevertex2attributes, true);
279             //GraphConstants.setAutoSize(bottomvertexattributes, true);
280             GraphConstants.setSelectable(insidevertex2attributes,true);
281             //GraphConstants.setSizeable(insidevertex2attributes,false);
282             //GraphConstants.setMoveable(insidevertex2attributes,false);
283             GraphConstants.setExtraLabels(insidevertex2attributes, new Object[] {"Frequency"});
284             port2=new DefaultPort();
285             bottomvertex.add(port2);
286             port2.setParent(insidevertex2);
287             attributemap.put(insidevertex2, insidevertex2attributes);
288             Insert.add(insidevertex2);
289          
290             RectangleGraphCell insidevertex3=new RectangleGraphCell();
291             AttributeMap insidevertex3attributes= new AttributeMap();
292             GraphConstants.setBounds(insidevertex3attributes, new Rectangle2D.Double(point.getX()+10+80,point.getY()+toprect.getHeight()+10+90,60,50));
293             GraphConstants.setBorderColor(insidevertex3attributes, Color.black);
294             GraphConstants.setOpaque(insidevertex3attributes, true);
295             //GraphConstants.setAutoSize(bottomvertexattributes, true);
296             GraphConstants.setSelectable(insidevertex3attributes,true);
297             //GraphConstants.setSizeable(insidevertex3attributes,false);
298             //GraphConstants.setMoveable(insidevertex3attributes,false);
299             GraphConstants.setExtraLabels(insidevertex3attributes, new Object[] {"RiskValue"});
300             port3=new DefaultPort();
301             bottomvertex.add(port3);
302             port3.setParent(insidevertex3);
303             attributemap.put(insidevertex3, insidevertex3attributes);
304             Insert.add(insidevertex3);
305             this.getGraphLayoutCache().insert(Insert.toArray(),attributemap,null,null,null);
306          
307             DefaultEdge edge1=new DefaultEdge();
308             AttributeMap edge1attributes= new AttributeMap();
309             GraphConstants.setOpaque(edge1attributes, true);
310             edge1.getAttributes().applyMap(edge1attributes);
311             this.getGraphLayoutCache().insertEdge(edge1, port1, port3);
312             Insert.add(edge1);
313          
314             DefaultEdge edge2=new DefaultEdge();
315             AttributeMap edge2attributes= new AttributeMap();
316             GraphConstants.setOpaque(edge2attributes, true);
317             edge2.getAttributes().applyMap(edge2attributes);
318             Insert.add(edge2);
319             this.getGraphLayoutCache().insertEdge(edge2, port2, port3);
320          
321          
322          
323             DefaultGraphCell group=new DefaultGraphCell();
324             ToolMap attr=new ToolMap();
325             GraphConstants.setExtraLabels(attr, new Object[] {"some"});
326             GraphConstants.setSelectable(attr,true);
327             attributemap.put(group, attr);
328             Object[] cells=this.order(Insert.toArray());
329             this.getGraphLayoutCache().insertGroup(group,cells);
330          
331          
332         }*/
333         else {
334             
335             //vertex= new IconGraphCell("<<"+selected+">>");
336             if(selected=="Stakeholder"){
337                 vertex= new StakeholderGraphCell();
338                 
339             }
340             else if(selected=="Asset"){
341                 vertex= new AssetGraphCell();
342             }
343             else if(selected=="EnterpriseAsset"||selected=="EnterpriseOpportunity"||selected=="EnterpriseStrength"
344             ||selected=="EnterpriseWeakness" ||selected=="EnterpriseThreat"){
345                 
346                 vertex= new SWOTGraphCell();
347             }
348             else
349                 vertex=new ThreatAgentGraphCell();
350             
351             URL icon=getClass().getClassLoader().getResource("com/vikash/firsttool/icons/"+selected+".gif");
352             GraphConstants.setIcon(attributes,new ImageIcon(icon));
353             GraphConstants.setBounds(attributes, new Rectangle2D.Double(point.getX(),point.getY(),150,150));
354             GraphConstants.setBorder(attributes, new LineBorder(Color.WHITE,0));
355             if (vertex instanceof AssetGraphCell)
356                 GraphConstants.setExtraLabels(attributes, new Object[] {selected,"Vulnerabilities:"}); // Asset has vulnerability as extra attribute
357             else
358                 GraphConstants.setExtraLabels(attributes, new Object[] {selected});
359                 GraphConstants.setBackground(attributes, Color.white);
360                 GraphConstants.setBorderColor(attributes, Color.black);
361                 GraphConstants.setFont(attributes,DEFAULTFONT);
362                 GraphConstants.setOpaque(attributes, true);
363                 
364                 vertex.add(new DefaultPort("CENTER"));
365                 attributemap.put(vertex, attributes);
366                 Insert.add(vertex);
367                 this.getGraphLayoutCache().insert(
368                 Insert.toArray(), attributemap, null, null, null);
369         }
370         
371         
372         
373     }
374     
375     public void connect(Port source, Port target,String icontype,String selected) {
376         
377         ToolEdge edge = new ToolEdge();
378         Map attributes=new Hashtable();
379         if (((ToolModel)this.getModel()).acceptsSource(edge, source,icontype,selected) && this.getModel().acceptsTarget(edge, target)) {
380             
381             if(icontype=="dependency"){
382                 Object name="<<"+selected+">>";
383                 edge.setUserObject(name);
384                 GraphConstants.setLineEnd(attributes, GraphConstants.ARROW_SIMPLE);
385                 float[] Dash={10f,10f};
386                 GraphConstants.setDashPattern(attributes, Dash); // for dashed line pattern
387             }
388             if(icontype=="association")  {
389                 GraphConstants.setLineEnd(attributes, GraphConstants.ARROW_SIMPLE);
390             }
391             
392             GraphConstants.setOpaque(attributes, true);
393             
394             
395             edge.getAttributes().applyMap(attributes);
396             this.getGraphLayoutCache().insertEdge(edge, source, target); //insert edge into graphmodel
397         }
398     }
399     public boolean isValidElement(DefaultMutableTreeNode cell){
400         return false;
401         
402     }
403     
404     public void setCopyFromTree(boolean copy,DefaultGraphCell cell){
405         this.copyFromTree=copy;
406         this.cellFromTree=cell;
407     }
408     public boolean isCopyFromTree(){
409         return this.copyFromTree;
410     }
411     public DefaultGraphCell copyCellFromTree(){
412         return this.cellFromTree;
413     }
414     public void insertCellFromTree(int x, int y){
415         
416         if(isCopyFromTree()){
417             DefaultGraphCell cellFromTree=copyCellFromTree();
418             if(cellFromTree!=null){
419                 Rectangle2D rect;
420                 if(cellFromTree instanceof ToolEdge){
421                     DefaultGraphCell sourceVertex,targetVertex;
422                     sourceVertex=(DefaultGraphCell)((DefaultPort)((ToolEdge)cellFromTree).getSource()).getParent();
423                     targetVertex=(DefaultGraphCell)((DefaultPort)((ToolEdge)cellFromTree).getTarget()).getParent();
424                     // if source and target vertex are already present in the diagram then just add the edge between them
425                     if(((ToolGraphLayoutCache)getGraphLayoutCache()).isVisible(sourceVertex) && ((ToolGraphLayoutCache)getGraphLayoutCache()).isVisible(targetVertex)){
426                         java.util.List points=new java.util.ArrayList();
427                         points.add(0,(DefaultPort)((ToolEdge)cellFromTree).getSource());
428                         points.add(1,(DefaultPort)((ToolEdge)cellFromTree).getTarget());
429                         AttributeMap map=cellFromTree.getAttributes();
430                         GraphConstants.setPoints(map,points);
431                         ((ToolGraphLayoutCache)getGraphLayoutCache()).editCell(cellFromTree, map);
432                         ((ToolGraphLayoutCache)getGraphLayoutCache()).setVisible(cellFromTree,true);
433                         
434                         return;
435                     }
436                     
437                     rect=new Rectangle2D.Double(x-150,y-150,0,0);
438                     ((ToolGraphLayoutCache)getGraphLayoutCache()).cellVisible(sourceVertex,true,rect);
439                     
440                     rect=new Rectangle2D.Double(x+150,y+150,0,0);
441                     // add it to diagram if it not already present
442                     ((ToolGraphLayoutCache)getGraphLayoutCache()).cellVisible(targetVertex,true,rect);
443                 }
444                 else{
445                     rect=new Rectangle2D.Double(x,y,0,0);
446                     ((ToolGraphLayoutCache)getGraphLayoutCache()).cellVisible(cellFromTree,true,rect);
447                 }
448             }
449             setCopyFromTree(false,null);
450             
451         }
452     }
453     public String getDiagramType(){
454         return diagramtype;
455     }
456     
457     
458 }
459 
460 class ToolDropTargetListener implements DropTargetListener{
461     ToolGraph graph;
462     DefaultGraphCell cell=null;
463     public ToolDropTargetListener(ToolGraph toolgraph){
464         this.graph=toolgraph;
465         
466     }
467     public void dragEnter(DropTargetDragEvent dtde) {
468         
469     }
470     
471     public void dragExit(DropTargetEvent dte) {
472     }
473     
474     public void dragOver(DropTargetDragEvent dtde) {
475         
476     }
477     
478     public void drop(DropTargetDropEvent dtde) {
479         Transferable transferable=dtde.getTransferable();
480         
481         try{
482             cell=(DefaultGraphCell)transferable.getTransferData(ToolTransferable.CELL_FLAVOR); 
483         }catch(Exception e){
484             
485         }
486         if (!isDropAcceptable()){
487             dtde.rejectDrop();
488             JOptionPane.showMessageDialog(null, "Is not a valid element of "+graph.getDiagramType(),"Error",JOptionPane.OK_OPTION);
489             return;
490         }
491         
492         
493         
494         int x=(int)dtde.getLocation().getX();
495         int y=(int)dtde.getLocation().getY();
496         
497         graph.insertCellFromTree(x, y);
498         /*Rectangle2D rect;
499        if(cell instanceof ToolEdge){
500             DefaultGraphCell edgeVertex;
501             edgeVertex=(DefaultGraphCell)((DefaultPort)((ToolEdge)cell).getSource()).getParent();
502             rect=new Rectangle2D.Double(x-150,y-150,0,0);
503             ((ToolGraphLayoutCache)graph.getGraphLayoutCache()).cellVisible(edgeVertex,true,rect);
504             edgeVertex=(DefaultGraphCell)((DefaultPort)((ToolEdge)cell).getTarget()).getParent();
505             rect=new Rectangle2D.Double(x+150,y+150,0,0);
506             ((ToolGraphLayoutCache)graph.getGraphLayoutCache()).cellVisible(edgeVertex,true,rect);
507         }
508         else{
509             rect=new Rectangle2D.Double(x,y,0,0);
510             ((ToolGraphLayoutCache)graph.getGraphLayoutCache()).cellVisible(cell,true,rect);
511         }*/
512         
513         dtde.dropComplete(true);
514     }
515     
516     public void dropActionChanged(DropTargetDragEvent dtde) {
517         if (!isDropAcceptable()){
518             dtde.rejectDrag();
519             return;
520         }
521     }
522     public boolean isDropAcceptable(){
523         if(!graph.isCopyFromTree())return false;
524         cell=graph.copyCellFromTree();
525         if (cell instanceof ToolEdge){
526             DefaultGraphCell source=(DefaultGraphCell)((DefaultPort)((ToolEdge)cell).getSource()).getParent();
527             DefaultGraphCell target=(DefaultGraphCell)((DefaultPort)((ToolEdge)cell).getTarget()).getParent();
528             if(graph.isValidElement(source) && graph.isValidElement(target))
529                 return true;
530             
531             
532         }
533         else{
534             if(graph != null && graph.isValidElement(cell))
535                 return true;
536         }
537         return false;
538     }
539     
540 }