svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / line / impl / AbstractLineSymbol.java @ 40560
History | View | Annotate | Download (12 KB)
1 | 40560 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40560 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 40435 | jjdelcerro | * This program is free software; you can redistribute it and/or
|
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40560 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | 40560 | jjdelcerro | *
|
11 | 40435 | jjdelcerro | * This program is distributed in the hope that it will be useful,
|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | 40560 | jjdelcerro | *
|
16 | 40435 | jjdelcerro | * You should have received a copy of the GNU General Public License
|
17 | * along with this program; if not, write to the Free Software
|
||
18 | 40560 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 40435 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40560 | jjdelcerro | *
|
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.impl; |
||
25 | |||
26 | import java.awt.Color; |
||
27 | import java.awt.Graphics2D; |
||
28 | import java.awt.Rectangle; |
||
29 | import java.awt.geom.AffineTransform; |
||
30 | import java.awt.geom.PathIterator; |
||
31 | import java.awt.geom.Point2D; |
||
32 | import java.util.ArrayList; |
||
33 | import java.util.List; |
||
34 | |||
35 | import org.gvsig.compat.print.PrintAttributes; |
||
36 | import org.gvsig.fmap.geom.Geometry; |
||
37 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
38 | import org.gvsig.fmap.geom.GeometryLocator; |
||
39 | import org.gvsig.fmap.geom.GeometryManager; |
||
40 | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
||
41 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
42 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
43 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
||
44 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.AbstractSymbol; |
||
45 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit; |
||
46 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
||
47 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ILineStyle; |
||
48 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.SimpleLineStyle; |
||
49 | import org.gvsig.tools.ToolsLocator; |
||
50 | import org.gvsig.tools.dynobject.DynStruct; |
||
51 | import org.gvsig.tools.persistence.PersistenceManager; |
||
52 | import org.gvsig.tools.persistence.PersistentState; |
||
53 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
54 | import org.gvsig.tools.util.Callable; |
||
55 | |||
56 | |||
57 | /**
|
||
58 | * AbstractLineSymbol is the class that implements the interface for line symbols.
|
||
59 | * It is considered as the father of all the XXXLineSymbols and will implement all the
|
||
60 | * methods that these classes had not developed (and correspond with one of the methods
|
||
61 | * of AbstractLineSymbol class).
|
||
62 | *
|
||
63 | *
|
||
64 | * @author 2005-2009 jaume dominguez faus - jaume.dominguez@iver.es
|
||
65 | * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
||
66 | */
|
||
67 | public abstract class AbstractLineSymbol extends AbstractSymbol implements ILineSymbol { |
||
68 | |||
69 | private static final String FIELD_LINESTYLE = "lineStyle"; |
||
70 | |||
71 | private static final String FIELD_COLOR = "color"; |
||
72 | |||
73 | public static final String LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME = "LineSymbol"; |
||
74 | |||
75 | |||
76 | private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
||
77 | private Color color; |
||
78 | private ILineStyle lineStyle = new SimpleLineStyle(); |
||
79 | |||
80 | public AbstractLineSymbol() {
|
||
81 | super();
|
||
82 | color = |
||
83 | MapContextLocator.getSymbolManager() |
||
84 | .getSymbolPreferences() |
||
85 | .getDefaultSymbolFillColor(); |
||
86 | } |
||
87 | |||
88 | public Color getColor() { |
||
89 | return color;
|
||
90 | } |
||
91 | |||
92 | public void setLineColor(Color color) { |
||
93 | this.color = color;
|
||
94 | } |
||
95 | |||
96 | public void setColor(Color color) { |
||
97 | setLineColor(color); |
||
98 | } |
||
99 | |||
100 | public int getOnePointRgb() { |
||
101 | return color.getRGB();
|
||
102 | } |
||
103 | |||
104 | public final int getSymbolType() { |
||
105 | return Geometry.TYPES.CURVE;
|
||
106 | } |
||
107 | |||
108 | public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) { |
||
109 | float cs = (float) getCartographicSize(viewPort, dpi, geom); |
||
110 | // TODO and add the line offset
|
||
111 | distances[0] = cs;
|
||
112 | distances[1] = cs;
|
||
113 | } |
||
114 | |||
115 | public boolean isSuitableFor(Geometry geom) { |
||
116 | switch(geom.getType()) {
|
||
117 | case Geometry.TYPES.CURVE:
|
||
118 | case Geometry.TYPES.ARC:
|
||
119 | return true; |
||
120 | } |
||
121 | return false; |
||
122 | } |
||
123 | |||
124 | public ILineStyle getLineStyle() {
|
||
125 | return lineStyle;
|
||
126 | } |
||
127 | |||
128 | public void setLineStyle(ILineStyle lineStyle) { |
||
129 | this.lineStyle = lineStyle;
|
||
130 | } |
||
131 | |||
132 | public int getAlpha() { |
||
133 | return color.getAlpha();
|
||
134 | } |
||
135 | |||
136 | public void setAlpha(int outlineAlpha) { |
||
137 | color = new Color(color.getRed(), color.getGreen(), color.getBlue(), outlineAlpha); |
||
138 | } |
||
139 | |||
140 | public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException { |
||
141 | final int hGap = (int) (r.getWidth() * 0.1); // the left and right margins |
||
142 | final int vPos = 1; // the top and bottom margins |
||
143 | final int splitCount = 3; // number of lines |
||
144 | final int splitHSize = (r.width - hGap - hGap) / splitCount; |
||
145 | int hPos = hGap;
|
||
146 | boolean swap = false; |
||
147 | |||
148 | GeneralPathX gpx = new GeneralPathX();
|
||
149 | gpx.moveTo(r.x + hPos, r.y + r.height-vPos); |
||
150 | |||
151 | for (int i = 0; i < splitCount; i++) { |
||
152 | swap = !swap; |
||
153 | gpx.lineTo(r.x + hPos + splitHSize, (swap ? vPos : r.height-vPos) + r.y); |
||
154 | hPos += splitHSize; |
||
155 | } |
||
156 | |||
157 | try {
|
||
158 | if (properties==null) |
||
159 | draw(g, new AffineTransform(), geomManager.createCurve(gpx, SUBTYPES.GEOM2D), null, null); |
||
160 | else
|
||
161 | print(g, new AffineTransform(), geomManager.createCurve(gpx, SUBTYPES.GEOM2D), properties); |
||
162 | } catch (Exception e) { |
||
163 | throw new SymbolDrawingException(SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS); |
||
164 | } |
||
165 | |||
166 | } |
||
167 | |||
168 | |||
169 | |||
170 | public void setCartographicSize(double cartographicSize, Geometry geom) { |
||
171 | getLineStyle().setLineWidth((float) cartographicSize);
|
||
172 | } |
||
173 | |||
174 | public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
||
175 | double oldSize = getLineWidth();
|
||
176 | setCartographicSize(getCartographicSize( |
||
177 | viewPort, |
||
178 | dpi, |
||
179 | geom), |
||
180 | geom); |
||
181 | return oldSize;
|
||
182 | } |
||
183 | |||
184 | public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
||
185 | return CartographicSupportToolkit.
|
||
186 | getCartographicLength(this,
|
||
187 | getLineWidth(), |
||
188 | viewPort, |
||
189 | dpi); |
||
190 | } |
||
191 | |||
192 | public static Geometry offsetFShape(Geometry shp, double offset) { |
||
193 | Geometry offsetFShape = null;
|
||
194 | if (shp != null) { |
||
195 | if (offset == 0) |
||
196 | return shp;
|
||
197 | |||
198 | List<Point2D[]> segments = new ArrayList<Point2D[]>(); |
||
199 | GeneralPathX gpx = new GeneralPathX(shp.getPathIterator(null)); |
||
200 | PathIterator it = gpx.getPathIterator(null); |
||
201 | double[] data = new double[6]; |
||
202 | Point2D segmentIni = null; |
||
203 | Point2D segmentEnd = null; |
||
204 | while (!it.isDone()) {
|
||
205 | switch (it.currentSegment(data)) {
|
||
206 | case PathIterator.SEG_MOVETO: |
||
207 | segmentEnd = new Point2D.Double( |
||
208 | data[0], data[1]); |
||
209 | break;
|
||
210 | |||
211 | case PathIterator.SEG_LINETO: |
||
212 | segmentEnd = segmentIni; |
||
213 | segmentIni = new Point2D.Double( |
||
214 | data[0], data[1]); |
||
215 | |||
216 | segments.add(getParallel(segmentIni, segmentEnd, offset)); |
||
217 | break;
|
||
218 | |||
219 | case PathIterator.SEG_QUADTO: |
||
220 | break;
|
||
221 | |||
222 | case PathIterator.SEG_CUBICTO: |
||
223 | break;
|
||
224 | |||
225 | case PathIterator.SEG_CLOSE: |
||
226 | break;
|
||
227 | } |
||
228 | } |
||
229 | |||
230 | } |
||
231 | |||
232 | |||
233 | return offsetFShape;
|
||
234 | } |
||
235 | |||
236 | private static Point2D[] getParallel(Point2D p1, Point2D p2, double distance) { |
||
237 | Point2D[] pParallel=new Point2D[2]; |
||
238 | pParallel[0]=getPerpendicularPoint(p1,p2,p1,distance);
|
||
239 | pParallel[1]=getPerpendicularPoint(p1,p2,p2,distance);
|
||
240 | return pParallel;
|
||
241 | } |
||
242 | |||
243 | /**
|
||
244 | * Obtiene el punto que se encuentra a una distancia 'dist' de la recta
|
||
245 | * p1-p2 y se encuentra en la recta perpendicular que pasa por perpPoint
|
||
246 | *
|
||
247 | * @param p1 Punto de la recta p1-p2
|
||
248 | * @param p2 Punto de la recta p1-p2
|
||
249 | * @param perpPoint Punto de la recta perpendicular
|
||
250 | * @param dist Distancia del punto que se quiere obtener a la recta p1-p2
|
||
251 | *
|
||
252 | * @return DOCUMENT ME!
|
||
253 | */
|
||
254 | private static Point2D getPerpendicularPoint(Point2D p1, Point2D p2, |
||
255 | Point2D perpPoint, double dist) { |
||
256 | Point2D[] p = getPerpendicular(p1, p2, perpPoint); |
||
257 | Point2D unit = getUnitVector(p[0], p[1]); |
||
258 | |||
259 | return new Point2D.Double(perpPoint.getX() + (unit.getX() * dist), |
||
260 | perpPoint.getY() + (unit.getY() * dist)); |
||
261 | } |
||
262 | |||
263 | |||
264 | /**
|
||
265 | * Obtiene un par de puntos que definen la recta perpendicular a p1-p2 que
|
||
266 | * pasa por el punto perp
|
||
267 | *
|
||
268 | * @param p1 punto de la recta p1-p2
|
||
269 | * @param p2 punto de la recta p1-p2
|
||
270 | * @param perp Punto por el que pasa la recta perpendicular, debe ser
|
||
271 | * distinto a p2
|
||
272 | *
|
||
273 | * @return Array con dos puntos que definen la recta resultante
|
||
274 | */
|
||
275 | private static Point2D[] getPerpendicular(Point2D p1, Point2D p2, |
||
276 | Point2D perp) {
|
||
277 | if ((p2.getY() - p1.getY()) == 0) { |
||
278 | return new Point2D[] { |
||
279 | new Point2D.Double(perp.getX(), 0), |
||
280 | new Point2D.Double(perp.getX(), 1) |
||
281 | }; |
||
282 | } |
||
283 | |||
284 | //Pendiente de la recta perpendicular
|
||
285 | double m = (p1.getX() - p2.getX()) / (p2.getY() - p1.getY());
|
||
286 | |||
287 | //b de la funcion de la recta perpendicular
|
||
288 | double b = perp.getY() - (m * perp.getX());
|
||
289 | |||
290 | //Obtenemos un par de puntos
|
||
291 | Point2D[] res = new Point2D[2]; |
||
292 | |||
293 | res[0] = new Point2D.Double(0, (m * 0) + b); |
||
294 | res[1] = new Point2D.Double(1000, (m * 1000) + b); |
||
295 | |||
296 | return res;
|
||
297 | } |
||
298 | |||
299 | /**
|
||
300 | * Devuelve un vector unitario en forma de punto a partir de dos puntos.
|
||
301 | *
|
||
302 | * @param p1 punto origen.
|
||
303 | * @param p2 punto destino.
|
||
304 | *
|
||
305 | * @return vector unitario.
|
||
306 | */
|
||
307 | private static Point2D getUnitVector(Point2D p1, Point2D p2) { |
||
308 | Point2D paux = new Point2D.Double(p2.getX() - p1.getX(), |
||
309 | p2.getY() - p1.getY()); |
||
310 | double v = Math.sqrt(Math.pow(paux.getX(), 2d) + |
||
311 | Math.pow(paux.getY(), 2d)); |
||
312 | paux = new Point2D.Double(paux.getX() / v, paux.getY() / v); |
||
313 | |||
314 | return paux;
|
||
315 | } |
||
316 | public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) { |
||
317 | double originalSize = getLineWidth();
|
||
318 | double size=originalSize;
|
||
319 | // scale it to size
|
||
320 | int pq = properties.getPrintQuality();
|
||
321 | if (pq == PrintAttributes.PRINT_QUALITY_NORMAL){
|
||
322 | size *= (double) 300/72; |
||
323 | }else if (pq == PrintAttributes.PRINT_QUALITY_HIGH){ |
||
324 | size *= (double) 600/72; |
||
325 | }else if (pq == PrintAttributes.PRINT_QUALITY_DRAFT){ |
||
326 | // size *= 72/72; // (which is the same than doing nothing)
|
||
327 | } |
||
328 | setLineWidth(size); |
||
329 | draw(g,at,geom,null, null); |
||
330 | setLineWidth(originalSize); |
||
331 | } |
||
332 | |||
333 | public Object clone() throws CloneNotSupportedException { |
||
334 | AbstractLineSymbol copy = (AbstractLineSymbol) super.clone();
|
||
335 | |||
336 | // Clone the line style
|
||
337 | if (lineStyle != null) { |
||
338 | copy.lineStyle = (ILineStyle) lineStyle.clone(); |
||
339 | } |
||
340 | |||
341 | return copy;
|
||
342 | } |
||
343 | |||
344 | |||
345 | public void loadFromState(PersistentState state) |
||
346 | throws PersistenceException {
|
||
347 | // Set parent symbol properties
|
||
348 | super.loadFromState(state);
|
||
349 | // Set own properties
|
||
350 | setColor((Color) state.get(FIELD_COLOR));
|
||
351 | setLineStyle((ILineStyle) state.get(FIELD_LINESTYLE)); |
||
352 | } |
||
353 | |||
354 | public void saveToState(PersistentState state) throws PersistenceException { |
||
355 | // Save parent symbol properties
|
||
356 | super.saveToState(state);
|
||
357 | // Save own properties
|
||
358 | state.set(FIELD_COLOR, getColor()); |
||
359 | state.set(FIELD_LINESTYLE, getLineStyle()); |
||
360 | } |
||
361 | |||
362 | public static class RegisterPersistence implements Callable { |
||
363 | |||
364 | public Object call() throws Exception { |
||
365 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
366 | if( manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) { |
||
367 | DynStruct definition = manager.addDefinition( |
||
368 | AbstractLineSymbol.class, |
||
369 | LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
||
370 | LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
||
371 | null,
|
||
372 | null
|
||
373 | ); |
||
374 | |||
375 | // Extend the Symbol base definition
|
||
376 | definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME)); |
||
377 | |||
378 | // Color
|
||
379 | definition.addDynFieldObject(FIELD_COLOR).setMandatory(false).setClassOfValue(Color.class); |
||
380 | // LineStyle
|
||
381 | definition.addDynFieldObject(FIELD_LINESTYLE).setMandatory(false).setClassOfValue(ILineStyle.class);
|
||
382 | } |
||
383 | return Boolean.TRUE; |
||
384 | } |
||
385 | |||
386 | } |
||
387 | |||
388 | |||
389 | } |