comparison org.eclipse.draw2d/src/org/eclipse/draw2d/ConnectionLocator.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.draw2d.ConnectionLocator;
14
15 import java.lang.all;
16
17 import org.eclipse.draw2d.geometry.Dimension;
18 import org.eclipse.draw2d.geometry.Point;
19 import org.eclipse.draw2d.geometry.PointList;
20 import org.eclipse.draw2d.AbstractLocator;
21 import org.eclipse.draw2d.Connection;
22
23 /**
24 * Repositions a {@link Figure} attached to a {@link Connection} when the Connection is
25 * moved. Provides for alignment at the start (source), middle, or end (target) of the
26 * Connection.
27 */
28 public class ConnectionLocator
29 : AbstractLocator
30 {
31
32 /** @deprecated Use {@link #SOURCE} */
33 public static const int START = 2;
34 /** The start (or source) of the Connection */
35 public static const int SOURCE = 2;
36
37 /** @deprecated Use {@link #TARGET} */
38 public static const int END = 3;
39 /** The end (or target) of the Connection */
40 public static const int TARGET = 3;
41
42 /**
43 * @deprecated Use {@link #MIDDLE} instead, since the location is not the midpoint of a
44 * line-segment, but the middle of a polyline.
45 */
46 public static const int MIDPOINT = 4;
47 /** The middle of the Connection */
48 public static const int MIDDLE = 4;
49
50 private Connection connection;
51 private int alignment;
52
53 /**
54 * Constructs a ConnectionLocator with the passed connection and {@link #MIDDLE}
55 * alignment.
56 *
57 * @param connection The Connection
58 * @since 2.0
59 */
60 public this(Connection connection) {
61 this(connection, MIDDLE);
62 }
63
64 /**
65 * Constructs a ConnectionLocator with the passed Connection and alignment. Valid values
66 * for the alignment are integer constants {@link #SOURCE}, {@link #MIDDLE}, and
67 * {@link #TARGET}.
68 *
69 * @param connection The Connection
70 * @param align The alignment
71 *
72 * @since 2.0
73 */
74 public this(Connection connection, int align_) {
75 setConnection(connection);
76 setAlignment(align_);
77 }
78
79 /**
80 * Returns the alignment of ConnectionLocator.
81 *
82 * @return The alignment
83 * @since 2.0
84 */
85 public int getAlignment() {
86 return alignment;
87 }
88
89 /**
90 * Returns connection associated with ConnectionLocator.
91 *
92 * @return The Connection
93 * @since 2.0
94 */
95 protected Connection getConnection() {
96 return connection;
97 }
98
99 /**
100 * Returns ConnectionLocator's reference point in absolute coordinates.
101 *
102 * @return The reference point
103 * @since 2.0
104 */
105 protected Point getReferencePoint() {
106 Point p = getLocation(getConnection().getPoints());
107 getConnection().translateToAbsolute(p);
108 return p;
109 }
110
111 /**
112 * Returns a point from the passed PointList, dependent on ConnectionLocator's alignment.
113 * If the alignment is {@link #SOURCE}, it returns the first point in <i>points</i>. If
114 * {@link #TARGET}, it returns the last point in <i>points</i>. If {@link #MIDDLE}, it
115 * returns the middle of line represented by <i>points</i>.
116 *
117 * @param points The points in the Connection
118 * @return The location
119 * @since 2.0
120 */
121 protected Point getLocation(PointList points) {
122 switch (getAlignment()) {
123 case SOURCE:
124 return points.getPoint(Point.SINGLETON, 0);
125 case TARGET:
126 return points.getPoint(Point.SINGLETON, points.size() - 1);
127 case MIDDLE:
128 if (points.size() % 2 is 0) {
129 int i = points.size() / 2;
130 int j = i - 1;
131 Point p1 = points.getPoint(j);
132 Point p2 = points.getPoint(i);
133 Dimension d = p2.getDifference(p1);
134 return Point.SINGLETON.setLocation(p1.x + d.width / 2,
135 p1.y + d.height / 2);
136 }
137 int i = (points.size() - 1) / 2;
138 return points.getPoint(Point.SINGLETON, i);
139 default:
140 return new Point();
141 }
142 }
143
144 /**
145 * Sets the alignment. Possible values are {@link #SOURCE}, {@link #MIDDLE}, and
146 * {@link #TARGET}.
147 *
148 * @param align The alignment
149 * @since 2.0
150 */
151 protected void setAlignment(int align_) {
152 alignment = align_;
153 }
154
155 /**
156 * Sets the Connection to be associated with this ConnectionLocator.
157 *
158 * @param connection The Connection
159 * @since 2.0
160 */
161 protected void setConnection(Connection connection) {
162 this.connection = connection;
163 }
164
165 }