Project

General

Profile

Download (4.93 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2008, 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
 *    Alexander Shatalin (Borland) - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.draw2d;
12

    
13
import java.util.Iterator;
14

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

    
18
/**
19
 * Base superclass for all polylines/polygons
20
 * 
21
 * @since 3.5
22
 */
23
public abstract class AbstractPointListShape extends Shape {
24

    
25
	PointList points = new PointList();
26

    
27
	/**
28
	 * @see org.eclipse.draw2d.IFigure#containsPoint(int, int)
29
	 */
30
	public boolean containsPoint(int x, int y) {
31
		if (!super.containsPoint(x, y)) {
32
			return false;
33
		}
34
		return shapeContainsPoint(x, y) || childrenContainsPoint(x, y);
35
	}
36

    
37
	/**
38
	 * Returns <code>true</code> if the point <code>(x, y)</code> is contained
39
	 * within one of the child figures.
40
	 * 
41
	 * @param x
42
	 *            The X coordinate
43
	 * @param y
44
	 *            The Y coordinate
45
	 * @return <code>true</code> if the point (x,y) is contained in one of the
46
	 *         child figures
47
	 */
48
	protected boolean childrenContainsPoint(int x, int y) {
49
		for (Iterator it = getChildren().iterator(); it.hasNext();) {
50
			IFigure nextChild = (IFigure) it.next();
51
			if (nextChild.containsPoint(x, y)) {
52
				return true;
53
			}
54
		}
55
		return false;
56
	}
57

    
58
	/**
59
	 * Returns <code>true</code> if the point <code>(x, y)</code> is contained
60
	 * within this figure.
61
	 * 
62
	 * @param x
63
	 *            The X coordinate
64
	 * @param y
65
	 *            The Y coordinate
66
	 * @return <code>true</code> if the point (x,y) is contained in this figure
67
	 */
68
	abstract protected boolean shapeContainsPoint(int x, int y);
69

    
70
	/**
71
	 * Adds the passed point to this figure.
72
	 * 
73
	 * @param pt
74
	 *            the Point to be added to this figure
75
	 */
76
	public void addPoint(Point pt) {
77
		points.addPoint(pt);
78
		repaint();
79
	}
80

    
81
	/**
82
	 * @return the first point in this figure
83
	 */
84
	public Point getStart() {
85
		return points.getFirstPoint();
86
	}
87

    
88
	/**
89
	 * Returns the last point in this Figure.
90
	 * 
91
	 * @return the last point
92
	 */
93
	public Point getEnd() {
94
		return points.getLastPoint();
95
	}
96

    
97
	/**
98
	 * Returns the points in this figure <B>by reference</B>. If the returned
99
	 * list is modified, this figure must be informed by calling
100
	 * {@link #setPoints(PointList)}. Failure to do so will result in layout and
101
	 * paint problems.
102
	 * 
103
	 * @return this Polyline's points
104
	 */
105
	public PointList getPoints() {
106
		return points;
107
	}
108

    
109
	/**
110
	 * Inserts a given point at a specified index in this figure.
111
	 * 
112
	 * @param pt
113
	 *            the point to be added
114
	 * @param index
115
	 *            the position in this figure where the point is to be added
116
	 */
117
	public void insertPoint(Point pt, int index) {
118
		points.insertPoint(pt, index);
119
		repaint();
120
	}
121

    
122
	/**
123
	 * Erases this figure and removes all of its {@link Point Points}.
124
	 */
125
	public void removeAllPoints() {
126
		erase();
127
		points.removeAllPoints();
128
	}
129

    
130
	/**
131
	 * Removes a point from this figure.
132
	 * 
133
	 * @param index
134
	 *            the position of the point to be removed
135
	 */
136
	public void removePoint(int index) {
137
		erase();
138
		points.removePoint(index);
139
		repaint();
140
	}
141

    
142
	/**
143
	 * Sets the start point of this figure
144
	 * 
145
	 * @param start
146
	 *            the point that will become the first point in this figure
147
	 */
148
	public void setStart(Point start) {
149
		if (points.size() == 0) {
150
			addPoint(start);
151
		} else {
152
			setPoint(start, 0);
153
		}
154
	}
155

    
156
	/**
157
	 * Sets the end point of this figure
158
	 * 
159
	 * @param end
160
	 *            the point that will become the last point in this figure
161
	 */
162
	public void setEnd(Point end) {
163
		if (points.size() < 2) {
164
			addPoint(end);
165
		} else {
166
			setPoint(end, points.size() - 1);
167
		}
168
	}
169

    
170
	/**
171
	 * Sets the points at both extremes of this figure
172
	 * 
173
	 * @param start
174
	 *            the point to become the first point in this figure
175
	 * @param end
176
	 *            the point to become the last point in this figure
177
	 */
178
	public void setEndpoints(Point start, Point end) {
179
		setStart(start);
180
		setEnd(end);
181
	}
182

    
183
	/**
184
	 * Sets the point at <code>index</code> to the Point <code>pt</code>. If
185
	 * you're going to set multiple Points, use {@link #setPoints(PointList)}.
186
	 * 
187
	 * @param pt
188
	 *            the point
189
	 * @param index
190
	 *            the index
191
	 */
192
	public void setPoint(Point pt, int index) {
193
		erase();
194
		points.setPoint(pt, index);
195
		repaint();
196
	}
197

    
198
	/**
199
	 * Sets the list of points to be used by this figure. Removes any previously
200
	 * existing points. This figure will hold onto the given list by reference.
201
	 * 
202
	 * @param points
203
	 *            new set of points
204
	 */
205
	public void setPoints(PointList points) {
206
		erase();
207
		this.points = points;
208
		repaint();
209
	}
210

    
211
}
(10-10/171)