root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / layout / geometryadapters / GeometryAdapter.java @ 33647
History | View | Annotate | Download (8.15 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 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.app.project.documents.layout.geometryadapters; |
42 |
|
43 |
import java.awt.Graphics2D; |
44 |
import java.awt.geom.AffineTransform; |
45 |
import java.awt.geom.Point2D; |
46 |
import java.awt.geom.Rectangle2D; |
47 |
import java.util.ArrayList; |
48 |
import java.util.Collections; |
49 |
import java.util.List; |
50 |
|
51 |
import org.gvsig.compat.print.PrintAttributes; |
52 |
import org.gvsig.fmap.geom.Geometry; |
53 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
54 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
55 |
import org.gvsig.tools.ToolsLocator; |
56 |
import org.gvsig.tools.dynobject.DynStruct; |
57 |
import org.gvsig.tools.lang.Cloneable; |
58 |
import org.gvsig.tools.persistence.PersistenceManager; |
59 |
import org.gvsig.tools.persistence.Persistent; |
60 |
import org.gvsig.tools.persistence.PersistentState; |
61 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
62 |
|
63 |
|
64 |
|
65 |
/**
|
66 |
* Abstract adaptor to relate the geometries with the fframes and to be able to
|
67 |
* integrate them in the Layout.
|
68 |
*
|
69 |
* @author Vicente Caballero Navarro
|
70 |
*/
|
71 |
public abstract class GeometryAdapter implements Persistent, Cloneable { |
72 |
public static final String PERSISTENCE_DEFINITION_NAME = "GeometryAdapter"; |
73 |
|
74 |
private static final String POINTS_FIELD = "points"; |
75 |
|
76 |
private List points = new ArrayList(); |
77 |
private GeneralPathX shape;
|
78 |
|
79 |
/**
|
80 |
* Add a point to de geometry.
|
81 |
*
|
82 |
* @param point Point that is added.
|
83 |
*
|
84 |
* @return Number of points that contains the geometry.
|
85 |
*/
|
86 |
public int addPoint(Point2D point) { |
87 |
points.add(point); |
88 |
|
89 |
return points.size();
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* End the creation of the geometry with the last point added.
|
94 |
*/
|
95 |
public void end() { |
96 |
obtainShape((Point2D) points.get(points.size() - 1)); |
97 |
} |
98 |
|
99 |
/**
|
100 |
* Adds the GeneralPathX with all the points of the geometry.
|
101 |
*
|
102 |
* @param gpx GeneralPathX
|
103 |
*/
|
104 |
protected void setGPX(GeneralPathX gpx) { |
105 |
shape = gpx; |
106 |
} |
107 |
|
108 |
/**
|
109 |
* Returns the GeneralPathX.
|
110 |
*
|
111 |
* @return GeneralPathX.
|
112 |
*/
|
113 |
protected GeneralPathX getGPX() {
|
114 |
return shape;
|
115 |
} |
116 |
|
117 |
/**
|
118 |
* Obtains the geometry passing him as parameter the last point.
|
119 |
*
|
120 |
* @param p Last Point.
|
121 |
*/
|
122 |
public abstract void obtainShape(Point2D p); |
123 |
|
124 |
/**
|
125 |
* Applies the transformation to all the points of the geometry.
|
126 |
*
|
127 |
* @param at AffineTransform
|
128 |
*/
|
129 |
public void applyTransform(AffineTransform at) { |
130 |
for (int i = 0; i < points.size(); i++) { |
131 |
at.transform((Point2D) points.get(i), (Point2D) points.get(i)); |
132 |
} |
133 |
|
134 |
shape.transform(at); |
135 |
} |
136 |
|
137 |
/**
|
138 |
* It draws the geometry on the Graphics that is passed like parameter.
|
139 |
*
|
140 |
* @param g Graphics
|
141 |
* @param at AffineTransform
|
142 |
* @param symbol FSymbol
|
143 |
*/
|
144 |
public abstract void draw(Graphics2D g, AffineTransform at, ISymbol symbol); |
145 |
/**
|
146 |
* It print the geometry on the Graphics that is passed like parameter.
|
147 |
*
|
148 |
* @param g Graphics
|
149 |
* @param at AffineTransform
|
150 |
* @param symbol ISymbol
|
151 |
* @param properties
|
152 |
*/
|
153 |
public abstract void print(Graphics2D g, AffineTransform at, |
154 |
ISymbol symbol, PrintAttributes properties); |
155 |
/**
|
156 |
* Paints the geometry on the Graphics adding him the last point if the
|
157 |
* parameter andLastPoint is true.
|
158 |
*
|
159 |
* @param g Graphics
|
160 |
* @param at AffineTransform
|
161 |
* @param andLastPoint If true add last point.
|
162 |
*/
|
163 |
public abstract void paint(Graphics2D g, AffineTransform at, |
164 |
boolean andLastPoint);
|
165 |
|
166 |
/**
|
167 |
* Set the point of cursor.
|
168 |
*
|
169 |
* @param p Point of cursor.
|
170 |
*/
|
171 |
public abstract void pointPosition(Point2D p); |
172 |
|
173 |
/**
|
174 |
* Obtains the shape of the Geometry.
|
175 |
*
|
176 |
* @return Geometry.
|
177 |
*/
|
178 |
public abstract Geometry getGeometry(AffineTransform at); |
179 |
|
180 |
/**
|
181 |
* Returns all the points of Geometry.
|
182 |
*
|
183 |
* @return Array of points.
|
184 |
*/
|
185 |
public Point2D[] getPoints() { |
186 |
return (Point2D[]) points.toArray(new Point2D[0]); |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Draws a handler in each vertex of the Geometry.
|
191 |
*
|
192 |
* @param g Graphics
|
193 |
* @param at AffineTransform.
|
194 |
*/
|
195 |
public void drawVertex(Graphics2D g, AffineTransform at) { |
196 |
Point2D[] ps = getPoints(); |
197 |
Point2D[] pointRes = new Point2D[ps.length]; |
198 |
at.transform(ps, 0, pointRes, 0, ps.length); |
199 |
|
200 |
int d = 3; |
201 |
|
202 |
for (int i = 0; i < pointRes.length; i++) { |
203 |
g.fillRect((int) pointRes[i].getX() - d,
|
204 |
(int) pointRes[i].getY() - d, d * 2, d * 2); |
205 |
} |
206 |
} |
207 |
|
208 |
/**
|
209 |
* Modifies a point of the Geometry from an index by the one that is passed
|
210 |
* like parameter.
|
211 |
*
|
212 |
* @param pos Index
|
213 |
* @param point Point
|
214 |
*/
|
215 |
public void changePoint(int pos, Point2D point) { |
216 |
this.points.set(pos, point);
|
217 |
} |
218 |
|
219 |
/**
|
220 |
* Add all the points of Geometry.
|
221 |
*
|
222 |
* @param points All points.
|
223 |
*/
|
224 |
public void setPoints(Point2D[] points) { |
225 |
this.points.clear();
|
226 |
|
227 |
for (int i = 0; i < points.length; i++) { |
228 |
this.points.add(points[i]);
|
229 |
} |
230 |
} |
231 |
|
232 |
|
233 |
/**
|
234 |
* Remove last point of Geometry.
|
235 |
*/
|
236 |
public void delLastPoint() { |
237 |
if (points.size() > 0) { |
238 |
points.remove(points.size() - 1);
|
239 |
} |
240 |
} |
241 |
public Object clone() throws CloneNotSupportedException{ |
242 |
GeometryAdapter cloneAdapter = (GeometryAdapter)super.clone();
|
243 |
Collections.copy(cloneAdapter.points, this.points); |
244 |
cloneAdapter.shape = (GeneralPathX)this.shape.clone();
|
245 |
return cloneAdapter;
|
246 |
} |
247 |
|
248 |
public Rectangle2D getBounds2D(){ |
249 |
Rectangle2D r=shape.getBounds2D();
|
250 |
double w=r.getWidth();
|
251 |
double h=r.getHeight();
|
252 |
double x=r.getX();
|
253 |
double y=r.getY();
|
254 |
boolean modified=false; |
255 |
if (r.getWidth()<0.5) { |
256 |
modified=true;
|
257 |
w=0.5;
|
258 |
x=x-0.25;
|
259 |
} |
260 |
if(r.getHeight()<0.5) { |
261 |
modified=true;
|
262 |
h=0.5;
|
263 |
y=y-0.25;
|
264 |
} |
265 |
if (modified) {
|
266 |
return new Rectangle2D.Double(x,y,w,h); |
267 |
} |
268 |
return r;
|
269 |
} |
270 |
|
271 |
public static void registerPersistent() { |
272 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
273 |
if( manager.getDefinition(PERSISTENCE_DEFINITION_NAME)==null ) { |
274 |
DynStruct definition = manager.addDefinition( |
275 |
GeometryAdapter.class, |
276 |
PERSISTENCE_DEFINITION_NAME, |
277 |
"GeometryAdapter persistence definition",
|
278 |
null,
|
279 |
null
|
280 |
); |
281 |
|
282 |
definition.addDynFieldList(POINTS_FIELD).setClassOfItems(Point2D.class).setMandatory(true); |
283 |
} |
284 |
} |
285 |
|
286 |
public void loadFromState(PersistentState state) |
287 |
throws PersistenceException {
|
288 |
points = state.getList(POINTS_FIELD); |
289 |
} |
290 |
|
291 |
public void saveToState(PersistentState state) throws PersistenceException { |
292 |
state.set(POINTS_FIELD, points); |
293 |
} |
294 |
} |