comparison org.eclipse.draw2d/src/org/eclipse/draw2d/PolygonDecoration.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.PolygonDecoration;
14
15 import java.lang.all;
16
17
18
19 import org.eclipse.swt.graphics.Color;
20 import org.eclipse.draw2d.geometry.Point;
21 import org.eclipse.draw2d.geometry.PointList;
22 import org.eclipse.draw2d.geometry.Transform;
23 import org.eclipse.draw2d.RotatableDecoration;
24 import org.eclipse.draw2d.Polygon;
25
26 /**
27 * A rotatable, polygon shaped decoration most commonly used for decorating the ends of
28 * {@link org.eclipse.draw2d.Polyline polylines}.
29 */
30 public class PolygonDecoration
31 : Polygon
32 , RotatableDecoration
33 {
34
35 /** Template for a triangle that points to the right when the rotation angle is 0 */
36 private static bool initStaticConsts_done = false;
37 private static PointList TRIANGLE_TIP_;
38 /** Template for a triangle that points to the left when the rotation angle is 0 */
39 private static PointList INVERTED_TRIANGLE_TIP_;
40
41 public static PointList TRIANGLE_TIP(){
42 if( !initStaticConsts_done ) initStaticConsts();
43 return TRIANGLE_TIP_;
44 }
45 public static PointList INVERTED_TRIANGLE_TIP(){
46 if( !initStaticConsts_done ) initStaticConsts();
47 return INVERTED_TRIANGLE_TIP_;
48 }
49
50 private static void initStaticConsts() {
51 synchronized( PolygonDecoration.classinfo ){
52 if( !initStaticConsts_done ){
53 TRIANGLE_TIP_ = new PointList();
54 TRIANGLE_TIP_.addPoint(0, 0);
55 TRIANGLE_TIP_.addPoint(-1, 1);
56 TRIANGLE_TIP_.addPoint(-1, -1);
57
58 INVERTED_TRIANGLE_TIP_ = new PointList();
59 INVERTED_TRIANGLE_TIP_.addPoint(0, 1);
60 INVERTED_TRIANGLE_TIP_.addPoint(0, -1);
61 INVERTED_TRIANGLE_TIP_.addPoint(-1, 0);
62
63 initStaticConsts_done = true;
64 }
65 }
66 }
67
68 private Point location;
69 private PointList template_;
70 private Transform transform;
71
72 /**
73 * Constructs a PolygonDecoration. Defaults the PolygonDecoration to fill its region
74 * with black.
75 *
76 * @since 2.0
77 */
78 public this() {
79 location = new Point();
80 template_ = TRIANGLE_TIP;
81 transform = new Transform();
82 setFill(true);
83 setScale(7, 3);
84 }
85
86 /**
87 * @see org.eclipse.draw2d.IFigure#getBackgroundColor()
88 */
89 public Color getLocalBackgroundColor() {
90 if (super.getLocalBackgroundColor() is null)
91 return getForegroundColor();
92 return super.getLocalBackgroundColor();
93 }
94
95 /**
96 * Returns the points in the PolygonDecoration as a PointList.
97 *
98 * @return the points in this PolygonDecoration
99 * @since 2.0
100 */
101 public PointList getPoints() {
102 if (points is null) {
103 points = new PointList();
104 for (int i = 0; i < template_.size(); i++)
105 points.addPoint(transform.getTransformed(template_.getPoint(i)));
106 }
107 return points;
108 }
109
110 /**
111 * Sets the location of this PolygonDecoration.
112 * @param p the new location
113 */
114 public void setLocation(Point p) {
115 points = null;
116 bounds = null;
117 location.setLocation(p);
118 transform.setTranslation(p.x, p.y);
119 }
120
121 /**
122 * Sets the PolygonDecorations point template_ to the passed PointList. This template_ is an
123 * outline of the PolygonDecoration's region. (The default value is TRIANGLE_TIP which is
124 * a triangle whose tip is pointing to the right).
125 *
126 * @param pl the PointList outline to use as the PolygonDecoration's region
127 * @since 2.0
128 */
129 public void setTemplate(PointList pl) {
130 erase();
131 template_ = pl;
132 points = null;
133 bounds = null;
134 repaint();
135 }
136
137 /**
138 * Sets the amount of scaling to be done along X and Y axes on the PolygonDecoration's
139 * template_.
140 *
141 * @param x X scaling
142 * @param y Y scaling
143 * @since 2.0
144 */
145 public void setScale(double x, double y) {
146 points = null;
147 bounds = null;
148 transform.setScale(x, y);
149 }
150
151 /**
152 * Sets the rotation of this decoration so that the decoration points toward the
153 * given reference point.
154 * @param ref the reference point
155 */
156 public void setReferencePoint(Point ref_) {
157 Point pt = Point.SINGLETON;
158 pt.setLocation(ref_);
159 pt.negate().translate(location);
160 setRotation(Math.atan2(pt.y, pt.x));
161 }
162
163 /**
164 * Sets the angle by which rotation is to be done on the PolygonDecoration.
165 *
166 * @param angle Angle of rotation
167 * @since 2.0
168 */
169 public void setRotation(double angle) {
170 points = null;
171 bounds = null;
172 transform.setRotation(angle);
173 }
174
175 }