Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5ad562b11273db749ca56a8bdf37347019646862 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012, 2013, 2014 Red Hat, Inc.
 *  All rights reserved.
 * This program is 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:
 * Red Hat, Inc. - initial API and implementation
 *
 * @author Bob Brodt
 ******************************************************************************/

package org.eclipse.bpmn2.modeler.core.utils;

import org.eclipse.bpmn2.modeler.core.features.GraphitiConstants;
import org.eclipse.bpmn2.modeler.core.utils.GraphicsUtil.LineSegment;
import org.eclipse.graphiti.datatypes.ILocation;
import org.eclipse.graphiti.mm.algorithms.styles.Point;
import org.eclipse.graphiti.mm.pictograms.FixPointAnchor;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.graphiti.services.Graphiti;

public enum AnchorSite {
	/** These ordinals MUST have the same meanings as {@see GraphicsUtil#getEdges(Shape)} */
	TOP("anchor.top"), BOTTOM("anchor.bottom"), LEFT("anchor.left"), RIGHT("anchor.right"), CENTER("anchor.center"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$

	private final String key;

	private AnchorSite(String key) {
		this.key = key;
	}

	public String getKey() {
		return key;
	}

	public static AnchorSite getSite(String key) {
		for (AnchorSite l : values()) {
			if (l.getKey().equals(key)) {
				return l;
			}
		}
		
		return CENTER;
//		throw new IllegalArgumentException("Cannot determine Anchor Site "+key);
	}
	
	public static AnchorSite getSite(FixPointAnchor anchor) {
		return getSite(Graphiti.getPeService().getPropertyValue(anchor, GraphitiConstants.ANCHOR_LOCATION));
	}
	
	public static void setSite(FixPointAnchor anchor, AnchorSite site) {
		Graphiti.getPeService().setPropertyValue(anchor, GraphitiConstants.ANCHOR_LOCATION, site.getKey());
	}
	
	/**
	 * Return the AnchorSide value of the shape's edge that is nearest
	 * to the given point.
	 * 
	 * @param shape
	 * @param p
	 * @return
	 */
	public static AnchorSite getNearestEdge(Shape shape, Point p) {
		LineSegment edge = GraphicsUtil.findNearestEdge(shape, p);
		ILocation loc = Graphiti.getPeService().getLocationRelativeToDiagram(shape);
		AnchorSite site;
		if (edge.isHorizontal()) {
			int y = edge.getStart().getY();
			if (y==loc.getY())
				site = TOP;
			else
				site = BOTTOM;
		}
		else {
			int x = edge.getStart().getX();
			if (x==loc.getX())
				site = LEFT;
			else
				site = RIGHT;
		}
		return site;
	}
	
	/**
	 * Return the AnchorSide value of the shape's edge that is nearest
	 * to the given point.
	 * 
	 * @param shape
	 * @param p
	 * @return
	 */
	public static AnchorSite getNearestEdge(Shape shape, Point p1, Point p2) {
		LineSegment edge = GraphicsUtil.findNearestEdge(shape, p1, p2);
		ILocation loc = Graphiti.getPeService().getLocationRelativeToDiagram(shape);
		AnchorSite site;
		if (edge.isHorizontal()) {
			int y = edge.getStart().getY();
			if (y==loc.getY())
				site = TOP;
			else
				site = BOTTOM;
		}
		else {
			int x = edge.getStart().getX();
			if (x==loc.getX())
				site = LEFT;
			else
				site = RIGHT;
		}
		return site;
	}
}

Back to the top