Project

General

Profile

Download (2.15 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 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
 *******************************************************************************/
11
package org.eclipse.draw2d;
12

    
13
import org.eclipse.draw2d.geometry.Point;
14

    
15
/**
16
 * A ConnectionLocator that is used to place figures at the midpoint between two
17
 * points on a {@link org.eclipse.draw2d.Connection}.
18
 */
19
public class MidpointLocator extends ConnectionLocator {
20

    
21
	private int index;
22

    
23
	/**
24
	 * Constructs a MidpointLocator with associated Connection <i>c</i> and
25
	 * index <i>i</i>. The points at index i and i+1 on the connection are used
26
	 * to calculate the midpoint of the line segment.
27
	 * 
28
	 * @param c
29
	 *            the connection associated with the locator
30
	 * @param i
31
	 *            the point from where the connection's midpoint will be
32
	 *            calculated.
33
	 * @since 2.0
34
	 */
35
	public MidpointLocator(Connection c, int i) {
36
		super(c);
37
		index = i;
38
	}
39

    
40
	/**
41
	 * Returns this MidpointLocator's index. This integer represents the
42
	 * position of the start point in this MidpointLocator's associated
43
	 * {@link Connection} from where midpoint calculation will be made.
44
	 * 
45
	 * @return the locator's index
46
	 * @since 2.0
47
	 */
48

    
49
	protected int getIndex() {
50
		return index;
51
	}
52

    
53
	/**
54
	 * Returns the point of reference associated with this locator. This point
55
	 * will be midway between points at 'index' and 'index' + 1.
56
	 * 
57
	 * @return the reference point
58
	 * @since 2.0
59
	 */
60
	protected Point getReferencePoint() {
61
		Connection conn = getConnection();
62
		Point p = Point.SINGLETON;
63
		Point p1 = conn.getPoints().getPoint(getIndex());
64
		Point p2 = conn.getPoints().getPoint(getIndex() + 1);
65
		conn.translateToAbsolute(p1);
66
		conn.translateToAbsolute(p2);
67
		p.x = (p2.x - p1.x) / 2 + p1.x;
68
		p.y = (p2.y - p1.y) / 2 + p1.y;
69
		return p;
70
	}
71

    
72
}
(106-106/171)