Project

General

Profile

Download (4.04 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.swt.graphics.Color;
14

    
15
import org.eclipse.draw2d.geometry.Point;
16
import org.eclipse.draw2d.geometry.PointList;
17
import org.eclipse.draw2d.geometry.Transform;
18

    
19
/**
20
 * A rotatable, polygon shaped decoration most commonly used for decorating the
21
 * ends of {@link org.eclipse.draw2d.Polyline polylines}.
22
 */
23
public class PolygonDecoration extends Polygon implements RotatableDecoration {
24

    
25
	/**
26
	 * Template for a triangle that points to the right when the rotation angle
27
	 * is 0
28
	 */
29
	public static final PointList TRIANGLE_TIP = new PointList();
30
	/**
31
	 * Template for a triangle that points to the left when the rotation angle
32
	 * is 0
33
	 */
34
	public static final PointList INVERTED_TRIANGLE_TIP = new PointList();
35

    
36
	static {
37
		TRIANGLE_TIP.addPoint(0, 0);
38
		TRIANGLE_TIP.addPoint(-1, 1);
39
		TRIANGLE_TIP.addPoint(-1, -1);
40

    
41
		INVERTED_TRIANGLE_TIP.addPoint(0, 1);
42
		INVERTED_TRIANGLE_TIP.addPoint(0, -1);
43
		INVERTED_TRIANGLE_TIP.addPoint(-1, 0);
44
	}
45

    
46
	private Point location = new Point();
47
	private PointList template = TRIANGLE_TIP;
48
	private Transform transform = new Transform();
49

    
50
	/**
51
	 * Constructs a PolygonDecoration. Defaults the PolygonDecoration to fill
52
	 * its region with black.
53
	 * 
54
	 * @since 2.0
55
	 */
56
	public PolygonDecoration() {
57
		setFill(true);
58
		setScale(7, 3);
59
	}
60

    
61
	/**
62
	 * @see org.eclipse.draw2d.IFigure#getBackgroundColor()
63
	 */
64
	public Color getLocalBackgroundColor() {
65
		if (super.getLocalBackgroundColor() == null)
66
			return getForegroundColor();
67
		return super.getLocalBackgroundColor();
68
	}
69

    
70
	/**
71
	 * Returns the points in the PolygonDecoration as a PointList.
72
	 * 
73
	 * @return the points in this PolygonDecoration
74
	 * @since 2.0
75
	 */
76
	public PointList getPoints() {
77
		if (points == null) {
78
			points = new PointList();
79
			for (int i = 0; i < template.size(); i++)
80
				points.addPoint(transform.getTransformed(template.getPoint(i)));
81
		}
82
		return points;
83
	}
84

    
85
	/**
86
	 * Sets the location of this PolygonDecoration.
87
	 * 
88
	 * @param p
89
	 *            the new location
90
	 */
91
	public void setLocation(Point p) {
92
		points = null;
93
		bounds = null;
94
		location.setLocation(p);
95
		transform.setTranslation(p.x, p.y);
96
	}
97

    
98
	/**
99
	 * Sets the PolygonDecorations point template to the passed PointList. This
100
	 * template is an outline of the PolygonDecoration's region. (The default
101
	 * value is TRIANGLE_TIP which is a triangle whose tip is pointing to the
102
	 * right).
103
	 * 
104
	 * @param pl
105
	 *            the PointList outline to use as the PolygonDecoration's region
106
	 * @since 2.0
107
	 */
108
	public void setTemplate(PointList pl) {
109
		erase();
110
		template = pl;
111
		points = null;
112
		bounds = null;
113
		repaint();
114
	}
115

    
116
	/**
117
	 * Sets the amount of scaling to be done along X and Y axes on the
118
	 * PolygonDecoration's template.
119
	 * 
120
	 * @param x
121
	 *            X scaling
122
	 * @param y
123
	 *            Y scaling
124
	 * @since 2.0
125
	 */
126
	public void setScale(double x, double y) {
127
		points = null;
128
		bounds = null;
129
		transform.setScale(x, y);
130
	}
131

    
132
	/**
133
	 * Sets the rotation of this decoration so that the decoration points toward
134
	 * the given reference point.
135
	 * 
136
	 * @param ref
137
	 *            the reference point
138
	 */
139
	public void setReferencePoint(Point ref) {
140
		Point pt = Point.SINGLETON;
141
		pt.setLocation(ref);
142
		pt.negate().translate(location);
143
		setRotation(Math.atan2(pt.y, pt.x));
144
	}
145

    
146
	/**
147
	 * Sets the angle by which rotation is to be done on the PolygonDecoration.
148
	 * 
149
	 * @param angle
150
	 *            Angle of rotation
151
	 * @since 2.0
152
	 */
153
	public void setRotation(double angle) {
154
		points = null;
155
		bounds = null;
156
		transform.setRotation(angle);
157
	}
158

    
159
}
(114-114/171)