Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / v02 / FLabel.java @ 32872

History | View | Annotate | Download (11.1 KB)

1
/*
2
 * Created on 13-jul-2004
3
 *
4
 * TODO To change the template for this generated file go to
5
 * Window - Preferences - Java - Code Generation - Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package com.iver.cit.gvsig.fmap.core.v02;
48

    
49
import java.awt.Color;
50
import java.awt.Font;
51
import java.awt.Graphics2D;
52
import java.awt.Shape;
53
import java.awt.geom.AffineTransform;
54
import java.awt.geom.Point2D;
55
import java.awt.geom.Rectangle2D;
56

    
57
import org.apache.batik.ext.awt.geom.PathLength;
58

    
59
import com.iver.cit.gvsig.fmap.ViewPort;
60
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D;
61
import com.iver.cit.gvsig.fmap.core.FPoint2D;
62
import com.iver.cit.gvsig.fmap.core.FShape;
63
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
64
import com.iver.utiles.XMLEntity;
65
import com.vividsolutions.jts.geom.Geometry;
66
import com.vividsolutions.jts.geom.Point;
67

    
68

    
69
/**
70
 * Se utiliza para etiquetar. Las capas vectoriales tienen un arrayList
71
 * (m_labels) de FLabel, un FLabel por cada registro.
72
 *
73
 * @author FJP
74
 */
75
public class FLabel implements Cloneable {
76
        public final static byte LEFT_TOP = 0;
77
        public final static byte LEFT_CENTER = 1;
78
        public final static byte LEFT_BOTTOM = 2;
79
        public final static byte CENTER_TOP = 6;
80
        public final static byte CENTER_CENTER = 7;
81
        public final static byte CENTER_BOTTOM = 8;
82
        public final static byte RIGHT_TOP = 12;
83
        public final static byte RIGHT_CENTER = 13;
84
        public final static byte RIGHT_BOTTOM = 14;
85
        private String m_Str = null;
86
        private Point2D m_Orig = null;
87
        private double m_Height;
88
        public static final double SQUARE = Math.sqrt(2.0);
89
        /**
90
         * <code>m_rotation</code> en grados, NO en radianes. Se convierte en
91
         * radianes al dibujar.
92
         */
93
        private double m_rotation;
94

    
95
        /**
96
         * Valores posibles para <code>m_Justification</code>: Left/Top = 0
97
         * Center/Top = 6                Right/Top = 12 Left/Center = 1
98
         * Center/Center = 7            Right/Center = 13 Left/Bottom = 2
99
         * Center/Bottom = 8            Right/Bottom = 14
100
         */
101
        private byte m_Justification = LEFT_BOTTOM; // Por defecto
102
        private int m_Style;
103
        private Rectangle2D boundBox;
104
        private Color color;
105
        private static Font font=new Font("Dialog",Font.PLAIN,12);
106
        /**
107
         * Crea un nuevo FLabel.
108
         */
109
        public FLabel() {
110
        }
111

    
112
        /**
113
         * Crea un nuevo FLabel.
114
         *
115
         * @param str DOCUMENT ME!
116
         */
117
        public FLabel(String str) {
118
                m_Str = str;
119
        }
120

    
121
        /**
122
         * Crea un nuevo FLabel.
123
         *
124
         * @param str
125
         * @param pOrig
126
         * @param heightText
127
         * @param rotation
128
         */
129
        public FLabel(String str, Point2D pOrig, double heightText, double rotation) {
130
                m_Str = str;
131
                m_Orig = pOrig;
132
                m_Height = heightText;
133
                m_rotation = rotation;
134
        }
135

    
136
        /**
137
         * Introduce un nuevo FLabel al ya existente.
138
         *
139
         * @param label
140
         */
141
        public void setFLabel(FLabel label) {
142
                m_Str = label.m_Str;
143
                m_Orig = label.m_Orig;
144
                m_Height = label.m_Height;
145
                m_rotation = label.m_rotation;
146
        }
147

    
148
        /**
149
         * Clona el FLabel.
150
         *
151
         * @return Object clonado.
152
         */
153
        public Object clone() {
154
                FLabel label=new FLabel(m_Str, m_Orig, m_Height, m_rotation);
155
                label.boundBox=(Rectangle2D)boundBox.clone();
156
                return label;
157
        }
158

    
159
        /**
160
         * Devuelve la altura del Label.
161
         *
162
         * @return Returns the m_Height.
163
         */
164
        public double getHeight() {
165
                return m_Height;
166
        }
167

    
168
        /**
169
         * Devuelve el punto de origen.
170
         *
171
         * @return Returns the m_Orig.
172
         */
173
        public Point2D getOrig() {
174
                return m_Orig;
175
        }
176

    
177
        /**
178
         * Devuelve la rotaci?n.
179
         *
180
         * @return Returns the m_rotation.
181
         */
182
        public double getRotation() {
183
                return m_rotation;
184
        }
185

    
186
        /**
187
         * Devuelve un String con el texto del Label.
188
         *
189
         * @return Returns the m_Str.
190
         */
191
        public String getString() {
192
                return m_Str;
193
        }
194

    
195
        /**
196
         * Introduce la altura del Label.
197
         *
198
         * @param height The m_Height to set.
199
         */
200
        public void setHeight(double height) {
201
                m_Height = height;
202
        }
203

    
204
        /**
205
         * Introduce el punto de origen del Label.
206
         *
207
         * @param orig The m_Orig to set.
208
         */
209
        public void setOrig(Point2D orig) {
210
                m_Orig = orig;
211
        }
212

    
213
        /**
214
         * Introduce la rotaci?n a aplicar al Label.
215
         *
216
         * @param m_rotation The m_rotation to set.
217
         */
218
        public void setRotation(double m_rotation) {
219
                this.m_rotation = Math.toRadians(-m_rotation);
220
        }
221

    
222
        /**
223
         * Introduce el texto del Label.
224
         *
225
         * @param str The m_Str to set.
226
         */
227
        public void setString(String str) {
228
                m_Str = str;
229
        }
230

    
231
        /**
232
         * Valores posibles para <code>m_Justification</code>: Left/Top = 0
233
         * Center/Top = 6                Right/Top = 12 Left/Center = 1
234
         * Center/Center = 7            Right/Center = 13 Left/Bottom = 2
235
         * Center/Bottom = 8            Right/Bottom = 14
236
         *
237
         * @return byte.
238
         */
239
        public byte getJustification() {
240
                return m_Justification;
241
        }
242

    
243
        /**
244
         * Valores posibles para <code>m_Justification</code>: Left/Top = 0
245
         * Center/Top = 6                Right/Top = 12 Left/Center = 1
246
         * Center/Center = 7            Right/Center = 13 Left/Bottom = 2
247
         * Center/Bottom = 8            Right/Bottom = 14
248
         *
249
         * @param justification byte
250
         */
251
        public void setJustification(byte justification) {
252
                m_Justification = justification;
253
        }
254

    
255
        /**
256
         * Devuelve un Objeto XMLEntity con la informaci?n los atributos necesarios
257
         * para poder despu?s volver a crear el objeto original.
258
         *
259
         * @return XMLEntity.
260
         */
261
        public XMLEntity getXMLEntity() {
262
                XMLEntity xml = new XMLEntity();
263
                xml.putProperty("className",this.getClass().getName());
264
                xml.setName("flabel");
265
                xml.putProperty("m_Height", m_Height);
266
                xml.putProperty("m_Justification", (int) m_Justification);
267
                xml.putProperty("m_OrigX", m_Orig.getX());
268
                xml.putProperty("m_OrigY", m_Orig.getY());
269
                xml.putProperty("m_rotation", m_rotation);
270
                xml.putProperty("m_Str", m_Str);
271

    
272
                return xml;
273
        }
274

    
275
        /**
276
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
277
         *
278
         * @param xml XMLEntity
279
         *
280
         * @return Objeto de esta clase.
281
         */
282
        public static FLabel createFLabel(XMLEntity xml) {
283
                FLabel label = new FLabel();
284
                label.setHeight(xml.getDoubleProperty("m_Height"));
285
                label.setJustification((byte) xml.getIntProperty("m_Justification"));
286
                label.setOrig(new Point2D.Double(xml.getDoubleProperty("m_OrigX"),
287
                                xml.getDoubleProperty("m_OrigY")));
288
                label.setString("m_Str");
289

    
290
                return label;
291
        }
292

    
293
        /**
294
         * M?todo est?tico que crea un FLabel a partir de un FShape.
295
         *
296
         * @param shp FShape.
297
         *
298
         * @return nuevo FLabel creado.
299
         */
300
        public static FLabel createFLabel(FShape shp) {
301
                float angle;
302
                Point2D pAux = createLabelPoint(shp);
303

    
304
                FLabel label = new FLabel();
305
                label.setOrig(pAux);
306
                switch (shp.getShapeType()) {
307
                        case FShape.LINE:
308
            case FShape.LINE + FShape.Z:
309
            case FShape.LINE | FShape.M: 
310
                                PathLength pathLen = new PathLength(shp);
311
                                float midDistance = pathLen.lengthOfPath() / 2;
312
                                angle = pathLen.angleAtLength(midDistance);
313

    
314
                                if (angle < 0) {
315
                                        angle = angle + (float) (2 * Math.PI);
316
                                }
317
                                if ((angle > (Math.PI / 2)) && (angle < ((3 * Math.PI) / 2))) {
318
                                        angle = angle - (float) Math.PI;
319
                                }
320
                                label.setRotation(Math.toDegrees(angle));
321
                                break;
322
                } // switch
323

    
324
                return label;
325
        }
326
        public static Point2D createLabelPoint(Shape shp) {
327
                Point2D pAux = null;
328
                if (shp instanceof FShape){
329
                switch (((FShape)shp).getShapeType()) {
330
                        case FShape.POINT:
331
            case FShape.POINT + FShape.Z:
332
                                pAux = new Point2D.Double(((FPoint2D) shp).getX(),
333
                                                ((FPoint2D) shp).getY());
334
                                return pAux;
335

    
336
                        case FShape.LINE:
337
            case FShape.LINE + FShape.Z:
338
            case FShape.LINE | FShape.M: 
339
                                PathLength pathLen = new PathLength(shp);
340

    
341
                                // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
342
                                float midDistance = pathLen.lengthOfPath() / 2;
343
                                pAux = pathLen.pointAtLength(midDistance);
344
                                return pAux;
345

    
346
                        case FShape.POLYGON:
347
                        case FShape.POLYGON | FShape.M:                                
348
            case FShape.POLYGON + FShape.Z:
349
                                Geometry geo = FConverter.java2d_to_jts((FShape)shp);
350

    
351
                                if (geo == null) {
352
                                        return null;
353
                                }
354

    
355
                                Point pJTS = geo.getCentroid();
356

    
357
                                if (pJTS != null) {
358
                                        FShape pLabel = FConverter.jts_to_java2d(pJTS);
359
                                        return new Point2D.Double(((FPoint2D) pLabel).getX(),
360
                                                        ((FPoint2D) pLabel).getY());
361
                                }
362
                } // switch
363
                }else if (shp instanceof FMultiPoint2D){
364
                        int num=((FMultiPoint2D)shp).getNumPoints();
365
                        Rectangle2D r=null;
366
                        if (num>0){
367
                                r= ((FMultiPoint2D)shp).getPoint(0).getBounds2D();
368
                                for (int i=1;i<num;i++){
369
                                        FPoint2D fp=((FMultiPoint2D)shp).getPoint(i);
370
                                        r.add(new Point2D.Double(fp.getX(),fp.getY()));
371
                                }
372
                        }
373
                        if (r!=null)
374
                        return new Point2D.Double(r.getCenterX(),r.getCenterY());
375
                }
376
                                return null;
377
        }
378
        public void setTypeFont(String t) {
379
                font=Font.getFont(t,font);
380
        }
381
        public int getStyle() {
382
                return m_Style;
383
        }
384

    
385
        public void setBoundBox(Rectangle2D boundBox) {
386
                this.boundBox=boundBox;
387

    
388
        }
389
        public Rectangle2D getBoundBox(){
390
                return boundBox;
391
        }
392

    
393
        public Rectangle2D getBoundingBox(ViewPort vp) {
394
                return vp.fromMapRectangle(boundBox);
395
        }
396

    
397
        public void setColor(int i) {
398
                color=new Color(i);
399
        }
400

    
401
        public Color getColor() {
402
                return color;
403
        }
404
        /*public Font getFont(AffineTransform at,boolean isInPixels){
405
                if (!isInPixels) {
406
                        float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE);
407
                        //Font nuevaFuente = font.deriveFont(alturaPixels);//new Font(getTypeFont(),getStyle(),(int)alturaPixels);
408
                        return font.deriveFont(alturaPixels);
409
                }
410
                //Font nuevaFuente = font.deriveFont((float)(getHeight()*SQUARE));//new Font(getTypeFont(),getStyle(),(int)(getHeight()*SQUARE));
411
                return font;//font.deriveFont((float)(getHeight()*SQUARE));
412
        }*/
413
        public Font getFont(AffineTransform at,Font font){
414
                float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE);
415
                return font.deriveFont(alturaPixels);
416
        }
417

    
418
        /**
419
         * Por ahora, para extender el renderizado de etiquetas, habr?a que
420
         * crear otro FLabel y reimplementar este m?todo para que haga caso
421
         * a otros s?mbolos. Es decir, NO usar FGraphicUtilities
422
         * @param g
423
         * @param affineTransform
424
         * @param theShape
425
         * @param theSymbol
426
         */
427
        public void draw(Graphics2D g, AffineTransform affineTransform, Shape theShape, ISymbol theSymbol) {
428
        FGraphicUtilities.DrawLabel(g, affineTransform,
429
                (FShape) theShape, (FSymbol) theSymbol, this);
430

    
431

    
432
        }
433

    
434
}