Statistics
| Revision:

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

History | View | Annotate | Download (10.4 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 452 fjp
import com.iver.cit.gvsig.fmap.core.FPoint2D;
61 757 fjp
import com.iver.cit.gvsig.fmap.core.FShape;
62 8765 jjdelcerro
import com.iver.cit.gvsig.fmap.core.ISymbol;
63 452 fjp
import com.iver.utiles.XMLEntity;
64 757 fjp
import com.vividsolutions.jts.geom.Geometry;
65
import com.vividsolutions.jts.geom.Point;
66 452 fjp
67 1005 vcaballero
68 452 fjp
/**
69 1005 vcaballero
 * Se utiliza para etiquetar. Las capas vectoriales tienen un arrayList
70
 * (m_labels) de FLabel, un FLabel por cada registro.
71
 *
72 452 fjp
 * @author FJP
73
 */
74
public class FLabel implements Cloneable {
75
        public final static byte LEFT_TOP = 0;
76
        public final static byte LEFT_CENTER = 1;
77
        public final static byte LEFT_BOTTOM = 2;
78
        public final static byte CENTER_TOP = 6;
79
        public final static byte CENTER_CENTER = 7;
80
        public final static byte CENTER_BOTTOM = 8;
81
        public final static byte RIGHT_TOP = 12;
82
        public final static byte RIGHT_CENTER = 13;
83
        public final static byte RIGHT_BOTTOM = 14;
84
        private String m_Str = null;
85
        private Point2D m_Orig = null;
86
        private double m_Height;
87 4256 caballero
        public static final double SQUARE = Math.sqrt(2.0);
88 452 fjp
        /**
89 1005 vcaballero
         * <code>m_rotation</code> en grados, NO en radianes. Se convierte en
90
         * radianes al dibujar.
91 452 fjp
         */
92
        private double m_rotation;
93 1005 vcaballero
94 452 fjp
        /**
95 1005 vcaballero
         * Valores posibles para <code>m_Justification</code>: Left/Top = 0
96
         * Center/Top = 6                Right/Top = 12 Left/Center = 1
97
         * Center/Center = 7            Right/Center = 13 Left/Bottom = 2
98
         * Center/Bottom = 8            Right/Bottom = 14
99 452 fjp
         */
100 4213 caballero
        private byte m_Justification = LEFT_BOTTOM; // Por defecto
101
        private int m_Style;
102
        private Rectangle2D boundBox;
103 4256 caballero
        private Color color;
104 4296 caballero
        private static Font font=new Font("Dialog",Font.PLAIN,12);
105 1005 vcaballero
        /**
106
         * Crea un nuevo FLabel.
107
         */
108
        public FLabel() {
109 452 fjp
        }
110 1005 vcaballero
111
        /**
112
         * Crea un nuevo FLabel.
113
         *
114
         * @param str DOCUMENT ME!
115
         */
116
        public FLabel(String str) {
117
                m_Str = str;
118 452 fjp
        }
119 1005 vcaballero
120
        /**
121
         * Crea un nuevo FLabel.
122
         *
123
         * @param str
124
         * @param pOrig
125
         * @param heightText
126
         * @param rotation
127
         */
128
        public FLabel(String str, Point2D pOrig, double heightText, double rotation) {
129 452 fjp
                m_Str = str;
130
                m_Orig = pOrig;
131
                m_Height = heightText;
132
                m_rotation = rotation;
133
        }
134 1005 vcaballero
135
        /**
136
         * Introduce un nuevo FLabel al ya existente.
137
         *
138
         * @param label
139
         */
140
        public void setFLabel(FLabel label) {
141 452 fjp
                m_Str = label.m_Str;
142
                m_Orig = label.m_Orig;
143
                m_Height = label.m_Height;
144
                m_rotation = label.m_rotation;
145
        }
146 1005 vcaballero
147
        /**
148
         * Clona el FLabel.
149
         *
150
         * @return Object clonado.
151
         */
152
        public Object clone() {
153 4951 caballero
                FLabel label=new FLabel(m_Str, m_Orig, m_Height, m_rotation);
154
                label.boundBox=(Rectangle2D)boundBox.clone();
155
                return label;
156 452 fjp
        }
157
158
        /**
159 1005 vcaballero
         * Devuelve la altura del Label.
160
         *
161 452 fjp
         * @return Returns the m_Height.
162
         */
163
        public double getHeight() {
164
                return m_Height;
165
        }
166 1005 vcaballero
167 452 fjp
        /**
168 1005 vcaballero
         * Devuelve el punto de origen.
169
         *
170 452 fjp
         * @return Returns the m_Orig.
171
         */
172
        public Point2D getOrig() {
173
                return m_Orig;
174
        }
175 1005 vcaballero
176 452 fjp
        /**
177 1005 vcaballero
         * Devuelve la rotaci?n.
178
         *
179 452 fjp
         * @return Returns the m_rotation.
180
         */
181
        public double getRotation() {
182
                return m_rotation;
183
        }
184 1005 vcaballero
185 452 fjp
        /**
186 1005 vcaballero
         * Devuelve un String con el texto del Label.
187
         *
188 452 fjp
         * @return Returns the m_Str.
189
         */
190
        public String getString() {
191
                return m_Str;
192
        }
193 1005 vcaballero
194 452 fjp
        /**
195 1005 vcaballero
         * Introduce la altura del Label.
196
         *
197 452 fjp
         * @param height The m_Height to set.
198
         */
199
        public void setHeight(double height) {
200
                m_Height = height;
201
        }
202 1005 vcaballero
203 452 fjp
        /**
204 1005 vcaballero
         * Introduce el punto de origen del Label.
205
         *
206 452 fjp
         * @param orig The m_Orig to set.
207
         */
208
        public void setOrig(Point2D orig) {
209
                m_Orig = orig;
210
        }
211 1005 vcaballero
212 452 fjp
        /**
213 1005 vcaballero
         * Introduce la rotaci?n a aplicar al Label.
214
         *
215 452 fjp
         * @param m_rotation The m_rotation to set.
216
         */
217
        public void setRotation(double m_rotation) {
218 4226 caballero
                this.m_rotation = Math.toRadians(-m_rotation);
219 452 fjp
        }
220 1005 vcaballero
221 452 fjp
        /**
222 1005 vcaballero
         * Introduce el texto del Label.
223
         *
224 452 fjp
         * @param str The m_Str to set.
225
         */
226
        public void setString(String str) {
227
                m_Str = str;
228
        }
229 1005 vcaballero
230 452 fjp
        /**
231 1005 vcaballero
         * Valores posibles para <code>m_Justification</code>: Left/Top = 0
232
         * Center/Top = 6                Right/Top = 12 Left/Center = 1
233
         * Center/Center = 7            Right/Center = 13 Left/Bottom = 2
234
         * Center/Bottom = 8            Right/Bottom = 14
235
         *
236
         * @return byte.
237 452 fjp
         */
238
        public byte getJustification() {
239
                return m_Justification;
240
        }
241 1005 vcaballero
242 452 fjp
        /**
243 1005 vcaballero
         * Valores posibles para <code>m_Justification</code>: Left/Top = 0
244
         * Center/Top = 6                Right/Top = 12 Left/Center = 1
245
         * Center/Center = 7            Right/Center = 13 Left/Bottom = 2
246
         * Center/Bottom = 8            Right/Bottom = 14
247
         *
248
         * @param justification byte
249 452 fjp
         */
250
        public void setJustification(byte justification) {
251
                m_Justification = justification;
252
        }
253 1005 vcaballero
254 452 fjp
        /**
255 1005 vcaballero
         * Devuelve un Objeto XMLEntity con la informaci?n los atributos necesarios
256
         * para poder despu?s volver a crear el objeto original.
257
         *
258
         * @return XMLEntity.
259
         */
260
        public XMLEntity getXMLEntity() {
261
                XMLEntity xml = new XMLEntity();
262 1094 vcaballero
                xml.putProperty("className",this.getClass().getName());
263 1005 vcaballero
                xml.setName("flabel");
264
                xml.putProperty("m_Height", m_Height);
265
                xml.putProperty("m_Justification", (int) m_Justification);
266
                xml.putProperty("m_OrigX", m_Orig.getX());
267
                xml.putProperty("m_OrigY", m_Orig.getY());
268
                xml.putProperty("m_rotation", m_rotation);
269
                xml.putProperty("m_Str", m_Str);
270 452 fjp
271 1005 vcaballero
                return xml;
272
        }
273 452 fjp
274 1005 vcaballero
        /**
275
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
276
         *
277
         * @param xml XMLEntity
278
         *
279
         * @return Objeto de esta clase.
280
         */
281
        public static FLabel createFLabel(XMLEntity xml) {
282
                FLabel label = new FLabel();
283
                label.setHeight(xml.getDoubleProperty("m_Height"));
284
                label.setJustification((byte) xml.getIntProperty("m_Justification"));
285
                label.setOrig(new Point2D.Double(xml.getDoubleProperty("m_OrigX"),
286
                                xml.getDoubleProperty("m_OrigY")));
287
                label.setString("m_Str");
288 757 fjp
289 1005 vcaballero
                return label;
290
        }
291
292
        /**
293
         * M?todo est?tico que crea un FLabel a partir de un FShape.
294
         *
295
         * @param shp FShape.
296
         *
297
         * @return nuevo FLabel creado.
298
         */
299
        public static FLabel createFLabel(FShape shp) {
300
                float angle;
301 6370 caballero
                Point2D pAux = createLabelPoint(shp);
302 1005 vcaballero
303
                FLabel label = new FLabel();
304 6370 caballero
                label.setOrig(pAux);
305 1005 vcaballero
                switch (shp.getShapeType()) {
306
                        case FShape.LINE:
307 2354 fjp
            case FShape.LINE + FShape.Z:
308 1005 vcaballero
                                PathLength pathLen = new PathLength(shp);
309
                                float midDistance = pathLen.lengthOfPath() / 2;
310
                                angle = pathLen.angleAtLength(midDistance);
311
312
                                if (angle < 0) {
313
                                        angle = angle + (float) (2 * Math.PI);
314
                                }
315
                                if ((angle > (Math.PI / 2)) && (angle < ((3 * Math.PI) / 2))) {
316
                                        angle = angle - (float) Math.PI;
317
                                }
318
                                label.setRotation(Math.toDegrees(angle));
319
                                break;
320 6370 caballero
                } // switch
321 1005 vcaballero
322 6370 caballero
                return label;
323
        }
324
        public static Point2D createLabelPoint(FShape shp) {
325
                Point2D pAux = null;
326
327
                switch (shp.getShapeType()) {
328
                        case FShape.POINT:
329
            case FShape.POINT + FShape.Z:
330
                                pAux = new Point2D.Double(((FPoint2D) shp).getX(),
331
                                                ((FPoint2D) shp).getY());
332
                                return pAux;
333
334
                        case FShape.LINE:
335
            case FShape.LINE + FShape.Z:
336
                                PathLength pathLen = new PathLength(shp);
337
338
                                // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
339
                                float midDistance = pathLen.lengthOfPath() / 2;
340
                                pAux = pathLen.pointAtLength(midDistance);
341
                                return pAux;
342
343 1005 vcaballero
                        case FShape.POLYGON:
344 2354 fjp
            case FShape.POLYGON + FShape.Z:
345 1005 vcaballero
                                Geometry geo = FConverter.java2d_to_jts(shp);
346
347
                                if (geo == null) {
348
                                        return null;
349
                                }
350
351
                                Point pJTS = geo.getCentroid();
352
353 6370 caballero
                                if (pJTS != null) {
354 1005 vcaballero
                                        FShape pLabel = FConverter.jts_to_java2d(pJTS);
355 6370 caballero
                                        return new Point2D.Double(((FPoint2D) pLabel).getX(),
356
                                                        ((FPoint2D) pLabel).getY());
357 1005 vcaballero
                                }
358
                } // switch
359 6370 caballero
                                return null;
360 1005 vcaballero
        }
361 4256 caballero
        public void setTypeFont(String t) {
362
                font=Font.getFont(t,font);
363 4213 caballero
        }
364
        public int getStyle() {
365
                return m_Style;
366
        }
367
368
        public void setBoundBox(Rectangle2D boundBox) {
369
                this.boundBox=boundBox;
370
371
        }
372
        public Rectangle2D getBoundBox(){
373
                return boundBox;
374
        }
375
376 4226 caballero
        public Rectangle2D getBoundingBox(ViewPort vp) {
377 4213 caballero
                return vp.fromMapRectangle(boundBox);
378
        }
379 4256 caballero
380
        public void setColor(int i) {
381
                color=new Color(i);
382
        }
383
384
        public Color getColor() {
385
                return color;
386
        }
387 4296 caballero
        /*public Font getFont(AffineTransform at,boolean isInPixels){
388 4256 caballero
                if (!isInPixels) {
389
                        float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE);
390 4296 caballero
                        //Font nuevaFuente = font.deriveFont(alturaPixels);//new Font(getTypeFont(),getStyle(),(int)alturaPixels);
391
                        return font.deriveFont(alturaPixels);
392 4256 caballero
                }
393 4296 caballero
                //Font nuevaFuente = font.deriveFont((float)(getHeight()*SQUARE));//new Font(getTypeFont(),getStyle(),(int)(getHeight()*SQUARE));
394
                return font;//font.deriveFont((float)(getHeight()*SQUARE));
395
        }*/
396
        public Font getFont(AffineTransform at,Font font){
397
                float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE);
398
                return font.deriveFont(alturaPixels);
399 4256 caballero
        }
400 4296 caballero
401 8765 jjdelcerro
        /**
402
         * Por ahora, para extender el renderizado de etiquetas, habr?a que
403
         * crear otro FLabel y reimplementar este m?todo para que haga caso
404
         * a otros s?mbolos. Es decir, NO usar FGraphicUtilities
405
         * @param g
406
         * @param affineTransform
407
         * @param theShape
408
         * @param theSymbol
409
         */
410
        public void draw(Graphics2D g, AffineTransform affineTransform, Shape theShape, ISymbol theSymbol) {
411
        FGraphicUtilities.DrawLabel(g, affineTransform,
412
                (FShape) theShape, (FSymbol) theSymbol, this);
413
414
415
        }
416
417 452 fjp
}