-
Notifications
You must be signed in to change notification settings - Fork 85
/
ShowHideABComponent.java
269 lines (229 loc) · 11 KB
/
ShowHideABComponent.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
/*******************************************************************************
* Copyright (c) 2006, 2020 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.showhide;
import static org.polarsys.capella.common.helpers.cache.ModelCache.getCache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.StreamSupport;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.description.DiagramElementMapping;
import org.eclipse.sirius.viewpoint.DSemanticDecorator;
import org.polarsys.capella.common.helpers.EcoreUtil2;
import org.polarsys.capella.core.data.cs.BlockArchitecture;
import org.polarsys.capella.core.data.cs.Component;
import org.polarsys.capella.core.data.cs.CsPackage;
import org.polarsys.capella.core.data.cs.DeploymentTarget;
import org.polarsys.capella.core.data.cs.Part;
import org.polarsys.capella.core.data.oa.Entity;
import org.polarsys.capella.core.data.pa.PhysicalComponent;
import org.polarsys.capella.core.data.pa.PhysicalComponentNature;
import org.polarsys.capella.core.model.helpers.BlockArchitectureExt;
import org.polarsys.capella.core.model.helpers.ComponentExt;
import org.polarsys.capella.core.model.helpers.PartExt;
import org.polarsys.capella.core.sirius.analysis.CsServices;
import org.polarsys.capella.core.sirius.analysis.DDiagramContents;
import org.polarsys.capella.core.sirius.analysis.constants.MappingConstantsHelper;
import org.polarsys.capella.core.sirius.analysis.tool.HashMapSet;
/**
* A ShowHide definition for ABCategory containers of category pins must be set with sourceParts and targetParts
* variables
*/
public class ShowHideABComponent extends AbstractShowHide {
public static final String SOURCE_PARTS = "sourceParts"; //$NON-NLS-1$
public static final String TARGET_PARTS = "targetParts"; //$NON-NLS-1$
boolean containsNodeDeployment = true; //yellow
boolean containsBehavioralDeployment = true; //blue
/**
* @param content_p
*/
public ShowHideABComponent(DDiagramContents content_p) {
super(content_p);
DiagramElementMapping mapping = getContent()
.getMapping(MappingConstantsHelper.getMappingABDeployedElement(getContent().getDDiagram()));
containsNodeDeployment = StreamSupport.stream(getContent().getDiagramElements(mapping).spliterator(), false)
.anyMatch(x -> getNature(x.getTarget()) == PhysicalComponentNature.NODE);
containsBehavioralDeployment = StreamSupport.stream(getContent().getDiagramElements(mapping).spliterator(), false)
.anyMatch(x -> getNature(x.getTarget()) == PhysicalComponentNature.BEHAVIOR);
}
/**
* {@inheritDoc}
*/
@Override
public HashMapSet<String, EObject> getRelatedObjects(EObject semantic_p, DiagramContext context_p) {
HashMapSet<String, EObject> value = super.getRelatedObjects(semantic_p, context_p);
ContextItemElement lastContext = context_p.getLast();
Collection<EObject> result = new ArrayList<EObject>();
BlockArchitecture blockArchitecture = BlockArchitectureExt.getRootBlockArchitecture(semantic_p);
Component systemComponent = blockArchitecture.getSystem();
if (lastContext.getValue() instanceof Entity) {
Entity entity = (Entity) lastContext.getValue();
Collection<Part> parts = getCache(ComponentExt::getRepresentingParts, entity);
if (parts.size() > 0) {
EObject targetContainer = EcoreUtil2.getFirstContainer(parts.iterator().next(), CsPackage.Literals.COMPONENT);
if (targetContainer instanceof Entity) {
result.add(targetContainer);
// Remove all parts of root component
if (systemComponent != null) {
result.remove(systemComponent);
}
}
}
}
if (lastContext.getValue() instanceof Part) {
Part part = (Part) lastContext.getValue();
// Retrieve all parts containing the given part
Collection<EObject> result3 = new HashSet<EObject>();
if (containsNodeDeployment || containsBehavioralDeployment) {
result3.addAll(getCache(PartExt::getDeployingElements, part));
}
Collection<EObject> result2 = new HashSet<EObject>();
EObject targetContainer = EcoreUtil2.getFirstContainer(part, CsPackage.Literals.COMPONENT);
if (targetContainer instanceof Component) {
result2.addAll(ComponentExt.getRepresentingParts((Component) targetContainer));
}
// Remove all parts of root component
if (systemComponent != null) {
result2.removeAll(systemComponent.getRepresentingParts());
}
// Retrains to already visible containers, to use the existing container displayed instead of display all
// available container. if none visible, we add element to diagram, not reveal parent
result.addAll(result2);
result.addAll(result3);
// if we don't show directly the part, but the part is shown because of a related, we restrains the part to the
// visible ones.
if (lastContext.getAncestor() != null) {
Collection<EObject> resul3 = new HashSet<EObject>();
for (EObject object : result) {
if (getContent().containsView(object)) {
resul3.add(object);
}
}
result = resul3;
}
}
value.putAll(CONTAINER, result);
return value;
}
@Override
protected boolean mustShow(ContextItemElement originCouple_p, DiagramContext context_p,
HashMapSet<String, DSemanticDecorator> relatedViews_p) {
// Some restriction on revealing parts
if ((originCouple_p.getValue() instanceof Part) || (originCouple_p.getValue() instanceof Entity)) {
// We don't reveal a parent part, if the getAncestor is already displayed somewhere
if (originCouple_p.getAncestor() != null) {
for (ContextItemView view : originCouple_p.getAncestor().getElement().getViews()) {
if (view.getViews().get(VIEWS).size() > 0) {
return false;
}
}
}
// We don't reveal a part if there is another view with the other mapping already revealed
DiagramElementMapping mapping = getMapping(originCouple_p.getValue(), context_p, relatedViews_p);
if (mapping == null) {
return false;
}
for (ContextItemView view : originCouple_p.getViews()) {
for (DSemanticDecorator dView : view.getViews().get(VIEWS)) {
if ((dView instanceof DDiagramElement)
&& !mapping.equals(((DDiagramElement) dView).getDiagramElementMapping())) {
return false;
}
}
}
// We don't reveal a part, if is it already displayed somewhere
if (originCouple_p.getAncestor() != null) {
for (ContextItemView view : originCouple_p.getViews()) {
if (view.getViews().get(INITIAL_VIEWS).size() > 0) {
return false;
}
}
}
}
return super.mustShow(originCouple_p, context_p, relatedViews_p);
}
@Override
protected boolean bypassRelatedElements(ContextItemElement originCouple_p, DiagramContext context_p) {
// To avoid recurse to the top of containment, we don't show the part container if not a deployment
if ((originCouple_p.getAncestor() != null)) {
if ((originCouple_p.getValue() instanceof Part)
&& (originCouple_p.getAncestor().getElement().getValue() instanceof Part)) {
if (!getCache(PartExt::getDeployingElements, (Part) originCouple_p.getAncestor().getElement().getValue())
.contains((originCouple_p.getValue()))) {
return true;
}
} else if ((originCouple_p.getValue() instanceof Entity)
&& (originCouple_p.getAncestor().getElement().getValue() instanceof Entity)) {
return true;
}
}
return super.bypassRelatedElements(originCouple_p, context_p);
}
@Override
public DiagramElementMapping getMapping(EObject semantic, DiagramContext context,
HashMapSet<String, DSemanticDecorator> relatedViews) {
DiagramElementMapping mapping = super.getMapping(semantic, context, relatedViews);
if (semantic instanceof Entity) {
mapping = getContent()
.getMapping(MappingConstantsHelper.getMappingABComponent(semantic, getContent().getDDiagram()));
} else if (semantic instanceof Part) {
Part part = (Part) semantic;
List<DeploymentTarget> deployingElements = getCache(PartExt::getDeployingElements, part);
PhysicalComponentNature nature = getNature(part);
if (((containsBehavioralDeployment && nature == PhysicalComponentNature.BEHAVIOR) ||
(containsNodeDeployment && nature == PhysicalComponentNature.NODE)) &&
!deployingElements.isEmpty()) {
Collection<DSemanticDecorator> targetViews = relatedViews.get(CONTAINER);
if (!targetViews.isEmpty() && deployingElements.contains((targetViews.iterator().next().getTarget()))) {
return getContent()
.getMapping(MappingConstantsHelper.getMappingABDeployedElement(getContent().getDDiagram()));
}
}
mapping = getContent()
.getMapping(MappingConstantsHelper.getMappingABComponent(semantic, getContent().getDDiagram()));
}
return mapping;
}
/**
* Returns the component nature for the target (only for Physical Component)
*
* @param object
* the part.
* @return the component nature, if type is a Physical Component.
*/
protected PhysicalComponentNature getNature(EObject object) {
EObject type = CsServices.getService().getComponentType(object);
PhysicalComponentNature nature = null;
if(type instanceof PhysicalComponent) {
nature = ((PhysicalComponent) type).getNature();
}
return nature;
}
@Override
protected Collection<DSemanticDecorator> retrieveDefaultContainer(EObject semantic_p, DiagramContext context_p,
Collection<DSemanticDecorator> targetViews_p) {
if ((semantic_p instanceof Part) || (semantic_p instanceof Entity)) {
// If no container has been found for a part, use diagram to put the given part
return Collections.singletonList((DSemanticDecorator) getContent().getDDiagram());
}
return super.retrieveDefaultContainer(semantic_p, context_p, targetViews_p);
}
@Override
protected boolean mustHide(ContextItemElement originCouple_p, DiagramContext context_p) {
EObject semantic = originCouple_p.getValue();
return (semantic instanceof Part) || (semantic instanceof Entity);
}
}