svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / geometryadapters / GeometryAdapter.java @ 946
History | View | Annotate | Download (9.13 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
*/
|
20 |
package org.gvsig.app.project.documents.layout.geometryadapters; |
21 |
|
22 |
import java.awt.Graphics2D; |
23 |
import java.awt.geom.AffineTransform; |
24 |
import java.awt.geom.Point2D; |
25 |
import java.awt.geom.Rectangle2D; |
26 |
import java.util.ArrayList; |
27 |
import java.util.Collections; |
28 |
import java.util.List; |
29 |
|
30 |
import org.gvsig.compat.print.PrintAttributes; |
31 |
import org.gvsig.fmap.geom.Geometry; |
32 |
import org.gvsig.fmap.geom.GeometryLocator; |
33 |
import org.gvsig.fmap.geom.GeometryManager; |
34 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
35 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
36 |
import org.gvsig.tools.ToolsLocator; |
37 |
import org.gvsig.tools.dynobject.DynStruct; |
38 |
import org.gvsig.tools.lang.Cloneable; |
39 |
import org.gvsig.tools.persistence.PersistenceManager; |
40 |
import org.gvsig.tools.persistence.Persistent; |
41 |
import org.gvsig.tools.persistence.PersistentState; |
42 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
43 |
|
44 |
/**
|
45 |
* Abstract adaptor to relate the geometries with the fframes and to be able to
|
46 |
* integrate them in the Layout.
|
47 |
*
|
48 |
* @author Vicente Caballero Navarro
|
49 |
*/
|
50 |
public abstract class GeometryAdapter implements Persistent, Cloneable { |
51 |
|
52 |
public static final String PERSISTENCE_DEFINITION_NAME = "GeometryAdapter"; |
53 |
|
54 |
private static final String POINTS_FIELD = "points"; |
55 |
|
56 |
protected static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
57 |
|
58 |
private List<Point2D> points = new ArrayList<Point2D>(); |
59 |
private Geometry geometry;
|
60 |
|
61 |
public GeometryAdapter() {
|
62 |
super();
|
63 |
} |
64 |
|
65 |
/**
|
66 |
* Add a point to de geometry.
|
67 |
*
|
68 |
* @param point Point that is added.
|
69 |
*
|
70 |
* @return Number of points that contains the geometry.
|
71 |
*/
|
72 |
public int addPoint(Point2D point) { |
73 |
points.add(point); |
74 |
|
75 |
return points.size();
|
76 |
} |
77 |
|
78 |
/**
|
79 |
* End the creation of the geometry with the last point added.
|
80 |
*/
|
81 |
public void end() { |
82 |
obtainShape((Point2D) points.get(points.size() - 1)); |
83 |
} |
84 |
|
85 |
/**
|
86 |
* Adds the GeneralPathX with all the points of the geometry.
|
87 |
*
|
88 |
* @param gpx GeneralPathX
|
89 |
*/
|
90 |
protected void setGeometry(Geometry geometry) { |
91 |
this.geometry = geometry;
|
92 |
} |
93 |
|
94 |
/**
|
95 |
* Obtains the geometry passing him as parameter the last point.
|
96 |
*
|
97 |
* @param p Last Point.
|
98 |
* @throws CreateGeometryException
|
99 |
*/
|
100 |
public abstract void obtainShape(Point2D p); |
101 |
|
102 |
/**
|
103 |
* Applies the transformation to all the points of the geometry.
|
104 |
*
|
105 |
* @param at AffineTransform
|
106 |
*/
|
107 |
public void applyTransform(AffineTransform at) { |
108 |
for (int i = 0; i < points.size(); i++) { |
109 |
at.transform((Point2D) points.get(i), (Point2D) points.get(i)); |
110 |
} |
111 |
|
112 |
geometry.transform(at); |
113 |
} |
114 |
|
115 |
/**
|
116 |
* It draws the geometry on the Graphics that is passed like parameter.
|
117 |
*
|
118 |
* @param g Graphics
|
119 |
* @param at AffineTransform
|
120 |
* @param symbol FSymbol
|
121 |
*/
|
122 |
public void draw(Graphics2D g, AffineTransform at, ISymbol symbol) { |
123 |
symbol.draw(g, at, geometry, null, null); |
124 |
} |
125 |
|
126 |
/**
|
127 |
* It print the geometry on the Graphics that is passed like parameter.
|
128 |
*
|
129 |
* @param g Graphics
|
130 |
* @param at AffineTransform
|
131 |
* @param symbol ISymbol
|
132 |
* @param properties
|
133 |
*/
|
134 |
public void print(Graphics2D g, AffineTransform at, |
135 |
ISymbol symbol, PrintAttributes properties) { |
136 |
Geometry geom = getGeometry(); |
137 |
symbol.print(g, at, geom, properties); |
138 |
} |
139 |
|
140 |
/**
|
141 |
* Paints the geometry on the Graphics adding him the last point if the
|
142 |
* parameter andLastPoint is true.
|
143 |
*
|
144 |
* @param g Graphics
|
145 |
* @param at AffineTransform
|
146 |
* @param andLastPoint If true add last point.
|
147 |
*/
|
148 |
public abstract void paint(Graphics2D g, AffineTransform at, |
149 |
boolean andLastPoint);
|
150 |
|
151 |
/**
|
152 |
* Set the point of cursor.
|
153 |
*
|
154 |
* @param p Point of cursor.
|
155 |
*/
|
156 |
public abstract void pointPosition(Point2D p); |
157 |
|
158 |
/**
|
159 |
* Obtains the shape of the Geometry.
|
160 |
*
|
161 |
* @return Geometry.
|
162 |
* @deprecated
|
163 |
*/
|
164 |
public Geometry getGeometry(AffineTransform at) { |
165 |
Geometry clonedGeom = geometry.cloneGeometry(); |
166 |
clonedGeom.transform(at); |
167 |
return clonedGeom;
|
168 |
} |
169 |
|
170 |
/**
|
171 |
* Returns the geometry to draw
|
172 |
*
|
173 |
* @return the geometry
|
174 |
*/
|
175 |
public Geometry getGeometry() {
|
176 |
return geometry;
|
177 |
} |
178 |
|
179 |
/**
|
180 |
* Returns all the points of Geometry.
|
181 |
*
|
182 |
* @return Array of points.
|
183 |
*/
|
184 |
public Point2D[] getPoints() { |
185 |
return (Point2D[]) points.toArray(new Point2D[0]); |
186 |
} |
187 |
|
188 |
/**
|
189 |
* Draws a handler in each vertex of the Geometry.
|
190 |
*
|
191 |
* @param g Graphics
|
192 |
* @param at AffineTransform.
|
193 |
*/
|
194 |
public void drawVertex(Graphics2D g, AffineTransform at) { |
195 |
Point2D[] ps = getPoints(); |
196 |
Point2D[] pointRes = new Point2D[ps.length]; |
197 |
at.transform(ps, 0, pointRes, 0, ps.length); |
198 |
|
199 |
int d = 3; |
200 |
|
201 |
for (int i = 0; i < pointRes.length; i++) { |
202 |
g.fillRect((int) pointRes[i].getX() - d, (int) pointRes[i].getY() |
203 |
- d, d * 2, d * 2); |
204 |
} |
205 |
} |
206 |
|
207 |
/**
|
208 |
* Modifies a point of the Geometry from an index by the one that is passed
|
209 |
* like parameter.
|
210 |
*
|
211 |
* @param pos Index
|
212 |
* @param point Point
|
213 |
*/
|
214 |
public void changePoint(int pos, Point2D point) { |
215 |
this.points.set(pos, point);
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* Add all the points of Geometry.
|
220 |
*
|
221 |
* @param points All points.
|
222 |
*/
|
223 |
public void setPoints(Point2D[] points) { |
224 |
this.points = new ArrayList<Point2D>(); |
225 |
|
226 |
for (int i = 0; i < points.length; i++) { |
227 |
this.points.add(points[i]);
|
228 |
} |
229 |
} |
230 |
|
231 |
/**
|
232 |
* Remove last point of Geometry.
|
233 |
*/
|
234 |
public void delLastPoint() { |
235 |
if (points.size() > 0) { |
236 |
points.remove(points.size() - 1);
|
237 |
} |
238 |
} |
239 |
|
240 |
public Object clone() throws CloneNotSupportedException { |
241 |
GeometryAdapter cloneAdapter = (GeometryAdapter) super.clone();
|
242 |
|
243 |
List<Point2D> clonedPoints = new ArrayList<Point2D>(); |
244 |
for (Point2D point : points) { |
245 |
clonedPoints.add(new Point2D.Double(point.getX(), point.getY())); |
246 |
} |
247 |
cloneAdapter.setPoints(clonedPoints.toArray(new Point2D[0])); |
248 |
|
249 |
cloneAdapter.geometry = (Geometry) this.geometry.cloneGeometry();
|
250 |
return cloneAdapter;
|
251 |
} |
252 |
|
253 |
public Rectangle2D getBounds2D() { |
254 |
Rectangle2D r = geometry.getBounds2D();
|
255 |
double w = r.getWidth();
|
256 |
double h = r.getHeight();
|
257 |
double x = r.getX();
|
258 |
double y = r.getY();
|
259 |
boolean modified = false; |
260 |
if (r.getWidth() < 0.5) { |
261 |
modified = true;
|
262 |
w = 0.5;
|
263 |
x = x - 0.25;
|
264 |
} |
265 |
if (r.getHeight() < 0.5) { |
266 |
modified = true;
|
267 |
h = 0.5;
|
268 |
y = y - 0.25;
|
269 |
} |
270 |
if (modified) {
|
271 |
return new Rectangle2D.Double(x, y, w, h); |
272 |
} |
273 |
return r;
|
274 |
} |
275 |
|
276 |
public static void registerPersistent() { |
277 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
278 |
if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) { |
279 |
DynStruct definition |
280 |
= manager.addDefinition(GeometryAdapter.class, |
281 |
PERSISTENCE_DEFINITION_NAME, |
282 |
"GeometryAdapter persistence definition", null, null); |
283 |
|
284 |
definition.addDynFieldList(POINTS_FIELD) |
285 |
.setClassOfItems(Point2D.class).setMandatory(true); |
286 |
} |
287 |
|
288 |
CircleAdapter.registerPersistent(); |
289 |
PointAdapter.registerPersistent(); |
290 |
PolygonAdapter.registerPersistent(); |
291 |
PolyLineAdapter.registerPersistent(); |
292 |
RectangleAdapter.registerPersistent(); |
293 |
} |
294 |
|
295 |
public void loadFromState(PersistentState state) |
296 |
throws PersistenceException {
|
297 |
points = state.getList(POINTS_FIELD); |
298 |
obtainShape((Point2D) points.get(points.size() - 1)); |
299 |
} |
300 |
|
301 |
public void saveToState(PersistentState state) throws PersistenceException { |
302 |
state.set(POINTS_FIELD, points); |
303 |
} |
304 |
} |