Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / AbstractLineSymbol.java @ 28367

History | View | Annotate | Download (10.4 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

    
42
/* CVS MESSAGES:
43
*
44
* $Id: AbstractLineSymbol.java 28367 2009-05-04 15:30:10Z vcaballero $
45
* $Log$
46
* Revision 1.14  2007-09-21 12:25:32  jaume
47
* cancellation support extended down to the IGeometry and ISymbol level
48
*
49
* Revision 1.13  2007/09/19 16:25:47  caballero
50
* FFrameLegend
51
*
52
* Revision 1.11  2007/09/18 14:50:31  caballero
53
* Leyendas sobre el Layout
54
*
55
* Revision 1.10  2007/09/17 09:33:47  jaume
56
* some multishapedsymbol bugs fixed
57
*
58
* Revision 1.9  2007/08/13 11:36:50  jvidal
59
* javadoc
60
*
61
* Revision 1.8  2007/07/18 06:54:34  jaume
62
* continuing with cartographic support
63
*
64
* Revision 1.7  2007/07/03 10:58:29  jaume
65
* first refactor on CartographicSupport
66
*
67
* Revision 1.6  2007/06/29 13:07:01  jaume
68
* +PictureLineSymbol
69
*
70
* Revision 1.5  2007/05/08 08:47:39  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.4  2007/03/20 16:00:00  jaume
74
* rename method
75
*
76
* Revision 1.3  2007/03/09 11:20:56  jaume
77
* Advanced symbology (start committing)
78
*
79
* Revision 1.2.2.3  2007/02/21 16:09:02  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.2.2.2  2007/02/15 16:23:44  jaume
83
* *** empty log message ***
84
*
85
* Revision 1.2.2.1  2007/02/09 07:47:04  jaume
86
* Isymbol moved
87
*
88
* Revision 1.2  2007/01/24 17:58:22  jaume
89
* new features and architecture error fixes
90
*
91
* Revision 1.1  2007/01/16 11:50:44  jaume
92
* *** empty log message ***
93
*
94
*
95
*/
96
package com.iver.cit.gvsig.fmap.core.symbols;
97

    
98
import java.awt.Color;
99
import java.awt.Graphics2D;
100
import java.awt.Rectangle;
101
import java.awt.geom.AffineTransform;
102
import java.awt.geom.PathIterator;
103
import java.awt.geom.Point2D;
104
import java.util.ArrayList;
105

    
106
import javax.print.attribute.PrintRequestAttributeSet;
107
import javax.print.attribute.standard.PrintQuality;
108

    
109
import com.iver.cit.gvsig.fmap.ViewPort;
110
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
111
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
112
import com.iver.cit.gvsig.fmap.core.FShape;
113
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
114
import com.iver.cit.gvsig.fmap.core.IGeometry;
115
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
116
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle;
117
import com.iver.cit.gvsig.fmap.core.styles.SimpleLineStyle;
118

    
119
/**
120
 * AbstractLineSymbol is the class that implements the interface for line symbols.
121
 * It is considered as the father of all the XXXLineSymbols and will implement all the
122
 * methods that these classes had not developed (and correspond with one of the methods
123
 * of AbstractLineSymbol class).
124
 *
125
 *
126
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
127
 */
128
public abstract class AbstractLineSymbol extends AbstractSymbol implements ILineSymbol {
129
        private Color color = SymbologyFactory.DefaultSymbolColor;
130
        private ILineStyle lineStyle;
131

    
132

    
133
        public Color getColor() {
134
                return color;
135
        }
136

    
137
        public void setLineColor(Color color) {
138
                this.color = color;
139
        }
140

    
141
         public int getOnePointRgb() {
142
                return color.getRGB();
143
        }
144

    
145
        public final int getSymbolType() {
146
                return FShape.LINE;
147
        }
148

    
149
        public void getPixExtentPlus(FShape shp, float[] distances, ViewPort viewPort, int dpi) {
150
                float cs = (float) getCartographicSize(viewPort, dpi, shp);
151
                // TODO and add the line offset
152
                distances[0] = cs;
153
                distances[1] = cs;
154
        }
155

    
156
        public boolean isSuitableFor(IGeometry geom) {
157
                switch(geom.getGeometryType()) {
158
                // pasted from FSymbol
159
                case FShape.LINE:
160
                case FShape.LINE + FShape.Z:
161
                case FShape.ARC:
162
                case FShape.ARC + FShape.Z:
163
                        return true;
164
                }
165
                return false;
166
        }
167

    
168
        public ILineStyle getLineStyle() {
169
                if (lineStyle == null)
170
                        lineStyle = new SimpleLineStyle();
171
                return lineStyle;
172
        }
173

    
174
        public void setLineStyle(ILineStyle lineStyle) {
175
                this.lineStyle = lineStyle;
176
        }
177

    
178
        public int getAlpha() {
179
                return color.getAlpha();
180
        }
181

    
182
        public void setAlpha(int outlineAlpha) {
183
                color = new Color(color.getRed(), color.getGreen(), color.getBlue(), outlineAlpha);
184
        }
185

    
186
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties) throws SymbolDrawingException {
187
                final int hGap = (int) (r.getWidth() * 0.1); // the left and right margins
188
                final int vPos = 1;                                                  // the top and bottom margins
189
                final int splitCount = 3;                                          // number of lines
190
                final int splitHSize = (r.width - hGap - hGap) / splitCount;
191
                int hPos = hGap;
192
                boolean swap = false;
193

    
194
                GeneralPathX gpx = new GeneralPathX();
195
                gpx.moveTo(r.x + hPos, r.y + r.height-vPos);
196

    
197
                for (int i = 0; i < splitCount; i++) {
198
                        swap = !swap;
199
                        gpx.lineTo(r.x + hPos + splitHSize, (swap ? vPos : r.height-vPos) + r.y);
200
                        hPos += splitHSize;
201
                }
202

    
203
                try {
204
                        if (properties==null)
205
                                draw(g, new AffineTransform(), new FPolygon2D(gpx), null);
206
                        else
207
                                print(g, new AffineTransform(), new FPolygon2D(gpx), properties);
208
                } catch (Exception e) {
209
                        throw new SymbolDrawingException(SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS);
210
                }
211
        }
212

    
213

    
214

    
215
        public void setCartographicSize(double cartographicSize, FShape shp) {
216
                getLineStyle().setLineWidth((float) cartographicSize);
217
        }
218

    
219
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
220
                double oldSize = getLineWidth();
221
                setCartographicSize(getCartographicSize(
222
                                                                viewPort,
223
                                                                dpi,
224
                                                                shp),
225
                                                        shp);
226
                return oldSize;
227
        }
228

    
229
        public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
230
                return CartographicSupportToolkit.
231
                                        getCartographicLength(this,
232
                                                                                  getLineWidth(),
233
                                                                                  viewPort,
234
                                                                                  dpi);
235
        }
236

    
237
        public static FShape offsetFShape(FShape shp, double offset) {
238
                FShape offsetFShape = null;
239
                if (shp != null) {
240
                        if (offset == 0)
241
                                return shp;
242

    
243
                        ArrayList<Point2D[]> segments = new ArrayList<Point2D[]>();
244
                        GeneralPathX gpx = new GeneralPathX(shp);
245
                        PathIterator it = gpx.getPathIterator(null);
246
                        double[] data = new double[6];
247
                        Point2D segmentIni = null;
248
                        Point2D segmentEnd = null;
249
                        while (!it.isDone()) {
250
                                switch (it.currentSegment(data)) {
251
                                case PathIterator.SEG_MOVETO:
252
                                        segmentEnd = new Point2D.Double(
253
                                                        data[0], data[1]);
254
                                        break;
255

    
256
                                case PathIterator.SEG_LINETO:
257
                                        segmentEnd = segmentIni;
258
                                        segmentIni = new Point2D.Double(
259
                                                        data[0], data[1]);
260

    
261
                                        segments.add(getParallel(segmentIni, segmentEnd, offset));
262
                                        break;
263

    
264
                                case PathIterator.SEG_QUADTO:
265
                                        break;
266

    
267
                                case PathIterator.SEG_CUBICTO:
268
                                        break;
269

    
270
                                case PathIterator.SEG_CLOSE:
271
                                        break;
272
                                }
273
                        }
274

    
275
                }
276

    
277

    
278
                return offsetFShape;
279
        }
280

    
281
        private static Point2D[] getParallel(Point2D p1, Point2D p2, double distance) {
282
                Point2D[] pParallel=new Point2D[2];
283
                pParallel[0]=getPerpendicularPoint(p1,p2,p1,distance);
284
                pParallel[1]=getPerpendicularPoint(p1,p2,p2,distance);
285
                return pParallel;
286
        }
287

    
288
        /**
289
         * Obtiene el punto que se encuentra a una distancia 'dist' de la recta
290
         * p1-p2 y se encuentra en la recta perpendicular que pasa por perpPoint
291
         *
292
         * @param p1 Punto de la recta p1-p2
293
         * @param p2 Punto de la recta p1-p2
294
         * @param perpPoint Punto de la recta perpendicular
295
         * @param dist Distancia del punto que se quiere obtener a la recta p1-p2
296
         *
297
         * @return DOCUMENT ME!
298
         */
299
        private static Point2D getPerpendicularPoint(Point2D p1, Point2D p2,
300
                Point2D perpPoint, double dist) {
301
                Point2D[] p = getPerpendicular(p1, p2, perpPoint);
302
                Point2D unit = getUnitVector(p[0], p[1]);
303

    
304
                return new Point2D.Double(perpPoint.getX() + (unit.getX() * dist),
305
                        perpPoint.getY() + (unit.getY() * dist));
306
        }
307

    
308

    
309
        /**
310
         * Obtiene un par de puntos que definen la recta perpendicular a p1-p2 que
311
         * pasa por el punto perp
312
         *
313
         * @param p1 punto de la recta p1-p2
314
         * @param p2 punto de la recta p1-p2
315
         * @param perp Punto por el que pasa la recta perpendicular, debe ser
316
         *                   distinto a p2
317
         *
318
         * @return Array con dos puntos que definen la recta resultante
319
         */
320
        private static Point2D[] getPerpendicular(Point2D p1, Point2D p2,
321
                Point2D perp) {
322
                if ((p2.getY() - p1.getY()) == 0) {
323
                        return new Point2D[] {
324
                                new Point2D.Double(perp.getX(), 0),
325
                                new Point2D.Double(perp.getX(), 1)
326
                        };
327
                }
328

    
329
                //Pendiente de la recta perpendicular
330
                double m = (p1.getX() - p2.getX()) / (p2.getY() - p1.getY());
331

    
332
                //b de la funcion de la recta perpendicular
333
                double b = perp.getY() - (m * perp.getX());
334

    
335
                //Obtenemos un par de puntos
336
                Point2D[] res = new Point2D[2];
337

    
338
                res[0] = new Point2D.Double(0, (m * 0) + b);
339
                res[1] = new Point2D.Double(1000, (m * 1000) + b);
340

    
341
                return res;
342
        }
343

    
344
        /**
345
         * Devuelve un vector unitario en forma de punto a partir de dos puntos.
346
         *
347
         * @param p1 punto origen.
348
         * @param p2 punto destino.
349
         *
350
         * @return vector unitario.
351
         */
352
        private static Point2D getUnitVector(Point2D p1, Point2D p2) {
353
                Point2D paux = new Point2D.Double(p2.getX() - p1.getX(),
354
                                p2.getY() - p1.getY());
355
                double v = Math.sqrt(Math.pow(paux.getX(), 2d) +
356
                                Math.pow(paux.getY(), 2d));
357
                paux = new Point2D.Double(paux.getX() / v, paux.getY() / v);
358

    
359
                return paux;
360
        }
361
        public void print(Graphics2D g,
362
                          AffineTransform at,
363
                          FShape shape,
364
                          PrintRequestAttributeSet properties) {
365
                double originalSize = getLineWidth();
366
                double size=originalSize;
367
                // scale it to size
368
                PrintQuality pq = (PrintQuality) properties.get(PrintQuality.class);
369
                if (pq.equals(PrintQuality.NORMAL)){
370
                        size *= (double) 300/72;
371
                }else if (pq.equals(PrintQuality.HIGH)){
372
                        size *= (double) 600/72;
373
                }else if (pq.equals(PrintQuality.DRAFT)){
374
                        //        unitFactor *= 72; (which is the same than doing nothing)
375
                }
376
                setLineWidth(size);
377
                draw(g,at,shape,null);
378
                setLineWidth(originalSize);
379
        }
380
}