Statistics
| Revision:

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

History | View | Annotate | Download (10.4 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.FPoint2D;
61
import com.iver.cit.gvsig.fmap.core.FShape;
62
import com.iver.cit.gvsig.fmap.core.ISymbol;
63
import com.iver.utiles.XMLEntity;
64
import com.vividsolutions.jts.geom.Geometry;
65
import com.vividsolutions.jts.geom.Point;
66

    
67

    
68
/**
69
 * Se utiliza para etiquetar. Las capas vectoriales tienen un arrayList
70
 * (m_labels) de FLabel, un FLabel por cada registro.
71
 *
72
 * @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
        public static final double SQUARE = Math.sqrt(2.0);
88
        /**
89
         * <code>m_rotation</code> en grados, NO en radianes. Se convierte en
90
         * radianes al dibujar.
91
         */
92
        private double m_rotation;
93

    
94
        /**
95
         * 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
         */
100
        private byte m_Justification = LEFT_BOTTOM; // Por defecto
101
        private int m_Style;
102
        private Rectangle2D boundBox;
103
        private Color color;
104
        private static Font font=new Font("Dialog",Font.PLAIN,12);
105
        /**
106
         * Crea un nuevo FLabel.
107
         */
108
        public FLabel() {
109
        }
110

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

    
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
                m_Str = str;
130
                m_Orig = pOrig;
131
                m_Height = heightText;
132
                m_rotation = rotation;
133
        }
134

    
135
        /**
136
         * Introduce un nuevo FLabel al ya existente.
137
         *
138
         * @param label
139
         */
140
        public void setFLabel(FLabel label) {
141
                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

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

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

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

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

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

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

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

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

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

    
230
        /**
231
         * 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
         */
238
        public byte getJustification() {
239
                return m_Justification;
240
        }
241

    
242
        /**
243
         * 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
         */
250
        public void setJustification(byte justification) {
251
                m_Justification = justification;
252
        }
253

    
254
        /**
255
         * 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
                xml.putProperty("className",this.getClass().getName());
263
                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

    
271
                return xml;
272
        }
273

    
274
        /**
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

    
289
                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
                Point2D pAux = createLabelPoint(shp);
302

    
303
                FLabel label = new FLabel();
304
                label.setOrig(pAux);
305
                switch (shp.getShapeType()) {
306
                        case FShape.LINE:
307
            case FShape.LINE + FShape.Z:
308
                                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
                } // switch
321

    
322
                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
                        case FShape.POLYGON:
344
            case FShape.POLYGON + FShape.Z:
345
                                Geometry geo = FConverter.java2d_to_jts(shp);
346

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

    
351
                                Point pJTS = geo.getCentroid();
352

    
353
                                if (pJTS != null) {
354
                                        FShape pLabel = FConverter.jts_to_java2d(pJTS);
355
                                        return new Point2D.Double(((FPoint2D) pLabel).getX(),
356
                                                        ((FPoint2D) pLabel).getY());
357
                                }
358
                } // switch
359
                                return null;
360
        }
361
        public void setTypeFont(String t) {
362
                font=Font.getFont(t,font);
363
        }
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
        public Rectangle2D getBoundingBox(ViewPort vp) {
377
                return vp.fromMapRectangle(boundBox);
378
        }
379

    
380
        public void setColor(int i) {
381
                color=new Color(i);
382
        }
383

    
384
        public Color getColor() {
385
                return color;
386
        }
387
        /*public Font getFont(AffineTransform at,boolean isInPixels){
388
                if (!isInPixels) {
389
                        float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE);
390
                        //Font nuevaFuente = font.deriveFont(alturaPixels);//new Font(getTypeFont(),getStyle(),(int)alturaPixels);
391
                        return font.deriveFont(alturaPixels);
392
                }
393
                //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
        }
400

    
401
        /**
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
}