Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4b73502d316c428eab9e8d4745d1c03943f18010 (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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
/**
 * Copyright (c) 2014, 2016 CEA LIST, Christian W. Damus, and others.
 *
 * 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:
 *  CEA LIST - Initial API and implementation
 *  Christian W. Damus - bug 468207
 */
package org.eclipse.papyrus.uml.diagram.statemachine.custom.helpers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.common.stereotype.display.helper.StereotypeDisplayUtil;
import org.eclipse.papyrus.uml.diagram.statemachine.edit.parts.RegionEditPart;
import org.eclipse.papyrus.uml.diagram.statemachine.edit.parts.StateMachineEditPart;
import org.eclipse.papyrus.uml.diagram.statemachine.part.UMLVisualIDRegistry;

import com.google.common.base.Strings;

/**
 * Helper class used to manage the encoding of a region position within a state
 * machine or a state.
 *
 * @author David Servat
 */
public class Zone {
	public final static int defaultWidth = 200;
	public final static int defaultHeight = 100;
	public final static int defaultHeader = 13;
	/**
	 * Key string for EAnnotation
	 */
	public static final String ANNOTATION_KEY = "RegionAnnotationKey";
	/**
	 * Key string for zone entry in EAnnotation
	 */
	public static final String ZONE_KEY = "RegionZoneKey";
	/**
	 * A default empty property string which serves when creating a region
	 * without initial graphical context.
	 */
	public static final String NONE = "";
	/**
	 * The code for a region in the TOP part of a given area.
	 */
	public static final String TOP = "T";
	/**
	 * The code for a region in the RIGHT part of a given area.
	 */
	public static final String RIGHT = "R";
	/**
	 * The code for a region in the BOTTOM part of a given area.
	 */
	public static final String BOTTOM = "B";
	/**
	 * The code for a region in the LEFT part of a given area.
	 */
	public static final String LEFT = "L";

	/**
	 * Returns a copy of the property string.
	 *
	 * @param s
	 *            a string
	 *
	 * @return a copy of string s
	 */
	public static String copy(String s) {
		return new String(s);
	}

	/**
	 * Helper to copy zone
	 */
	public static void copyZone(View from, View to) {
		if (to.getEAnnotation(ANNOTATION_KEY) == null) {
			throw new IllegalArgumentException();
		}
		if (!to.getEAnnotation(ANNOTATION_KEY).getDetails().containsKey(ZONE_KEY)) {
			throw new IllegalArgumentException();
		}
		String zone = copy(getZone(from));
		to.getEAnnotation(ANNOTATION_KEY).getDetails().put(ZONE_KEY, zone);
	}

	/**
	 * Helper to create an initialized Annotation
	 */
	public static void createRegionDefaultAnnotation(View region) {
		if (!isRegion(region)) {
			throw new IllegalArgumentException();
		}
		// now everything is fine we can go on
		// the given node is a region node
		// create EAnnotation to store region specifics
		EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
		// register annotation to region node
		eAnnotation.setEModelElement(region);
		eAnnotation.setSource(ANNOTATION_KEY);
		// assign default values for zone property
		eAnnotation.getDetails().put(ZONE_KEY, NONE);
	}

	/**
	 * Computes a bit-wise operation which account for the directions along
	 * which a region may be resized. E.g. if a region has no LEFT neighbours -
	 * sits directly at the LEFT border of a state machine - we do not allow for
	 * a resize along the WEST direction, user will have to resize the state
	 * machine or state directly. Bit-wise operations are performed according to
	 * the draw2D conventions
	 *
	 * @param s
	 *            a string
	 *
	 * @return an integer which is the result of a bit-wise operation
	 */
	public static int getAllowedResizeDirections(String s) {
		if (s == null) {
			return PositionConstants.NONE;
		}
		// this involves bit-wise operations
		// we start with none directions allowed
		// then add others
		int direction = PositionConstants.NONE;
		if (hasLeftNeighbours(s)) {
			// WEST allowed
			direction |= PositionConstants.WEST;
		}
		if (hasRightNeighbours(s)) {
			// EAST allowed
			direction |= PositionConstants.EAST;
		}
		if (hasTopNeighbours(s)) {
			// NORTH allowed
			direction |= PositionConstants.NORTH;
		}
		if (hasBottomNeighbours(s)) {
			// SOUTH allowed
			direction |= PositionConstants.SOUTH;
		}
		return direction;
	}

	/**
	 * Helper to get bounds.
	 *
	 * @param view
	 *            the region view
	 *
	 * @return a new Rectangle with same bounds as the region
	 */
	public static Rectangle getBounds(View view) {
		int x = (Integer) ViewUtil.getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_X());
		int y = (Integer) ViewUtil.getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_Y());
		int width = (Integer) ViewUtil.getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getSize_Width());
		int height = (Integer) ViewUtil.getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getSize_Height());
		return new Rectangle(x, y, width, height);
	}

	/**
	 * Helper to get height of the given view
	 *
	 * @param view
	 *            the region view
	 *
	 * @return the height of the region
	 */
	public static int getHeight(View view) {
		int height = (Integer) ViewUtil.getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getSize_Height());
		return height;
	}

	/**
	 * Checks the regions passed in the list w.r.t. to zone criterion expressed
	 * as an initial pattern to be matched and a zone not to be found in the
	 * final pattern expression and returns a list of regions matching the
	 * criterion
	 *
	 * @param regionList
	 *            a list of region nodes
	 * @param initPattern
	 *            the initial pattern the region zone must match
	 * @param excludingZoneInFinalPattern
	 *            the type of zone to exclude in the final pattern
	 *
	 * @return a list of the regions in the list matching the criterion
	 */
	public static List<View> getMatchingRegionsFromList(List<View> regionList, String initPattern, String excludingZoneInFinalPattern) {
		List<View> matchingRegions = new ArrayList<View>();
		Iterator<View> it = regionList.iterator();
		while (it.hasNext()) {
			View view = it.next();
			// get the zone of current region
			if (isStereotype(view)) {
				// stereotype comment not display
				continue;
			}
			String zone = Zone.getZone(view);
			String zoneLastPart = (zone.length() <= initPattern.length()) ? "" : zone.substring(initPattern.length());
			if (zone.startsWith(initPattern) && !zoneLastPart.contains(excludingZoneInFinalPattern)) {
				matchingRegions.add(view);
			}
		}
		return matchingRegions;
	}

	public static boolean isStereotype(View view) {
		return StereotypeDisplayUtil.getInstance().isStereotypeComment(view);
	}

	/**
	 * Provides the list of nodes which are close to the inside of the given
	 * region BOTTOM border.
	 *
	 * @param region
	 *            the region graph node
	 *
	 * @return a vector of the neighbouring graph nodes
	 */
	public static List<View> getRegionBottomBorderInsideNeighbours(View region) {
		// get the region zone
		String zone = Zone.getZone(region);
		// we are looking for regions that matches the zone of the given region
		// i.e. if region is initPattern+"T"+finalPattern where finalPattern
		// does not contain any
		// "T"
		// a matching region is initPattern+"T"+otherFinalPattern where
		// otherFinalPattern does not
		// contain any "T"
		int index = zone.lastIndexOf(Zone.TOP);
		String initPattern = zone.substring(0, index) + Zone.TOP;
		String excludingZoneInFinalPattern = Zone.TOP;
		// get the stateMachine compartment
		View stateMachineCompartment = (View) region.eContainer();
		// then the list of regions
		List<View> regionList = new ArrayList<View>();
		for (Object child : stateMachineCompartment.getChildren()) {
			if (child instanceof View) {
				regionList.add((View) child);
			}
		}
		List<View> neighbours = getMatchingRegionsFromList(regionList, initPattern, excludingZoneInFinalPattern);
		return neighbours;
	}

	/**
	 * Provides the list of regions which are close to the outside of the given
	 * region BOTTOM border.
	 *
	 * @param region
	 *            the region node
	 *
	 * @return a vector of the neighbouring regions
	 */
	public static List<View> getRegionBottomBorderOutsideNeighbours(View region) {
		// get the region zone
		String zone = Zone.getZone(region);
		// we are looking for regions that matches the zone of the given region
		// i.e. if region is initPattern+"T"+finalPattern where finalPattern
		// does not contain any
		// "T"
		// a matching region is initPattern+"B"+otherFinalPattern where
		// otherFinalPattern does not
		// contain any "B"
		int index = zone.lastIndexOf(Zone.TOP);
		String initPattern = zone.substring(0, index) + Zone.BOTTOM;
		String excludingZoneInFinalPattern = Zone.BOTTOM;
		// get the stateMachine compartment
		View stateMachineCompartment = (View) region.eContainer();
		// then the list of regions
		List<View> regionList = new ArrayList<View>();
		for (Object child : stateMachineCompartment.getChildren()) {
			if (child instanceof View) {
				regionList.add((View) child);
			}
		}
		List<View> neighbours = getMatchingRegionsFromList(regionList, initPattern, excludingZoneInFinalPattern);
		return neighbours;
	}

	/**
	 * Provides the list of nodes which are close to the inside of the given
	 * region LEFT border.
	 *
	 * @param region
	 *            the region graph node
	 *
	 * @return a vector of the neighbouring graph nodes
	 */
	public static List<View> getRegionLeftBorderInsideNeighbours(View region) {
		// get the region zone
		String zone = Zone.getZone(region);
		// we are looking for regions that matches the zone of the given region
		// i.e. if region is initPattern+"R"+finalPattern where finalPattern
		// does not contain any
		// "R"
		// a matching region is initPattern+"R"+otherFinalPattern where
		// otherFinalPattern does not
		// contain any "R"
		int index = zone.lastIndexOf(Zone.RIGHT);
		String initPattern = zone.substring(0, index) + Zone.RIGHT;
		String excludingZoneInFinalPattern = Zone.RIGHT;
		// get the stateMachine compartment
		View stateMachineCompartment = (View) region.eContainer();
		// then the list of regions
		List<View> regionList = new ArrayList<View>();
		for (Object child : stateMachineCompartment.getChildren()) {
			if (child instanceof View) {
				regionList.add((View) child);
			}
		}
		List<View> neighbours = getMatchingRegionsFromList(regionList, initPattern, excludingZoneInFinalPattern);
		return neighbours;
	}

	/**
	 * Provides the list of nodes which are close to the outside of the given
	 * region LEFT border.
	 *
	 * @param region
	 *            the region graph node
	 *
	 * @return a list of the neighbouring graph nodes
	 */
	public static List<View> getRegionLeftBorderOutsideNeighbours(View region) {
		// get the region zone
		String zone = Zone.getZone(region);
		// we are looking for regions that matches the zone of the given region
		// i.e. if region is initPattern+"R"+finalPattern where finalPattern
		// does not contain any
		// "R"
		// a matching region is initPattern+"L"+otherFinalPattern where
		// otherFinalPattern does not
		// contain any "L"
		int index = zone.lastIndexOf(Zone.RIGHT);
		String initPattern = zone.substring(0, index) + Zone.LEFT;
		String excludingZoneInFinalPattern = Zone.LEFT;
		// get the stateMachine compartment
		View stateMachineCompartment = (View) region.eContainer();
		// then the list of regions
		List<View> regionList = new ArrayList<View>();
		for (Object child : stateMachineCompartment.getChildren()) {
			if (child instanceof View) {
				regionList.add((View) child);
			}
		}
		List<View> neighbours = getMatchingRegionsFromList(regionList, initPattern, excludingZoneInFinalPattern);
		return neighbours;
	}

	/**
	 * Provides the list of nodes which are close ot the inside of the given
	 * region RIGHT border.
	 *
	 * @param region
	 *            the region graph node
	 *
	 * @return a vector of the neighbouring graph nodes
	 */
	public static List<View> getRegionRightBorderInsideNeighbours(View region) {
		// get the region zone
		String zone = Zone.getZone(region);
		// we are looking for regions that matches the zone of the given region
		// i.e. if region is initPattern+"L"+finalPattern where finalPattern
		// does not contain any
		// "L"
		// a matching region is initPattern+"L"+otherFinalPattern where
		// otherFinalPattern does not
		// contain any "L"
		int index = zone.lastIndexOf(Zone.LEFT);
		String initPattern = zone.substring(0, index) + Zone.LEFT;
		String excludingZoneInFinalPattern = Zone.LEFT;
		// get the stateMachine compartment
		View stateMachineCompartment = (View) region.eContainer();
		// then the list of regions
		List<View> regionList = new ArrayList<View>();
		for (Object child : stateMachineCompartment.getChildren()) {
			if (child instanceof View) {
				regionList.add((View) child);
			}
		}
		List<View> neighbours = getMatchingRegionsFromList(regionList, initPattern, excludingZoneInFinalPattern);
		return neighbours;
	}

	/**
	 * Provides the list of nodes which are at the outside of the given region
	 * RIGHT border.
	 *
	 * @param region
	 *            the region graph node
	 *
	 * @return a vector of the neighbouring graph nodes
	 */
	public static List<View> getRegionRightBorderOutsideNeighbours(View region) {
		// get the region zone
		String zone = Zone.getZone(region);
		// we are looking for regions that matches the zone of the given region
		// i.e. if region is initPattern+"L"+finalPattern where finalPattern
		// does not contain any
		// "L"
		// a matching region is initPattern+"R"+otherFinalPattern where
		// otherFinalPattern does not
		// contain any "R"
		int index = zone.lastIndexOf(Zone.LEFT);
		String initPattern = zone.substring(0, index) + Zone.RIGHT;
		String excludingZoneInFinalPattern = Zone.RIGHT;
		// get the stateMachine compartment
		View stateMachineCompartment = (View) region.eContainer();
		// then the list of regions
		List<View> regionList = new ArrayList<View>();
		for (Object child : stateMachineCompartment.getChildren()) {
			if (child instanceof View) {
				regionList.add((View) child);
			}
		}
		List<View> neighbours = getMatchingRegionsFromList(regionList, initPattern, excludingZoneInFinalPattern);
		return neighbours;
	}

	/**
	 * Provides the list of nodes which are close to the inside of the given
	 * region TOP border.
	 *
	 * @param region
	 *            the region node
	 *
	 * @return a vector of the neighbouring regions
	 */
	public static List<View> getRegionTopBorderInsideNeighbours(View region) {
		// get the region zone
		String zone = Zone.getZone(region);
		// we are looking for regions that matches the zone of the given region
		// i.e. if region is initPattern+"B"+finalPattern where finalPattern
		// does not contain any
		// "B"
		// a matching region is initPattern+"B"+otherFinalPattern where
		// otherFinalPattern does not
		// contain any "B"
		int index = zone.lastIndexOf(Zone.BOTTOM);
		String initPattern = zone.substring(0, index) + Zone.BOTTOM;
		String excludingZoneInFinalPattern = Zone.BOTTOM;
		// get the stateMachine compartment
		View stateMachineCompartment = (View) region.eContainer();
		// then the list of regions
		List<View> regionList = new ArrayList<View>();
		for (Object child : stateMachineCompartment.getChildren()) {
			if (child instanceof View) {
				regionList.add((View) child);
			}
		}
		List<View> neighbours = getMatchingRegionsFromList(regionList, initPattern, excludingZoneInFinalPattern);
		return neighbours;
	}

	/**
	 * Provides the list of nodes which are close to the outside of the given
	 * region TOP border.
	 *
	 * @param region
	 *            the region graph node
	 *
	 * @return a vector of the neighbouring graph nodes
	 */
	public static List<View> getRegionTopBorderOutsideNeighbours(View region) {
		// get the region zone
		String zone = Zone.getZone(region);
		// we are looking for regions that matches the zone of the given region
		// i.e. if region is initPattern+"B"+finalPattern where finalPattern
		// does not contain any
		// "B"
		// a matching region is initPattern+"T"+otherFinalPattern where
		// otherFinalPattern does not
		// contain any "T"
		int index = zone.lastIndexOf(Zone.BOTTOM);
		String initPattern = zone.substring(0, index) + Zone.TOP;
		String excludingZoneInFinalPattern = Zone.TOP;
		// get the stateMachine compartment
		View stateMachineCompartment = (View) region.eContainer();
		// then the list of regions
		List<View> regionList = new ArrayList<View>();
		for (Object child : stateMachineCompartment.getChildren()) {
			if (child instanceof View) {
				regionList.add((View) child);
			}
		}
		List<View> neighbours = getMatchingRegionsFromList(regionList, initPattern, excludingZoneInFinalPattern);
		return neighbours;
	}

	/**
	 * Helper to get width
	 *
	 * @param region
	 *            the region graph node
	 *
	 * @return the width of the region
	 */
	public static int getWidth(View view) {
		int width = (Integer) ViewUtil.getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getSize_Width());
		return width;
	}

	/**
	 * Helper to get x
	 *
	 * @param region
	 *            the region graph node
	 *
	 * @return the x of the region
	 */
	public static int getX(View view) {
		int x = (Integer) ViewUtil.getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_X());
		return x;
	}

	/**
	 * Helper to get y
	 *
	 * @param region
	 *            the region graph node
	 *
	 * @return the y of the region
	 */
	public static int getY(View view) {
		int y = (Integer) ViewUtil.getStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_Y());
		return y;
	}

	/**
	 * Helper to access zone
	 *
	 * @param region
	 *            the region graph node
	 *
	 * @return the zone of the region
	 */
	public static String getZone(View view) {
		if (view.getEAnnotation(ANNOTATION_KEY) == null) {
			return null;
		}
		if (!view.getEAnnotation(ANNOTATION_KEY).getDetails().containsKey(ZONE_KEY)) {
			throw new IllegalArgumentException();
		}
		return view.getEAnnotation(ANNOTATION_KEY).getDetails().get(ZONE_KEY);
	}

	/**
	 * Returns the zone counterpart of the given zone i.e. if Zone.RIGHT then
	 * Zone.LEFT is returned, etc.
	 *
	 * @param zone
	 *            the zone
	 *
	 * @return the counterpart zone
	 */
	private static String getZoneCounterpart(String s) {
		if (!Strings.isNullOrEmpty(s)) {
			String cs = s.substring(0, s.length() - 1);
			if (Zone.isRight(s)) {
				return Zone.setLeft(cs);
			}
			if (Zone.isLeft(s)) {
				return Zone.setRight(cs);
			}
			if (Zone.isBottom(s)) {
				return Zone.setTop(cs);
			}
			if (Zone.isTop(s)) {
				return Zone.setBottom(cs);
			}
		}
		return s;
	}

	/**
	 * Computes the zone at location within bounds using absolute coordinates
	 *
	 * @param location
	 *            the location
	 * @param rect
	 *            the bounds
	 *
	 * @return the zone
	 */
	public static String getZoneFromLocationInRectangleWithAbsoluteCoordinates(Point location, Rectangle rect) {
		// d1 is for the first diagonal (going up) rect
		double d1 = location.y - 1.0 * rect.height / rect.width * (rect.x - location.x) - rect.y - rect.height;
		// d2 is for the second (going down)
		double d2 = location.y + 1.0 * rect.height / rect.width * (rect.x - location.x) - rect.y;
		if ((d1 <= 0) && (d2 <= 0)) {
			return Zone.TOP;
		}
		if ((d1 <= 0) && (d2 > 0)) {
			return Zone.LEFT;
		}
		if ((d1 > 0) && (d2 <= 0)) {
			return Zone.RIGHT;
		}
		if ((d1 > 0) && (d2 > 0)) {
			return Zone.BOTTOM;
		}
		return Zone.NONE;
	}

	/**
	 * Computes the zone at location within bounds using local coordinates
	 *
	 * @param location
	 *            the location
	 * @param rect
	 *            the bounds
	 *
	 * @return the zone
	 */
	public static String getZoneFromLocationInRectangleWithLocalCoordinates(Point location, Rectangle rect) {
		// d1 is for the first diagonal (going up) rect
		double d1 = location.y + 1.0 * rect.height * location.x / rect.width - rect.height;
		// d2 is for the second (going down)
		double d2 = location.y - 1.0 * rect.height * location.x / rect.width;
		if ((d1 <= 0) && (d2 <= 0)) {
			return Zone.TOP;
		}
		if ((d1 <= 0) && (d2 > 0)) {
			return Zone.LEFT;
		}
		if ((d1 > 0) && (d2 <= 0)) {
			return Zone.RIGHT;
		}
		if ((d1 > 0) && (d2 > 0)) {
			return Zone.BOTTOM;
		}
		return Zone.NONE;
	}

	/**
	 * Checks whether the given location has any BOTTOM neighbours. Or said
	 * differently has a BOTTOM border.
	 *
	 * @param s
	 *            a string
	 *
	 * @return boolean true or false
	 */
	public static boolean hasBottomNeighbours(String s) {
		if (s == null) {
			throw new IllegalArgumentException();
		}
		return s.contains(Zone.TOP);
	}

	/**
	 * Checks whether the given location has any LEFT neighbours. Or said
	 * differently has a LEFT border.
	 *
	 * @param s
	 *            a string
	 *
	 * @return boolean true or false
	 */
	public static boolean hasLeftNeighbours(String s) {
		if (s == null) {
			throw new IllegalArgumentException();
		}
		return s.contains(Zone.RIGHT);
	}

	/**
	 * Checks whether the given location has any RIGHT neighbours. Or said
	 * differently has a RIGHT border.
	 *
	 * @param s
	 *            a string
	 *
	 * @return boolean true or false
	 */
	public static boolean hasRightNeighbours(String s) {
		if (s == null) {
			throw new IllegalArgumentException();
		}
		return s.contains(Zone.LEFT);
	}

	/**
	 * Checks whether the given location has any TOP neighbours. Or said
	 * differently has a TOP border.
	 *
	 * @param s
	 *            a string
	 *
	 * @return boolean true or false
	 */
	public static boolean hasTopNeighbours(String s) {
		if (s == null) {
			throw new IllegalArgumentException();
		}
		return s.contains(Zone.BOTTOM);
	}

	/**
	 * Checks whether the leaf location encoded is BOTTOM.
	 *
	 * @param s
	 *            a string
	 *
	 * @return boolean true or false
	 */
	public static boolean isBottom(String s) {
		if (s == null) {
			return false;
		}
		return s.endsWith(Zone.BOTTOM);
	}

	/**
	 * Checks whether the leaf location encoded is LEFT.
	 *
	 * @param s
	 *            a string
	 *
	 * @return boolean true or false
	 */
	public static boolean isLeft(String s) {
		if (s == null) {
			return false;
		}
		return s.endsWith(Zone.LEFT);
	}

	/**
	 * Checks whether the view is a region
	 *
	 * @param view
	 *            a View
	 * @return boolean true or false
	 */
	public static boolean isRegion(View view) {
		if (view == null) {
			return false;
		}
		return (UMLVisualIDRegistry.getVisualID(view.getType()) == RegionEditPart.VISUAL_ID);
	}

	/**
	 * Checks whether the leaf location encoded is RIGHT.
	 *
	 * @param s
	 *            a string
	 *
	 * @return boolean true or false
	 */
	public static boolean isRight(String s) {
		if (s == null) {
			return false;
		}
		return s.endsWith(Zone.RIGHT);
	}

	/**
	 * Checks whether the view is a state machine
	 *
	 * @param view
	 *            a View
	 * @return boolean true or false
	 */
	public static boolean isStateMachine(View view) {
		if (view == null) {
			throw new IllegalArgumentException();
		}
		return (UMLVisualIDRegistry.getVisualID(view.getType()) == StateMachineEditPart.VISUAL_ID);
	}

	/**
	 * Checks whether the leaf location encoded is TOP.
	 *
	 * @param s
	 *            a string
	 * @return boolean true or false
	 */
	public static boolean isTop(String s) {
		if (s == null) {
			return false;
		}
		return s.endsWith(Zone.TOP);
	}

	/**
	 * Resets the zone of the region counterparts i.e. if region passed has a
	 * "XXXR" zone then its counterparts are "XXXL*", etc. Resetting their zones
	 * mean they are changed into "XXX*" "L" is removed
	 *
	 * @param region
	 *            the region node
	 */
	public static void resetRegionCounterpartZone(View region) {
		// get the region zone
		String zone = getZone(region);
		// get its counterpart
		if (Strings.isNullOrEmpty(zone)) {
			return;
		}
		String cZone = getZoneCounterpart(zone);
		// get the stateMachine compartment
		View stateMachineCompartment = (View) region.eContainer();
		Iterator<?> it = stateMachineCompartment.getChildren().iterator();
		while (it.hasNext()) {
			Object next = it.next();
			if (next instanceof View) {
				View view = (View) next;
				String currentZone = getZone(view);
				if (currentZone.startsWith(cZone)) {
					String initPart = currentZone.substring(0, cZone.length() - 1);
					String finalPart = (currentZone.length() <= cZone.length()) ? "" : currentZone.substring(cZone.length());
					currentZone = initPart + finalPart;
					setZone(view, currentZone);
				}
			}
		}
	}

	/**
	 * Adds a final "B" to the given property string. This is used when a
	 * horizontal region is created. The new region is always at the BOTTOM, the
	 * old one is on TOP.
	 *
	 * @param s
	 *            a string
	 *
	 * @return the updated string
	 */
	public static String setBottom(String s) {
		if (s == null) {
			throw new IllegalArgumentException();
		}
		return s + Zone.BOTTOM;
	}

	/**
	 * Helper to set zone
	 *
	 * @param view
	 *            a View
	 */
	public static void setBottom(View view) {
		if (view.getEAnnotation(ANNOTATION_KEY) == null) {
			throw new IllegalArgumentException();
		}
		if (!view.getEAnnotation(ANNOTATION_KEY).getDetails().containsKey(ZONE_KEY)) {
			throw new IllegalArgumentException();
		}
		String currentZone = Zone.getZone(view);
		String zoneToSet = Zone.setBottom(currentZone);
		view.getEAnnotation(ANNOTATION_KEY).getDetails().put(ZONE_KEY, zoneToSet);
	}

	/**
	 * Helper to set bounds
	 *
	 * @param view
	 *            a View
	 * @param bounds
	 *            the bounds
	 */
	public static void setBounds(View view, Rectangle bounds) {
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_X(), bounds.x);
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_Y(), bounds.y);
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getSize_Width(), bounds.width);
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getSize_Height(), bounds.height);
	}

	/**
	 * Helper to set height
	 *
	 * @param view
	 *            a View
	 * @param height
	 *            the height
	 */
	public static void setHeight(View view, int height) {
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getSize_Height(), height);
	}

	/**
	 * Adds a final "L" to the given property string. This is used when a
	 * vertical region is created. The new region is always on the RIGHT, the
	 * old one is on the LEFT.
	 *
	 * @param s
	 *            a string
	 *
	 * @return the updated string
	 */
	public static String setLeft(String s) {
		if (s == null) {
			throw new IllegalArgumentException();
		}
		return s + Zone.LEFT;
	}

	/**
	 * Helper to set zone
	 *
	 * @param view
	 *            a View
	 */
	public static void setLeft(View view) {
		if (view.getEAnnotation(ANNOTATION_KEY) == null) {
			throw new IllegalArgumentException();
		}
		if (!view.getEAnnotation(ANNOTATION_KEY).getDetails().containsKey(ZONE_KEY)) {
			throw new IllegalArgumentException();
		}
		String currentZone = Zone.getZone(view);
		String zoneToSet = Zone.setLeft(currentZone);
		view.getEAnnotation(ANNOTATION_KEY).getDetails().put(ZONE_KEY, zoneToSet);
	}

	/**
	 * Adds a final "R" to the given property string. This is used when a
	 * vertical region is created. The new region is always on the RIGHT, the
	 * old one is on the LEFT.
	 *
	 * @param s
	 *            a string
	 *
	 * @return the updated string
	 */
	public static String setRight(String s) {
		if (s == null) {
			throw new IllegalArgumentException();
		}
		return s + Zone.RIGHT;
	}

	/**
	 * Helper to set zone
	 *
	 * @param view
	 *            a View
	 */
	public static void setRight(View view) {
		if (view.getEAnnotation(ANNOTATION_KEY) == null) {
			throw new IllegalArgumentException();
		}
		if (!view.getEAnnotation(ANNOTATION_KEY).getDetails().containsKey(ZONE_KEY)) {
			throw new IllegalArgumentException();
		}
		String currentZone = Zone.getZone(view);
		String zoneToSet = Zone.setRight(currentZone);
		view.getEAnnotation(ANNOTATION_KEY).getDetails().put(ZONE_KEY, zoneToSet);
	}

	/**
	 * Adds a final "T" to the given property string. This is used when a
	 * horizontal region is created. The new region is always at the BOTTOM, the
	 * old one is on TOP.
	 *
	 * @param s
	 *            a string
	 *
	 * @return the updated string
	 */
	public static String setTop(String s) {
		if (s == null) {
			throw new IllegalArgumentException();
		}
		return s + Zone.TOP;
	}

	/**
	 * Helper to set zone
	 *
	 * @param view
	 *            a View
	 */
	public static void setTop(View view) {
		if (view.getEAnnotation(ANNOTATION_KEY) == null) {
			throw new IllegalArgumentException();
		}
		if (!view.getEAnnotation(ANNOTATION_KEY).getDetails().containsKey(ZONE_KEY)) {
			throw new IllegalArgumentException();
		}
		String currentZone = Zone.getZone(view);
		String zoneToSet = Zone.setTop(currentZone);
		view.getEAnnotation(ANNOTATION_KEY).getDetails().put(ZONE_KEY, zoneToSet);
	}

	/**
	 * Helper to set width
	 *
	 * @param view
	 *            a View
	 * @param width
	 *            the width
	 */
	public static void setWidth(View view, int width) {
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getSize_Width(), width);
	}

	/**
	 * Helper to set x
	 *
	 * @param view
	 *            a View
	 * @param x
	 *            the x
	 */
	public static void setX(View view, int x) {
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_X(), x);
	}

	/**
	 * Helper to set y
	 *
	 * @param view
	 *            a View
	 * @param y
	 *            the y
	 */
	public static void setY(View view, int y) {
		ViewUtil.setStructuralFeatureValue(view, NotationPackage.eINSTANCE.getLocation_Y(), y);
	}

	/**
	 * Helper to set zone with passed zone
	 *
	 * @param view
	 *            a View
	 * @param zone
	 *            a Zone
	 */
	public static void setZone(View view, String zone) {
		if (view.getEAnnotation(ANNOTATION_KEY) == null) {
			throw new IllegalArgumentException();
		}
		if (!view.getEAnnotation(ANNOTATION_KEY).getDetails().containsKey(ZONE_KEY)) {
			throw new IllegalArgumentException();
		}
		view.getEAnnotation(ANNOTATION_KEY).getDetails().put(ZONE_KEY, zone);
	}
}

Back to the top