Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6b13cf9a483920cf96333a972fcbcc2545946ffc (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
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
/*****************************************************************************
 * Copyright (c) 2010 CEA
 *
 *    
 * 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:
 *   Soyatec - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.edit.parts;

import java.util.List;

import org.eclipse.draw2d.Border;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineShape;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.handles.HandleBounds;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.requests.ReconnectRequest;
import org.eclipse.gmf.runtime.common.core.util.StringStatics;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IBorderItemEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ResizableShapeEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.figures.IBorderItemLocator;
import org.eclipse.gmf.runtime.diagram.ui.render.editparts.RenderedDiagramRootEditPart;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateConnectionViewRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateUnspecifiedTypeRequest;
import org.eclipse.gmf.runtime.draw2d.ui.geometry.PointListUtilities;
import org.eclipse.gmf.runtime.draw2d.ui.mapmode.IMapMode;
import org.eclipse.gmf.runtime.draw2d.ui.mapmode.MapModeUtil;
import org.eclipse.gmf.runtime.emf.type.core.IHintedType;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.papyrus.infra.gmfdiag.common.preferences.PreferencesConstantsHelper;
import org.eclipse.papyrus.uml.diagram.common.draw2d.LinesBorder;
import org.eclipse.papyrus.uml.diagram.common.helper.PreferenceInitializerForElementHelper;
import org.eclipse.papyrus.uml.diagram.common.locator.ExternalLabelPositionLocator;
import org.eclipse.papyrus.uml.diagram.sequence.edit.helpers.AnchorHelper;
import org.eclipse.papyrus.uml.diagram.sequence.edit.helpers.AnchorHelper.FixedAnchorEx;
import org.eclipse.papyrus.uml.diagram.sequence.edit.policies.ExternalLabelPrimaryDragRoleEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.edit.policies.semantic.CustomDurationConstraintItemSemanticEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.part.UMLDiagramEditorPlugin;
import org.eclipse.papyrus.uml.diagram.sequence.part.UMLVisualIDRegistry;
import org.eclipse.papyrus.uml.diagram.sequence.providers.UMLElementTypes;
import org.eclipse.papyrus.uml.diagram.sequence.util.SequenceRequestConstant;
import org.eclipse.papyrus.uml.diagram.sequence.util.SequenceUtil;
import org.eclipse.uml2.uml.DurationConstraint;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.MessageOccurrenceSpecification;

/**
 * @author Jin Liu (jin.liu@soyatec.com)
 */
public class CustomDurationConstraintEditPart extends DurationConstraintEditPart {

	private static final String ARROW = "Arrow";

	private static final String DIRECTION = "Direction";

	private static final String VERTICAL = "vertical";

	private static final String HORIZONTAL = "horizontal";

	/**
	 * Constructor.
	 * 
	 * @param view
	 */
	public CustomDurationConstraintEditPart(View view) {
		super(view);
	}

	/**
	 * @Override
	 */
	@Override
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE, new CustomDurationConstraintItemSemanticEditPolicy());
	}

	/**
	 * @see org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.Request)
	 * 
	 * @param request
	 *        The request
	 * @return The anchor
	 */
	@Override
	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		IHintedType type = (IHintedType)UMLElementTypes.CommentAnnotatedElement_4010;
		if(request instanceof CreateConnectionViewRequest) {
			String hint = ((CreateConnectionViewRequest)request).getConnectionViewDescriptor().getSemanticHint();
			if(hint.equals(type.getSemanticHint())) {
				return new RotateAnchor(getFigure(), computeAnchorLocation(((CreateConnectionViewRequest)request).getLocation()));
			}
		} else if(request instanceof ReconnectRequest) {
			ConnectionEditPart linkPart = ((ReconnectRequest)request).getConnectionEditPart();
			if(linkPart instanceof CommentAnnotatedElementEditPart) {
				return new RotateAnchor(getFigure(), computeAnchorLocation(((ReconnectRequest)request).getLocation()));
			}
		}
		return super.getSourceConnectionAnchor(request);
	}

	protected int computeAnchorLocation(Point location) {
		Rectangle box = getBox();
		if(isArrowVertical()) {
			if(location.getDistance2(box.getTop()) > location.getDistance2(box.getBottom()))
				return PositionConstants.BOTTOM;
			return PositionConstants.TOP;
		} else {
			if(location.getDistance2(box.getLeft()) > location.getDistance2(box.getRight()))
				return PositionConstants.RIGHT;
			return PositionConstants.LEFT;
		}
	}

	protected Rectangle getBox() {
		Rectangle rBox = getFigure() instanceof HandleBounds ? new PrecisionRectangle(((HandleBounds)getFigure()).getHandleBounds()) : new PrecisionRectangle(getFigure().getBounds());
		getFigure().translateToAbsolute(rBox);
		return rBox;
	}

	public boolean canCreateLink(Point point) {
		DurationConstraint dc = (DurationConstraint)this.resolveSemanticElement();

		int count = 0; // link counts
		List list = this.getSourceConnections();
		for(Object o : list)
			if(o instanceof CustomCommentAnnotatedElementEditPart) {
				count++;
			}
		if(count >= 2)
			return false;

		if(count < 1)
			return true;

		int targetPosition = computeAnchorLocation(point);
		if(findLinkAtPosition(targetPosition))
			return false;
		return true;
	}

	//	protected void refreshSourceConnections() {
	//		super.refreshSourceConnections();
	//		Display.getDefault().asyncExec(new Runnable(){
	//			public void run() {
	//				refreshBorder();
	//			}
	//		});
	//	}
	//	
	//	public void refresh(){
	//		super.refresh();
	//		refreshBorder();
	//	}
	//
	//	private void refreshBorder() {
	//		if(getPrimaryShape() instanceof CustomDurationConstraintFigure){
	//			CustomDurationConstraintFigure fig = (CustomDurationConstraintFigure)getPrimaryShape();
	//			fig.setBorderVisible(hasTopLink(), hasBottomLink());
	//		}
	//	}

	public boolean hasTopLink() {
		return findLinkAtPosition(PositionConstants.TOP);
	}

	public boolean hasBottomLink() {
		return findLinkAtPosition(PositionConstants.BOTTOM);
	}

	protected boolean findLinkAtPosition(int targetPos) {
		List list = this.getSourceConnections();
		for(Object o : list)
			if(o instanceof CustomCommentAnnotatedElementEditPart) {
				CustomCommentAnnotatedElementEditPart connPart = (CustomCommentAnnotatedElementEditPart)o;
				if((targetPos & getSourceAnchorPosition(connPart)) > 0)
					return true;

			}
		return false;
	}

	/**
	 * @see org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.ConnectionEditPart)
	 * 
	 * @param connEditPart
	 *        The connection edit part.
	 * @return The anchor.
	 */
	@Override
	public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connEditPart) {
		ConnectionAnchor anchor = null;
		if(connEditPart instanceof CommentAnnotatedElementEditPart) {
			anchor = new RotateAnchor(getFigure(), getSourceAnchorPosition((CommentAnnotatedElementEditPart)connEditPart));
		} else {
			anchor = super.getSourceConnectionAnchor(connEditPart);
		}

		return anchor;
	}

	private int getSourceAnchorPosition(CommentAnnotatedElementEditPart connEditPart) {
		String terminal = AnchorHelper.getAnchorId(getEditingDomain(), connEditPart, true);
		if(terminal.length() > 0) {
			return parseLocation(terminal);
		}
		return PositionConstants.TOP;
	}

	@Override
	protected void refreshBounds() {
		super.refreshBounds();

		int width = ((Integer)getStructuralFeatureValue(NotationPackage.eINSTANCE.getSize_Width())).intValue();
		int height = ((Integer)getStructuralFeatureValue(NotationPackage.eINSTANCE.getSize_Height())).intValue();
		// restore to default size
		if(width == -1)
			width = getFigure().getPreferredSize().width;
		if(height == -1)
			height = getFigure().getPreferredSize().height;

		if(width != -1 && height != -1) {
			Dimension size = primaryShape.getBounds().getSize();
			//if(size.width != width || size.height != height) { // if resize bounds
			primaryShape.setBounds(new Rectangle(primaryShape.getBounds().getLocation(), new Dimension(width, height)));
			//}
		}

		//fix combined fragment move
		this.getFigure().getParent().getLayoutManager().layout(this.getFigure().getParent());
		relocateLabelEditPart();
	}

	@Override
	protected IFigure createNodeShape() {
		return primaryShape = new CustomDurationConstraintFigure();
	}

	/**
	 * This method creates a specific edit policy for time realted elements
	 * 
	 * @see org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderItemEditPart#getPrimaryDragEditPolicy()
	 * 
	 * @return <code>EditPolicy</code>
	 * @Override
	 */
	@Override
	public EditPolicy getPrimaryDragEditPolicy() {
		EditPolicy policy = getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE);
		return policy != null ? policy : new ResizableShapeEditPolicy() {

			protected Command getResizeCommand(ChangeBoundsRequest request) {
				ChangeBoundsRequest req = new ChangeBoundsRequest(REQ_RESIZE_CHILDREN);
				req.setEditParts(getHost());
				req.setMoveDelta(request.getMoveDelta());
				req.setSizeDelta(request.getSizeDelta());
				req.setLocation(request.getLocation());
				req.setExtendedData(request.getExtendedData());
				req.setResizeDirection(request.getResizeDirection());
				return getHost().getParent().getCommand(req);
			}

			protected void showChangeBoundsFeedback(ChangeBoundsRequest request) {
				IFigure feedback = getDragSourceFeedbackFigure();

				PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy());
				getHostFigure().translateToAbsolute(rect);
				rect.translate(request.getMoveDelta());
				rect.resize(request.getSizeDelta());

				IFigure f = getHostFigure();
				Dimension max = f.getMaximumSize().getCopy();
				IMapMode mmode = MapModeUtil.getMapMode(f);
				max.height = mmode.LPtoDP(max.height);
				max.width = mmode.LPtoDP(max.width);

				// no minimal size
				if(max.width < rect.width)
					rect.width = max.width;

				if(max.height < rect.height)
					rect.height = max.height;

				feedback.translateToRelative(rect);
				feedback.setBounds(rect);
			}
		};
	}

	/**
	 * @Override use ExternalLabelPrimaryDragRoleEditPolicy
	 */
	@Override
	protected LayoutEditPolicy createLayoutEditPolicy() {
		org.eclipse.gmf.runtime.diagram.ui.editpolicies.LayoutEditPolicy lep = new org.eclipse.gmf.runtime.diagram.ui.editpolicies.LayoutEditPolicy() {

			@Override
			protected EditPolicy createChildEditPolicy(EditPart child) {
				View childView = (View)child.getModel();
				switch(UMLVisualIDRegistry.getVisualID(childView)) {
				case DurationConstraintLabelEditPart.VISUAL_ID:
				case DurationConstraintAppliedStereotypeEditPart.VISUAL_ID:
					// use ExternalLabelPrimaryDragRoleEditPolicy
					return new ExternalLabelPrimaryDragRoleEditPolicy();
				}
				EditPolicy result = child.getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE);
				if(result == null) {
					result = new NonResizableEditPolicy();
				}
				return result;
			}

			@Override
			protected Command getMoveChildrenCommand(Request request) {
				return null;
			}

			@Override
			protected Command getCreateCommand(CreateRequest request) {
				return null;
			}
		};
		return lep;
	}

	/**
	 * @Override use ExternalLabelPositionLocator
	 */
	@Override
	protected void addBorderItem(IFigure borderItemContainer, IBorderItemEditPart borderItemEditPart) {
		if(borderItemEditPart instanceof DurationConstraintLabelEditPart) {
			DurationConstraintLabelLocator locator = new DurationConstraintLabelLocator(getMainFigure());
			locator.setParentEditPart(this);
			borderItemContainer.add(borderItemEditPart.getFigure(), locator);
		} else if(borderItemEditPart instanceof DurationConstraintAppliedStereotypeEditPart) {
			//use ExternalLabelPositionLocator
			IBorderItemLocator locator = new ExternalLabelPositionLocator(getMainFigure());
			borderItemContainer.add(borderItemEditPart.getFigure(), locator);
		} else {
			super.addBorderItem(borderItemContainer, borderItemEditPart);
		}
	}

	/**
	 * @Override Override for redirecting creation request to the lifeline
	 */
	@Override
	public Command getCommand(Request request) {
		if(request instanceof CreateUnspecifiedTypeRequest) {
			return getParent().getCommand(request);
		}
		return super.getCommand(request);
	}

	/**
	 * @Override Override for redirecting creation request to the lifeline
	 */
	@Override
	public void showSourceFeedback(Request request) {
		if(request instanceof CreateUnspecifiedTypeRequest) {
			getParent().showSourceFeedback(request);
		}
		super.showSourceFeedback(request);
	}

	/**
	 * @Override Override for redirecting creation request to the lifeline
	 */
	@Override
	public void eraseSourceFeedback(Request request) {
		if(request instanceof CreateUnspecifiedTypeRequest) {
			getParent().eraseSourceFeedback(request);
		}
		super.eraseSourceFeedback(request);
	}

	@Override
	protected void handleNotificationEvent(Notification notification) {
		super.handleNotificationEvent(notification);
		Object feature = notification.getFeature();
		if((getModel() != null) && (getModel() == notification.getNotifier())) {
			if(NotationPackage.eINSTANCE.getLineStyle_LineWidth().equals(feature)) {
				refreshLineWidth();
			}
		}

		if(notification.getNewValue() instanceof EAnnotation && ARROW.equals(((EAnnotation)notification.getNewValue()).getSource())) {
			refreshArrowDirection((EAnnotation)notification.getNewValue());
		} else if(notification.getNotifier() instanceof EAnnotation && ARROW.equals(((EAnnotation)notification.getNotifier()).getSource())) {
			refreshArrowDirection((EAnnotation)notification.getNotifier()); //notification.getEventType() == 
		}
	}

	@Override
	protected void refreshVisuals() {
		super.refreshVisuals();
		refreshArrowDirection(null);
		refreshLineWidth();
	}

	private void relocateLabelEditPart() {
		List list = this.getChildren();
		for(Object o : list) {
			if(o instanceof DurationConstraintLabelEditPart) {
				DurationConstraintLabelEditPart label = (DurationConstraintLabelEditPart)o;
				if(label.getBorderItemLocator() != null) {
					IBorderItemLocator loc = label.getBorderItemLocator();
					loc.relocate(label.getFigure());
				}
			}
		}
	}

	public Rectangle updateMoveBounds(Rectangle newBounds) {
		if(getCurrentSideOfParent() == PositionConstants.WEST) {
			Rectangle bounds = this.getFigure().getBounds();
			return newBounds.translate(-bounds.width, 0); // keep bounds in left side
		}
		return newBounds;
	}

	public int getCurrentSideOfParent() {
		IBorderItemLocator locator = this.getBorderItemLocator();
		if(locator != null) {
			return locator.getCurrentSideOfParent();
		}
		return PositionConstants.EAST;
	}

	public class CustomDurationConstraintFigure extends DurationConstraintFigure {

		/**
		 * Height of the arrow end
		 * 
		 */
		private static final int ARROW_HEIGHT = 8;

		/**
		 * Width of the half of the arrow end
		 * 
		 */
		private static final int ARROW_SEMI_WIDTH = 7;

		boolean vertical = true;

		public CustomDurationConstraintFigure() {
			setBorder(createBorder0());
			setDashBorder();
		}

		private Border createBorder0() {
			LinesBorder result = new LinesBorder() {

				@Override
				public void paint(IFigure figure, Graphics graphics, Insets insets) {
					tempRect.setBounds(getPaintRectangle(figure, insets));

					int one = MapModeUtil.getMapMode(figure).DPtoLP(1);
					int widthInDP = getWidth() / one;

					int halfWidthInLP = MapModeUtil.getMapMode(figure).DPtoLP(widthInDP / 2);

					graphics.setLineWidth(getWidth());
					graphics.setLineStyle(getStyle());
					// adapt tempRect so that borders do not overflow the initial bounds
					tempRect.x += halfWidthInLP;
					tempRect.width -= getWidth();
					tempRect.y += halfWidthInLP;
					tempRect.height -= getWidth();

					if((getPositions() & PositionConstants.TOP) > 0) {
						//graphics.drawLine(tempRect.getTopLeft(), tempRect.getTopRight());
						graphics.drawLine(new Point(tempRect.getTopLeft().x(), tempRect.getTopLeft().y() + 1), new Point(tempRect.getTopRight().x(), tempRect.getTopRight().y() + 1));
					}
					if((getPositions() & PositionConstants.LEFT) > 0) {
						//graphics.drawLine(tempRect.getTopLeft(), tempRect.getBottomLeft());
						graphics.drawLine(new Point(tempRect.getTopLeft().x() + 1, tempRect.getTopLeft().y()), new Point(tempRect.getBottomLeft().x() + 1, tempRect.getBottomLeft().y()));
					}
					if((getPositions() & PositionConstants.BOTTOM) > 0) {
						graphics.drawLine(tempRect.getBottomLeft(), tempRect.getBottomRight());
					}
					if((getPositions() & PositionConstants.RIGHT) > 0) {
						graphics.drawLine(tempRect.getTopRight(), tempRect.getBottomRight());
					}
				}
			};
			result.setSides(PositionConstants.TOP | PositionConstants.BOTTOM);
			return result;
		}

		protected void setDashBorder() {
			if(this.getBorder() instanceof LinesBorder) {
				LinesBorder lb = (LinesBorder)this.getBorder();
				lb.setStyle(Graphics.LINE_DASH);
				if(vertical)
					lb.setSides(PositionConstants.TOP | PositionConstants.BOTTOM);
				else
					lb.setSides(PositionConstants.LEFT | PositionConstants.RIGHT);
			}
		}

		@Override
		public Insets getInsets() {
			if(getBorder() != null)
				return getBorder().getInsets(this);
			return NO_INSETS;
		}

		public void setVertical(boolean val) {
			if(val != vertical) {
				vertical = val;
				setDashBorder();
				revalidate();
			}
		}

		//		public void setBorderVisible(boolean top, boolean bottom){
		//			if(this.getBorder() instanceof LinesBorder){
		//				LinesBorder lb = (LinesBorder) this.getBorder();
		//				if(top){
		//					int value = bottom ? PositionConstants.TOP | PositionConstants.BOTTOM : PositionConstants.TOP;
		//					lb.setSides(value);
		//				}else{
		//					int value = bottom ? PositionConstants.BOTTOM : PositionConstants.NONE;
		//					lb.setSides(value);
		//				}
		//				this.repaint();
		//			}
		//		}

		/**
		 * Sets the bounds of this Figure to the Rectangle <i>rect</i>.
		 * This also updates sub-figures.
		 * 
		 * @see Figure#setBounds(Rectangle)
		 * @param rect
		 *        The new bounds
		 */
		@Override
		public void setBounds(Rectangle rect) {
			updateArrow(rect.width, rect.height);

			Rectangle c = rect.getCopy();
			this.getParent().translateToAbsolute(c);

			super.setBounds(rect);
		}

		/**
		 * Update the arrow polyline taking in account new figure's size
		 * 
		 * @param figureWidth
		 *        the new figure width
		 * @param figureHeight
		 *        the new figure height
		 */
		public void updateArrow(int figureWidth, int figureHeight) {
			// handle insets to avoid figure growing indefinitely
			figureWidth -= getInsets().left + getInsets().right;
			figureHeight -= getInsets().top + getInsets().bottom;
			if(getDurationArrow() != null) {
				int halfLineWidth = getLineWidth() / 2;
				if(vertical) {
					PointList points = new PointList(8);
					int centerX = figureWidth / 2;
					points.addPoint(centerX - ARROW_SEMI_WIDTH, halfLineWidth + ARROW_HEIGHT);
					points.addPoint(centerX, halfLineWidth);
					points.addPoint(centerX + ARROW_SEMI_WIDTH, halfLineWidth + ARROW_HEIGHT);
					points.addPoint(centerX, halfLineWidth);
					points.addPoint(centerX, figureHeight - halfLineWidth);
					points.addPoint(centerX - ARROW_SEMI_WIDTH, figureHeight - halfLineWidth - ARROW_HEIGHT);
					points.addPoint(centerX, figureHeight - halfLineWidth);
					points.addPoint(centerX + ARROW_SEMI_WIDTH, figureHeight - halfLineWidth - ARROW_HEIGHT);
					getDurationArrow().setPoints(points);
					Point topLeft = getLocation().getTranslated(getInsets().left, getInsets().top);
					getDurationArrow().setBounds(new Rectangle(topLeft, new Dimension(figureWidth, figureHeight)));
				} else {
					PointList points = new PointList(8);
					int centerY = figureHeight / 2;
					points.addPoint(halfLineWidth + ARROW_HEIGHT, centerY - ARROW_SEMI_WIDTH);
					points.addPoint(halfLineWidth, centerY);
					points.addPoint(halfLineWidth + ARROW_HEIGHT, centerY + ARROW_SEMI_WIDTH);
					points.addPoint(halfLineWidth, centerY);
					points.addPoint(figureWidth - halfLineWidth, centerY);

					points.addPoint(figureWidth - halfLineWidth - ARROW_HEIGHT, centerY - ARROW_SEMI_WIDTH);
					points.addPoint(figureWidth - halfLineWidth, centerY);
					points.addPoint(figureWidth - halfLineWidth - ARROW_HEIGHT, centerY + ARROW_SEMI_WIDTH);

					getDurationArrow().setPoints(points);
					Point topLeft = getLocation().getTranslated(getInsets().left, getInsets().top);
					getDurationArrow().setBounds(new Rectangle(topLeft, new Dimension(figureWidth, figureHeight)));
				}
			}
		}

		@Override
		public boolean containsPoint(int x, int y) {
			boolean containsPoint = super.containsPoint(x, y);
			if(!containsPoint) {
				return false;
			}
			PolylineShape durationArrow = getDurationArrow();
			if(durationArrow != null) {
				return PointListUtilities.containsPoint(durationArrow.getPoints(), new Point(x, y));
				//				return fDurationArrow.containsPoint(x, y);
			}
			return containsPoint;
		}

		public void paintFigure(Graphics graphics) {
			graphics.setLineWidth(lineWidth);
			super.paintFigure(graphics);
		}

		@Override
		public void setLineWidth(int w) {
			LinesBorder lb = (LinesBorder)getBorder();
			lb.setWidth(w);
			getDurationArrow().setLineWidth(w);
			super.setLineWidth(w);
		}
	}

	public static Rectangle fixMessageBounds(Rectangle newBounds, Request cvr, LifelineEditPart host) {
		Object oc1 = getFirstElement(cvr.getExtendedData().get(SequenceRequestConstant.NEAREST_OCCURRENCE_SPECIFICATION));
		Object oc2 = getFirstElement(cvr.getExtendedData().get(SequenceRequestConstant.NEAREST_OCCURRENCE_SPECIFICATION_2));
		if(oc1 != null && oc2 != null && (oc1 instanceof MessageOccurrenceSpecification || oc2 instanceof MessageOccurrenceSpecification)) {
			Point start = null, end = null;
			Rectangle bounds = null;
			if(oc1 instanceof InteractionFragment) {
				start = SequenceUtil.findLocationOfEvent(host, (InteractionFragment)oc1, true);
			}
			if(oc2 instanceof InteractionFragment) {
				end = SequenceUtil.findLocationOfEvent(host, (InteractionFragment)oc2, true);
			}
			if(start != null && end != null) {
				bounds = (start.y < end.y) ? new Rectangle(start, end) : new Rectangle(end, start);
			}
			if(bounds != null) {
				IFigure parentFigure = host.getFigure();
				Point parentFigDelta = parentFigure.getBounds().getLocation().getCopy().negate();
				parentFigure.translateToRelative(bounds);
				bounds.translate(parentFigDelta);
				if(bounds.y != newBounds.y || newBounds.height != bounds.height) {
					newBounds.y = bounds.y;
					newBounds.height = bounds.height;
				}
			}
		}
		return newBounds;
	}

	static class DurationConstraintLabelLocator extends ExternalLabelPositionLocator {

		private CustomDurationConstraintEditPart durationConstraintEditPart;

		public DurationConstraintLabelLocator(IFigure mainFigure) {
			super(mainFigure);
		}

		public void setParentEditPart(CustomDurationConstraintEditPart durationConstraintEditPart) {
			this.durationConstraintEditPart = durationConstraintEditPart;
		}

		@Override
		public void relocate(IFigure target) {
			if(constraint.y == 0) {
				if(durationConstraintEditPart.getCurrentSideOfParent() == PositionConstants.WEST) {
					Point r = parentFigure.getBounds().getLeft().translate(-20, -5);
					target.setBounds(new Rectangle(r, target.getPreferredSize()));
				} else {
					Point r = parentFigure.getBounds().getRight().translate(5, -5);
					target.setBounds(new Rectangle(r, target.getPreferredSize()));
				}
			} else
				super.relocate(target);
		}
	}

	static Object getFirstElement(Object obj) {
		if(obj != null && obj instanceof List) {
			List list = (List)obj;
			if(list.size() > 0) {
				return list.get(0);
			}
		}
		return null;
	}


	protected void refreshArrowDirection(EAnnotation newValue) {
		CustomDurationConstraintFigure dc = (CustomDurationConstraintFigure)getPrimaryShape();
		dc.setVertical(isArrowVertical());
		refreshBounds();
	}

	public void setArrowDirection(String dir) {
		View view = this.getNotationView();
		if(view == null)
			return;

		EAnnotation ea = view.getEAnnotation(ARROW);
		if(ea == null) {
			ea = EcoreFactory.eINSTANCE.createEAnnotation();
			ea.setSource(ARROW);
			ea.getDetails().put(DIRECTION, dir);
			view.getEAnnotations().add(ea);
		} else {
			ea.getDetails().clear();
			ea.getDetails().put(DIRECTION, dir);
		}
	}

	public String getArrowDirection() {
		View view = this.getNotationView();
		if(view == null)
			return "";
		EAnnotation ea = view.getEAnnotation(ARROW);
		if(ea != null) {
			String pos = ea.getDetails().get(DIRECTION);
			if(pos != null) {
				return pos;
			}
		}
		return VERTICAL;
	}

	public boolean isArrowVertical() {
		return VERTICAL.equals(getArrowDirection());
	}

	public void rotateArrow() {
		if(isArrowVertical()) {
			setArrowDirection(HORIZONTAL);
		} else {
			setArrowDirection(VERTICAL);
		}
	}

	private Object getStructuralFeatureValue(View view, EStructuralFeature feature) {
		return ViewUtil.getPropertyValue(view, feature, feature.getEContainingClass());
	}

	public Rectangle getBounds() {
		View view = this.getNotationView();
		int width = ((Integer)getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getSize_Width())).intValue();
		int height = ((Integer)getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getSize_Height())).intValue();
		int x = ((Integer)getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_X())).intValue();
		int y = ((Integer)getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_Y())).intValue();
		String prefElementId = ViewUtil.resolveSemanticElement(view).eClass().getName();
		IPreferenceStore store = UMLDiagramEditorPlugin.getInstance().getPreferenceStore();
		String preferenceConstantWitdh = PreferenceInitializerForElementHelper.getpreferenceKey(view, prefElementId, PreferencesConstantsHelper.WIDTH);
		String preferenceConstantHeight = PreferenceInitializerForElementHelper.getpreferenceKey(view, prefElementId, PreferencesConstantsHelper.HEIGHT);
		if(width <= 0) {
			width = store.getInt(preferenceConstantWitdh);
		}
		if(height <= 0) {
			height = store.getInt(preferenceConstantHeight);
		}
		return new Rectangle(x, y, width, height);
	}

	public static class RotateArrowCommand extends org.eclipse.emf.transaction.RecordingCommand {

		private CustomDurationConstraintEditPart part;

		public RotateArrowCommand(TransactionalEditingDomain domain, CustomDurationConstraintEditPart part) {
			super(domain);
			this.part = part;
		}

		@Override
		protected void doExecute() {
			List list = part.getSourceConnections();
			if(part.isArrowVertical()) {
				part.setArrowDirection(HORIZONTAL);
			} else {
				part.setArrowDirection(VERTICAL);
			}
		}
	}

	class RotateAnchor extends FixedAnchorEx {

		public RotateAnchor(IFigure f, int location) {
			super(f, location);
		}

		public Point getLocation(Point reference) {
			double zoom = 0;
			if(CustomDurationConstraintEditPart.this.getRoot() instanceof RenderedDiagramRootEditPart) {
				RenderedDiagramRootEditPart render = (RenderedDiagramRootEditPart)CustomDurationConstraintEditPart.this.getRoot();
				zoom = render.getZoomManager().getZoom();
			}
			if(position == PositionConstants.TOP) {
				return new PrecisionPoint(getBox().getTop().preciseX(), getBox().getTop().preciseY() + 1 * zoom);
			} else if(position == PositionConstants.BOTTOM) {
				if(zoom > 1) {
					zoom = zoom - 1;
				}
				return new PrecisionPoint(getBox().getBottom().preciseX(), getBox().getBottom().preciseY() - 1 * zoom);
			} else if(position == PositionConstants.LEFT) {
				if(zoom > 1) {
					zoom = zoom - 1;
				}
				return new PrecisionPoint(getBox().getLeft().preciseX(), getBox().getLeft().preciseY() + 1 * zoom);
			} else if(position == PositionConstants.RIGHT) {
				if(zoom > 1) {
					zoom = zoom - 1;
				}
				return new PrecisionPoint(getBox().getRight().preciseX(), getBox().getRight().preciseY() + 1 * zoom);
			}
			return super.getLocation(reference);
		}

		public String getTerminal() {
			if(isDefaultAnchor())
				return StringStatics.BLANK;
			if(position == PositionConstants.TOP) {
				return "(0.5,0){T}";
			} else if(position == PositionConstants.BOTTOM) {
				return "(0.5,1){D}";
			} else if(position == PositionConstants.LEFT) {
				return "(0,0.5){L}";
			} else if(position == PositionConstants.RIGHT) {
				return "(1,0.5){R}";
			}
			return "(0.5,0){T}";
		}

	}

	private int parseLocation(String terminal) {
		if(terminal.length() > 0) {
			int start = terminal.indexOf("{") + 1;
			if(start > 0) {
				char ch = Character.toUpperCase(terminal.charAt(start));
				switch(ch) {
				case 'L':
					return PositionConstants.LEFT;
				case 'R':
					return PositionConstants.RIGHT;
				case 'D':
					return PositionConstants.BOTTOM;
				default:
					return PositionConstants.TOP;
				}
			}
		}
		return PositionConstants.TOP;
	}
}

Back to the top