Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1006 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / v02 / FLabel.java @ 12458

History | View | Annotate | Download (11 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.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 = FConstant.FONT_HEIGHT_SCALE_FACTOR;//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
                                PathLength pathLen = new PathLength(shp);
310
                                float midDistance = pathLen.lengthOfPath() / 2;
311
                                angle = pathLen.angleAtLength(midDistance);
312

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

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

    
335
                        case FShape.LINE:
336
            case FShape.LINE + FShape.Z:
337
                                PathLength pathLen = new PathLength(shp);
338

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

    
344
                        case FShape.POLYGON:
345
            case FShape.POLYGON + FShape.Z:
346
                                Geometry geo = FConverter.java2d_to_jts((FShape)shp);
347

    
348
                                if (geo == null) {
349
                                        return null;
350
                                }
351

    
352
                                Point pJTS = geo.getCentroid();
353

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

    
382
        public void setBoundBox(Rectangle2D boundBox) {
383
                this.boundBox=boundBox;
384

    
385
        }
386
        public Rectangle2D getBoundBox(){
387
                return boundBox;
388
        }
389

    
390
        public Rectangle2D getBoundingBox(ViewPort vp) {
391
                return vp.fromMapRectangle(boundBox);
392
        }
393

    
394
//        public void setColor(int i) {
395
//                color=new Color(i);
396
//        }
397
//
398
//        public Color getColor() {
399
//                return color;
400
//        }
401
        /*public Font getFont(AffineTransform at,boolean isInPixels){
402
                if (!isInPixels) {
403
                        float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE);
404
                        //Font nuevaFuente = font.deriveFont(alturaPixels);//new Font(getTypeFont(),getStyle(),(int)alturaPixels);
405
                        return font.deriveFont(alturaPixels);
406
                }
407
                //Font nuevaFuente = font.deriveFont((float)(getHeight()*SQUARE));//new Font(getTypeFont(),getStyle(),(int)(getHeight()*SQUARE));
408
                return font;//font.deriveFont((float)(getHeight()*SQUARE));
409
        }*/
410
        public Font getFont(AffineTransform at,Font font){
411
                float alturaPixels = (float) ((getHeight() * at.getScaleX())*FConstant.FONT_HEIGHT_SCALE_FACTOR);
412
                return font.deriveFont(alturaPixels);
413
        }
414

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

    
428

    
429
        }
430

    
431
}