-
Notifications
You must be signed in to change notification settings - Fork 85
/
DiagramServices.java
2158 lines (1879 loc) · 80 KB
/
DiagramServices.java
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2006, 2023 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.core.sirius.analysis;
import static org.polarsys.capella.common.helpers.cache.ModelCache.getCache;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang.StringUtils;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramGraphicalViewer;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.sirius.business.api.query.EObjectQuery;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.business.api.session.SessionManager;
import org.eclipse.sirius.common.tools.api.interpreter.IInterpreter;
import org.eclipse.sirius.common.tools.api.util.RefreshIdsHolder;
import org.eclipse.sirius.common.ui.tools.api.util.EclipseUIUtil;
import org.eclipse.sirius.diagram.AbstractDNode;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.DDiagramElementContainer;
import org.eclipse.sirius.diagram.DEdge;
import org.eclipse.sirius.diagram.DNode;
import org.eclipse.sirius.diagram.DNodeContainer;
import org.eclipse.sirius.diagram.DNodeList;
import org.eclipse.sirius.diagram.DNodeListElement;
import org.eclipse.sirius.diagram.DSemanticDiagram;
import org.eclipse.sirius.diagram.DragAndDropTarget;
import org.eclipse.sirius.diagram.EdgeTarget;
import org.eclipse.sirius.diagram.business.api.componentization.DiagramMappingsManager;
import org.eclipse.sirius.diagram.business.api.componentization.DiagramMappingsManagerRegistry;
import org.eclipse.sirius.diagram.business.api.helper.graphicalfilters.HideFilterHelper;
import org.eclipse.sirius.diagram.business.api.query.AbstractNodeMappingQuery;
import org.eclipse.sirius.diagram.business.api.query.DDiagramElementQuery;
import org.eclipse.sirius.diagram.business.api.query.DDiagramQuery;
import org.eclipse.sirius.diagram.business.api.query.DiagramElementMappingQuery;
import org.eclipse.sirius.diagram.business.api.query.EdgeMappingQuery;
import org.eclipse.sirius.diagram.business.internal.helper.decoration.DecorationHelperInternal;
import org.eclipse.sirius.diagram.business.internal.metamodel.helper.EdgeMappingHelper;
import org.eclipse.sirius.diagram.business.internal.query.AbstractNodeMappingApplicabilityTester;
import org.eclipse.sirius.diagram.business.internal.sync.DDiagramElementSynchronizer;
import org.eclipse.sirius.diagram.business.internal.sync.DDiagramSynchronizer;
import org.eclipse.sirius.diagram.business.internal.sync.DEdgeCandidate;
import org.eclipse.sirius.diagram.business.internal.sync.DNodeCandidate;
import org.eclipse.sirius.diagram.description.AbstractNodeMapping;
import org.eclipse.sirius.diagram.description.ContainerMapping;
import org.eclipse.sirius.diagram.description.DiagramDescription;
import org.eclipse.sirius.diagram.description.DiagramElementMapping;
import org.eclipse.sirius.diagram.description.EdgeMapping;
import org.eclipse.sirius.diagram.description.EdgeMappingImport;
import org.eclipse.sirius.diagram.description.IEdgeMapping;
import org.eclipse.sirius.diagram.description.MappingBasedDecoration;
import org.eclipse.sirius.diagram.description.NodeMapping;
import org.eclipse.sirius.diagram.model.business.internal.helper.ContentHelper;
import org.eclipse.sirius.diagram.model.business.internal.helper.MappingHelper;
import org.eclipse.sirius.diagram.model.business.internal.operations.DDiagramSpecOperations;
import org.eclipse.sirius.diagram.ui.business.api.view.SiriusGMFHelper;
import org.eclipse.sirius.diagram.ui.edit.api.part.IDiagramNameEditPart;
import org.eclipse.sirius.diagram.ui.internal.edit.parts.DEdgeBeginNameEditPart;
import org.eclipse.sirius.diagram.ui.internal.edit.parts.DEdgeEndNameEditPart;
import org.eclipse.sirius.diagram.ui.tools.api.editor.DDiagramEditor;
import org.eclipse.sirius.diagram.ui.tools.api.part.IDiagramDialectGraphicalViewer;
import org.eclipse.sirius.ecore.extender.business.api.accessor.ModelAccessor;
import org.eclipse.sirius.tools.api.SiriusPlugin;
import org.eclipse.sirius.ui.business.api.dialect.DialectEditor;
import org.eclipse.sirius.viewpoint.DRepresentation;
import org.eclipse.sirius.viewpoint.DSemanticDecorator;
import org.eclipse.sirius.viewpoint.ViewpointPackage;
import org.eclipse.sirius.viewpoint.description.RepresentationDescription;
import org.eclipse.sirius.viewpoint.description.SemanticBasedDecoration;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.polarsys.capella.common.data.modellingcore.AbstractType;
import org.polarsys.capella.core.data.capellacore.CapellaElement;
import org.polarsys.capella.core.data.cs.Part;
import org.polarsys.capella.core.data.cs.PhysicalLink;
import org.polarsys.capella.core.data.cs.PhysicalLinkCategory;
import org.polarsys.capella.core.data.cs.PhysicalPath;
import org.polarsys.capella.core.data.fa.ExchangeCategory;
import org.polarsys.capella.core.data.fa.FunctionalChain;
import org.polarsys.capella.core.data.fa.FunctionalExchange;
import org.polarsys.capella.core.diagram.helpers.DiagramHelper;
import org.polarsys.capella.core.diagram.helpers.naming.DiagramNamingConstants;
import org.polarsys.capella.core.model.helpers.FunctionalChainExt;
import org.polarsys.capella.core.model.helpers.PhysicalPathExt;
import org.polarsys.capella.core.model.utils.CapellaLayerCheckingExt;
import com.google.common.collect.Lists;
/**
*/
@SuppressWarnings("restriction")
public class DiagramServices {
private static DiagramServices singleton = null;
public static final int OVERLLAPING_LABEL_MAX_LENGTH = 30;
public static DiagramServices getDiagramServices() {
if (singleton == null) {
singleton = new DiagramServices();
}
return singleton;
}
/**
* Return the EList of owned diagram elements of the given container
*
* @param container
* @return
*/
public EList<DDiagramElement> getOwnedDiagramElements(DragAndDropTarget container) {
if (container instanceof DDiagram) {
return ((DDiagram) container).getOwnedDiagramElements();
} else if (container instanceof DNodeContainer) {
return ((DNodeContainer) container).getOwnedDiagramElements();
}
return null;
}
/**
* Return the EList of owned diagram elements of the given container
*
* @param container
* @return
*/
public EList<DDiagramElement> getOwnedDiagramElements(EObject container) {
if (container instanceof DDiagram) {
return ((DDiagram) container).getOwnedDiagramElements();
} else if (container instanceof DNodeContainer) {
return ((DNodeContainer) container).getOwnedDiagramElements();
}
return null;
}
/**
* Return the EList of flat owned diagram elements of the given container
*
* @param container
* @return
*/
public List<DDiagramElement> getFlatOwnedDiagramElements(EObject container) {
List<DDiagramElement> result = new ArrayList<>();
getFlatOwnedDiagramElements(container, result);
return result;
}
private void getFlatOwnedDiagramElements(EObject container, List<DDiagramElement> acumulator) {
List<DDiagramElement> ownedDiagramElements = getOwnedDiagramElements(container);
acumulator.addAll(ownedDiagramElements);
for (DDiagramElement dDiagramElement : ownedDiagramElements) {
if (dDiagramElement instanceof DNodeContainer) {
getFlatOwnedDiagramElements(dDiagramElement, acumulator);
}
}
}
/**
* Returns owned Nodes from the given element
*/
public Collection<DDiagramElement> getOwnedNodes(DSemanticDecorator element) {
ArrayList<DDiagramElement> views = new ArrayList<DDiagramElement>();
if (element instanceof DDiagram) {
for (DDiagramElement view : ((DDiagram) element).getOwnedDiagramElements()) {
if (view instanceof DNode) {
views.add(view);
}
}
} else if (element instanceof DNodeContainer) {
for (DDiagramElement view : ((DNodeContainer) element).getOwnedDiagramElements()) {
if (view instanceof DNode) {
views.add(view);
}
}
} else if (element instanceof AbstractDNode) {
views.addAll(((AbstractDNode) element).getOwnedBorderedNodes());
}
return views;
}
/**
* Returns owned Nodes from the given element
*/
public Collection<DDiagramElement> getOwnedNodeListElements(DSemanticDecorator element) {
ArrayList<DDiagramElement> views = new ArrayList<DDiagramElement>();
if (element instanceof DDiagram) {
for (DDiagramElement view : ((DDiagram) element).getOwnedDiagramElements()) {
if (view instanceof DNodeListElement) {
views.add(view);
}
}
} else if (element instanceof DNodeList) {
views.addAll(((DNodeList) element).getOwnedElements());
}
return views;
}
/**
* Returns owned NodeContainers from the given element
*/
public Collection<DDiagramElement> getOwnedContainers(DSemanticDecorator element) {
ArrayList<DDiagramElement> views = new ArrayList<DDiagramElement>();
if (element instanceof DDiagram) {
for (DDiagramElement view : ((DDiagram) element).getOwnedDiagramElements()) {
if (view instanceof DDiagramElementContainer) {
views.add(view);
}
}
} else if (element instanceof DNodeContainer) {
for (DDiagramElement view : ((DNodeContainer) element).getOwnedDiagramElements()) {
if (view instanceof DDiagramElementContainer) {
views.add(view);
}
}
}
return views;
}
public Collection<DDiagramElement> getAllAbstractNodes(EObject element) {
return Lists.newArrayList(getAllAbstractNodes(element, true));
}
public Iterable<DDiagramElement> getAllAbstractNodes(EObject element, boolean borderedNode) {
return getDiagramElements(element, false, true, true, borderedNode);
}
public Iterable<AbstractDNode> getAllNodeContainers(EObject element) {
return (Iterable) getDiagramElements(element, false, false, true, false);
}
public Iterable<DDiagramElement> getAllBorderedNodes(DSemanticDecorator element) {
return getDiagramElements(element, false, false, false, true);
}
/**
* Returns owned NodeContainers from the given element
*/
public Collection<DDiagramElement> getOwnedAbstractNodes(DSemanticDecorator element) {
ArrayList<DDiagramElement> views = new ArrayList<DDiagramElement>();
if (element instanceof DDiagram) {
for (DDiagramElement view : ((DDiagram) element).getOwnedDiagramElements()) {
if (view instanceof AbstractDNode) {
views.add(view);
}
}
} else if (element instanceof DNodeContainer) {
for (DDiagramElement view : ((DNodeContainer) element).getOwnedDiagramElements()) {
if (view instanceof AbstractDNode) {
views.add(view);
}
}
}
if (element instanceof AbstractDNode)
{
views.addAll(((AbstractDNode) element).getOwnedBorderedNodes());
}
return views;
}
/**
* @deprecated Use getMappingByName instead
*/
@Deprecated
public DiagramElementMapping getMapping(String eClass, DDiagram diagram) {
return getMappingByName(diagram.getDescription(), eClass);
}
public List<NodeMapping> getAllBorderedNodeMapping(AbstractNodeMapping mapping) {
List<NodeMapping> returnedList = new ArrayList<NodeMapping>();
returnedList.addAll(mapping.getBorderedNodeMappings());
if (mapping instanceof ContainerMapping) {
ContainerMapping currentContainerMapping = (ContainerMapping) mapping;
for (ContainerMapping aMapping : currentContainerMapping.getSubContainerMappings()) {
returnedList.addAll(getAllBorderedNodeMapping(aMapping));
}
for (NodeMapping aMapping : currentContainerMapping.getSubNodeMappings()) {
returnedList.addAll(getAllBorderedNodeMapping(aMapping));
}
}
return returnedList;
}
public List<NodeMapping> getAllNodeMappings(ContainerMapping mapping) {
List<NodeMapping> returnedList = new ArrayList<NodeMapping>();
returnedList.addAll(mapping.getSubNodeMappings());
for (ContainerMapping aMapping : mapping.getSubContainerMappings()) {
returnedList.addAll(getAllNodeMappings(aMapping));
}
return returnedList;
}
public List<ContainerMapping> getAllContainerMappings(ContainerMapping mapping) {
List<ContainerMapping> returnedList = new ArrayList<ContainerMapping>();
returnedList.add(mapping);
for (ContainerMapping aMapping : mapping.getSubContainerMappings()) {
returnedList.addAll(getAllContainerMappings(aMapping));
}
return returnedList;
}
public AbstractNodeMapping getAbstractNodeMapping(final DiagramDescription description, String mappingName) {
for (NodeMapping nodeMapping : ContentHelper.getAllNodeMappings(description, false)) {
if (nodeMapping.getName().equals(mappingName)) {
return nodeMapping;
}
for (NodeMapping borderedMapping : MappingHelper.getAllBorderedNodeMappings(nodeMapping)) {
if (borderedMapping.getName().equals(mappingName)) {
return borderedMapping;
}
}
}
for (ContainerMapping nodeMapping : ContentHelper.getAllContainerMappings(description, false)) {
if (nodeMapping.getName().equals(mappingName)) {
return nodeMapping;
}
for (DiagramElementMapping mapping : MappingHelper.getAllMappings(nodeMapping)) {
if ((mapping instanceof AbstractNodeMapping)) {
if (mapping.getName().equals(mappingName)) {
return (AbstractNodeMapping) mapping;
}
for (NodeMapping borderedMapping : MappingHelper.getAllBorderedNodeMappings((AbstractNodeMapping) mapping)) {
if (borderedMapping.getName().equals(mappingName)) {
return borderedMapping;
}
}
}
}
}
return null;
}
public AbstractNodeMapping getAbstractNodeMapping(final DDiagram diagram, String mappingName) {
final DiagramDescription description = diagram.getDescription();
return getAbstractNodeMapping(description, mappingName);
}
public NodeMapping getNodeMapping(final DDiagram diagram, String mappingName) {
final DiagramDescription description = diagram.getDescription();
for (final NodeMapping nodeMapping : ContentHelper.getAllNodeMappings(description, false)) {
if (nodeMapping.getName().equals(mappingName)) {
return nodeMapping;
}
}
for (ContainerMapping aMapping : ContentHelper.getAllContainerMappings(description, false)) {
for (NodeMapping aNodeMapping : getAllNodeMappings(aMapping)) {
if (aNodeMapping.getName().equals(mappingName)) {
return aNodeMapping;
}
}
}
return null;
}
public List<NodeMapping> getBorderedNodeMapping(final DDiagram diagram, List<String> mappingNames) {
List<NodeMapping> result = new ArrayList<NodeMapping>();
for (String name : mappingNames) {
result.add(getBorderedNodeMapping(diagram, name));
}
return result;
}
public NodeMapping getBorderedNodeMapping(final DDiagram diagram, String mappingName) {
final DiagramDescription description = diagram.getDescription();
for (final NodeMapping nodeMapping : ContentHelper.getAllNodeMappings(description, false)) {
for (NodeMapping aBorderedNodeMapping : getAllBorderedNodeMapping(nodeMapping)) {
if (aBorderedNodeMapping.getName().equals(mappingName)) {
return aBorderedNodeMapping;
}
}
}
for (ContainerMapping aMapping : ContentHelper.getAllContainerMappings(description, false)) {
for (NodeMapping aBorderedNodeMapping : getAllBorderedNodeMapping(aMapping)) {
if (aBorderedNodeMapping.getName().equals(mappingName)) {
return aBorderedNodeMapping;
}
}
}
return null;
}
public ContainerMapping getContainerMapping(final DDiagram diagram, String mappingName) {
final DiagramDescription description = diagram.getDescription();
for (ContainerMapping aContainerMapping : ContentHelper.getAllContainerMappings(description, false)) {
for (ContainerMapping aSubContainerMapping : getAllContainerMappings(aContainerMapping)) {
if (aSubContainerMapping.getName().equals(mappingName)) {
return aSubContainerMapping;
}
}
}
return null;
}
public EdgeMapping getEdgeMapping(final DiagramDescription description, String mappingName) {
for (final EdgeMapping edgeMapping : ContentHelper.getAllEdgeMappings(description, false)) {
if (edgeMapping.getName().equals(mappingName)) {
return edgeMapping;
}
}
return null;
}
public EdgeMapping getEdgeMapping(final DDiagram diagram, String mappingName) {
final DiagramDescription description = diagram.getDescription();
return getEdgeMapping(description, mappingName);
}
public DNode createNode(NodeMapping mapping, EObject modelElement, DragAndDropTarget container, DDiagram diagram) {
return (DNode) createAbstractDNode(mapping, modelElement, container, diagram);
}
public DNode createBorderedNode(NodeMapping mapping, EObject modelElement, DragAndDropTarget container,
DDiagram diagram) {
final DDiagram diag = diagram;
ModelAccessor accessor = SiriusPlugin.getDefault().getModelAccessorRegistry().getModelAccessor(modelElement);
IInterpreter interpreter = SiriusPlugin.getDefault().getInterpreterRegistry().getInterpreter(modelElement);
final DDiagramSynchronizer diagramSync = new DDiagramSynchronizer(interpreter, diag.getDescription(), accessor);
diagramSync.setDiagram((DSemanticDiagram) diagram);
final DDiagramElementSynchronizer elementSync = diagramSync.getElementSynchronizer();
RefreshIdsHolder rId = RefreshIdsHolder.getOrCreateHolder(diagram);
DNodeCandidate nodeCandidate = new DNodeCandidate(mapping, modelElement, container, rId);
return (DNode) elementSync.createNewNode(getMappingManager((DSemanticDiagram) diag), nodeCandidate, true);
}
public AbstractDNode createDNodeListElement(AbstractNodeMapping mapping, EObject modelElement,
DragAndDropTarget container, DDiagram diagram) {
return createDNodeListElement(mapping, modelElement, container, diagram, -1);
}
public AbstractDNode createDNodeListElement(AbstractNodeMapping mapping, EObject modelElement,
DragAndDropTarget container, DDiagram diagram, int index) {
final DDiagram diag = diagram;
ModelAccessor accessor = SiriusPlugin.getDefault().getModelAccessorRegistry().getModelAccessor(modelElement);
IInterpreter interpreter = SiriusPlugin.getDefault().getInterpreterRegistry().getInterpreter(modelElement);
final DDiagramSynchronizer diagramSync = new DDiagramSynchronizer(interpreter, diag.getDescription(), accessor);
diagramSync.setDiagram((DSemanticDiagram) diagram);
final DDiagramElementSynchronizer elementSync = diagramSync.getElementSynchronizer();
RefreshIdsHolder rId = RefreshIdsHolder.getOrCreateHolder(diagram);
DNodeCandidate nodeCandidate = new DNodeCandidate(mapping, modelElement, container, rId);
return elementSync.createNewNode(getMappingManager((DSemanticDiagram) diag), nodeCandidate, false, index);
}
public DNodeContainer createContainer(ContainerMapping mapping, EObject modelElement, DragAndDropTarget container,
DDiagram diagram, int index) {
final DDiagram diag = diagram;
ModelAccessor accessor = SiriusPlugin.getDefault().getModelAccessorRegistry().getModelAccessor(modelElement);
IInterpreter interpreter = SiriusPlugin.getDefault().getInterpreterRegistry().getInterpreter(modelElement);
final DDiagramSynchronizer diagramSync = new DDiagramSynchronizer(interpreter, diag.getDescription(), accessor);
diagramSync.setDiagram((DSemanticDiagram) diagram);
final DDiagramElementSynchronizer elementSync = diagramSync.getElementSynchronizer();
RefreshIdsHolder rId = RefreshIdsHolder.getOrCreateHolder(diagram);
DNodeCandidate nodeCandidate = new DNodeCandidate(mapping, modelElement, container, rId);
return (DNodeContainer) elementSync.createNewNode(getMappingManager((DSemanticDiagram) diag), nodeCandidate, false,
index);
}
public DNodeContainer createContainer(ContainerMapping mapping, EObject modelElement, DragAndDropTarget container,
DDiagram diagram) {
return createContainer(mapping, modelElement, container, diagram, -1);
}
@Deprecated
public AbstractDNode createAbstractDNodeContainer(AbstractNodeMapping mapping, EObject modelElement,
DragAndDropTarget container, DDiagram diagram) {
return createAbstractDNode(mapping, modelElement, container, diagram);
}
public boolean isBorderedNodeMapping(DiagramElementMapping mapping) {
return (mapping != null)
&& org.eclipse.sirius.diagram.description.DescriptionPackage.Literals.ABSTRACT_NODE_MAPPING__BORDERED_NODE_MAPPINGS
.equals(mapping.eContainingFeature());
}
/**
* Evaluate precondition of the given edge mapping.
*/
public boolean evaluateEdgePrecondition(EdgeMapping edgeMapping, DDiagram diagram, EObject semantic,
DSemanticDecorator sourceView, DSemanticDecorator targetView) {
IInterpreter interpreter = SiriusPlugin.getDefault().getInterpreterRegistry().getInterpreter(semantic);
return new EdgeMappingQuery(edgeMapping).evaluatePrecondition((DSemanticDiagram) diagram,
(DragAndDropTarget) diagram, interpreter, semantic, sourceView, targetView);
}
/**
* Evaluate precondition of the given node mapping.
*/
public boolean evaluateNodePrecondition(AbstractNodeMapping nodeMapping, DDiagram diagram,
DSemanticDecorator containerView, EObject semantic) {
IInterpreter interpreter = SiriusPlugin.getDefault().getInterpreterRegistry().getInterpreter(semantic);
return new AbstractNodeMappingQuery(nodeMapping).evaluatePrecondition((DSemanticDiagram) diagram,
(DragAndDropTarget) containerView, interpreter, semantic);
}
/**
* Evaluate candidate expression of the given node mapping.
*/
public Collection<EObject> evaluateCandidateExpression(AbstractNodeMapping nodeMapping, DDiagram diagram,
DSemanticDecorator containerView, EObject semantic) {
IInterpreter interpreter = SiriusPlugin.getDefault().getInterpreterRegistry().getInterpreter(semantic);
return new AbstractNodeMappingQuery(nodeMapping).evaluateCandidateExpression((DSemanticDiagram) diagram,
interpreter, (DragAndDropTarget) containerView);
}
public AbstractDNode createAbstractDNode(AbstractNodeMapping mapping, EObject modelElement,
DragAndDropTarget container, DDiagram diagram) {
final DDiagram diag = diagram;
if (mapping == null) {
return null;
}
ModelAccessor accessor = SiriusPlugin.getDefault().getModelAccessorRegistry().getModelAccessor(modelElement);
IInterpreter interpreter = SiriusPlugin.getDefault().getInterpreterRegistry().getInterpreter(modelElement);
final DDiagramSynchronizer diagramSync = new DDiagramSynchronizer(interpreter, diag.getDescription(), accessor);
diagramSync.setDiagram((DSemanticDiagram) diagram);
final DDiagramElementSynchronizer elementSync = diagramSync.getElementSynchronizer();
RefreshIdsHolder rId = RefreshIdsHolder.getOrCreateHolder(diagram);
DNodeCandidate nodeCandidate = new DNodeCandidate(mapping, modelElement, container, rId);
return elementSync.createNewNode(getMappingManager((DSemanticDiagram) diag), nodeCandidate,
isBorderedNodeMapping(mapping));
}
public DEdge createEdge(EdgeMapping mapping, EdgeTarget sourceView, EdgeTarget targetView, EObject semanticObject) {
ModelAccessor accessor = SiriusPlugin.getDefault().getModelAccessorRegistry().getModelAccessor(semanticObject);
IInterpreter interpreter = SiriusPlugin.getDefault().getInterpreterRegistry().getInterpreter(semanticObject);
if (mapping == null) {
return null;
}
if ((sourceView == null) || (targetView == null)) {
return null;
}
final DDiagram diagram = CapellaServices.getService().getDiagramContainer(sourceView);
RefreshIdsHolder rId = RefreshIdsHolder.getOrCreateHolder(diagram);
DEdgeCandidate edgeCandidate = new DEdgeCandidate(mapping, semanticObject, sourceView, targetView, rId);
final DDiagramSynchronizer diagramSync = new DDiagramSynchronizer(interpreter, diagram.getDescription(), accessor);
diagramSync.setDiagram((DSemanticDiagram) diagram);
final DDiagramElementSynchronizer elementSync = diagramSync.getElementSynchronizer();
/* maps for decorations */
final Map<EdgeMapping, Collection<MappingBasedDecoration>> edgeToMappingBasedDecoration = new HashMap<EdgeMapping, Collection<MappingBasedDecoration>>();
final Map<String, Collection<SemanticBasedDecoration>> edgeToSemanticBasedDecoration = new HashMap<String, Collection<SemanticBasedDecoration>>();
/* create the mapping to edge targets map */
final Map<DiagramElementMapping, Collection<EdgeTarget>> mappingsToEdgeTargets = new HashMap<DiagramElementMapping, Collection<EdgeTarget>>();
DDiagramElement sourceElement = null;
DDiagramElement targetElement = null;
DiagramElementMapping sourceMapping = null;
DiagramElementMapping targetMapping = null;
if (sourceView instanceof DDiagramElement) {
sourceElement = (DDiagramElement) sourceView;
sourceMapping = sourceElement.getDiagramElementMapping();
}
if (targetView instanceof DDiagramElement) {
targetElement = (DDiagramElement) targetView;
targetMapping = targetElement.getDiagramElementMapping();
}
if (sourceMapping != null) {
mappingsToEdgeTargets.put(sourceMapping, new ArrayList<EdgeTarget>());
}
if ((targetMapping != null) && !targetMapping.equals(sourceMapping)) {
mappingsToEdgeTargets.put(targetMapping, new ArrayList<EdgeTarget>());
}
if (sourceMapping != null) {
mappingsToEdgeTargets.get(sourceMapping).add(sourceView);
}
if ((targetMapping != null) && !sourceView.equals(targetView)) {
mappingsToEdgeTargets.get(targetMapping).add(targetView);
}
new DecorationHelperInternal(diagram, interpreter, accessor).computeDecorations(mappingsToEdgeTargets,
edgeToSemanticBasedDecoration, edgeToMappingBasedDecoration);
return elementSync.createNewEdge(getMappingManager((DSemanticDiagram) diagram), edgeCandidate,
mappingsToEdgeTargets, edgeToMappingBasedDecoration, edgeToSemanticBasedDecoration);
}
public boolean isHiddenLabel(DDiagramElement context) {
return new DDiagramElementQuery(context).isLabelHidden();
}
public EObject hideLabel(DDiagramElement context) {
HideFilterHelper.INSTANCE.hideLabel(context);
return context;
}
public boolean isHidden(DDiagramElement context) {
DDiagramElementQuery query = new DDiagramElementQuery(context);
return query.isHidden();
}
public boolean isFiltered(DDiagramElement context) {
DDiagramElementQuery query = new DDiagramElementQuery(context);
return query.isFiltered() || query.isIndirectlyFiltered();
}
public EObject hide(DDiagramElement context) {
HideFilterHelper.INSTANCE.hide(context);
return context;
}
/**
* Checks if an element is onto diagram
*/
public boolean isVisible(DDiagramElement edge) {
return !isHidden(edge);
}
/**
* Checks if an element is onto diagram
*/
public boolean isOnDiagram(DNodeContainer diagramElement, EObject semanticTarget) {
for (DDiagramElement element : diagramElement.getContainers()) {
if (element.getTarget() == semanticTarget) {
return true;
}
}
return false;
}
/**
* Checks if an element is directly displayed on a diagram, or indirectly through the type of a Part.
*
* @param diagramElement
* the diagram element
* @param semanticTarget
* the semantic target
* @return true if an element is directly displayed on a diagram, or indirectly through the type of a Part, false
* otherwise.
*/
public boolean isIndirectlyOnDiagram(DNodeContainer diagramElement, EObject semanticTarget) {
for (DDiagramElement element : diagramElement.getContainers()) {
EObject elementTarget = element.getTarget();
if (elementTarget != null && elementTarget.equals(semanticTarget)) {
return true;
}
if (elementTarget instanceof Part) {
AbstractType partType = ((Part) elementTarget).getAbstractType();
if (partType != null && partType.equals(semanticTarget)) {
return true;
}
}
}
return false;
}
/**
* Checks if an element is on the current diagram.
*
* @param diagramTarget
* the diagram target
* @param semanticTarget
* the semantic target
* @return true if an element is on the current diagram, false otherwise.
*/
public boolean isOnDiagram(DDiagram diagramTarget, EObject semanticTarget) {
return getDiagramElement(diagramTarget, semanticTarget) != null;
}
/**
* @return true if the current settings contains a 'target' feature.
*/
protected Predicate<Setting> isValidTargetFeature() {
return setting -> ViewpointPackage.Literals.DSEMANTIC_DECORATOR__TARGET.equals(setting.getEStructuralFeature())
|| ViewpointPackage.Literals.DREPRESENTATION_ELEMENT__SEMANTIC_ELEMENTS.equals(setting.getEStructuralFeature());
}
/**
* Return whether the given node mapping can be created into the given container
*/
public boolean isValidMapping(AbstractNodeMapping mapping_p, DSemanticDecorator container) {
AbstractNodeMappingApplicabilityTester tester = new AbstractNodeMappingApplicabilityTester(
Arrays.asList(mapping_p));
if (container instanceof DDiagram) {
return tester.canCreateIn((DDiagram) container);
} else if (container instanceof DDiagramElementContainer) {
return tester.canCreateIn((DDiagramElementContainer) container);
} else if (container instanceof DNode) {
return tester.canCreateIn((DNode) container);
}
return true;
}
/**
* Returns a stream containing elements for the diagram target that have the specified semantic target.
*
* @param diagramTarget
* the diagram target
* @param semanticTarget
* the semantic target
* @return a stream containing elements for the diagram target that have the specified semantic target.
*/
protected Stream<DDiagramElement> getDiagramElementsStream(DRepresentation diagramTarget, EObject semanticTarget) {
Session session = SessionManager.INSTANCE.getSession(semanticTarget);
return session.getSemanticCrossReferencer().getInverseReferences(semanticTarget).stream()
.filter(isValidTargetFeature()).filter(setting -> setting.getEObject() instanceof DDiagramElement)
.map(setting -> (DDiagramElement) setting.getEObject())
.filter(decorator -> diagramTarget.equals(DiagramHelper.getService().getRepresentation(decorator)));
}
/**
* Returns the element on the diagram target that has the semantic target.
*
* @param diagramTarget
* the diagram target
* @param semanticTarget
* the semantic target
* @return the element on the diagram target that has the semantic target.
*/
public DDiagramElement getDiagramElement(DDiagram diagramTarget, EObject semanticTarget) {
return getDiagramElementsStream(diagramTarget, semanticTarget).findFirst().orElse(null);
}
/**
* Returns the elements on the diagram target that have the specified semantic target.
*
* @param diagramTarget
* the diagram target
* @param semanticTarget
* the semantic target
* @return the elements on the diagram target that have the specified semantic target.
*/
public Collection<DSemanticDecorator> getDiagramElements(DRepresentation diagramTarget, EObject semanticTarget) {
return getDiagramElementsStream(diagramTarget, semanticTarget).collect(Collectors.toSet());
}
public Iterable<DDiagramElement> getDiagramElements(DDiagram diagram, DiagramElementMapping mapping) {
boolean isEdgeMapping = mapping instanceof EdgeMapping || mapping instanceof EdgeMappingImport;
final DiagramIterator iterator = new DiagramIterator(diagram, mapping, isEdgeMapping, !isEdgeMapping,
!isEdgeMapping, !isEdgeMapping);
return new Iterable<DDiagramElement>() {
@Override
public Iterator<DDiagramElement> iterator() {
return iterator;
}
};
}
// TODO Optimize
public Iterable<DDiagramElement> getDiagramElements(DDiagramElement container, DiagramElementMapping mapping) {
boolean isEdgeMapping = mapping instanceof EdgeMapping || mapping instanceof EdgeMappingImport;
final DiagramIterator iterator = new DiagramIterator(container, mapping, isEdgeMapping, !isEdgeMapping,
!isEdgeMapping, !isEdgeMapping);
return new Iterable<DDiagramElement>() {
@Override
public Iterator<DDiagramElement> iterator() {
return iterator;
}
};
}
/**
* Returns the first element on the diagram target that has the specified semantic and mapping target.
*
* @param diagramTarget
* the diagram target
* @param mappingTarget
* the mapping target
* @param semanticTarget
* the semantic target
* @return the first element on the diagram target that has the specified semantic and mapping target.
*/
public DDiagramElement getDiagramElement(DDiagram diagramTarget, DiagramElementMapping mappingTarget,
EObject semanticTarget) {
return getDiagramElementsStream(diagramTarget, semanticTarget).filter(DDiagramElement.class::isInstance)
.map(DDiagramElement.class::cast).filter(element -> element.getMapping().equals(mappingTarget)).findFirst()
.orElse(null);
}
/**
* Returns the elements on the diagram target that have the specified semantic and mapping target.
*
* @param diagramTarget
* the diagram target
* @param mappingTarget
* the mapping target
* @param semanticTarget
* the semantic target
* @return the elements on the diagram target that have the specified semantic and mapping target.
*/
public Collection<DDiagramElement> getDiagramElements(DDiagram diagramTarget, DiagramElementMapping mappingTarget,
EObject semanticTarget) {
return getDiagramElementsStream(diagramTarget, semanticTarget).filter(DDiagramElement.class::isInstance)
.map(DDiagramElement.class::cast).filter(element -> element.getMapping().equals(mappingTarget))
.collect(Collectors.toSet());
}
/**
* An optimized version of diagram.getDiagramElements()
*/
@Deprecated
public Iterable<DDiagramElement> getDiagramElements(EObject context) {
final DiagramIterator iterator = new DiagramIterator(context);
return new Iterable<DDiagramElement>() {
@Override
public Iterator<DDiagramElement> iterator() {
return iterator;
}
};
}
@Deprecated
public Iterable<DDiagramElement> getDiagramElements(EObject context, boolean edges, boolean nodes, boolean containers,
boolean borderedNodes) {
final DiagramIterator iterator = new DiagramIterator(context, edges, nodes, containers, borderedNodes);
return new Iterable<DDiagramElement>() {
@Override
public Iterator<DDiagramElement> iterator() {
return iterator;
}
};
}
/**
* Checks if an element is onto diagram
*/
public boolean isEdgeOnDiagram(EdgeTarget sourceView, EdgeTarget targetView, EObject semantic) {
return getEdgeOnDiagram(sourceView, targetView, semantic) != null;
}
/**
* Checks if an element is onto diagram
*/
public DEdge getEdgeOnDiagram(EdgeTarget sourceView, EdgeTarget targetView, EObject semantic) {
if (sourceView != null) {
if (targetView != null) {
DDiagram diagram = CapellaServices.getService().getDiagramContainer(sourceView);
for (DEdge edge : (sourceView).getOutgoingEdges()) {
if ((diagram != null) && diagram.getEdges().contains(edge)) {
if ((edge.getTarget() != null) && targetView.equals(edge.getTargetNode())
&& edge.getTarget().equals(semantic)) {
return edge;
}
}
}
}
}
return null;
}
/**
* remove a Node view
*
* @param node
* a node
*/
public void removeNodeView(DNode node) {
EObject container = node.eContainer();
if (container != null) {
if (container instanceof DDiagram) {
((DDiagram) container).getOwnedDiagramElements().remove(node);
}
if (container instanceof DNodeContainer) {
DNodeContainer nodeContainer = (DNodeContainer) container;
if (nodeContainer.getOwnedDiagramElements().contains(node)) {
nodeContainer.getOwnedDiagramElements().remove(node);
}
if (nodeContainer.getOwnedBorderedNodes().contains(node)) {
nodeContainer.getOwnedBorderedNodes().remove(node);
}
}
if (container instanceof DNode) {
((DNode) container).getOwnedBorderedNodes().remove(node);
}
}
}
public void removeNodeListElementView(AbstractDNode node) {
EObject container = node.eContainer();
if ((container != null) && (container instanceof DNodeList)) {
((DNodeList) container).getOwnedElements().remove(node);
}
}
public void removeAbstractDNodeView(AbstractDNode node) {
EObject container = node.eContainer();
if (container != null) {
if (container instanceof DDiagram) {
((DDiagram) container).getOwnedDiagramElements().remove(node);
} else if (container instanceof DNodeContainer) {
DNodeContainer nodeContainer = (DNodeContainer) container;
if (nodeContainer.getOwnedDiagramElements().contains(node)) {
nodeContainer.getOwnedDiagramElements().remove(node);
}
if (nodeContainer.getOwnedBorderedNodes().contains(node)) {
nodeContainer.getOwnedBorderedNodes().remove(node);
}
} else if (container instanceof DNode) {
((DNode) container).getOwnedBorderedNodes().remove(node);
} else if (container instanceof DNodeList) {
((DNodeList) container).getOwnedElements().remove(node);
}
}
}
/**
* remove a container View
*
* @param container
* a container
*/
public void removeContainerView(EObject container) {
EObject owner = container.eContainer();
if (owner != null) {
if (owner instanceof DDiagram) {
((DDiagram) owner).getOwnedDiagramElements().remove(container);
}
if (owner instanceof DNodeContainer) {
DNodeContainer nodeContainer = (DNodeContainer) owner;
if (nodeContainer.getOwnedDiagramElements().contains(container)) {
nodeContainer.getOwnedDiagramElements().remove(container);
}
}
}
}
/**
* remove an edge from a diagram
*
* @param anEdge
* the edge to remove from diagram