Skip to main content
summaryrefslogtreecommitdiffstats
blob: 822d37af065a33255ff1ad14c205cb29511ea88a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/******************************************************************************* 
 * Copyright (c) 2011, 2012 Red Hat, Inc. 
 *  All rights reserved. 
 * This program is made available under the terms of the 
 * Eclipse Public License v1.0 which accompanies this distribution, 
 * and is available at http://www.eclipse.org/legal/epl-v10.html 
 * 
 * Contributors: 
 * Red Hat, Inc. - initial API and implementation 
 *
 * @author Ivar Meikas
 ******************************************************************************/
package org.eclipse.bpmn2.modeler.core.di;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.bpmn2.Association;
import org.eclipse.bpmn2.BaseElement;
import org.eclipse.bpmn2.Collaboration;
import org.eclipse.bpmn2.Definitions;
import org.eclipse.bpmn2.DocumentRoot;
import org.eclipse.bpmn2.FlowElementsContainer;
import org.eclipse.bpmn2.MessageFlow;
import org.eclipse.bpmn2.RootElement;
import org.eclipse.bpmn2.SequenceFlow;
import org.eclipse.bpmn2.di.BPMNDiagram;
import org.eclipse.bpmn2.di.BPMNEdge;
import org.eclipse.bpmn2.di.BPMNLabel;
import org.eclipse.bpmn2.di.BPMNPlane;
import org.eclipse.bpmn2.di.BPMNShape;
import org.eclipse.bpmn2.di.BpmnDiFactory;
import org.eclipse.bpmn2.modeler.core.adapters.ExtendedPropertiesProvider;
import org.eclipse.bpmn2.modeler.core.preferences.Bpmn2Preferences;
import org.eclipse.bpmn2.modeler.core.preferences.ShapeStyle;
import org.eclipse.bpmn2.modeler.core.utils.BusinessObjectUtil;
import org.eclipse.bpmn2.modeler.core.utils.ModelUtil;
import org.eclipse.bpmn2.util.Bpmn2Resource;
import org.eclipse.dd.dc.Bounds;
import org.eclipse.dd.dc.DcFactory;
import org.eclipse.dd.dc.Point;
import org.eclipse.dd.di.DiagramElement;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.EcoreUtil.UsageCrossReferencer;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.graphiti.datatypes.IDimension;
import org.eclipse.graphiti.datatypes.ILocation;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.algorithms.styles.Color;
import org.eclipse.graphiti.mm.algorithms.styles.Font;
import org.eclipse.graphiti.mm.algorithms.styles.Style;
import org.eclipse.graphiti.mm.pictograms.Anchor;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.FreeFormConnection;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.mm.pictograms.PictogramLink;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.graphiti.platform.IDiagramBehavior;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.services.IGaService;
import org.eclipse.graphiti.services.ILayoutService;

public class DIUtils {
	
	/**
	 * Creates a BPMNShape if it does not already exist, and then links it to
	 * the given {@code BaseElement}.
	 *
	 * @param shape the Container Shape
	 * @param elem the BaseElement
	 * @param bpmnShape the BPMNShape object. If null, a new one is created and
	 *            inserted into the BPMNDiagram.
	 * @param applyDefaults if true, apply User Preference defaults for certain
	 *            BPMN DI attributes, e.g. isHorizontal, isExpanded, etc.
	 * @return the BPMNShape
	 */
	public static BPMNShape createDIShape(Shape shape, BaseElement elem, BPMNShape bpmnShape, IFeatureProvider fp) {
		if (bpmnShape == null) {
			Diagram diagram = Graphiti.getPeService().getDiagramForShape(shape);
			ILocation loc = Graphiti.getLayoutService().getLocationRelativeToDiagram(shape);
			int x = loc.getX();
			int y = loc.getY();
			int w = shape.getGraphicsAlgorithm().getWidth();
			int h = shape.getGraphicsAlgorithm().getHeight();
			bpmnShape = createDIShape(shape, elem, x, y, w, h, fp, diagram);
		}
		else {
			fp.link(shape, new Object[] { elem, bpmnShape });
		}
		return bpmnShape;
	}

	public static BPMNShape createDIShape(Shape shape, BaseElement elem, int x, int y, int w, int h,
			IFeatureProvider fp, Diagram diagram) {

		EList<EObject> businessObjects = Graphiti.getLinkService().getLinkForPictogramElement(diagram)
				.getBusinessObjects();
		BPMNShape bpmnShape = null;

		for (EObject eObject : businessObjects) {
			if (eObject instanceof BPMNDiagram) {
				BPMNDiagram bpmnDiagram = (BPMNDiagram) eObject;

				bpmnShape = BpmnDiFactory.eINSTANCE.createBPMNShape();
				bpmnShape.setBpmnElement(elem);
				Bounds bounds = DcFactory.eINSTANCE.createBounds();
				bounds.setX(x);
				bounds.setY(y);
				bounds.setWidth(w);
				bounds.setHeight(h);
				bpmnShape.setBounds(bounds);
				getOrCreateDILabel(shape, bpmnShape);

				Bpmn2Preferences.getInstance(bpmnDiagram.eResource()).applyBPMNDIDefaults(bpmnShape, null);

				addDIElement(bpmnShape,bpmnDiagram);
				ModelUtil.setID(bpmnShape);

				fp.link(shape, new Object[] { elem, bpmnShape });
				break;
			}
		}

		return bpmnShape;
	}

	public static void updateDIShape(PictogramElement element) {
		BPMNShape bpmnShape = BusinessObjectUtil.getFirstElementOfType(element, BPMNShape.class);
		if (bpmnShape == null) {
			return;
		}

		ILocation loc = Graphiti.getLayoutService().getLocationRelativeToDiagram((Shape) element);
		Bounds bounds = bpmnShape.getBounds();

		bounds.setX(loc.getX());
		bounds.setY(loc.getY());

		GraphicsAlgorithm graphicsAlgorithm = element.getGraphicsAlgorithm();
		IDimension size = Graphiti.getGaService().calculateSize(graphicsAlgorithm);
		bounds.setHeight(size.getHeight());
		bounds.setWidth(size.getWidth());

		if (element instanceof ContainerShape) {
			EList<Shape> children = ((ContainerShape) element).getChildren();
			for (Shape shape : children) {
				if (shape instanceof ContainerShape) {
					updateDIShape(shape);
				}
			}
		}

		updateConnections(element);
	}
	
	private static void updateConnections(PictogramElement element) {
		if (element instanceof Shape) {
			EList<Anchor> anchors = ((Shape) element).getAnchors();
			
			for (Anchor anchor : anchors) {
				List<Connection> connections = Graphiti.getPeService().getAllConnections(anchor);
				for (Connection connection : connections){
					updateDIEdge(connection);
				}
				connections.size();
			}
			
			anchors.size();
		}
	}

	/**
	 * Creates a BPMNEdge if it does not already exist, and then links it to
	 * the given {@code BaseElement}.
	 *
	 * @param connection the connection
	 * @param elem the BaseElement
	 * @param bpmnEdge the BPMNEdge object. If null, a new one is created and
	 *            inserted into the BPMNDiagram.
	 * @return the BPMNEdge
	 */
	public static BPMNEdge createDIEdge(Connection connection, BaseElement elem, BPMNEdge bpmnEdge, IFeatureProvider fp) {
		if (bpmnEdge == null) {
			Diagram diagram = Graphiti.getPeService().getDiagramForPictogramElement(connection);
			EList<EObject> businessObjects = Graphiti.getLinkService().getLinkForPictogramElement(diagram)
					.getBusinessObjects();
			for (EObject eObject : businessObjects) {
				if (eObject instanceof BPMNDiagram) {
					BPMNDiagram bpmnDiagram = (BPMNDiagram) eObject;

					bpmnEdge = BpmnDiFactory.eINSTANCE.createBPMNEdge();
					bpmnEdge.setBpmnElement(elem);

					if (elem instanceof Association) {
						bpmnEdge.setSourceElement(DIUtils.findDiagramElement(
								((Association) elem).getSourceRef()));
						bpmnEdge.setTargetElement(DIUtils.findDiagramElement(
								((Association) elem).getTargetRef()));
					} else if (elem instanceof MessageFlow) {
						bpmnEdge.setSourceElement(DIUtils.findDiagramElement(
								(BaseElement) ((MessageFlow) elem).getSourceRef()));
						bpmnEdge.setTargetElement(DIUtils.findDiagramElement(
								(BaseElement) ((MessageFlow) elem).getTargetRef()));
					} else if (elem instanceof SequenceFlow) {
						bpmnEdge.setSourceElement(DIUtils.findDiagramElement(
								((SequenceFlow) elem).getSourceRef()));
						bpmnEdge.setTargetElement(DIUtils.findDiagramElement(
								((SequenceFlow) elem).getTargetRef()));
					}

					ILocation sourceLoc = Graphiti.getPeService().getLocationRelativeToDiagram(connection.getStart());
					ILocation targetLoc = Graphiti.getPeService().getLocationRelativeToDiagram(connection.getEnd());

					Point point = DcFactory.eINSTANCE.createPoint();
					point.setX(sourceLoc.getX());
					point.setY(sourceLoc.getY());
					bpmnEdge.getWaypoint().add(point);

					point = DcFactory.eINSTANCE.createPoint();
					point.setX(targetLoc.getX());
					point.setY(targetLoc.getY());
					bpmnEdge.getWaypoint().add(point);

					getOrCreateDILabel(connection, bpmnEdge);

					DIUtils.addDIElement(bpmnEdge, bpmnDiagram);
					ModelUtil.setID(bpmnEdge);
				}
			}
		}
		fp.link(connection, new Object[] { elem, bpmnEdge });
		return bpmnEdge;
	}

	public static void updateDIEdge(Connection connection) {
		ILayoutService layoutService = Graphiti.getLayoutService();
		EObject be = BusinessObjectUtil.getFirstElementOfType(connection, BaseElement.class);
		BPMNEdge edge = DIUtils.findBPMNEdge(be);
		if (edge!=null) {
			Point point = DcFactory.eINSTANCE.createPoint();

			List<Point> waypoint = edge.getWaypoint();
			waypoint.clear();

			ILocation loc;
			loc = layoutService.getLocationRelativeToDiagram(connection.getStart());
			point.setX(loc.getX());
			point.setY(loc.getY());
			waypoint.add(point);

			if (connection instanceof FreeFormConnection) {
				FreeFormConnection freeForm = (FreeFormConnection) connection;
				EList<org.eclipse.graphiti.mm.algorithms.styles.Point> bendpoints = freeForm.getBendpoints();
				for (org.eclipse.graphiti.mm.algorithms.styles.Point bp : bendpoints) {
					point = DcFactory.eINSTANCE.createPoint();
					point.setX(bp.getX());
					point.setY(bp.getY());
					waypoint.add(point);
				}
			}

			point = DcFactory.eINSTANCE.createPoint();
			loc = layoutService.getLocationRelativeToDiagram(connection.getEnd());
			point.setX(loc.getX());
			point.setY(loc.getY());
			waypoint.add(point);
		}
	}

	/**
	 * Add a DiagramElement to a BPMNDiagram container.
	 * 
	 * @param elem the Diagram Element to add
	 * @param bpmnDiagram the BPMNDiagram container to which it is added
	 */
	public static void addDIElement(DiagramElement elem, BPMNDiagram bpmnDiagram) {
		List<DiagramElement> elements = bpmnDiagram.getPlane().getPlaneElement();
		elements.add(elem);
	}
	
	public static BPMNLabel getOrCreateDILabel(PictogramElement pe, DiagramElement de) {
		BPMNLabel bpmnLabel = null;
		EStructuralFeature feature = de.eClass().getEStructuralFeature("label");
		if (feature!=null) {
			bpmnLabel = (BPMNLabel) de.eGet(feature);
			if (bpmnLabel==null) {
				bpmnLabel = BpmnDiFactory.eINSTANCE.createBPMNLabel();
				de.eSet(feature, bpmnLabel);
			}
		}
		return bpmnLabel;
	}
	
	public static void updateDILabel(PictogramElement pe, int x, int y, int w, int h) {
		DiagramElement de = BusinessObjectUtil.getFirstElementOfType(pe, BPMNShape.class);
		if (de==null) {
			de = BusinessObjectUtil.getFirstElementOfType(pe, BPMNEdge.class);
		}
		
		if (de!=null) {
			BPMNLabel bpmnLabel = getOrCreateDILabel(pe, de);
			if (w==0 && h==0) {
				bpmnLabel.setBounds(null);
			}
			else {
				Bounds bounds = bpmnLabel.getBounds();
				if (bounds==null)
					bounds = DcFactory.eINSTANCE.createBounds();
				bounds.setX((float)x);
				bounds.setY((float)y);
				bounds.setWidth((float)w);
				bounds.setHeight((float)h);
				bpmnLabel.setBounds(bounds);
			}
		}
	}
	
	public static DiagramElement findDiagramElement(List<BPMNDiagram> diagrams, BaseElement bpmnElement) {
		for (BPMNDiagram d : diagrams) {
			BPMNPlane plane = d.getPlane();
			List<DiagramElement> planeElements = plane.getPlaneElement();
			return findPlaneElement(planeElements, bpmnElement);
		}
		return null;
	}

	public static DiagramElement findPlaneElement(List<DiagramElement> planeElements, BaseElement bpmnElement) {
		for (DiagramElement de : planeElements) {
			if (de instanceof BPMNShape) {
				if (bpmnElement == ((BPMNShape)de).getBpmnElement())
					return de;
			}
			if (de instanceof BPMNEdge) {
				if (bpmnElement == ((BPMNEdge)de).getBpmnElement())
					return de;
			}
			else if (de instanceof BPMNPlane) {
				return findPlaneElement(((BPMNPlane)de).getPlaneElement(), bpmnElement);
			}
		}
		return null;
	}

	/**
	 * Return the Graphiti Diagram for the given BPMNDiagram. If one does not exist, create it.
	 * 
	 * @param editor
	 * @param bpmnDiagram
	 * @return
	 */
	public static Diagram getOrCreateDiagram(final IDiagramBehavior editor, final BPMNDiagram bpmnDiagram) {
		// do we need to create a new Diagram or is this already in the model?
		Diagram diagram = findDiagram(editor, bpmnDiagram);
		if (diagram!=null) {
			// already exists
			return diagram;
		}

		// create a new one
		IDiagramTypeProvider dtp = editor.getDiagramContainer().getDiagramTypeProvider();
		final Diagram newDiagram = createDiagram(bpmnDiagram.getName());
		final IFeatureProvider featureProvider = dtp.getFeatureProvider();
		final Resource resource = dtp.getDiagram().eResource();
		TransactionalEditingDomain domain = editor.getEditingDomain();
		domain.getCommandStack().execute(new RecordingCommand(domain) {
			protected void doExecute() {
				resource.getContents().add(newDiagram);
				newDiagram.setActive(true);
				featureProvider.link(newDiagram, bpmnDiagram);
			}
		});
		return newDiagram;
	}
	
	public static Diagram createDiagram(String diagramName) {
		final Diagram diagram = Graphiti.getPeCreateService().createDiagram("BPMN2", diagramName, true); //$NON-NLS-1$
		Bpmn2Preferences prefs = Bpmn2Preferences.getInstance();
		ShapeStyle ss = prefs.getShapeStyle(ShapeStyle.Category.GRID);
		diagram.setGridUnit(ss.getDefaultWidth());
		diagram.setVerticalGridUnit(ss.getDefaultHeight());
		diagram.setSnapToGrid(ss.getSnapToGrid());
		GraphicsAlgorithm ga = diagram.getGraphicsAlgorithm();
		IGaService gaService = Graphiti.getGaService();
		ga.setForeground(gaService.manageColor(diagram, ss.getShapeForeground()));
		ss = prefs.getShapeStyle(ShapeStyle.Category.CANVAS);
		ga.setBackground(gaService.manageColor(diagram, ss.getShapeBackground()));
		return diagram;
	}
	
	/**
	 * Find the Graphiti Diagram that corresponds to the given BPMNDiagram object.
	 * 
	 * @param editor
	 * @param bpmnDiagram
	 * @return
	 */
	public static Diagram findDiagram(final IDiagramBehavior editor, final BPMNDiagram bpmnDiagram) {
		ResourceSet resourceSet = editor.getEditingDomain().getResourceSet();
		if (resourceSet!=null) {
			return findDiagram(resourceSet, bpmnDiagram);
		}
		return null;
	}
	
	public static Diagram findDiagram(ResourceSet resourceSet, final BPMNDiagram bpmnDiagram) {
		if (resourceSet!=null) {
			for (Resource r : resourceSet.getResources()) {
				for (EObject o : r.getContents()) {
					if (o instanceof Diagram) {
						Diagram diagram = (Diagram)o;
						if (BusinessObjectUtil.getFirstElementOfType(diagram, BPMNDiagram.class) == bpmnDiagram) {
							return diagram;
						}
					}
				}
			}
		}
		return null;
	}
	
	public static void deleteDiagram(final IDiagramBehavior editor, final BPMNDiagram bpmnDiagram) {
		Diagram diagram = DIUtils.findDiagram(editor, bpmnDiagram);
		if (diagram!=null) {
			List<EObject> list = new ArrayList<EObject>();
			TreeIterator<EObject> iter = diagram.eAllContents();
			while (iter.hasNext()) {
				EObject o = iter.next();
				if (o instanceof PictogramLink) {
					((PictogramLink)o).getBusinessObjects().clear();
					if (!list.contains(o))
						list.add(o);
				}
				else if (o instanceof Color) {
					if (!list.contains(o))
						list.add(o);
				}
				else if (o instanceof Font) {
					if (!list.contains(o))
						list.add(o);
				}
				else if (o instanceof Style) {
					if (!list.contains(o))
						list.add(o);
				}
			}
			for (EObject o : list)
				EcoreUtil.delete(o);
			
			EcoreUtil.delete(diagram);
			EcoreUtil.delete(bpmnDiagram);
		}	
	}
	
	/**
	 * Find the BPMNDiagram in the editor's Resource Set that corresponds to the given BaseElement.
	 * The BaseElement is expected be some kind of container class such as a Process or SubProcess.
	 * 
	 * @param editor
	 * @param baseElement
	 * @return
	 */
	public static BPMNDiagram findBPMNDiagram(final BaseElement baseElement) {
		return findBPMNDiagram(baseElement, false);
	}
	
	/**
	 * Find the BPMNDiagram in the editor's Resource Set that references the given BaseElement.
	 * 
	 * If the parameter "contains" is TRUE, then the BaseElement's ancestor hierarchy is searched recursively.
	 * 
	 * The BaseElement may be either a container (i.e. Process, SubProcess, Participant, etc.) or
	 * a simple shape (Task, Gateway, etc.)
	 * 
	 * @param editor
	 * @param baseElement
	 * @param contains
	 * @return
	 */
	public static BPMNDiagram findBPMNDiagram(final BaseElement baseElement, boolean contains) {
		if (baseElement==null || baseElement.eResource()==null)
			return null;
		ResourceSet resourceSet = baseElement.eResource().getResourceSet();
		if (resourceSet==null)
			return null;
		for (Resource r : resourceSet.getResources()) {
			if (r instanceof Bpmn2Resource) {
				for (EObject o : r.getContents()) {
					if (o instanceof DocumentRoot) {
						DocumentRoot root = (DocumentRoot)o;
						Definitions defs = root.getDefinitions();
						BaseElement bpmnElement;
						for (BPMNDiagram d : defs.getDiagrams()) {
							BPMNDiagram bpmnDiagram = (BPMNDiagram)d;
							bpmnElement = bpmnDiagram.getPlane().getBpmnElement();
							if (bpmnElement == baseElement)
								return bpmnDiagram;
						}
						if (contains) {
							for (BPMNDiagram d : defs.getDiagrams()) {
								BPMNDiagram bpmnDiagram = (BPMNDiagram)d;
								for (DiagramElement de : bpmnDiagram.getPlane().getPlaneElement()) {
									if (de instanceof BPMNShape)
										bpmnElement = ((BPMNShape)de).getBpmnElement();
									else if (de instanceof BPMNEdge)
										bpmnElement = ((BPMNEdge)de).getBpmnElement();
									else
										continue;
									if (bpmnElement == baseElement)
										return bpmnDiagram;
								}
							}
							EObject parent = baseElement.eContainer();
							if (parent instanceof BaseElement && !(parent instanceof Definitions)) {
								BPMNDiagram bpmnDiagram = findBPMNDiagram((BaseElement)parent, true);
								if (bpmnDiagram!=null)
									return bpmnDiagram;
							}
						}
//						for (BPMNDiagram d : defs.getDiagrams()) {
//							BPMNDiagram bpmnDiagram = (BPMNDiagram)d;
//							bpmnElement = bpmnDiagram.getPlane().getBpmnElement();
//							if (bpmnElement instanceof Collaboration) {
//								Collaboration collaboration = (Collaboration)bpmnElement;
//								for (Participant p : collaboration.getParticipants()) {
//									if (baseElement==p)
//										return bpmnDiagram;
//									if (baseElement==p.getProcessRef())
//										return bpmnDiagram;
//								}
//							}
//						}
					}
				}
			}
		}
		return null;
	}
	
	public static BPMNDiagram createBPMNDiagram(Definitions definitions, BaseElement container) {
		
		Resource resource = definitions.eResource();
        BPMNDiagram bpmnDiagram = BpmnDiFactory.eINSTANCE.createBPMNDiagram();
		ModelUtil.setID(bpmnDiagram, resource);
        bpmnDiagram.setName(ExtendedPropertiesProvider.getTextValue(container));

		BPMNPlane plane = BpmnDiFactory.eINSTANCE.createBPMNPlane();
		ModelUtil.setID(plane, resource);
		plane.setBpmnElement(container);
		
		bpmnDiagram.setPlane(plane);
		
		// this has to happen last because the IResourceChangeListener in the DesignEditor
		// looks for add/remove to Definitions.diagrams
        definitions.getDiagrams().add(bpmnDiagram);

		return bpmnDiagram;
	}
	
	/**
	 * 
	 * @param baseElement
	 * @return
	 */
	public static BPMNShape findBPMNShape(BaseElement baseElement) {
		Definitions definitions = ModelUtil.getDefinitions(baseElement);
		if (definitions!=null) {
			for (BPMNDiagram d : definitions.getDiagrams()) {
				BPMNDiagram bpmnDiagram = (BPMNDiagram)d;
				BaseElement bpmnElement = null;
				for (DiagramElement de : bpmnDiagram.getPlane().getPlaneElement()) {
					if (de instanceof BPMNShape) {
						bpmnElement = ((BPMNShape)de).getBpmnElement();
						if (bpmnElement == baseElement)
							return (BPMNShape)de;
					}
				}
			}
		}
		return null;
	}
	
	public static BPMNEdge findBPMNEdge(EObject baseElement) {
		Definitions definitions = ModelUtil.getDefinitions(baseElement);
		if (definitions!=null) {
			for (BPMNDiagram d : definitions.getDiagrams()) {
				BPMNDiagram bpmnDiagram = (BPMNDiagram)d;
				BaseElement bpmnElement = null;
				for (DiagramElement de : bpmnDiagram.getPlane().getPlaneElement()) {
					if (de instanceof BPMNEdge) {
						bpmnElement = ((BPMNEdge)de).getBpmnElement();
						if (bpmnElement == baseElement)
							return (BPMNEdge)de;
					}
				}
			}
		}
		return null;
	}
	
	public static DiagramElement findDiagramElement(EObject object) {
		Definitions definitions = ModelUtil.getDefinitions(object);
		for (BPMNDiagram d : definitions.getDiagrams()) {
			BPMNDiagram bpmnDiagram = (BPMNDiagram)d;
			BaseElement bpmnElement = null;
			for (DiagramElement de : bpmnDiagram.getPlane().getPlaneElement()) {
				EStructuralFeature f = de.eClass().getEStructuralFeature("bpmnElement"); //$NON-NLS-1$
				if (f!=null) {
					bpmnElement = (BaseElement) de.eGet(f);
					if (bpmnElement == object)
						return de;
				}
			}
		}
		return null;
	}
	

	/**
	 * Returns the BPMNDiagram element that owns the given DiagramElement.
	 * 
	 * @param de
	 * @return
	 */
	public static BPMNDiagram getBPMNDiagram(DiagramElement de) {
		EObject container = de.eContainer();
		while (!(container instanceof Definitions)) {
			if (container instanceof BPMNDiagram)
				return (BPMNDiagram)container;
			container = container.eContainer();
		}
		return null;
	}
	
	public static boolean deleteContainerIfPossible(RootElement rootElement) {
		if (rootElement==null)
			return false;
		
		boolean canDelete = false;
		Definitions definitions = (Definitions)rootElement.eContainer();
		// check if this thing has already been deleted (eContainer == null)
		if (definitions!=null) {
			BPMNDiagram bpmnDiagram = DIUtils.findBPMNDiagram(rootElement);
			RootElement newRootElement = null;
			if (bpmnDiagram==null) {
				List<EObject> allObjects = new ArrayList<EObject>();
				Map<EObject, Collection<EStructuralFeature.Setting>> usages;
				TreeIterator<EObject> iter = definitions.eAllContents();
				while (iter.hasNext())
					allObjects.add(iter.next());
					
		        usages = UsageCrossReferencer.findAll(allObjects, rootElement);
				canDelete = usages.isEmpty();
			}
			else {
				// find a replacement for the BPMNDiagram target element
				for (RootElement r : definitions.getRootElements()) {
					if (r!=rootElement && (r instanceof FlowElementsContainer || r instanceof Collaboration)) {
						if (rootElement instanceof Collaboration) {
							Collaboration collaboration = (Collaboration) rootElement;
							if (collaboration.getParticipants().isEmpty()) {
								newRootElement = r;
								canDelete = true;
								break;
							}
								
						}
						else if (rootElement instanceof FlowElementsContainer) {
							FlowElementsContainer container = (FlowElementsContainer) rootElement;
							if (container.getFlowElements().isEmpty()) {
								newRootElement = r;
								canDelete = true;
								break;
							}
						}
						break;
					}
				}
			}
			
			if (canDelete) {
				EcoreUtil.delete(rootElement);
				if (bpmnDiagram!=null && newRootElement!=null)
					bpmnDiagram.getPlane().setBpmnElement(newRootElement);
			}
		}
		return canDelete;
	}
	
	public static void updateDiagramType(Diagram diagram) {
		BPMNDiagram bpmnDiagram = BusinessObjectUtil.getFirstElementOfType(diagram, BPMNDiagram.class);
		if (bpmnDiagram!=null && bpmnDiagram.getPlane()!=null) {
			BaseElement bpmnElement = bpmnDiagram.getPlane().getBpmnElement();
			if (bpmnElement==null) {
				// this BPMNDiagram has had its bpmnElement deleted.
				// make it point to the first valid RootElement.
				Definitions definitions = ModelUtil.getDefinitions(bpmnDiagram);
				for (RootElement r : definitions.getRootElements()) {
					if (r instanceof FlowElementsContainer || r instanceof Collaboration) {
						bpmnDiagram.getPlane().setBpmnElement(r);
						break;
					}
				}
			}
		}
	}

	/**
	 * Returns a list of all PictogramElements that reference the given BaseElement in all Graphiti Diagrams
	 * contained in all Resources of the given ResourceSet
	 * 
	 * @param resourceSet
	 * @param baseElement
	 * @return
	 */
	public static List<PictogramElement> getPictogramElements(ResourceSet resourceSet, BaseElement baseElement) {
		List<PictogramElement> elements = new ArrayList<PictogramElement>();
		for (Resource r : resourceSet.getResources()) {
			for (EObject o : r.getContents()) {
				if (o instanceof Diagram) {
					Diagram diagram = (Diagram)o;
					elements.addAll( Graphiti.getLinkService().getPictogramElements(diagram, baseElement) );
				}
			}
		}
		return elements;
	}
	
	/**
	 * Convenience method to return only the Graphiti ContainerShapes that reference the given BaseElement
	 * in all Diagrams of the given ResourceSet
	 * 
	 * @param resourceSet
	 * @param baseElement
	 * @return
	 */
	public static List<ContainerShape> getContainerShapes(ResourceSet resourceSet, BaseElement baseElement) {
		List<ContainerShape> shapes = new ArrayList<ContainerShape>();
		List<PictogramElement> pes = DIUtils.getPictogramElements(resourceSet, baseElement);
		for (PictogramElement pe : pes) {
			if (pe instanceof ContainerShape) {
				if (BusinessObjectUtil.getFirstElementOfType(pe, BPMNShape.class) != null)
					shapes.add((ContainerShape)pe);
			}
		}
		return shapes;
	}
	
	/**
	 * Convenience method to return only the Graphiti Connections that reference the given BaseElement
	 * in all Diagrams of the given ResourceSet
	 * 
	 * @param resourceSet
	 * @param baseElement
	 * @return
	 */
	public static List<Connection> getConnections(ResourceSet resourceSet, BaseElement baseElement) {
		List<Connection> connections = new ArrayList<Connection>();
		List<PictogramElement> pes = DIUtils.getPictogramElements(resourceSet, baseElement);
		for (PictogramElement pe : pes) {
			if (pe instanceof Connection) {
				if (BusinessObjectUtil.getFirstElementOfType(pe, BPMNEdge.class) != null)
					connections.add((Connection)pe);
			}
		}
		return connections;
	}
}

Back to the top