svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / geometryadapters / GeometryAdapter.java @ 21300
History | View | Annotate | Download (7.8 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 com.iver.cit.gvsig.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 |
|
49 |
import org.gvsig.fmap.geom.Geometry; |
50 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
51 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
52 |
|
53 |
import com.iver.utiles.XMLEntity; |
54 |
|
55 |
|
56 |
/**
|
57 |
* Abstract adaptor to relate the geometries with the fframes and to be able to
|
58 |
* integrate them in the Layout.
|
59 |
*
|
60 |
* @author Vicente Caballero Navarro
|
61 |
*/
|
62 |
public abstract class GeometryAdapter { |
63 |
private ArrayList points = new ArrayList(); |
64 |
private GeneralPathX shape;
|
65 |
|
66 |
/**
|
67 |
* Add a point to de geometry.
|
68 |
*
|
69 |
* @param point Point that is added.
|
70 |
*
|
71 |
* @return Number of points that contains the geometry.
|
72 |
*/
|
73 |
public int addPoint(Point2D point) { |
74 |
points.add(point); |
75 |
|
76 |
return points.size();
|
77 |
} |
78 |
|
79 |
/**
|
80 |
* End the creation of the geometry with the last point added.
|
81 |
*/
|
82 |
public void end() { |
83 |
obtainShape((Point2D) points.get(points.size() - 1)); |
84 |
} |
85 |
|
86 |
/**
|
87 |
* Adds the GeneralPathX with all the points of the geometry.
|
88 |
*
|
89 |
* @param gpx GeneralPathX
|
90 |
*/
|
91 |
protected void setGPX(GeneralPathX gpx) { |
92 |
shape = gpx; |
93 |
} |
94 |
|
95 |
/**
|
96 |
* Returns the GeneralPathX.
|
97 |
*
|
98 |
* @return GeneralPathX.
|
99 |
*/
|
100 |
protected GeneralPathX getGPX() {
|
101 |
return shape;
|
102 |
} |
103 |
|
104 |
/**
|
105 |
* Obtains the geometry passing him as parameter the last point.
|
106 |
*
|
107 |
* @param p Last Point.
|
108 |
*/
|
109 |
public abstract void obtainShape(Point2D p); |
110 |
|
111 |
/**
|
112 |
* Applies the transformation to all the points of the geometry.
|
113 |
*
|
114 |
* @param at AffineTransform
|
115 |
*/
|
116 |
public void applyTransform(AffineTransform at) { |
117 |
for (int i = 0; i < points.size(); i++) { |
118 |
at.transform((Point2D) points.get(i), (Point2D) points.get(i)); |
119 |
} |
120 |
|
121 |
shape.transform(at); |
122 |
} |
123 |
|
124 |
/**
|
125 |
* It draws the geometry on the Graphics that is passed like parameter.
|
126 |
*
|
127 |
* @param g Graphics
|
128 |
* @param at AffineTransform
|
129 |
* @param symbol FSymbol
|
130 |
*/
|
131 |
public abstract void draw(Graphics2D g, AffineTransform at, ISymbol symbol); |
132 |
|
133 |
/**
|
134 |
* Paints the geometry on the Graphics adding him the last point if the
|
135 |
* parameter andLastPoint is true.
|
136 |
*
|
137 |
* @param g Graphics
|
138 |
* @param at AffineTransform
|
139 |
* @param andLastPoint If true add last point.
|
140 |
*/
|
141 |
public abstract void paint(Graphics2D g, AffineTransform at, |
142 |
boolean andLastPoint);
|
143 |
|
144 |
/**
|
145 |
* Set the point of cursor.
|
146 |
*
|
147 |
* @param p Point of cursor.
|
148 |
*/
|
149 |
public abstract void pointPosition(Point2D p); |
150 |
|
151 |
/**
|
152 |
* Obtains the shape of the Geometry.
|
153 |
*
|
154 |
* @return FShape.
|
155 |
*/
|
156 |
public abstract Geometry getGeometry(AffineTransform at); |
157 |
|
158 |
/**
|
159 |
* Returns all the points of Geometry.
|
160 |
*
|
161 |
* @return Array of points.
|
162 |
*/
|
163 |
public Point2D[] getPoints() { |
164 |
return (Point2D[]) points.toArray(new Point2D[0]); |
165 |
} |
166 |
|
167 |
/**
|
168 |
* Draws a handler in each vertex of the Geometry.
|
169 |
*
|
170 |
* @param g Graphics
|
171 |
* @param at AffineTransform.
|
172 |
*/
|
173 |
public void drawVertex(Graphics2D g, AffineTransform at) { |
174 |
Point2D[] ps = getPoints(); |
175 |
Point2D[] pointRes = new Point2D[ps.length]; |
176 |
at.transform(ps, 0, pointRes, 0, ps.length); |
177 |
|
178 |
int d = 3; |
179 |
|
180 |
for (int i = 0; i < pointRes.length; i++) { |
181 |
g.fillRect((int) pointRes[i].getX() - d,
|
182 |
(int) pointRes[i].getY() - d, d * 2, d * 2); |
183 |
} |
184 |
} |
185 |
|
186 |
/**
|
187 |
* Modifies a point of the Geometry from an index by the one that is passed
|
188 |
* like parameter.
|
189 |
*
|
190 |
* @param pos Index
|
191 |
* @param point Point
|
192 |
*/
|
193 |
public void changePoint(int pos, Point2D point) { |
194 |
this.points.set(pos, point);
|
195 |
} |
196 |
|
197 |
/**
|
198 |
* Add all the points of Geometry.
|
199 |
*
|
200 |
* @param points All points.
|
201 |
*/
|
202 |
public void setPoints(Point2D[] points) { |
203 |
this.points.clear();
|
204 |
|
205 |
for (int i = 0; i < points.length; i++) { |
206 |
this.points.add(points[i]);
|
207 |
} |
208 |
} |
209 |
|
210 |
/**
|
211 |
* DOCUMENT ME!
|
212 |
*
|
213 |
* @return DOCUMENT ME!
|
214 |
*/
|
215 |
public XMLEntity getXMLEntity() {
|
216 |
XMLEntity xml = new XMLEntity();
|
217 |
double[] ps = new double[points.size() * 2]; |
218 |
int j = 0; |
219 |
|
220 |
for (int i = 0; i < points.size(); i++) { |
221 |
ps[j] = (((Point2D) (points.get(i))).getX());
|
222 |
ps[j + 1] = (((Point2D) (points.get(i))).getY()); |
223 |
j = j + 2;
|
224 |
} |
225 |
|
226 |
xml.putProperty("points", ps);
|
227 |
xml.putProperty("className", this.getClass().getName()); |
228 |
|
229 |
return xml;
|
230 |
} |
231 |
|
232 |
/**
|
233 |
* DOCUMENT ME!
|
234 |
*
|
235 |
* @param xml DOCUMENT ME!
|
236 |
*
|
237 |
* @return DOCUMENT ME!
|
238 |
*/
|
239 |
public static GeometryAdapter createFromXML(XMLEntity xml) { |
240 |
GeometryAdapter geometry = null;
|
241 |
|
242 |
try {
|
243 |
Class clase = Class.forName(xml.getStringProperty("className")); |
244 |
geometry = (GeometryAdapter) clase.newInstance(); |
245 |
} catch (Exception e) { |
246 |
} |
247 |
|
248 |
double[] ps = xml.getDoubleArrayProperty("points"); |
249 |
Point2D[] pointsAux = new Point2D[ps.length / 2]; |
250 |
int j = 0; |
251 |
|
252 |
for (int i = 0; i < ps.length; i = i + 2) { |
253 |
pointsAux[j] = new Point2D.Double(ps[i], ps[i + 1]); |
254 |
j++; |
255 |
} |
256 |
|
257 |
geometry.setPoints(pointsAux); |
258 |
geometry.end(); |
259 |
|
260 |
return geometry;
|
261 |
} |
262 |
|
263 |
/**
|
264 |
* Remove last point of Geometry.
|
265 |
*/
|
266 |
public void delLastPoint() { |
267 |
if (points.size() > 0) { |
268 |
points.remove(points.size() - 1);
|
269 |
} |
270 |
} |
271 |
public GeometryAdapter cloneAdapter(){
|
272 |
GeometryAdapter cloneAdapter=null;
|
273 |
try {
|
274 |
cloneAdapter = (GeometryAdapter)this.getClass().newInstance();
|
275 |
} catch (InstantiationException e) { |
276 |
} catch (IllegalAccessException e) { |
277 |
} |
278 |
cloneAdapter.points=(ArrayList)this.points.clone(); |
279 |
cloneAdapter.shape=(GeneralPathX)this.shape.clone();
|
280 |
return cloneAdapter;
|
281 |
} |
282 |
public Rectangle2D getBounds2D(){ |
283 |
Rectangle2D r=shape.getBounds2D();
|
284 |
if (r.getWidth()<0.5) { |
285 |
return new Rectangle2D.Double(r.getX()-0.25,r.getY(),0.5,r.getHeight()); |
286 |
}else if(r.getHeight()<0.5) { |
287 |
return new Rectangle2D.Double(r.getX(),r.getY()-0.25,r.getWidth(),0.5); |
288 |
} |
289 |
return shape.getBounds2D();
|
290 |
} |
291 |
} |