Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.symbology / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / line / impl / AbstractLineSymbol.java @ 31544

History | View | Annotate | Download (11.4 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * 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
 * 
16
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.impl;
23

    
24
import java.awt.Color;
25
import java.awt.Graphics2D;
26
import java.awt.Rectangle;
27
import java.awt.geom.AffineTransform;
28
import java.awt.geom.PathIterator;
29
import java.awt.geom.Point2D;
30
import java.util.ArrayList;
31
import java.util.List;
32

    
33
import org.gvsig.compat.print.PrintAttributes;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.GeometryLocator;
36
import org.gvsig.fmap.geom.GeometryManager;
37
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
38
import org.gvsig.fmap.geom.primitive.GeneralPathX;
39
import org.gvsig.fmap.mapcontext.MapContextLocator;
40
import org.gvsig.fmap.mapcontext.ViewPort;
41
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
42
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.AbstractSymbol;
43
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit;
44
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
45
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ILineStyle;
46
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.SimpleLineStyle;
47
import org.gvsig.tools.ToolsLocator;
48
import org.gvsig.tools.dynobject.DynClass;
49
import org.gvsig.tools.persistence.PersistenceException;
50
import org.gvsig.tools.persistence.PersistentState;
51

    
52

    
53
/**
54
 * AbstractLineSymbol is the class that implements the interface for line symbols.
55
 * It is considered as the father of all the XXXLineSymbols and will implement all the
56
 * methods that these classes had not developed (and correspond with one of the methods
57
 * of AbstractLineSymbol class).
58
 *
59
 *
60
 * @author 2005-2009 jaume dominguez faus - jaume.dominguez@iver.es
61
 * @author 2009-     <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
62
 */
63
public abstract class AbstractLineSymbol extends AbstractSymbol implements ILineSymbol {
64

    
65
    private static final String FIELD_LINESTYLE = "lineStyle";
66

    
67
    private static final String FIELD_COLOR = "color";
68

    
69
    public static final String LINE_SYMBOL_DYNCLASS_NAME = "LineSymbol";
70

    
71
    
72
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
73
        private Color color;
74
        private ILineStyle lineStyle = new SimpleLineStyle();
75

    
76
        public AbstractLineSymbol() {
77
                super();
78
                color =
79
                                MapContextLocator.getSymbolManager()
80
                                                .getSymbolPreferences()
81
                                                .getDefaultSymbolColor();
82
        }
83

    
84
        public Color getColor() {
85
                return color;
86
        }
87

    
88
        public void setLineColor(Color color) {
89
                this.color = color;
90
        }
91
        
92
        public void setColor(Color color) {
93
                setLineColor(color);
94
        }
95

    
96
        public int getOnePointRgb() {
97
                return color.getRGB();
98
        }
99

    
100
        public final int getSymbolType() {
101
                return Geometry.TYPES.CURVE;
102
        }
103

    
104
        public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) {
105
                float cs = (float) getCartographicSize(viewPort, dpi, geom);
106
                // TODO and add the line offset
107
                distances[0] = cs;
108
                distances[1] = cs;
109
        }
110

    
111
        public boolean isSuitableFor(Geometry geom) {
112
                switch(geom.getType()) {
113
                // pasted from FSymbol
114
                case Geometry.TYPES.CURVE:
115
                case Geometry.TYPES.ARC:
116
                        return true;
117
                }
118
                return false;
119
        }
120

    
121
        public ILineStyle getLineStyle() {
122
                return lineStyle;
123
        }
124

    
125
        public void setLineStyle(ILineStyle lineStyle) {
126
                this.lineStyle = lineStyle;
127
        }
128

    
129
        public int getAlpha() {
130
                return color.getAlpha();
131
        }
132

    
133
        public void setAlpha(int outlineAlpha) {
134
                color = new Color(color.getRed(), color.getGreen(), color.getBlue(), outlineAlpha);
135
        }
136

    
137
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
138
                final int hGap = (int) (r.getWidth() * 0.1); // the left and right margins
139
                final int vPos = 1;                                                  // the top and bottom margins
140
                final int splitCount = 3;                                          // number of lines
141
                final int splitHSize = (r.width - hGap - hGap) / splitCount;
142
                int hPos = hGap;
143
                boolean swap = false;
144

    
145
                GeneralPathX gpx = new GeneralPathX();
146
                gpx.moveTo(r.x + hPos, r.y + r.height-vPos);
147

    
148
                for (int i = 0; i < splitCount; i++) {
149
                        swap = !swap;
150
                        gpx.lineTo(r.x + hPos + splitHSize, (swap ? vPos : r.height-vPos) + r.y);
151
                        hPos += splitHSize;
152
                }
153

    
154
                try {
155
                        if (properties==null)
156
                                draw(g, new AffineTransform(), geomManager.createSurface(gpx, SUBTYPES.GEOM2D), null, null);
157
                        else
158
                                print(g, new AffineTransform(), geomManager.createSurface(gpx, SUBTYPES.GEOM2D), properties);
159
                } catch (Exception e) {
160
                        throw new SymbolDrawingException(SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
161
                }
162

    
163
        }
164

    
165

    
166

    
167
        public void setCartographicSize(double cartographicSize, Geometry geom) {
168
                getLineStyle().setLineWidth((float) cartographicSize);
169
        }
170

    
171
        public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
172
                double oldSize = getLineWidth();
173
                setCartographicSize(getCartographicSize(
174
                                                                viewPort,
175
                                                                dpi,
176
                                                                geom),
177
                                                        geom);
178
                return oldSize;
179
        }
180

    
181
        public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
182
                return CartographicSupportToolkit.
183
                                        getCartographicLength(this,
184
                                                                                  getLineWidth(),
185
                                                                                  viewPort,
186
                                                                                  dpi);
187
        }
188

    
189
        public static Geometry offsetFShape(Geometry shp, double offset) {
190
                Geometry offsetFShape = null;
191
                if (shp != null) {
192
                        if (offset == 0)
193
                                return shp;
194

    
195
                        List<Point2D[]> segments = new ArrayList<Point2D[]>();
196
                        GeneralPathX gpx = new GeneralPathX(shp.getPathIterator(null));
197
                        PathIterator it = gpx.getPathIterator(null);
198
                        double[] data = new double[6];
199
                        Point2D segmentIni = null;
200
                        Point2D segmentEnd = null;
201
                        while (!it.isDone()) {
202
                                switch (it.currentSegment(data)) {
203
                                case PathIterator.SEG_MOVETO:
204
                                        segmentEnd = new Point2D.Double(
205
                                                        data[0], data[1]);
206
                                        break;
207

    
208
                                case PathIterator.SEG_LINETO:
209
                                        segmentEnd = segmentIni;
210
                                        segmentIni = new Point2D.Double(
211
                                                        data[0], data[1]);
212

    
213
                                        segments.add(getParallel(segmentIni, segmentEnd, offset));
214
                                        break;
215

    
216
                                case PathIterator.SEG_QUADTO:
217
                                        break;
218

    
219
                                case PathIterator.SEG_CUBICTO:
220
                                        break;
221

    
222
                                case PathIterator.SEG_CLOSE:
223
                                        break;
224
                                }
225
                        }
226

    
227
                }
228

    
229

    
230
                return offsetFShape;
231
        }
232

    
233
        private static Point2D[] getParallel(Point2D p1, Point2D p2, double distance) {
234
                Point2D[] pParallel=new Point2D[2];
235
                pParallel[0]=getPerpendicularPoint(p1,p2,p1,distance);
236
                pParallel[1]=getPerpendicularPoint(p1,p2,p2,distance);
237
                return pParallel;
238
        }
239

    
240
        /**
241
         * Obtiene el punto que se encuentra a una distancia 'dist' de la recta
242
         * p1-p2 y se encuentra en la recta perpendicular que pasa por perpPoint
243
         *
244
         * @param p1 Punto de la recta p1-p2
245
         * @param p2 Punto de la recta p1-p2
246
         * @param perpPoint Punto de la recta perpendicular
247
         * @param dist Distancia del punto que se quiere obtener a la recta p1-p2
248
         *
249
         * @return DOCUMENT ME!
250
         */
251
        private static Point2D getPerpendicularPoint(Point2D p1, Point2D p2,
252
                Point2D perpPoint, double dist) {
253
                Point2D[] p = getPerpendicular(p1, p2, perpPoint);
254
                Point2D unit = getUnitVector(p[0], p[1]);
255

    
256
                return new Point2D.Double(perpPoint.getX() + (unit.getX() * dist),
257
                        perpPoint.getY() + (unit.getY() * dist));
258
        }
259

    
260

    
261
        /**
262
         * Obtiene un par de puntos que definen la recta perpendicular a p1-p2 que
263
         * pasa por el punto perp
264
         *
265
         * @param p1 punto de la recta p1-p2
266
         * @param p2 punto de la recta p1-p2
267
         * @param perp Punto por el que pasa la recta perpendicular, debe ser
268
         *                   distinto a p2
269
         *
270
         * @return Array con dos puntos que definen la recta resultante
271
         */
272
        private static Point2D[] getPerpendicular(Point2D p1, Point2D p2,
273
                Point2D perp) {
274
                if ((p2.getY() - p1.getY()) == 0) {
275
                        return new Point2D[] {
276
                                new Point2D.Double(perp.getX(), 0),
277
                                new Point2D.Double(perp.getX(), 1)
278
                        };
279
                }
280

    
281
                //Pendiente de la recta perpendicular
282
                double m = (p1.getX() - p2.getX()) / (p2.getY() - p1.getY());
283

    
284
                //b de la funcion de la recta perpendicular
285
                double b = perp.getY() - (m * perp.getX());
286

    
287
                //Obtenemos un par de puntos
288
                Point2D[] res = new Point2D[2];
289

    
290
                res[0] = new Point2D.Double(0, (m * 0) + b);
291
                res[1] = new Point2D.Double(1000, (m * 1000) + b);
292

    
293
                return res;
294
        }
295

    
296
        /**
297
         * Devuelve un vector unitario en forma de punto a partir de dos puntos.
298
         *
299
         * @param p1 punto origen.
300
         * @param p2 punto destino.
301
         *
302
         * @return vector unitario.
303
         */
304
        private static Point2D getUnitVector(Point2D p1, Point2D p2) {
305
                Point2D paux = new Point2D.Double(p2.getX() - p1.getX(),
306
                                p2.getY() - p1.getY());
307
                double v = Math.sqrt(Math.pow(paux.getX(), 2d) +
308
                                Math.pow(paux.getY(), 2d));
309
                paux = new Point2D.Double(paux.getX() / v, paux.getY() / v);
310

    
311
                return paux;
312
        }
313
        public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) {
314
                double originalSize = getLineWidth();
315
                double size=originalSize;
316
                // scale it to size
317
                int pq = properties.getPrintQuality();
318
                if (pq == PrintAttributes.PRINT_QUALITY_NORMAL){
319
                        size *= (double) 300/72;
320
                }else if (pq == PrintAttributes.PRINT_QUALITY_HIGH){
321
                        size *= (double) 600/72;
322
                }else if (pq == PrintAttributes.PRINT_QUALITY_DRAFT){
323
                        // size *= 72/72; // (which is the same than doing nothing)
324
                }
325
                setLineWidth(size);
326
                draw(g,at,geom,null, null);
327
                setLineWidth(originalSize);
328
        }
329
        
330
        public Object clone() throws CloneNotSupportedException {
331
                AbstractLineSymbol copy = (AbstractLineSymbol) super.clone();
332
                
333
                // Clone the line style
334
                if (lineStyle != null) {
335
                        copy.lineStyle = (ILineStyle) lineStyle.clone();
336
                }
337
                
338
                return copy;
339
        }
340

    
341
        
342
        public void loadFromState(PersistentState state)
343
                        throws PersistenceException {
344
                // Set parent symbol properties
345
                super.loadFromState(state);
346
                // Set own properties
347
                setColor((Color) state.get(FIELD_COLOR));
348
                setLineStyle((ILineStyle) state.get(FIELD_LINESTYLE));
349
        }
350

    
351
        public void saveToState(PersistentState state) throws PersistenceException {
352
                // Save parent symbol properties
353
                super.saveToState(state);
354
                // Save own properties
355
                state.set(FIELD_COLOR, getColor());
356
                state.set(FIELD_LINESTYLE, getLineStyle());
357
        }
358

    
359
        public static void registerPersistence() {
360
                // Add the AbstractLineSymbol DynClass definition.
361
                DynClass dynClass = ToolsLocator.getDynObjectManager().add(
362
                                LINE_SYMBOL_DYNCLASS_NAME);
363

    
364
                // Extend the Symbol base definition
365
                dynClass.extend(SYMBOL_DYNCLASS_NAME);
366

    
367
                // Color
368
                dynClass.addDynFieldObject(FIELD_COLOR).setMandatory(true);
369
                // LineStyle
370
                dynClass.addDynFieldObject(FIELD_LINESTYLE).setMandatory(true);
371
        }
372
}