1
2
3
4
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
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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333 else {
334
335
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:"});
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);
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);
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
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
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
499
500
501
502
503
504
505
506
507
508
509
510
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 }