Statistics
| Revision:

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

History | View | Annotate | Download (11 KB)

1 452 fjp
/*
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 1100 fjp
/* 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 452 fjp
package com.iver.cit.gvsig.fmap.core.v02;
48
49 4256 caballero
import java.awt.Color;
50 4213 caballero
import java.awt.Font;
51 8765 jjdelcerro
import java.awt.Graphics2D;
52
import java.awt.Shape;
53 4256 caballero
import java.awt.geom.AffineTransform;
54 4213 caballero
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 12139 caballero
import com.iver.cit.gvsig.fmap.core.FMultiPoint2D;
61 452 fjp
import com.iver.cit.gvsig.fmap.core.FPoint2D;
62 757 fjp
import com.iver.cit.gvsig.fmap.core.FShape;
63 8765 jjdelcerro
import com.iver.cit.gvsig.fmap.core.ISymbol;
64 452 fjp
import com.iver.utiles.XMLEntity;
65 757 fjp
import com.vividsolutions.jts.geom.Geometry;
66
import com.vividsolutions.jts.geom.Point;
67 452 fjp
68 1005 vcaballero
69 452 fjp
/**
70 1005 vcaballero
 * Se utiliza para etiquetar. Las capas vectoriales tienen un arrayList
71
 * (m_labels) de FLabel, un FLabel por cada registro.
72
 *
73 452 fjp
 * @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 11547 caballero
        //public static final double SQUARE = FConstant.FONT_HEIGHT_SCALE_FACTOR;//Math.sqrt(2.0);
89 452 fjp
        /**
90 1005 vcaballero
         * <code>m_rotation</code> en grados, NO en radianes. Se convierte en
91
         * radianes al dibujar.
92 452 fjp
         */
93
        private double m_rotation;
94 1005 vcaballero
95 452 fjp
        /**
96 1005 vcaballero
         * 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 452 fjp
         */
101 4213 caballero
        private byte m_Justification = LEFT_BOTTOM; // Por defecto
102
        private int m_Style;
103
        private Rectangle2D boundBox;
104 11547 caballero
//        private Color color;
105 4296 caballero
        private static Font font=new Font("Dialog",Font.PLAIN,12);
106 1005 vcaballero
        /**
107
         * Crea un nuevo FLabel.
108
         */
109
        public FLabel() {
110 452 fjp
        }
111 1005 vcaballero
112
        /**
113
         * Crea un nuevo FLabel.
114
         *
115
         * @param str DOCUMENT ME!
116
         */
117
        public FLabel(String str) {
118
                m_Str = str;
119 452 fjp
        }
120 1005 vcaballero
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 452 fjp
                m_Str = str;
131
                m_Orig = pOrig;
132
                m_Height = heightText;
133
                m_rotation = rotation;
134
        }
135 1005 vcaballero
136
        /**
137
         * Introduce un nuevo FLabel al ya existente.
138
         *
139
         * @param label
140
         */
141
        public void setFLabel(FLabel label) {
142 452 fjp
                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 1005 vcaballero
148
        /**
149
         * Clona el FLabel.
150
         *
151
         * @return Object clonado.
152
         */
153
        public Object clone() {
154 4951 caballero
                FLabel label=new FLabel(m_Str, m_Orig, m_Height, m_rotation);
155
                label.boundBox=(Rectangle2D)boundBox.clone();
156
                return label;
157 452 fjp
        }
158
159
        /**
160 1005 vcaballero
         * Devuelve la altura del Label.
161
         *
162 452 fjp
         * @return Returns the m_Height.
163
         */
164
        public double getHeight() {
165
                return m_Height;
166
        }
167 1005 vcaballero
168 452 fjp
        /**
169 1005 vcaballero
         * Devuelve el punto de origen.
170
         *
171 452 fjp
         * @return Returns the m_Orig.
172
         */
173
        public Point2D getOrig() {
174
                return m_Orig;
175
        }
176 1005 vcaballero
177 452 fjp
        /**
178 1005 vcaballero
         * Devuelve la rotaci?n.
179
         *
180 452 fjp
         * @return Returns the m_rotation.
181
         */
182
        public double getRotation() {
183
                return m_rotation;
184
        }
185 1005 vcaballero
186 452 fjp
        /**
187 1005 vcaballero
         * Devuelve un String con el texto del Label.
188
         *
189 452 fjp
         * @return Returns the m_Str.
190
         */
191
        public String getString() {
192
                return m_Str;
193
        }
194 1005 vcaballero
195 452 fjp
        /**
196 1005 vcaballero
         * Introduce la altura del Label.
197
         *
198 452 fjp
         * @param height The m_Height to set.
199
         */
200
        public void setHeight(double height) {
201
                m_Height = height;
202
        }
203 1005 vcaballero
204 452 fjp
        /**
205 1005 vcaballero
         * Introduce el punto de origen del Label.
206
         *
207 452 fjp
         * @param orig The m_Orig to set.
208
         */
209
        public void setOrig(Point2D orig) {
210
                m_Orig = orig;
211
        }
212 1005 vcaballero
213 452 fjp
        /**
214 1005 vcaballero
         * Introduce la rotaci?n a aplicar al Label.
215
         *
216 452 fjp
         * @param m_rotation The m_rotation to set.
217
         */
218
        public void setRotation(double m_rotation) {
219 4226 caballero
                this.m_rotation = Math.toRadians(-m_rotation);
220 452 fjp
        }
221 1005 vcaballero
222 452 fjp
        /**
223 1005 vcaballero
         * Introduce el texto del Label.
224
         *
225 452 fjp
         * @param str The m_Str to set.
226
         */
227
        public void setString(String str) {
228
                m_Str = str;
229
        }
230 1005 vcaballero
231 452 fjp
        /**
232 1005 vcaballero
         * 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 452 fjp
         */
239
        public byte getJustification() {
240
                return m_Justification;
241
        }
242 1005 vcaballero
243 452 fjp
        /**
244 1005 vcaballero
         * 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 452 fjp
         */
251
        public void setJustification(byte justification) {
252
                m_Justification = justification;
253
        }
254 1005 vcaballero
255 452 fjp
        /**
256 1005 vcaballero
         * 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 1094 vcaballero
                xml.putProperty("className",this.getClass().getName());
264 1005 vcaballero
                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 452 fjp
272 1005 vcaballero
                return xml;
273
        }
274 452 fjp
275 1005 vcaballero
        /**
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 757 fjp
290 1005 vcaballero
                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 6370 caballero
                Point2D pAux = createLabelPoint(shp);
303 1005 vcaballero
304
                FLabel label = new FLabel();
305 6370 caballero
                label.setOrig(pAux);
306 1005 vcaballero
                switch (shp.getShapeType()) {
307
                        case FShape.LINE:
308 2354 fjp
            case FShape.LINE + FShape.Z:
309 1005 vcaballero
                                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 6370 caballero
                } // switch
322 1005 vcaballero
323 6370 caballero
                return label;
324
        }
325 12139 caballero
        public static Point2D createLabelPoint(Shape shp) {
326 6370 caballero
                Point2D pAux = null;
327 12139 caballero
                if (shp instanceof FShape){
328
                switch (((FShape)shp).getShapeType()) {
329 6370 caballero
                        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 1005 vcaballero
                        case FShape.POLYGON:
345 2354 fjp
            case FShape.POLYGON + FShape.Z:
346 12139 caballero
                                Geometry geo = FConverter.java2d_to_jts((FShape)shp);
347 1005 vcaballero
348
                                if (geo == null) {
349
                                        return null;
350
                                }
351
352
                                Point pJTS = geo.getCentroid();
353
354 6370 caballero
                                if (pJTS != null) {
355 1005 vcaballero
                                        FShape pLabel = FConverter.jts_to_java2d(pJTS);
356 6370 caballero
                                        return new Point2D.Double(((FPoint2D) pLabel).getX(),
357
                                                        ((FPoint2D) pLabel).getY());
358 1005 vcaballero
                                }
359
                } // switch
360 12139 caballero
                }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 6370 caballero
                                return null;
374 1005 vcaballero
        }
375 4256 caballero
        public void setTypeFont(String t) {
376
                font=Font.getFont(t,font);
377 4213 caballero
        }
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 4226 caballero
        public Rectangle2D getBoundingBox(ViewPort vp) {
391 4213 caballero
                return vp.fromMapRectangle(boundBox);
392
        }
393 4256 caballero
394 11547 caballero
//        public void setColor(int i) {
395
//                color=new Color(i);
396
//        }
397
//
398
//        public Color getColor() {
399
//                return color;
400
//        }
401 4296 caballero
        /*public Font getFont(AffineTransform at,boolean isInPixels){
402 4256 caballero
                if (!isInPixels) {
403
                        float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE);
404 4296 caballero
                        //Font nuevaFuente = font.deriveFont(alturaPixels);//new Font(getTypeFont(),getStyle(),(int)alturaPixels);
405
                        return font.deriveFont(alturaPixels);
406 4256 caballero
                }
407 4296 caballero
                //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 11547 caballero
                float alturaPixels = (float) ((getHeight() * at.getScaleX())*FConstant.FONT_HEIGHT_SCALE_FACTOR);
412 4296 caballero
                return font.deriveFont(alturaPixels);
413 4256 caballero
        }
414 4296 caballero
415 8765 jjdelcerro
        /**
416 11547 caballero
         * Por ahora, para extender el renderizado de etiquetas, habr?a que
417 8765 jjdelcerro
         * 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 11547 caballero
429 8765 jjdelcerro
        }
430
431 452 fjp
}