Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ebbeaa932f6233b54dcfb2bcb1b08dd2a5de202b (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
/*******************************************************************************
 * Copyright (c) 2010 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
 * 
 * CONTRIBUTORS:
 * 		Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

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

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.etrice.core.validation.ValidationUtil;
import org.eclipse.etrice.ui.structure.ImageProvider;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.features.IAddFeature;
import org.eclipse.graphiti.features.ICreateConnectionFeature;
import org.eclipse.graphiti.features.IDeleteFeature;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.IReason;
import org.eclipse.graphiti.features.IReconnectionFeature;
import org.eclipse.graphiti.features.IRemoveFeature;
import org.eclipse.graphiti.features.IUpdateFeature;
import org.eclipse.graphiti.features.context.IAddConnectionContext;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.features.context.ICreateConnectionContext;
import org.eclipse.graphiti.features.context.IDeleteContext;
import org.eclipse.graphiti.features.context.IReconnectionContext;
import org.eclipse.graphiti.features.context.IRemoveContext;
import org.eclipse.graphiti.features.context.IUpdateContext;
import org.eclipse.graphiti.features.context.impl.AddConnectionContext;
import org.eclipse.graphiti.features.context.impl.ReconnectionContext;
import org.eclipse.graphiti.features.context.impl.RemoveContext;
import org.eclipse.graphiti.features.impl.AbstractAddFeature;
import org.eclipse.graphiti.features.impl.AbstractCreateConnectionFeature;
import org.eclipse.graphiti.features.impl.AbstractUpdateFeature;
import org.eclipse.graphiti.features.impl.DefaultReconnectionFeature;
import org.eclipse.graphiti.features.impl.DefaultRemoveFeature;
import org.eclipse.graphiti.features.impl.Reason;
import org.eclipse.graphiti.mm.algorithms.Polyline;
import org.eclipse.graphiti.mm.pictograms.Anchor;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.services.IGaService;
import org.eclipse.graphiti.services.IPeCreateService;
import org.eclipse.graphiti.tb.DefaultToolBehaviorProvider;
import org.eclipse.graphiti.tb.IToolBehaviorProvider;
import org.eclipse.graphiti.ui.features.DefaultDeleteFeature;
import org.eclipse.graphiti.ui.features.DefaultFeatureProvider;
import org.eclipse.graphiti.util.ColorConstant;
import org.eclipse.graphiti.util.IColorConstant;

import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.ActorContainerClass;
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.LogicalSystem;
import org.eclipse.etrice.core.room.Port;
import org.eclipse.etrice.core.room.RoomFactory;
import org.eclipse.etrice.core.room.StructureClass;
import org.eclipse.etrice.core.room.SubSystemClass;

public class BindingSupport {

	public static final IColorConstant LINE_COLOR = new ColorConstant(0, 0, 0);
	public static final IColorConstant INHERITED_COLOR = new ColorConstant(100, 100, 100);

	class FeatureProvider extends DefaultFeatureProvider {
		
		private class CreateFeature extends AbstractCreateConnectionFeature {
			
			private boolean justStarted = false;

			public CreateFeature(IFeatureProvider fp) {
				super(fp, "Binding", "create Binding");
			}
			
			@Override
			public String getCreateImageId() {
				return ImageProvider.IMG_BINDING;
			}
	
			@Override
			public boolean canCreate(ICreateConnectionContext context) {
				IFeatureProvider featureProvider = getFeatureProvider();
				Port src = SupportUtil.getPort(context.getSourceAnchor(), featureProvider);
				Port tgt = SupportUtil.getPort(context.getTargetAnchor(), featureProvider);
				ActorContainerRef srcRef = SupportUtil.getRef(context.getSourceAnchor(), featureProvider);
				
				if (justStarted) {
					justStarted = false;
					beginHighLightMatches(src, srcRef);
				}
				
				if (src==null || tgt==null) {
					return false;
				}
				
				StructureClass ac = SupportUtil.getParent(context, featureProvider);
				if (ac==null) {
					return false;
				}
				
				ActorContainerRef tgtRef = SupportUtil.getRef(context.getTargetAnchor(), featureProvider);
				
				return ValidationUtil.isConnectable(src, srcRef, tgt, tgtRef, ac).isOk();
			}
			
			public boolean canStartConnection(ICreateConnectionContext context) {
				Port src = SupportUtil.getPort(context.getSourceAnchor(), getFeatureProvider());
				boolean canStart = src!=null;
				if (canStart) {
					ActorContainerRef ref = SupportUtil.getRef(context.getSourceAnchor(), getFeatureProvider());
					if (ref==null) {
						// this port is local, i.e. owned by the parent itself
						ActorContainerClass acc = (ActorContainerClass) src.eContainer();
						if (!ValidationUtil.isConnectable(src, null, acc).isOk())
							canStart = false;
					}
					else {
						ActorContainerClass acc = (ActorContainerClass) ref.eContainer();
						if (!ValidationUtil.isConnectable(src, ref, acc).isOk())
							canStart = false;
					}
				}
				if (canStart)
					justStarted = true;
				return canStart;
			}
			
			@Override
			public Connection create(ICreateConnectionContext context) {
				Connection newConnection = null;
				
				endHighLightMatches();
				
				IFeatureProvider featureProvider = getFeatureProvider();
				Port src = SupportUtil.getPort(context.getSourceAnchor(), featureProvider);
				Port dst = SupportUtil.getPort(context.getTargetAnchor(), featureProvider);
				StructureClass ac = SupportUtil.getParent(context, featureProvider);
				if (src!=null && dst!=null && ac!=null) {
					Binding bind = RoomFactory.eINSTANCE.createBinding();
					BindingEndPoint ep1 = RoomFactory.eINSTANCE.createBindingEndPoint();
					ActorContainerRef ar1 = SupportUtil.getRef(context.getSourceAnchor(), featureProvider);
					ep1.setPort(src);
					ep1.setActorRef(ar1);
					BindingEndPoint ep2 = RoomFactory.eINSTANCE.createBindingEndPoint();
					ActorContainerRef ar2 = SupportUtil.getRef(context.getTargetAnchor(), featureProvider);
					ep2.setPort(dst);
					ep2.setActorRef(ar2);
					bind.setEndpoint1(ep1);
					bind.setEndpoint2(ep2);
					ac.getBindings().add(bind);
					
					AddConnectionContext addContext = new AddConnectionContext(context.getSourceAnchor(), context.getTargetAnchor());
					addContext.setNewObject(bind);
					newConnection = (Connection) featureProvider.addIfPossible(addContext);
				}
				
				return newConnection;
			}
			
			private void beginHighLightMatches(Port port, ActorContainerRef ref) {
				if (port==null)
					return;
				
				StructureClass acc = (ActorContainerClass) ((ref!=null)? ref.eContainer():port.eContainer());
				if (acc instanceof ActorClass) {
					
				}
				else if (acc instanceof SubSystemClass) {
					
				}
				else if (acc instanceof LogicalSystem) {
					
				}
				else {
					assert(false): "unknown kind of StructureClass";
				}
			}

			private void endHighLightMatches() {
			}
		}
		
		private class AddFeature extends AbstractAddFeature {

			public AddFeature(IFeatureProvider fp) {
				super(fp);
			}

			@Override
			public boolean canAdd(IAddContext context) {
				if (context instanceof IAddConnectionContext && context.getNewObject() instanceof Binding) {
					return true;
				}
				return false;
			}

			@Override
			public PictogramElement add(IAddContext context) {
				IAddConnectionContext addConContext = (IAddConnectionContext) context;
				Binding bind = (Binding) context.getNewObject();

				boolean inherited = isInherited(getDiagram(), bind);
				
				IPeCreateService peCreateService = Graphiti.getPeCreateService();
				// CONNECTION WITH POLYLINE
				Connection connection = peCreateService.createFreeFormConnection(getDiagram());
				connection.setStart(addConContext.getSourceAnchor());
				connection.setEnd(addConContext.getTargetAnchor());
				
				Graphiti.getPeService().setPropertyValue(connection, Constants.TYPE_KEY, Constants.BIND_TYPE);

				IGaService gaService = Graphiti.getGaService();
				Polyline polyline = gaService.createPolyline(connection);
				polyline.setForeground(manageColor(inherited?INHERITED_COLOR:LINE_COLOR));

				// create link and wire it
				link(connection, bind);

				return connection;
			}
			
		}
		
		private class ReconnectionFeature extends DefaultReconnectionFeature {

			private boolean doneChanges = false;

			public ReconnectionFeature(IFeatureProvider fp) {
				super(fp);
			}
			
			@Override
			public boolean canReconnect(IReconnectionContext context) {
				if (!super.canReconnect(context))
					return false;
				
				Binding bind = (Binding) getBusinessObjectForPictogramElement(context.getConnection());
				if (isInherited(getDiagram(), bind))
					return false;
				
				Anchor asrc = context.getConnection().getStart();
				Anchor atgt = context.getConnection().getEnd();
				if (context.getReconnectType().equals(ReconnectionContext.RECONNECT_SOURCE))
					asrc = context.getNewAnchor();
				else
					atgt = context.getNewAnchor();
				
				IFeatureProvider featureProvider = getFeatureProvider();
				Port src = SupportUtil.getPort(asrc, featureProvider);
				Port tgt = SupportUtil.getPort(atgt, featureProvider);
				ActorContainerRef srcRef = SupportUtil.getRef(asrc, featureProvider);
				
				if (src==null || tgt==null) {
					return false;
				}
				
				StructureClass ac = SupportUtil.getParent(getDiagram(), featureProvider);
				if (ac==null) {
					return false;
				}
				
				ActorContainerRef tgtRef = SupportUtil.getRef(atgt, featureProvider);
				
				return ValidationUtil.isConnectable(src, srcRef, tgt, tgtRef, ac, bind).isOk();
			}
			
			@Override
			public void postReconnect(IReconnectionContext context) {
				super.postReconnect(context);

				IFeatureProvider featureProvider = getFeatureProvider();
				Port src = SupportUtil.getPort(context.getConnection().getStart(), featureProvider);
				Port dst = SupportUtil.getPort(context.getConnection().getEnd(), featureProvider);
				StructureClass ac = SupportUtil.getParent(getDiagram(), featureProvider);
				if (src!=null && dst!=null && ac!=null) {
					doneChanges = true;

					Binding bind = (Binding) getBusinessObjectForPictogramElement(context.getConnection());
					BindingEndPoint ep1 = RoomFactory.eINSTANCE.createBindingEndPoint();
					ActorContainerRef ar1 = SupportUtil.getRef(context.getConnection().getStart(), featureProvider);
					ep1.setPort(src);
					ep1.setActorRef(ar1);
					BindingEndPoint ep2 = RoomFactory.eINSTANCE.createBindingEndPoint();
					ActorContainerRef ar2 = SupportUtil.getRef(context.getConnection().getEnd(), featureProvider);
					ep2.setPort(dst);
					ep2.setActorRef(ar2);
					bind.setEndpoint1(ep1);
					bind.setEndpoint2(ep2);
				}
			}
			
			@Override
			public boolean hasDoneChanges() {
				return doneChanges ;
			}
		}
		
		private class UpdateFeature extends AbstractUpdateFeature {

			public UpdateFeature(IFeatureProvider fp) {
				super(fp);
			}

			@Override
			public boolean canUpdate(IUpdateContext context) {
				Object bo = getBusinessObjectForPictogramElement(context.getPictogramElement());
				if (bo instanceof EObject && ((EObject)bo).eIsProxy())
					return true;
				
				return false;
			}

			@Override
			public IReason updateNeeded(IUpdateContext context) {
				Object bo = getBusinessObjectForPictogramElement(context.getPictogramElement());
				if (bo instanceof EObject && ((EObject)bo).eIsProxy()) {
					return Reason.createTrueReason("Binding deleted from model");
				}
				return Reason.createFalseReason();
			}

			@Override
			public boolean update(IUpdateContext context) {
				PictogramElement pe = context.getPictogramElement();
				Object bo = getBusinessObjectForPictogramElement(pe);
				if (bo instanceof EObject && ((EObject)bo).eIsProxy()) {
					IRemoveContext rc = new RemoveContext(pe);
					IFeatureProvider featureProvider = getFeatureProvider();
					IRemoveFeature removeFeature = featureProvider.getRemoveFeature(rc);
					if (removeFeature != null) {
						removeFeature.remove(rc);
					}
					EcoreUtil.delete((EObject) bo);
					return true;
				}
				return false;
			}
		}
		
		private class RemoveFeature extends DefaultRemoveFeature {

			public RemoveFeature(IFeatureProvider fp) {
				super(fp);
			}
			
			@Override
			public boolean canRemove(IRemoveContext context) {
				return false;
			}
		}
		
		private class DeleteFeature extends DefaultDeleteFeature {

			public DeleteFeature(IFeatureProvider fp) {
				super(fp);
			}
			
			@Override
			public boolean canDelete(IDeleteContext context) {
				EObject bo = Graphiti.getLinkService().getBusinessObjectForLinkedPictogramElement(context.getPictogramElement());
				if (bo instanceof Binding) {
					Binding b = (Binding) bo;
					if (isInherited(getDiagram(), b))
						return false;
				}
				
				return true;
			}
		}
		
		private IFeatureProvider fp;
		
		public FeatureProvider(IDiagramTypeProvider dtp, IFeatureProvider fp) {
			super(dtp);
			this.fp = fp;
		}

		private boolean isInherited(Diagram diag, Binding b) {
			ActorContainerClass ac = (ActorContainerClass) Graphiti.getLinkService().getBusinessObjectForLinkedPictogramElement(diag.getChildren().get(0));
			return (b.eContainer()!=ac);
		}
		
		@Override
		public ICreateConnectionFeature[] getCreateConnectionFeatures() {
			return new ICreateConnectionFeature[] { new CreateFeature(fp) };
		}
		
		@Override
		public IAddFeature getAddFeature(IAddContext context) {
			return new AddFeature(fp);
		}
		
		@Override
		public IUpdateFeature getUpdateFeature(IUpdateContext context) {
			return new UpdateFeature(fp);
		}

		@Override
		public IReconnectionFeature getReconnectionFeature(IReconnectionContext context) {
			return new ReconnectionFeature(fp);
		}
		
		@Override
		public IRemoveFeature getRemoveFeature(IRemoveContext context) {
			return new RemoveFeature(fp);
		}
		
		@Override
		public IDeleteFeature getDeleteFeature(IDeleteContext context) {
			return new DeleteFeature(fp);
		}
	}
	
	class BehaviorProvider extends DefaultToolBehaviorProvider {

		public BehaviorProvider(IDiagramTypeProvider dtp) {
			super(dtp);
		}
	}
	
	private FeatureProvider pfp;
	private BehaviorProvider tbp;
	
	public BindingSupport(IDiagramTypeProvider dtp, IFeatureProvider fp) {
		pfp = new FeatureProvider(dtp,fp);
		tbp = new BehaviorProvider(dtp);
	}
	
	public IFeatureProvider getFeatureProvider() {
		return pfp;
	}
	
	public IToolBehaviorProvider getToolBehaviorProvider() {
		return tbp;
	}
}

Back to the top