Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d8b3ea14b9218dcfbe08ba8df1cc428e538dd835 (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
783
784
785
786
787
788
789
790
791
792
793
/*******************************************************************************
 * 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 java.util.List;

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.room.ActorClass;
import org.eclipse.etrice.core.room.ActorContainerClass;
import org.eclipse.etrice.core.room.ActorContainerRef;
import org.eclipse.etrice.core.room.ActorRef;
import org.eclipse.etrice.core.room.InterfaceItem;
import org.eclipse.etrice.core.room.Port;
import org.eclipse.etrice.core.room.SPPRef;
import org.eclipse.etrice.core.room.SubSystemRef;
import org.eclipse.etrice.core.room.util.RoomHelpers;
import org.eclipse.etrice.ui.common.support.NoResizeFeature;
import org.eclipse.etrice.ui.structure.DiagramAccess;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.features.IDeleteFeature;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.IMoveShapeFeature;
import org.eclipse.graphiti.features.IReason;
import org.eclipse.graphiti.features.IRemoveFeature;
import org.eclipse.graphiti.features.IResizeShapeFeature;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.features.context.ICreateContext;
import org.eclipse.graphiti.features.context.ICustomContext;
import org.eclipse.graphiti.features.context.IDeleteContext;
import org.eclipse.graphiti.features.context.ILocationContext;
import org.eclipse.graphiti.features.context.IMoveShapeContext;
import org.eclipse.graphiti.features.context.IRemoveContext;
import org.eclipse.graphiti.features.context.IResizeShapeContext;
import org.eclipse.graphiti.features.context.ITargetContext;
import org.eclipse.graphiti.features.context.IUpdateContext;
import org.eclipse.graphiti.features.context.impl.RemoveContext;
import org.eclipse.graphiti.features.custom.AbstractCustomFeature;
import org.eclipse.graphiti.features.impl.AbstractAddFeature;
import org.eclipse.graphiti.features.impl.AbstractCreateFeature;
import org.eclipse.graphiti.features.impl.AbstractUpdateFeature;
import org.eclipse.graphiti.features.impl.DefaultMoveShapeFeature;
import org.eclipse.graphiti.features.impl.DefaultRemoveFeature;
import org.eclipse.graphiti.features.impl.Reason;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.algorithms.Rectangle;
import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.algorithms.styles.Color;
import org.eclipse.graphiti.mm.algorithms.styles.Orientation;
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 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.ui.features.DefaultDeleteFeature;
import org.eclipse.graphiti.ui.features.DefaultFeatureProvider;
import org.eclipse.graphiti.util.ColorConstant;
import org.eclipse.graphiti.util.IColorConstant;

public class InterfaceItemSupport {
	
	public static final int ITEM_SIZE = StructureClassSupport.MARGIN;
	public static final int ITEM_SIZE_SMALL = (int) (ActorContainerRefSupport.MARGIN*0.8);
	public static final int MARGIN = StructureClassSupport.MARGIN;
	public static final int MARGIN_SMALL = ActorContainerRefSupport.MARGIN;

	protected static final int LINE_WIDTH = 2;
	protected static final IColorConstant DARK_COLOR = new ColorConstant(0, 0, 0);
	protected static final IColorConstant INHERITED_COLOR = new ColorConstant(100, 100, 100);
	protected static final IColorConstant BRIGHT_COLOR = new ColorConstant(255, 255, 255);
	protected static final String PROP_KIND = "item-kind";
	
	protected static class FeatureProvider extends DefaultFeatureProvider {

		protected abstract static class CreateFeature extends AbstractCreateFeature {
	
			protected boolean internal;
			protected boolean doneChanges = false;
	
			public CreateFeature(IFeatureProvider fp, boolean internal, String name, String description) {
				super(fp, name, description);
				this.internal = internal;
			}
	
			@Override
			public boolean canCreate(ICreateContext context) {
				if (context.getTargetContainer().getLink()!=null)
					if (context.getTargetContainer().getLink().getBusinessObjects().size()==1) {
						EObject obj = context.getTargetContainer().getLink().getBusinessObjects().get(0);
						if (obj instanceof ActorContainerClass) {
							if (obj instanceof ActorClass)
								return isValidPosition(context, context, internal, StructureClassSupport.MARGIN);
							else
								return !internal;
						}
					}
				return false;
			}
			
			@Override
			public boolean hasDoneChanges() {
				return doneChanges;
			}
		}
		
		protected abstract static class AddFeature extends AbstractAddFeature {
	
			public AddFeature(IFeatureProvider fp) {
				super(fp);
			}
	
			@Override
			public boolean canAdd(IAddContext context) {
				if (context.getNewObject() instanceof InterfaceItem) {
					if (context.getTargetContainer().getLink().getBusinessObjects().size()==1) {
						EObject obj = context.getTargetContainer().getLink().getBusinessObjects().get(0);
						if (obj instanceof ActorContainerClass) {
							return true;
						}
						if (obj instanceof ActorContainerRef) {
							return true;
						}
					}
				}
				return false;
			}
	
			@Override
			public PictogramElement add(IAddContext context) {
				InterfaceItem port = (InterfaceItem) context.getNewObject();
				boolean internal = isInternal(port);
				ContainerShape acShape = context.getTargetContainer();
				Object bo = getBusinessObjectForPictogramElement(acShape);
				boolean inherited = isInherited(port, bo, acShape);
				boolean refport = (bo instanceof ActorContainerRef);
	
				int margin = refport?MARGIN_SMALL:MARGIN;
				int size = refport?ITEM_SIZE_SMALL:ITEM_SIZE;

				// CONTAINER SHAPE WITH RECTANGLE
				IPeCreateService peCreateService = Graphiti.getPeCreateService();
				ContainerShape containerShape =
					peCreateService.createContainerShape(acShape, true);
	
				Graphiti.getPeService().setPropertyValue(containerShape, Constants.TYPE_KEY, Constants.PORT_TYPE);
				
				String kind = getItemKind(port);
				Graphiti.getPeService().setPropertyValue(containerShape, PROP_KIND, kind);
				
				// the context point is the midpoint relative to the invisible rectangle
				int x = context.getX();
				int y = context.getY();
				int width = acShape.getGraphicsAlgorithm().getWidth();
				int height = acShape.getGraphicsAlgorithm().getHeight();
				if (internal) {
					if (x<2*margin)
						x = 2*margin;
					else if (x>width-2*margin)
						x = width-2*margin;
					if (y<2*margin)
						y = 2*margin;
					else if (y>height-2*margin)
						y = height-2*margin;
				}
				else {
					// TODOHRR: remove duplicate code
					int dx = (x<=width/2)? x:width-x;
					int dy = (y<=height/2)? y:height-y;
					if (dx>dy) {
						// keep x, project y
						if (y<=height/2)
							y = margin;
						else
							y = height-margin;
						
						if (x<margin)
							x = margin;
						else if (x>width-margin)
							x = width-margin;
					}
					else {
						// keep y, project x
						if (x<=width/2)
							x = margin;
						else
							x = width-margin;
						
						if (y<margin)
							y = margin;
						else if (y>height-margin)
							y = height-margin;
					}
				}
				// finally we subtract the midpoint to get coordinates of the upper left corner
				x -= margin;
				y -= margin;
				
				Color dark = manageColor(inherited? INHERITED_COLOR:DARK_COLOR);
				IGaService gaService = Graphiti.getGaService();
				{
					final Rectangle invisibleRectangle = gaService.createInvisibleRectangle(containerShape);
					gaService.setLocationAndSize(invisibleRectangle, x, y, 2*margin, 2*margin);
	
					createItemFigure(port, refport,
							containerShape,
							invisibleRectangle,
							dark,
							manageColor(BRIGHT_COLOR));
	
					// create link and wire it
					link(containerShape, port);
				}
				
				{
					Shape labelShape = peCreateService.createShape(containerShape, false);
					Text label = gaService.createDefaultText(getDiagram(), labelShape, port.getName());
					label.setForeground(dark);
					label.setBackground(dark);
					label.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER);
					label.setVerticalAlignment(Orientation.ALIGNMENT_CENTER);
					gaService.setLocationAndSize(label, 0, 3*margin/2, 2*margin, margin/2);
					adjustLabel(label, x, y, width, margin, size);
				}

				// call the layout feature
				layoutPictogramElement(containerShape);
	
				return containerShape;
	
			}
	
			protected abstract String getItemKind(InterfaceItem item);
			protected abstract void createItemFigure(InterfaceItem item, boolean refitem,
					ContainerShape containerShape,
					GraphicsAlgorithm invisibleRectangle, Color darkColor, Color brightDolor);
		}
	
		protected class MoveShapeFeature extends DefaultMoveShapeFeature {
	
			public MoveShapeFeature(IFeatureProvider fp) {
				super(fp);
			}
	
			@Override
			public boolean canMoveShape(IMoveShapeContext context) {
				boolean canMove = super.canMoveShape(context);
	
				if (canMove) {
					Object bo = getBusinessObjectForPictogramElement(context.getPictogramElement());
					if (bo instanceof InterfaceItem) {
						InterfaceItem item = (InterfaceItem) bo;
						
						ContainerShape acShape = context.getTargetContainer();
						Object parentBO = getBusinessObjectForPictogramElement(acShape);
						boolean refport = (parentBO instanceof ActorContainerRef);
						if (refport)
							return true;
						
						if (isInherited(item, parentBO, acShape))
							return false;
						
						int margin = refport?ActorContainerRefSupport.MARGIN:StructureClassSupport.MARGIN;
						return isValidPosition(context, context, isInternal(item), margin);
					}
					return false;
				}
				
				return canMove;
			}
			
			@Override
			protected void postMoveShape(IMoveShapeContext context) {
				ContainerShape shapeToMove = (ContainerShape) context.getShape();
	
				InterfaceItem item = (InterfaceItem) getBusinessObjectForPictogramElement(shapeToMove);
				boolean internal = isInternal(item);
				ContainerShape acShape = context.getTargetContainer();
				boolean refport = (getBusinessObjectForPictogramElement(acShape) instanceof ActorContainerRef);
	
				int margin = refport?MARGIN_SMALL:MARGIN;
				int size = refport?ITEM_SIZE_SMALL:ITEM_SIZE;
	
				int x = context.getX();
				int y = context.getY();
				int width = context.getTargetContainer().getGraphicsAlgorithm().getGraphicsAlgorithmChildren().get(0).getWidth();
				int height = context.getTargetContainer().getGraphicsAlgorithm().getGraphicsAlgorithmChildren().get(0).getHeight();
				
				if (internal) {
					// nothing to do
				}
				else {
					// project onto boundary
					if (refport) {
						int dx = (x<=width/2)? x:width-x;
						int dy = (y<=height/2)? y:height-y;
						if (dx>dy) {
							// keep x, project y
							if (y<=height/2)
								y = 0;
							else
								y = height-0;
							
							if (x<0)
								x = 0;
							else if (x>width-0)
								x = width-0;
						}
						else {
							// keep y, project x
							if (x<=width/2)
								x = 0;
							else
								x = width-0;
							
							if (y<0)
								y = 0;
							else if (y>height-0)
								y = height-0;
						}
					}
					else {
						if (x<=margin)
							x = 0;
						if (y<=margin)
							y = 0;
						if ((width-margin)<=x)
							x = width;
						if ((height-margin)<=y)
							y = height;
					}
				}
	
				Graphiti.getGaService().setLocation(shapeToMove.getGraphicsAlgorithm(), x, y, avoidNegativeCoordinates());
				
				GraphicsAlgorithm ga = shapeToMove.getChildren().get(0).getGraphicsAlgorithm();
				if (ga instanceof Text) {
					adjustLabel((Text) ga, x, y, width, margin, size);
				}
			}
		}
		
		protected abstract 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 bo instanceof InterfaceItem;
			}

			@Override
			public IReason updateNeeded(IUpdateContext context) {
				Object bo = getBusinessObjectForPictogramElement(context.getPictogramElement());
				if (bo instanceof EObject && ((EObject)bo).eIsProxy()) {
					return Reason.createTrueReason("InterfaceItem deleted from model");
				}
				InterfaceItem port = (InterfaceItem) bo;
				
				String reason = "";
				
				// check if port still owned/inherited
				ContainerShape containerShape = (ContainerShape)context.getPictogramElement();
				bo = getBusinessObjectForPictogramElement(containerShape);
				if (bo instanceof ActorClass) {
					ActorClass ac = (ActorClass) bo;
					boolean found = false;
					do {
						if (ac==port.eContainer())
							found = true;
						ac = ac.getBase();
					}
					while (!found && ac!=null);
					
					if (!found)
						reason += "InterfaceItem not inherited anymore\n";
				}
				
				GraphicsAlgorithm ga = containerShape.getChildren().get(0).getGraphicsAlgorithm();
				if (ga instanceof Text) {
					if (!port.getName().equals(((Text)ga).getValue()))
						reason += "Name is out of date\n";

					String kind = getItemKind(port);
					if (!kind.equals(Graphiti.getPeService().getPropertyValue(context.getPictogramElement(), PROP_KIND)))
						reason += "Figure is out of date\n";
				}
				
				if (!reason.isEmpty())
					return Reason.createTrueReason(reason.substring(0, reason.length()-1));
				
				return Reason.createFalseReason();
			}

			@Override
			public boolean update(IUpdateContext context) {
				ContainerShape containerShape = (ContainerShape)context.getPictogramElement();
				Object bo = getBusinessObjectForPictogramElement(containerShape);
				if (bo instanceof EObject && ((EObject)bo).eIsProxy()) {
					IRemoveContext rc = new RemoveContext(containerShape);
					IFeatureProvider featureProvider = getFeatureProvider();
					IRemoveFeature removeFeature = featureProvider.getRemoveFeature(rc);
					if (removeFeature != null) {
						removeFeature.remove(rc);
					}
					EcoreUtil.delete((EObject) bo);
					return true;
				}
				InterfaceItem port = (InterfaceItem) bo;
				
				boolean inherited = isInherited(port, bo, containerShape);
				
				Color dark = manageColor(inherited? INHERITED_COLOR:DARK_COLOR);
				updateFigure(port, containerShape, dark, manageColor(BRIGHT_COLOR));
				String kind = getItemKind(port);
				Graphiti.getPeService().setPropertyValue(containerShape, PROP_KIND, kind);
				return true;
			}
			
			protected abstract String getItemKind(InterfaceItem item);
			protected abstract void updateFigure(InterfaceItem item, PictogramElement pe, Color dark, Color bright);
		}
		
		
		protected static abstract class PropertyFeature extends AbstractCustomFeature {

			private String name;
			private String description;

			public PropertyFeature(IFeatureProvider fp, String name, String description) {
				super(fp);
				this.name = name;
				this.description = description;
			}

			@Override
			public String getName() {
				return name;
			}
			
			@Override
			public String getDescription() {
				return description;
			}
			
			@Override
			public boolean canExecute(ICustomContext context) {
				PictogramElement[] pes = context.getPictogramElements();
				if (pes != null && pes.length == 1 && pes[0] instanceof ContainerShape) {
					Object bo = getBusinessObjectForPictogramElement(pes[0]);
					if (bo instanceof InterfaceItem) {
						ContainerShape shape = ((ContainerShape)pes[0]).getContainer();
						InterfaceItem item = (InterfaceItem) bo;
						Object parentBO = getBusinessObjectForPictogramElement(shape.getContainer());
						
						return !isRefItem(shape) && !isInherited(item, parentBO, shape);
					}
				}
				return false;
			}
		}
		
		protected static class RemoveFeature extends DefaultRemoveFeature {

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

			public boolean canRemove(IRemoveContext context) {
				return false;
			}
		}
		
		protected static class DeleteFeature extends DefaultDeleteFeature {

			public DeleteFeature(IFeatureProvider fp) {
				super(fp);
			}
			
			@Override
			public boolean canDelete(IDeleteContext context) {
				ContainerShape shape = (ContainerShape) context.getPictogramElement();
				Object bo = getBusinessObjectForPictogramElement(shape);
				if (bo instanceof EObject && ((EObject)bo).eIsProxy())
					return true;
				
				InterfaceItem item = (InterfaceItem) bo;
				Object parentBO = getBusinessObjectForPictogramElement(shape.getContainer());
				
				return !isRefItem(shape) && !isInherited(item, parentBO, shape);
			}
		}
		
		protected IFeatureProvider fp;
		
		protected FeatureProvider(IDiagramTypeProvider dtp, IFeatureProvider fp) {
			super(dtp);
			this.fp = fp;
		}

		protected static boolean isRefItem(PictogramElement pe) {
			if (!(pe instanceof ContainerShape))
				return false;
			
			ContainerShape acShape = ((ContainerShape)pe).getContainer();
			
			if (acShape.getLink()==null || acShape.getLink().getBusinessObjects().isEmpty())
				return false;
			
			Object parent = acShape.getLink().getBusinessObjects().get(0);
			
			return (parent instanceof ActorContainerRef);
		}
	
		protected static boolean isInternal(InterfaceItem item) {
			if (item instanceof Port) {
				Port port = (Port) item;
				
				// NB: the port's container might be a base class of the depicted actor class
				ActorContainerClass acc = (ActorContainerClass) port.eContainer();
				if (acc instanceof ActorClass) {
					ActorClass ac = (ActorClass) acc;
					if (ac.getIntPorts().contains(port))
						return true;
				}
			}
			else if (item instanceof SPPRef) {
				return false;
			}
			else {
				assert(false): "unexpected sub type";
			}

			return false;
		}
		
		protected static boolean isInherited(InterfaceItem item, Object container, ContainerShape cs) {
			if (container instanceof ActorClass) {
				ActorClass ac = (ActorClass) container;
				return item.eContainer()!=ac;
			}
			else if (container instanceof ActorRef) {
				// have to check whether the ActorRef is inherited
				ActorRef ar = (ActorRef) container;
				ContainerShape arCont = cs.getContainer();
				EObject cls = arCont.getLink().getBusinessObjects().get(0);
				if (cls instanceof ActorClass)
					return ar.eContainer()!=cls;

				// cls is a SubSystemClass
				return false;
			}
			else if (container instanceof SubSystemRef) {
				// SubSystemRefs only occur in LogicalSystems, no inheritance
				return false;
			}

			return false;
		}
		
		protected static boolean isValidPosition(ILocationContext loc, ITargetContext tgt, boolean internal, int margin) {
			//System.out.println("isValidPosition "+tgt.getTargetContainer());
			if (tgt.getTargetContainer().getGraphicsAlgorithm()==null)
				return false;
			if (tgt.getTargetContainer().getGraphicsAlgorithm().getGraphicsAlgorithmChildren().isEmpty())
				return false;

			int x = loc.getX();
			int y = loc.getY();
			if (loc instanceof ICreateContext) {
				// adjust position as relative to visible rectangle
				x -= margin;
				y -= margin;
			}
			int width = tgt.getTargetContainer().getGraphicsAlgorithm().getGraphicsAlgorithmChildren().get(0).getWidth();
			int height = tgt.getTargetContainer().getGraphicsAlgorithm().getGraphicsAlgorithmChildren().get(0).getHeight();
			
			boolean inStripe = false;
			
			// may move in a stripe along the actor class border
			int stripe = margin/2;
			if (x<=stripe)
				inStripe = true;
			else if (y<=stripe)
				inStripe = true;
			else if ((width-stripe)<=x)
				inStripe = true;
			else if ((height-stripe)<=y)
				inStripe = true;
			
			//System.out.println("w:"+width+" h:"+height+" x:"+x+" y:"+y+" in stripe "+inStripe);

			if (internal)
				return !inStripe;
			else
				return inStripe;
		}
		
		@Override
		public IMoveShapeFeature getMoveShapeFeature(IMoveShapeContext context) {
			return new MoveShapeFeature(fp);
		}
		
		@Override
		public IResizeShapeFeature getResizeShapeFeature(IResizeShapeContext context) {
			return new NoResizeFeature(fp);
		}
		
		@Override
		public IRemoveFeature getRemoveFeature(IRemoveContext context) {
			return new RemoveFeature(fp);
		}
		
		@Override
		public IDeleteFeature getDeleteFeature(IDeleteContext context) {
			return new DeleteFeature(fp);
		}
		
		protected static void adjustLabel(Text label, int x, int y, int width, int margin, int size) {
			Orientation align = Orientation.ALIGNMENT_CENTER;
			label.setHorizontalAlignment(align);
	
			int pos = 3*margin/2;
			
			if (x<=margin)
				align = Orientation.ALIGNMENT_LEFT;
			else if ((width-margin)<=x)
				align = Orientation.ALIGNMENT_RIGHT;
			if (y<=margin)
				pos = 0;
	
			if (align!=label.getHorizontalAlignment()) {
				label.setHorizontalAlignment(align);
			}
			if (pos!=label.getY()) {
				IGaService gaService = Graphiti.getGaService();
				gaService.setLocationAndSize(label, 0, pos, 2*margin, margin/2);
			}
		}
		
	}

	protected static class BehaviorProvider extends DefaultToolBehaviorProvider {

		public BehaviorProvider(IDiagramTypeProvider dtp) {
			super(dtp);
		}
		
		@Override
		public GraphicsAlgorithm[] getClickArea(PictogramElement pe) {
            GraphicsAlgorithm invisible = pe.getGraphicsAlgorithm();
            GraphicsAlgorithm rectangle =
                            invisible.getGraphicsAlgorithmChildren().get(0);
            return new GraphicsAlgorithm[] { rectangle };
		}
		
		@Override
		public GraphicsAlgorithm getSelectionBorder(PictogramElement pe) {
            GraphicsAlgorithm invisible = pe.getGraphicsAlgorithm();

            GraphicsAlgorithm rectangle =
                invisible.getGraphicsAlgorithmChildren().get(0);
            return rectangle;
		}
		
		@Override
		public String getToolTip(GraphicsAlgorithm ga) {
			// if this is called we know there is a business object!=null
			PictogramElement pe = ga.getPictogramElement();
			
			EObject bo = Graphiti.getLinkService().getBusinessObjectForLinkedPictogramElement(pe);
			if (bo instanceof InterfaceItem) {
				String name = ((InterfaceItem) bo).getName();
				String protocol = ((InterfaceItem) bo).getProtocol().getName();
				if (bo instanceof Port)
					if (((Port) bo).isConjugated())
						protocol = "conj "+protocol;
				
				return name+"\n("+protocol+")";
			}
			return super.getToolTip(ga);
		}
	}
	
	public static void createRefItems(ActorContainerRef acr, ContainerShape refShape, IFeatureProvider featureProvider) {
		
		ActorContainerClass refClass = (acr instanceof ActorRef)?((ActorRef)acr).getType():((SubSystemRef)acr).getType();
		List<? extends InterfaceItem> refItems = RoomHelpers.getInterfaceItems(refClass, true);
		
		if (refShape!=null && refClass!=null &&!refItems.isEmpty()) {
			
			// the diagram will be created and initialized if not present
			// TODOHRR: how to proceed if diagram not up to date?
			Diagram refDiag = new DiagramAccess().getDiagram(refClass);

			ResourceSet rs = acr.eResource().getResourceSet();
			
			if (!refDiag.getChildren().isEmpty()) {
				ContainerShape refAcShape = (ContainerShape) refDiag.getChildren().get(0);
				Object bo = featureProvider.getBusinessObjectForPictogramElement(refAcShape);
				if (bo instanceof ActorContainerClass) {
					ActorContainerClass extRefClass = (ActorContainerClass) bo;
					assert(extRefClass.getName().equals(refClass.getName())): "structure class names must match";

					List<InterfaceItem> extRefItems = RoomHelpers.getInterfaceItems(extRefClass, true);
					List<InterfaceItem> intRefItems = SupportUtil.getInterfaceItems(refShape, featureProvider);
					
					int scaleX = refAcShape.getGraphicsAlgorithm().getWidth()/ActorContainerRefSupport.DEFAULT_SIZE_X;
					int scaleY = refAcShape.getGraphicsAlgorithm().getHeight()/ActorContainerRefSupport.DEFAULT_SIZE_Y;
					for (Shape childShape : refAcShape.getChildren()) {
						bo = featureProvider.getBusinessObjectForPictogramElement(childShape);
						if (bo instanceof InterfaceItem) {
							if (extRefItems.contains(bo)) {
								// this is an interface item, insert it

								EObject ownObject = SupportUtil.getOwnObject((InterfaceItem)bo, rs);
								if (!intRefItems.contains(ownObject)) {
									int x = ITEM_SIZE_SMALL/2 + childShape.getGraphicsAlgorithm().getX()/scaleX;
									int y = ITEM_SIZE_SMALL/2 + childShape.getGraphicsAlgorithm().getY()/scaleY;
									SupportUtil.addItem(ownObject, x, y, refShape, featureProvider);
								}
							}
						}
					}
				}
			}
			else {
				assert(false): "empty referenced structure class diagram";
			}
		}
	}

	protected static void createInheritedRefItems(ActorContainerRef acr, ContainerShape arShape, IFeatureProvider fp) {
		
		ActorClass ac = (ActorClass) acr.eContainer();
		ResourceSet rs = ac.eResource().getResourceSet();
		List<InterfaceItem> presentObjects = SupportUtil.getInterfaceItems(arShape, fp);

		Diagram refDiag = new DiagramAccess().getDiagram(ac);
		if (!refDiag.getChildren().isEmpty()) {
			ContainerShape refAcShape = (ContainerShape) refDiag.getChildren().get(0);
			Object bo = fp.getBusinessObjectForPictogramElement(refAcShape);
			if (bo instanceof ActorClass) {
				ActorClass extRefClass = (ActorClass) bo;
				assert(extRefClass.getName().equals(ac.getName())): "actor class names must match";
				
				for (Shape childShape : refAcShape.getChildren()) {
					bo = fp.getBusinessObjectForPictogramElement(childShape);
					if (bo instanceof ActorRef) {
						EObject ownObject = SupportUtil.getOwnObject((ActorRef)bo, rs);
						if (ownObject==acr) {
							int subScaleX = arShape.getGraphicsAlgorithm().getWidth()/ActorContainerRefSupport.DEFAULT_SIZE_X;
							int subScaleY = arShape.getGraphicsAlgorithm().getHeight()/ActorContainerRefSupport.DEFAULT_SIZE_Y;
							
							// add items of actor ref
							for (Shape grandChildShape : ((ContainerShape)childShape).getChildren()) {
								bo = fp.getBusinessObjectForPictogramElement(grandChildShape);
								if (bo instanceof InterfaceItem) {
									ownObject = SupportUtil.getOwnObject((Port)bo, rs);
									if (!presentObjects.contains(ownObject)) {
										int x = ITEM_SIZE_SMALL/2 + grandChildShape.getGraphicsAlgorithm().getX()/subScaleX;
										int y = ITEM_SIZE_SMALL/2 + grandChildShape.getGraphicsAlgorithm().getY()/subScaleY;
										SupportUtil.addItem(ownObject, x, y, arShape, fp);
									}
								}
							}
							break;
						}
					}
				}
			}
		}
	}
}

Back to the top