Statistics
| Revision:

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 / impl / FLabel.java @ 40560

History | View | Annotate | Download (12.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * Created on 13-jul-2004
26
 *
27
 * TODO To change the template for this generated file go to
28
 * Window - Preferences - Java - Code Generation - Code and Comments
29
 */
30
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
31
 *
32
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
33
 *
34
 * This program is free software; you can redistribute it and/or
35
 * modify it under the terms of the GNU General Public License
36
 * as published by the Free Software Foundation; either version 2
37
 * of the License, or (at your option) any later version.
38
 *
39
 * This program is distributed in the hope that it will be useful,
40
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42
 * GNU General Public License for more details.
43
 *
44
 * You should have received a copy of the GNU General Public License
45
 * along with this program; if not, write to the Free Software
46
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
47
 *
48
 * For more information, contact:
49
 *
50
 *  Generalitat Valenciana
51
 *   Conselleria d'Infraestructures i Transport
52
 *   Av. Blasco Ib??ez, 50
53
 *   46010 VALENCIA
54
 *   SPAIN
55
 *
56
 *      +34 963862235
57
 *   gvsig@gva.es
58
 *      www.gvsig.gva.es
59
 *
60
 *    or
61
 *
62
 *   IVER T.I. S.A
63
 *   Salamanca 50
64
 *   46005 Valencia
65
 *   Spain
66
 *
67
 *   +34 963163400
68
 *   dac@iver.es
69
 */
70
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl;
71

    
72
import java.awt.Color;
73
import java.awt.Font;
74
import java.awt.Graphics2D;
75
import java.awt.Shape;
76
import java.awt.geom.AffineTransform;
77
import java.awt.geom.Point2D;
78
import java.awt.geom.Rectangle2D;
79

    
80
import org.apache.batik.ext.awt.geom.PathLength;
81
import org.slf4j.Logger;
82
import org.slf4j.LoggerFactory;
83

    
84
import org.gvsig.fmap.geom.aggregate.MultiPoint;
85
import org.gvsig.fmap.geom.exception.CreateGeometryException;
86
import org.gvsig.fmap.geom.primitive.Point;
87
import org.gvsig.fmap.mapcontext.ViewPort;
88
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
89
import org.gvsig.utils.XMLEntity;
90

    
91
/**
92
 * Se utiliza para etiquetar. Las capas vectoriales tienen un arrayList
93
 * (m_labels) de FLabel, un FLabel por cada registro.
94
 *
95
 * @author FJP
96
 * @deprecated
97
 */
98
public class FLabel implements Cloneable {
99
    
100
    private static final Logger logger = LoggerFactory.getLogger(FLabel.class);
101

    
102
    
103
        public final static byte LEFT_TOP = 0;
104
        public final static byte LEFT_CENTER = 1;
105
        public final static byte LEFT_BOTTOM = 2;
106
        public final static byte CENTER_TOP = 6;
107
        public final static byte CENTER_CENTER = 7;
108
        public final static byte CENTER_BOTTOM = 8;
109
        public final static byte RIGHT_TOP = 12;
110
        public final static byte RIGHT_CENTER = 13;
111
        public final static byte RIGHT_BOTTOM = 14;
112
        private String m_Str = null;
113
        private Point2D m_Orig = null;
114
        private double m_Height;
115
        public static final double SQUARE = Math.sqrt(2.0);
116
        /**
117
         * <code>m_rotation</code> en grados, NO en radianes. Se convierte en
118
         * radianes al dibujar.
119
         */
120
        private double m_rotation;
121

    
122
        /**
123
         * Valores posibles para <code>m_Justification</code>: Left/Top = 0
124
         * Center/Top = 6                Right/Top = 12 Left/Center = 1
125
         * Center/Center = 7            Right/Center = 13 Left/Bottom = 2
126
         * Center/Bottom = 8            Right/Bottom = 14
127
         */
128
        private byte m_Justification = LEFT_BOTTOM; // Por defecto
129
        private int m_Style;
130
        private Rectangle2D boundBox;
131
        private Color color;
132
        private static Font font=new Font("Dialog",Font.PLAIN,12);
133
        /**
134
         * Crea un nuevo FLabel.
135
         */
136
        public FLabel() {
137
                // nothing to do
138
        }
139

    
140
        /**
141
         * Crea un nuevo FLabel.
142
         *
143
         * @param str DOCUMENT ME!
144
         */
145
        public FLabel(String str) {
146
                m_Str = str;
147
        }
148

    
149
        /**
150
         * Crea un nuevo FLabel.
151
         *
152
         * @param str
153
         * @param pOrig
154
         * @param heightText
155
         * @param rotation
156
         */
157
        public FLabel(String str, Point2D pOrig, double heightText, double rotation) {
158
                m_Str = str;
159
                m_Orig = pOrig;
160
                m_Height = heightText;
161
                m_rotation = rotation;
162
        }
163

    
164
        /**
165
         * Introduce un nuevo FLabel al ya existente.
166
         *
167
         * @param label
168
         */
169
        public void setFLabel(FLabel label) {
170
                m_Str = label.m_Str;
171
                m_Orig = label.m_Orig;
172
                m_Height = label.m_Height;
173
                m_rotation = label.m_rotation;
174
        }
175

    
176
        /**
177
         * Clona el FLabel.
178
         *
179
         * @return Object clonado.
180
         */
181
        public Object clone() {
182
                FLabel label=new FLabel(m_Str, m_Orig, m_Height, m_rotation);
183
                label.boundBox=(Rectangle2D)boundBox.clone();
184
                return label;
185
        }
186

    
187
        /**
188
         * Devuelve la altura del Label.
189
         *
190
         * @return Returns the m_Height.
191
         */
192
        public double getHeight() {
193
                return m_Height;
194
        }
195

    
196
        /**
197
         * Devuelve el punto de origen.
198
         *
199
         * @return Returns the m_Orig.
200
         */
201
        public Point2D getOrig() {
202
                return m_Orig;
203
        }
204

    
205
        /**
206
         * Devuelve la rotaci?n.
207
         *
208
         * @return Returns the m_rotation.
209
         */
210
        public double getRotation() {
211
                return m_rotation;
212
        }
213

    
214
        /**
215
         * Devuelve un String con el texto del Label.
216
         *
217
         * @return Returns the m_Str.
218
         */
219
        public String getString() {
220
                return m_Str;
221
        }
222

    
223
        /**
224
         * Introduce la altura del Label.
225
         *
226
         * @param height The m_Height to set.
227
         */
228
        public void setHeight(double height) {
229
                m_Height = height;
230
        }
231

    
232
        /**
233
         * Introduce el punto de origen del Label.
234
         *
235
         * @param orig The m_Orig to set.
236
         */
237
        public void setOrig(Point2D orig) {
238
                m_Orig = orig;
239
        }
240

    
241
        /**
242
         * Introduce la rotaci?n a aplicar al Label.
243
         *
244
         * @param m_rotation The m_rotation to set.
245
         */
246
        public void setRotation(double m_rotation) {
247
                this.m_rotation = Math.toRadians(-m_rotation);
248
        }
249

    
250
        /**
251
         * Introduce el texto del Label.
252
         *
253
         * @param str The m_Str to set.
254
         */
255
        public void setString(String str) {
256
                m_Str = str;
257
        }
258

    
259
        /**
260
         * Valores posibles para <code>m_Justification</code>: Left/Top = 0
261
         * Center/Top = 6                Right/Top = 12 Left/Center = 1
262
         * Center/Center = 7            Right/Center = 13 Left/Bottom = 2
263
         * Center/Bottom = 8            Right/Bottom = 14
264
         *
265
         * @return byte.
266
         */
267
        public byte getJustification() {
268
                return m_Justification;
269
        }
270

    
271
        /**
272
         * Valores posibles para <code>m_Justification</code>: Left/Top = 0
273
         * Center/Top = 6                Right/Top = 12 Left/Center = 1
274
         * Center/Center = 7            Right/Center = 13 Left/Bottom = 2
275
         * Center/Bottom = 8            Right/Bottom = 14
276
         *
277
         * @param justification byte
278
         */
279
        public void setJustification(byte justification) {
280
                m_Justification = justification;
281
        }
282

    
283
        /**
284
         * Devuelve un Objeto XMLEntity con la informaci?n los atributos necesarios
285
         * para poder despu?s volver a crear el objeto original.
286
         *
287
         * @return XMLEntity.
288
         */
289
        public XMLEntity getXMLEntity() {
290
                XMLEntity xml = new XMLEntity();
291
                xml.putProperty("className",this.getClass().getName());
292
                xml.setName("flabel");
293
                xml.putProperty("m_Height", m_Height);
294
                xml.putProperty("m_Justification", (int) m_Justification);
295
                xml.putProperty("m_OrigX", m_Orig.getX());
296
                xml.putProperty("m_OrigY", m_Orig.getY());
297
                xml.putProperty("m_rotation", m_rotation);
298
                xml.putProperty("m_Str", m_Str);
299

    
300
                return xml;
301
        }
302

    
303
        /**
304
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
305
         *
306
         * @param xml XMLEntity
307
         *
308
         * @return Objeto de esta clase.
309
         */
310
        public static FLabel createFLabel(XMLEntity xml) {
311
                FLabel label = new FLabel();
312
                label.setHeight(xml.getDoubleProperty("m_Height"));
313
                label.setJustification((byte) xml.getIntProperty("m_Justification"));
314
                label.setOrig(new Point2D.Double(xml.getDoubleProperty("m_OrigX"),
315
                                xml.getDoubleProperty("m_OrigY")));
316
                label.setString("m_Str");
317

    
318
                return label;
319
        }
320

    
321
        /**
322
         * M?todo est?tico que crea un FLabel a partir de una Geometry.
323
         *
324
         * @param geom Geometry.
325
         *
326
         * @return nuevo FLabel creado.
327
         * @throws CreateGeometryException 
328
         */
329
        public static FLabel createFLabel(org.gvsig.fmap.geom.Geometry geom) throws CreateGeometryException {
330
                float angle;
331
                Point2D pAux = createLabelPoint(geom);
332

    
333
                FLabel label = new FLabel();
334
                label.setOrig(pAux);
335
                switch (geom.getType()) {
336
                        case org.gvsig.fmap.geom.Geometry.TYPES.CURVE:
337
                           PathLength pathLen = new PathLength(geom.getShape());
338
                                float midDistance = pathLen.lengthOfPath() / 2;
339
                                angle = pathLen.angleAtLength(midDistance);
340

    
341
                                if (angle < 0) {
342
                                        angle = angle + (float) (2 * Math.PI);
343
                                }
344
                                if ((angle > (Math.PI / 2)) && (angle < ((3 * Math.PI) / 2))) {
345
                                        angle = angle - (float) Math.PI;
346
                                }
347
                                label.setRotation(Math.toDegrees(angle));
348
                                break;
349
                } // switch
350

    
351
                return label;
352
        }
353
        public static Point2D createLabelPoint(org.gvsig.fmap.geom.Geometry geom) throws CreateGeometryException {
354
                Point2D pAux = null;
355
                switch (geom.getType()) {
356
                        case org.gvsig.fmap.geom.Geometry.TYPES.POINT:
357
                    pAux = new Point2D.Double(((org.gvsig.fmap.geom.primitive.Point) geom).getX(),
358
                                                ((org.gvsig.fmap.geom.primitive.Point) geom).getY());
359
                                return pAux;
360

    
361
                        case org.gvsig.fmap.geom.Geometry.TYPES.CURVE:
362
                    PathLength pathLen = new PathLength(geom.getShape());
363

    
364
                                // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
365
                                float midDistance = pathLen.lengthOfPath() / 2;
366
                                pAux = pathLen.pointAtLength(midDistance);
367
                                return pAux;
368

    
369
                        case org.gvsig.fmap.geom.Geometry.TYPES.SURFACE:
370
                            
371
                            try {
372
                        Point pLabel = geom.centroid();
373
                        return new Point2D.Double(pLabel.getX(), pLabel.getY());
374
                            } catch (Exception ex) {
375
                                logger.info("Error while computing centroid. ", ex);
376
                            }
377
                                break;
378
            case org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT:
379
                    int num=((MultiPoint)geom).getPrimitivesNumber();
380
                            Rectangle2D r=null;
381
                            if (num>0){
382
                                    r= ((MultiPoint)geom).getPointAt(0).getBounds2D();
383
                                    for (int i=1;i<num;i++){
384
                                            org.gvsig.fmap.geom.primitive.Point fp=((MultiPoint)geom).getPointAt(i);
385
                                            r.add(new Point2D.Double(fp.getX(),fp.getY()));
386
                                    }
387
                            }
388
                            if (r!=null)
389
                            return new Point2D.Double(r.getCenterX(),r.getCenterY());
390
                            break;
391

    
392
                } // switch
393
                                
394
                return null;
395
        }
396
        public void setTypeFont(String t) {
397
                font=Font.getFont(t,font);
398
        }
399
        public int getStyle() {
400
                return m_Style;
401
        }
402

    
403
        public void setBoundBox(Rectangle2D boundBox) {
404
                this.boundBox=boundBox;
405

    
406
        }
407
        public Rectangle2D getBoundBox(){
408
                return boundBox;
409
        }
410

    
411
        public Rectangle2D getBoundingBox(ViewPort vp) {
412
                return vp.fromMapRectangle(boundBox);
413
        }
414

    
415
        public void setColor(int i) {
416
                color=new Color(i);
417
        }
418

    
419
        public Color getColor() {
420
                return color;
421
        }
422
        /*public Font getFont(AffineTransform at,boolean isInPixels){
423
                if (!isInPixels) {
424
                        float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE);
425
                        //Font nuevaFuente = font.deriveFont(alturaPixels);//new Font(getTypeFont(),getStyle(),(int)alturaPixels);
426
                        return font.deriveFont(alturaPixels);
427
                }
428
                //Font nuevaFuente = font.deriveFont((float)(getHeight()*SQUARE));//new Font(getTypeFont(),getStyle(),(int)(getHeight()*SQUARE));
429
                return font;//font.deriveFont((float)(getHeight()*SQUARE));
430
        }*/
431
        public Font getFont(AffineTransform at,Font font){
432
                float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE);
433
                //return font.deriveFont(alturaPixels);
434
        return font.deriveFont(font.getStyle(),alturaPixels);
435
        }
436

    
437
        /**
438
         * Por ahora, para extender el renderizado de etiquetas, habr?a que
439
         * crear otro FLabel y reimplementar este m?todo para que haga caso
440
         * a otros s?mbolos. Es decir, NO usar FGraphicUtilities
441
         * @param g
442
         * @param affineTransform
443
         * @param theShape
444
         * @param theSymbol
445
         */
446
        public void draw(Graphics2D g, AffineTransform affineTransform, Shape theShape, ISymbol theSymbol) {
447
        FGraphicUtilities.DrawLabel(g, affineTransform,
448
                (org.gvsig.fmap.geom.Geometry) theShape, (FSymbol) theSymbol, this);
449

    
450

    
451
        }
452

    
453
}