svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / symbols / PictureLineSymbol.java @ 19091
History | View | Annotate | Download (10.8 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 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.symbology.fmap.symbols; |
42 |
|
43 |
import java.awt.BasicStroke; |
44 |
import java.awt.Color; |
45 |
import java.awt.Graphics2D; |
46 |
import java.awt.Rectangle; |
47 |
import java.awt.geom.AffineTransform; |
48 |
import java.awt.geom.PathIterator; |
49 |
import java.awt.geom.Point2D; |
50 |
import java.io.File; |
51 |
import java.io.IOException; |
52 |
import java.net.URL; |
53 |
|
54 |
import javax.print.attribute.PrintRequestAttributeSet; |
55 |
|
56 |
import org.apache.batik.ext.awt.geom.PathLength; |
57 |
import org.gvsig.symbology.fmap.styles.BackgroundFileStyle; |
58 |
import org.gvsig.symbology.fmap.styles.ImageStyle; |
59 |
import org.gvsig.symbology.fmap.styles.SVGStyle; |
60 |
|
61 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
62 |
import com.iver.cit.gvsig.fmap.ViewPort; |
63 |
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit; |
64 |
import com.iver.cit.gvsig.fmap.core.FPolyline2D; |
65 |
import com.iver.cit.gvsig.fmap.core.FShape; |
66 |
import com.iver.cit.gvsig.fmap.core.SymbologyFactory; |
67 |
import com.iver.cit.gvsig.fmap.core.symbols.AbstractLineSymbol; |
68 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
69 |
import com.iver.utiles.XMLEntity; |
70 |
import com.iver.utiles.swing.threads.Cancellable; |
71 |
|
72 |
/**
|
73 |
* PictureLineSymbol allows to use any symbol defined as an image (by an image file)
|
74 |
* supported by gvSIG.This symbol will be used as an initial object.The line will be
|
75 |
* painted as a succession of puntual symbols through the path defined by it(the line).
|
76 |
*
|
77 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
78 |
*/
|
79 |
public class PictureLineSymbol extends AbstractLineSymbol { |
80 |
// transient private BufferedImage img;
|
81 |
// transient private BufferedImage selImg;
|
82 |
transient private PictureLineSymbol selectionSym; |
83 |
private String selImagePath; |
84 |
private double width; |
85 |
private String imagePath; |
86 |
private boolean selected; |
87 |
private double xScale = 1, csXScale = xScale; |
88 |
private double yScale = 1, csYScale = yScale; |
89 |
|
90 |
private BackgroundFileStyle bgImage;
|
91 |
private BackgroundFileStyle bgSelImage;
|
92 |
/**
|
93 |
* Constructor method
|
94 |
*
|
95 |
*/
|
96 |
public PictureLineSymbol() {
|
97 |
super();
|
98 |
} |
99 |
/**
|
100 |
* Constructor method
|
101 |
* @param imageURL, URL of the normal image
|
102 |
* @param selImageURL, URL of the image when it is selected in the map
|
103 |
* @throws IOException
|
104 |
*/
|
105 |
|
106 |
public PictureLineSymbol(URL imageURL, URL selImageURL) throws IOException { |
107 |
this(new File(imageURL.getFile()), selImageURL !=null ? new File(selImageURL.getFile()) : null); |
108 |
} |
109 |
/**
|
110 |
* Constructor method
|
111 |
* @param imageFile, File of the normal image
|
112 |
* @param selImageFile, File of the image when it is selected in the map
|
113 |
* @throws IOException
|
114 |
*/
|
115 |
public PictureLineSymbol(File imageFile, File selImageFile) throws IOException { |
116 |
setImage(imageFile); |
117 |
if (selImageFile!=null && selImageFile.exists()) |
118 |
setSelImage(selImageFile); |
119 |
else setSelImage(imageFile);
|
120 |
} |
121 |
/**
|
122 |
* Sets the file for the image to be used as a picture line symbol
|
123 |
* @param imageFile, File
|
124 |
* @throws IOException
|
125 |
*/
|
126 |
public void setImage(File imageFile) throws IOException{ |
127 |
imagePath = imageFile.getAbsolutePath(); |
128 |
bgImage= (imageFile.getAbsolutePath().toLowerCase().endsWith(".svg")) ?
|
129 |
new SVGStyle() :
|
130 |
new ImageStyle();
|
131 |
bgImage.setSource(imageFile.toURL()); |
132 |
} |
133 |
/**
|
134 |
* Sets the file for the image to be used as a picture line symbol (when it is selected in the map)
|
135 |
* @param imageFile, File
|
136 |
* @throws IOException
|
137 |
*/
|
138 |
public void setSelImage(File imageFile) throws IOException{ |
139 |
selImagePath = imageFile.getAbsolutePath(); |
140 |
bgSelImage= (imageFile.getAbsolutePath().toLowerCase().endsWith(".svg")) ?
|
141 |
new SVGStyle() :
|
142 |
new ImageStyle();
|
143 |
bgSelImage.setSource(imageFile.toURL()); |
144 |
} |
145 |
|
146 |
public void setLineWidth(double width) { |
147 |
this.width = width;
|
148 |
getLineStyle().setLineWidth((float) width);
|
149 |
} |
150 |
|
151 |
public double getLineWidth() { |
152 |
return width;
|
153 |
} |
154 |
|
155 |
public ISymbol getSymbolForSelection() {
|
156 |
if (selectionSym == null) { |
157 |
selectionSym = (PictureLineSymbol) SymbologyFactory.createSymbolFromXML(getXMLEntity(), getDescription()); |
158 |
selectionSym.selected=true;
|
159 |
selectionSym.selectionSym = selectionSym; // avoid too much lazy creations
|
160 |
|
161 |
} |
162 |
return selectionSym;
|
163 |
|
164 |
} |
165 |
|
166 |
public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) { |
167 |
float csWidth = getLineStyle().getLineWidth();
|
168 |
g.setClip(new BasicStroke((float) csWidth, BasicStroke.CAP_ROUND,BasicStroke.CAP_ROUND).createStrokedShape(shp)); |
169 |
BackgroundFileStyle bg = (!selected) ? bgImage : bgSelImage ; |
170 |
|
171 |
if (csXScale<=0 && csYScale<=0) |
172 |
return;
|
173 |
|
174 |
Rectangle bounds = bg.getBounds();
|
175 |
final double imageWidth = bounds.getWidth() * csXScale; |
176 |
final double imageHeight = bounds.getHeight() * csYScale; |
177 |
|
178 |
if (imageWidth==0 || imageHeight==0) return; |
179 |
int height = (int) csWidth; |
180 |
|
181 |
PathLength pl = new PathLength(shp);
|
182 |
PathIterator iterator = ((FPolyline2D) shp).getPathIterator(null, 0.8); |
183 |
double[] theData = new double[6]; |
184 |
Point2D firstPoint = null, startPoint = null, endPoint = null; |
185 |
if (!iterator.isDone()) {
|
186 |
if ( iterator.currentSegment(theData) != PathIterator.SEG_CLOSE) { |
187 |
firstPoint = new Point2D.Double(theData[0], theData[1]); |
188 |
} |
189 |
} |
190 |
float currentPathLength = 1; |
191 |
|
192 |
Rectangle rect = new Rectangle(); |
193 |
|
194 |
while ((cancel==null || !cancel.isCanceled()) && !iterator.isDone()) { |
195 |
|
196 |
int theType = iterator.currentSegment(theData);
|
197 |
switch (theType) {
|
198 |
case PathIterator.SEG_MOVETO: |
199 |
startPoint = new Point2D.Double(theData[0], theData[1]); |
200 |
|
201 |
endPoint = null;
|
202 |
iterator.next(); |
203 |
|
204 |
continue;
|
205 |
|
206 |
case PathIterator.SEG_LINETO: |
207 |
case PathIterator.SEG_QUADTO: |
208 |
case PathIterator.SEG_CUBICTO: |
209 |
endPoint = new Point2D.Double(theData[0], theData[1]); |
210 |
|
211 |
break;
|
212 |
case PathIterator.SEG_CLOSE: |
213 |
endPoint = startPoint; |
214 |
startPoint = firstPoint; |
215 |
break;
|
216 |
} |
217 |
|
218 |
double a = endPoint.getX() - startPoint.getX();
|
219 |
double b = endPoint.getY() - startPoint.getY();
|
220 |
double theta = pl.angleAtLength(currentPathLength);
|
221 |
|
222 |
double x = startPoint.getX();
|
223 |
double y = startPoint.getY();
|
224 |
|
225 |
// Theorem of Pythagoras
|
226 |
float segmentLength = (float) Math.sqrt(a*a + b*b); |
227 |
|
228 |
// compute how many times the image has to be drawn
|
229 |
// to completely cover this segment's length
|
230 |
int count = (int) Math.ceil(segmentLength/imageWidth); |
231 |
|
232 |
for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < count; i++) { |
233 |
g.translate(x, y); |
234 |
g.rotate(theta); |
235 |
|
236 |
double xOffsetTranslation = imageWidth*i;
|
237 |
|
238 |
|
239 |
g.translate(xOffsetTranslation, -csWidth); |
240 |
|
241 |
rect.setBounds(0, 0, (int) Math.ceil(imageWidth), height); |
242 |
bg.drawInsideRectangle(g, rect, false);
|
243 |
g.setColor(Color.red);
|
244 |
g.translate(-xOffsetTranslation, csWidth); |
245 |
|
246 |
g.rotate(-theta); |
247 |
g.translate(-x, -y); |
248 |
} |
249 |
|
250 |
startPoint = endPoint; |
251 |
currentPathLength += segmentLength; |
252 |
iterator.next(); |
253 |
} |
254 |
|
255 |
g.setClip(null);
|
256 |
g.setColor(Color.red);
|
257 |
|
258 |
} |
259 |
|
260 |
|
261 |
public XMLEntity getXMLEntity() {
|
262 |
XMLEntity xml = new XMLEntity();
|
263 |
xml.putProperty("className", getClassName());
|
264 |
xml.putProperty("isShapeVisible", isShapeVisible());
|
265 |
xml.putProperty("desc", getDescription());
|
266 |
xml.putProperty("imagePath", imagePath);
|
267 |
xml.putProperty("selImagePath", selImagePath);
|
268 |
xml.putProperty("lineWidth", getLineWidth());
|
269 |
xml.putProperty("xScale", getXScale());
|
270 |
xml.putProperty("yScale", getYScale());
|
271 |
|
272 |
// measure unit
|
273 |
xml.putProperty("unit", getUnit());
|
274 |
|
275 |
// reference system
|
276 |
xml.putProperty("referenceSystem", getReferenceSystem());
|
277 |
|
278 |
return xml;
|
279 |
|
280 |
} |
281 |
|
282 |
public void setXMLEntity(XMLEntity xml) { |
283 |
setDescription(xml.getStringProperty("desc"));
|
284 |
setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
|
285 |
imagePath = xml.getStringProperty("imagePath");
|
286 |
selImagePath = xml.getStringProperty("selImagePath");
|
287 |
setLineWidth(xml.getDoubleProperty("lineWidth"));
|
288 |
setXScale(xml.getDoubleProperty("xScale"));
|
289 |
setYScale(xml.getDoubleProperty("yScale"));
|
290 |
setReferenceSystem(xml.getIntProperty("referenceSystem"));
|
291 |
setUnit(xml.getIntProperty("unit"));
|
292 |
|
293 |
try {
|
294 |
setImage(new File(imagePath)); |
295 |
setSelImage(new File(selImagePath)); |
296 |
} catch (IOException e) { |
297 |
e.printStackTrace(); |
298 |
} |
299 |
|
300 |
} |
301 |
/**
|
302 |
* Sets the yscale for the picture line symbol
|
303 |
* @param yScale
|
304 |
*/
|
305 |
public void setYScale(double yScale) { |
306 |
this.yScale = yScale;
|
307 |
this.csYScale = yScale;
|
308 |
} |
309 |
/**
|
310 |
* Sets the xscale for the picture line symbol
|
311 |
* @param xScale
|
312 |
*/
|
313 |
public void setXScale(double xScale) { |
314 |
this.xScale = xScale;
|
315 |
this.csXScale = xScale;
|
316 |
} |
317 |
|
318 |
public String getClassName() { |
319 |
return getClass().getName();
|
320 |
} |
321 |
|
322 |
public void print(Graphics2D g, AffineTransform at, FShape shape, |
323 |
PrintRequestAttributeSet properties) throws ReadDriverException { |
324 |
// TODO Implement it
|
325 |
throw new Error("Not yet implemented!"); |
326 |
|
327 |
} |
328 |
/**
|
329 |
* Returns the path of the image that is used as a picture line symbol (when it
|
330 |
* is selected in the map)
|
331 |
* @return selimagePath,String
|
332 |
*/
|
333 |
public String getSelImagePath() { |
334 |
return selImagePath;
|
335 |
} |
336 |
/**
|
337 |
* Returns the path of the image that is used as a picture line symbol
|
338 |
* @return imagePath,String
|
339 |
*/
|
340 |
public String getImagePath() { |
341 |
return imagePath;
|
342 |
} |
343 |
/**
|
344 |
* Returns the xscale for the picture line symbol
|
345 |
* @param xScale
|
346 |
*/
|
347 |
public double getXScale() { |
348 |
return xScale;
|
349 |
} |
350 |
/**
|
351 |
* Returns the yscale for the picture line symbol
|
352 |
* @param yScale
|
353 |
*/
|
354 |
public double getYScale() { |
355 |
return yScale;
|
356 |
} |
357 |
|
358 |
@Override
|
359 |
public void setCartographicSize(double cartographicSize, FShape shp) { |
360 |
getLineStyle().setLineWidth((float) cartographicSize);
|
361 |
double scale = cartographicSize/width;
|
362 |
csXScale = xScale * scale; |
363 |
csYScale = yScale * scale; |
364 |
} |
365 |
|
366 |
@Override
|
367 |
public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) { |
368 |
double s = super.toCartographicSize(viewPort, dpi, shp); |
369 |
setCartographicSize(CartographicSupportToolkit. |
370 |
getCartographicLength(this, width, viewPort, dpi), shp);
|
371 |
return s;
|
372 |
} |
373 |
} |