Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aaa6dda3c2570fa52b147b462e8bfd0b9745b342 (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
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are 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
 *******************************************************************************/

package org.eclipse.etrice.ui.structure.support;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.etrice.core.naming.RoomNameProvider;
import org.eclipse.etrice.core.room.ActorContainerRef;
import org.eclipse.etrice.core.room.Binding;
import org.eclipse.etrice.core.room.BindingEndPoint;
import org.eclipse.etrice.core.room.InterfaceItem;
import org.eclipse.etrice.core.room.LayerConnection;
import org.eclipse.etrice.core.room.Port;
import org.eclipse.etrice.core.room.RefSAPoint;
import org.eclipse.etrice.core.room.RelaySAPoint;
import org.eclipse.etrice.core.room.SAPoint;
import org.eclipse.etrice.core.room.SPP;
import org.eclipse.etrice.core.room.SPPoint;
import org.eclipse.etrice.core.room.StructureClass;
import org.eclipse.etrice.core.room.util.RoomHelpers;
import org.eclipse.etrice.core.room.util.RoomUtil;
import org.eclipse.etrice.core.ui.RoomUiModule;
import org.eclipse.etrice.core.validation.ValidationUtil;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.ICreateConnectionContext;
import org.eclipse.graphiti.features.context.impl.AddConnectionContext;
import org.eclipse.graphiti.features.context.impl.AddContext;
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.PictogramElement;
import org.eclipse.graphiti.mm.pictograms.Shape;

import com.google.inject.Inject;
import com.google.inject.Injector;

/**
 * description
 *
 * @author Henrik Rentz-Reichert initial contribution and API
 *
 */
public class SupportUtil {

	public static final String SEP = "#";
	
	private static SupportUtil instance = null;
	
	
	/**
	 * @return the instance
	 */
	public static SupportUtil getInstance() {
		if (instance==null) {
			Injector injector = RoomUiModule.getInjector();
	        instance = injector.getInstance(SupportUtil.class);
		}
		return instance;
	}
	
	@Inject
	private RoomHelpers roomHelpers;
	@Inject
	private ValidationUtil validationUtil;
	@Inject
	private RoomNameProvider roomNameProvider;
	@Inject
	private RoomUtil roomUtil;

	/**
	 * @return the roomHelpers
	 */
	public RoomHelpers getRoomHelpers() {
		return roomHelpers;
	}

	/**
	 * @return the validationUtil
	 */
	public ValidationUtil getValidationUtil() {
		return validationUtil;
	}
	
	/**
	 * @return the roomUtil
	 */
	public RoomUtil getRoomUtil() {
		return roomUtil;
	}

	/**
	 * @return the roomNameProvider
	 */
	public RoomNameProvider getRoomNameProvider() {
		return roomNameProvider;
	}
	
	public EObject getOwnObject(EObject obj, ResourceSet rs) {
		URI uri = EcoreUtil.getURI(obj);
		EObject own = rs.getEObject(uri, true);
		assert(own!=null): "own object must exist";
		return own;
	}
	
	public ContainerShape addItem(EObject obj, int x, int y, ContainerShape container, IFeatureProvider fp) {
		return addItem(obj, x, y, container, null, fp);
	}
	
	public ContainerShape addItem(EObject obj, int x, int y, ContainerShape container, Map<String,Anchor> ifitem2anchor, IFeatureProvider fp) {
		AddContext addContext = new AddContext();

		addContext.setNewObject(obj);
		addContext.setTargetContainer(container);
		addContext.setX(x);
		addContext.setY(y);
		ContainerShape newShape = (ContainerShape) fp.addIfPossible(addContext);
		assert(newShape!=null): "shape creation must succeed";
		
		if (ifitem2anchor!=null) {
			if (obj instanceof ActorContainerRef)
				getAnchors((ActorContainerRef)obj, newShape, ifitem2anchor);
			else if (obj instanceof InterfaceItem) {
				assert(!newShape.getAnchors().isEmpty()): "interface item must have an anchor";
				ifitem2anchor.put(SEP+((InterfaceItem)obj).getName(), newShape.getAnchors().get(0));
			}
		}
		
		return newShape;
	}

	public void getAnchors(ActorContainerRef acr, PictogramElement refShape,
			final Map<String, Anchor> ifitem2anchor) {
		
		if (refShape instanceof ContainerShape) {
			ifitem2anchor.put(acr.getName()+SEP, ((ContainerShape)refShape).getAnchors().get(0));
			for (Shape child : ((ContainerShape) refShape).getChildren()) {
				if (child instanceof ContainerShape) {
					ContainerShape childShape = (ContainerShape) child;
					if (!childShape.getAnchors().isEmpty()) {
						if (!childShape.getLink().getBusinessObjects().isEmpty()) {
							EObject obj = childShape.getLink().getBusinessObjects().get(0);
							if (obj instanceof Port) {
								ifitem2anchor.put(acr.getName()+SEP+((Port)obj).getName(), childShape.getAnchors().get(0));
							}
							if (obj instanceof SPP) {
								ifitem2anchor.put(acr.getName()+SEP+((SPP)obj).getName(), childShape.getAnchors().get(0));
							}
						}
					}
				}
			}
		}
	}
	
	public void addRefItem(ActorContainerRef obj, ContainerShape acShape, int x, int y, IFeatureProvider featureProvider, final Map<String, Anchor> ifitem2anchor) {
		AddContext addContext = new AddContext();
		addContext.setNewObject(obj);
		addContext.setTargetContainer(acShape);
		addContext.setX(x);
		addContext.setY(y);
		
		ContainerShape refShape = (ContainerShape) featureProvider.addIfPossible(addContext);
		
		getAnchors(obj, refShape, ifitem2anchor);
	}

	public void addInterfaceItem(InterfaceItem item, ContainerShape acShape, int x, int y, IFeatureProvider featureProvider, final Map<String, Anchor> ifitem2anchor) {
		AddContext addContext = new AddContext();
		addContext.setNewObject(item);
		addContext.setTargetContainer(acShape);
		addContext.setX(x);
		addContext.setY(y);
		
		ContainerShape pe = (ContainerShape) featureProvider.addIfPossible(addContext);
		assert(!pe.getAnchors().isEmpty()): "interface item should have an anchor";
		ifitem2anchor.put(SEP+item.getName(), pe.getAnchors().get(0));
	}

	public void addBinding(Binding bind, IFeatureProvider featureProvider,
			final Map<String, Anchor> ifitem2anchor) {
		String ep1 = getName(bind.getEndpoint1());
		String ep2 = getName(bind.getEndpoint2());
		Anchor a1 = ifitem2anchor.get(ep1);
		Anchor a2 = ifitem2anchor.get(ep2);
		assert(a1!=null && a2!=null): "ports for binding must be present";
		
		AddConnectionContext context = new AddConnectionContext(a1, a2);
		context.setNewObject(bind);
		featureProvider.addIfPossible(context);
	}

	public void addLayerConnection(LayerConnection lc, IFeatureProvider featureProvider,
			final Map<String, Anchor> ifitem2anchor) {
		String ep1 = getName(lc.getFrom());
		String ep2 = getName(lc.getTo());
		Anchor a1 = ifitem2anchor.get(ep1);
		Anchor a2 = ifitem2anchor.get(ep2);
		assert(a1!=null && a2!=null): "spps for layer connection must be present";
		
		AddConnectionContext context = new AddConnectionContext(a1, a2);
		context.setNewObject(lc);
		featureProvider.addIfPossible(context);
	}

	public void addInterfaceItems(List<? extends InterfaceItem> items, int y, ContainerShape acShape, int width,
			IFeatureProvider fp,
			final Map<String, Anchor> ifitem2anchor) {
		
		int n = items.size();
		int delta = width/(n+1);
		int pos = delta;
		for (InterfaceItem item : items) {
			addInterfaceItem(item, acShape, pos+StructureClassSupport.MARGIN, y, fp, ifitem2anchor);
			pos += delta;
		}
	}
	
	public void addRefItems(List<? extends ActorContainerRef> actorRefs,
			ContainerShape acShape, int width,
			IFeatureProvider fp, final Map<String, Anchor> ifitem2anchor) {
		int ncols = width/ActorContainerRefSupport.DEFAULT_SIZE_X;
		int nrows = actorRefs.size()/ncols;
		int gap = (width-(ncols*ActorContainerRefSupport.DEFAULT_SIZE_X))/(ncols+1);
		int delta = gap+ActorContainerRefSupport.DEFAULT_SIZE_X;
		int x0 = gap+ActorContainerRefSupport.DEFAULT_SIZE_X/2;
		int y0 = ActorContainerRefSupport.DEFAULT_SIZE_Y*5/2;
		int i = 0;
		for (ActorContainerRef ar : actorRefs) {
			int row = i/ncols;
			int col = i%ncols;
			if (row>=nrows) {
				int nc = actorRefs.size()%ncols;
				gap = (width-(nc*ActorContainerRefSupport.DEFAULT_SIZE_X))/(nc+1);
				delta = gap+ActorContainerRefSupport.DEFAULT_SIZE_X;
				x0 = gap+ActorContainerRefSupport.DEFAULT_SIZE_X/2;
			}
			int x = x0+delta*col;
			int y = y0+(ActorContainerRefSupport.MARGIN+ActorContainerRefSupport.DEFAULT_SIZE_Y)*row;
			addRefItem(ar, acShape, x+StructureClassSupport.MARGIN, y+StructureClassSupport.MARGIN, fp, ifitem2anchor);
			++i;
		}
	}

	public String getName(BindingEndPoint ep) {
		String ar = ep.getActorRef()==null? "":ep.getActorRef().getName();
		String p = ep.getPort().getName();
		return ar+SEP+p;
	}

	public String getName(SAPoint sapt) {
		if (sapt instanceof RelaySAPoint) {
			return SEP+((RelaySAPoint)sapt).getRelay().getName();
		}
		else if (sapt instanceof RefSAPoint) {
			RefSAPoint rpt = (RefSAPoint) sapt;
			return rpt.getRef().getName()+SEP;
		}
		assert(false): "unexpected sub type";
		return null;
	}

	public String getName(SPPoint sppt) {
		return sppt.getRef().getName()+SEP+sppt.getService().getName();
	}

	public List<InterfaceItem> getInterfaceItems(ContainerShape shape, IFeatureProvider fp) {
		return getInterfaceItems(shape, fp, null);
	}
	
	public List<InterfaceItem> getInterfaceItems(ContainerShape shape, IFeatureProvider fp, Map<String, Anchor> ifitem2anchor) {
		List<InterfaceItem> items = new ArrayList<InterfaceItem>();
		for (Shape ch : shape.getChildren()) {
			Object bo = fp.getBusinessObjectForPictogramElement(ch);
			if (bo instanceof InterfaceItem) {
				items.add((InterfaceItem)bo);
				if (ifitem2anchor!=null)
					ifitem2anchor.put(SEP+((InterfaceItem)bo).getName(), ch.getAnchors().get(0));
			}
		}
		return items;
	}
	
	public List<ActorContainerRef> getRefs(ContainerShape shape, IFeatureProvider fp) {
		return getRefs(shape, fp, null);
	}
	
	public List<ActorContainerRef> getRefs(ContainerShape shape, IFeatureProvider fp, Map<String, Anchor> ifitem2anchor) {
		List<ActorContainerRef> refs = new ArrayList<ActorContainerRef>();
		for (Shape ch : shape.getChildren()) {
			Object bo = fp.getBusinessObjectForPictogramElement(ch);
			if (bo instanceof ActorContainerRef) {
				refs.add((ActorContainerRef)bo);
				if (ifitem2anchor!=null)
					getAnchors((ActorContainerRef)bo, ch, ifitem2anchor);
			}
		}
		return refs;
	}

	public List<Binding> getBindings(Diagram diag, IFeatureProvider fp) {
		List<Binding> bindings = new ArrayList<Binding>();
		
		for (Connection conn : diag.getConnections()) {
			Object bo = fp.getBusinessObjectForPictogramElement(conn);
			if (bo instanceof Binding)
				bindings.add((Binding)bo);
		}
		return bindings;
	}

	public List<LayerConnection> getConnections(Diagram diag, IFeatureProvider fp) {
		List<LayerConnection> bindings = new ArrayList<LayerConnection>();
		
		for (Connection conn : diag.getConnections()) {
			Object bo = fp.getBusinessObjectForPictogramElement(conn);
			if (bo instanceof LayerConnection)
				bindings.add((LayerConnection)bo);
		}
		return bindings;
	}
	
	public StructureClass getParent(ICreateConnectionContext context, IFeatureProvider fp) {
		ContainerShape shape = (ContainerShape) context.getSourcePictogramElement().eContainer();
		return getParent(shape, fp);
	}
	
	public StructureClass getParent(ContainerShape shape, IFeatureProvider fp) {
		if(shape == null)
			return null;
		
		Object bo = fp.getBusinessObjectForPictogramElement(shape);
		if (bo instanceof StructureClass)
			return (StructureClass) bo;
		
		shape = (ContainerShape) shape.eContainer();
		bo = fp.getBusinessObjectForPictogramElement(shape);
		if (bo instanceof StructureClass)
			return (StructureClass) bo;
		
		return null;
	}

	public Port getPort(Anchor anchor, IFeatureProvider fp) {
		if (anchor != null) {
			Object obj = fp.getBusinessObjectForPictogramElement(anchor.getParent());
			if (obj instanceof Port) {
				return (Port) obj;
			}
		}
		return null;
	}

	public SPP getSPP(Anchor anchor, IFeatureProvider fp) {
		if (anchor != null) {
			Object obj = fp.getBusinessObjectForPictogramElement(anchor.getParent());
			if (obj instanceof SPP) {
				return (SPP) obj;
			}
		}
		return null;
	}
	
	public ActorContainerRef getRef(Anchor anchor, IFeatureProvider fp) {
		if (anchor != null) {
			ContainerShape shape = (ContainerShape) anchor.getParent().eContainer();
			Object bo = fp.getBusinessObjectForPictogramElement(shape);
			if (bo instanceof ActorContainerRef)
				return (ActorContainerRef) bo;
			shape = (ContainerShape) anchor.getParent();
			bo = fp.getBusinessObjectForPictogramElement(shape);
			if (bo instanceof ActorContainerRef)
				return (ActorContainerRef) bo;
		}				
		return null;
	}
}

Back to the top