Project

General

Profile

Download (2.51 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
import org.eclipse.draw2d.geometry.PointList;
15
import org.eclipse.draw2d.geometry.Ray;
16

    
17
/**
18
 * Automatic router that spreads its {@link Connection Connections} in a
19
 * fan-like fashion upon collision.
20
 */
21
public class FanRouter extends AutomaticRouter {
22

    
23
	private int separation = 10;
24

    
25
	/**
26
	 * Returns the separation in pixels between fanned connections.
27
	 * 
28
	 * @return the separation
29
	 * @since 2.0
30
	 */
31
	public int getSeparation() {
32
		return separation;
33
	}
34

    
35
	/**
36
	 * Modifies a given PointList that collides with some other PointList. The
37
	 * given <i>index</i> indicates that this it the i<sup>th</sup> PointList in
38
	 * a group of colliding points.
39
	 * 
40
	 * @param points
41
	 *            the colliding points
42
	 * @param index
43
	 *            the index
44
	 */
45
	protected void handleCollision(PointList points, int index) {
46
		Point start = points.getFirstPoint();
47
		Point end = points.getLastPoint();
48

    
49
		if (start.equals(end))
50
			return;
51

    
52
		Point midPoint = new Point((end.x + start.x) / 2, (end.y + start.y) / 2);
53
		int position = end.getPosition(start);
54
		Ray ray;
55
		if (position == PositionConstants.SOUTH
56
				|| position == PositionConstants.EAST)
57
			ray = new Ray(start, end);
58
		else
59
			ray = new Ray(end, start);
60
		double length = ray.length();
61

    
62
		double xSeparation = separation * ray.x / length;
63
		double ySeparation = separation * ray.y / length;
64

    
65
		Point bendPoint;
66

    
67
		if (index % 2 == 0) {
68
			bendPoint = new Point(
69
					midPoint.x + (index / 2) * (-1 * ySeparation), midPoint.y
70
							+ (index / 2) * xSeparation);
71
		} else {
72
			bendPoint = new Point(midPoint.x + (index / 2) * ySeparation,
73
					midPoint.y + (index / 2) * (-1 * xSeparation));
74
		}
75
		if (!bendPoint.equals(midPoint))
76
			points.insertPoint(bendPoint, 1);
77
	}
78

    
79
	/**
80
	 * Sets the colliding {@link Connection Connection's} separation in pixels.
81
	 * 
82
	 * @param value
83
	 *            the separation
84
	 * @since 2.0
85
	 */
86
	public void setSeparation(int value) {
87
		separation = value;
88
	}
89

    
90
}
(59-59/171)