Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / impl / FLabel.java @ 38802

History | View | Annotate | Download (11.8 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 org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl;
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
import org.slf4j.Logger;
59
import org.slf4j.LoggerFactory;
60

    
61
import org.gvsig.fmap.geom.aggregate.MultiPoint;
62
import org.gvsig.fmap.geom.exception.CreateGeometryException;
63
import org.gvsig.fmap.geom.primitive.Point;
64
import org.gvsig.fmap.mapcontext.ViewPort;
65
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
66
import org.gvsig.utils.XMLEntity;
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
 * @deprecated
74
 */
75
public class FLabel implements Cloneable {
76
    
77
    private static final Logger logger = LoggerFactory.getLogger(FLabel.class);
78

    
79
    
80
        public final static byte LEFT_TOP = 0;
81
        public final static byte LEFT_CENTER = 1;
82
        public final static byte LEFT_BOTTOM = 2;
83
        public final static byte CENTER_TOP = 6;
84
        public final static byte CENTER_CENTER = 7;
85
        public final static byte CENTER_BOTTOM = 8;
86
        public final static byte RIGHT_TOP = 12;
87
        public final static byte RIGHT_CENTER = 13;
88
        public final static byte RIGHT_BOTTOM = 14;
89
        private String m_Str = null;
90
        private Point2D m_Orig = null;
91
        private double m_Height;
92
        public static final double SQUARE = Math.sqrt(2.0);
93
        /**
94
         * <code>m_rotation</code> en grados, NO en radianes. Se convierte en
95
         * radianes al dibujar.
96
         */
97
        private double m_rotation;
98

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

    
117
        /**
118
         * Crea un nuevo FLabel.
119
         *
120
         * @param str DOCUMENT ME!
121
         */
122
        public FLabel(String str) {
123
                m_Str = str;
124
        }
125

    
126
        /**
127
         * Crea un nuevo FLabel.
128
         *
129
         * @param str
130
         * @param pOrig
131
         * @param heightText
132
         * @param rotation
133
         */
134
        public FLabel(String str, Point2D pOrig, double heightText, double rotation) {
135
                m_Str = str;
136
                m_Orig = pOrig;
137
                m_Height = heightText;
138
                m_rotation = rotation;
139
        }
140

    
141
        /**
142
         * Introduce un nuevo FLabel al ya existente.
143
         *
144
         * @param label
145
         */
146
        public void setFLabel(FLabel label) {
147
                m_Str = label.m_Str;
148
                m_Orig = label.m_Orig;
149
                m_Height = label.m_Height;
150
                m_rotation = label.m_rotation;
151
        }
152

    
153
        /**
154
         * Clona el FLabel.
155
         *
156
         * @return Object clonado.
157
         */
158
        public Object clone() {
159
                FLabel label=new FLabel(m_Str, m_Orig, m_Height, m_rotation);
160
                label.boundBox=(Rectangle2D)boundBox.clone();
161
                return label;
162
        }
163

    
164
        /**
165
         * Devuelve la altura del Label.
166
         *
167
         * @return Returns the m_Height.
168
         */
169
        public double getHeight() {
170
                return m_Height;
171
        }
172

    
173
        /**
174
         * Devuelve el punto de origen.
175
         *
176
         * @return Returns the m_Orig.
177
         */
178
        public Point2D getOrig() {
179
                return m_Orig;
180
        }
181

    
182
        /**
183
         * Devuelve la rotaci?n.
184
         *
185
         * @return Returns the m_rotation.
186
         */
187
        public double getRotation() {
188
                return m_rotation;
189
        }
190

    
191
        /**
192
         * Devuelve un String con el texto del Label.
193
         *
194
         * @return Returns the m_Str.
195
         */
196
        public String getString() {
197
                return m_Str;
198
        }
199

    
200
        /**
201
         * Introduce la altura del Label.
202
         *
203
         * @param height The m_Height to set.
204
         */
205
        public void setHeight(double height) {
206
                m_Height = height;
207
        }
208

    
209
        /**
210
         * Introduce el punto de origen del Label.
211
         *
212
         * @param orig The m_Orig to set.
213
         */
214
        public void setOrig(Point2D orig) {
215
                m_Orig = orig;
216
        }
217

    
218
        /**
219
         * Introduce la rotaci?n a aplicar al Label.
220
         *
221
         * @param m_rotation The m_rotation to set.
222
         */
223
        public void setRotation(double m_rotation) {
224
                this.m_rotation = Math.toRadians(-m_rotation);
225
        }
226

    
227
        /**
228
         * Introduce el texto del Label.
229
         *
230
         * @param str The m_Str to set.
231
         */
232
        public void setString(String str) {
233
                m_Str = str;
234
        }
235

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

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

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

    
277
                return xml;
278
        }
279

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

    
295
                return label;
296
        }
297

    
298
        /**
299
         * M?todo est?tico que crea un FLabel a partir de una Geometry.
300
         *
301
         * @param geom Geometry.
302
         *
303
         * @return nuevo FLabel creado.
304
         * @throws CreateGeometryException 
305
         */
306
        public static FLabel createFLabel(org.gvsig.fmap.geom.Geometry geom) throws CreateGeometryException {
307
                float angle;
308
                Point2D pAux = createLabelPoint(geom);
309

    
310
                FLabel label = new FLabel();
311
                label.setOrig(pAux);
312
                switch (geom.getType()) {
313
                        case org.gvsig.fmap.geom.Geometry.TYPES.CURVE:
314
                           PathLength pathLen = new PathLength(geom.getShape());
315
                                float midDistance = pathLen.lengthOfPath() / 2;
316
                                angle = pathLen.angleAtLength(midDistance);
317

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

    
328
                return label;
329
        }
330
        public static Point2D createLabelPoint(org.gvsig.fmap.geom.Geometry geom) throws CreateGeometryException {
331
                Point2D pAux = null;
332
                switch (geom.getType()) {
333
                        case org.gvsig.fmap.geom.Geometry.TYPES.POINT:
334
                    pAux = new Point2D.Double(((org.gvsig.fmap.geom.primitive.Point) geom).getX(),
335
                                                ((org.gvsig.fmap.geom.primitive.Point) geom).getY());
336
                                return pAux;
337

    
338
                        case org.gvsig.fmap.geom.Geometry.TYPES.CURVE:
339
                    PathLength pathLen = new PathLength(geom.getShape());
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 org.gvsig.fmap.geom.Geometry.TYPES.SURFACE:
347
                            
348
                            try {
349
                        Point pLabel = geom.centroid();
350
                        return new Point2D.Double(pLabel.getX(), pLabel.getY());
351
                            } catch (Exception ex) {
352
                                logger.info("Error while computing centroid. ", ex);
353
                            }
354
                                break;
355
            case org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT:
356
                    int num=((MultiPoint)geom).getPrimitivesNumber();
357
                            Rectangle2D r=null;
358
                            if (num>0){
359
                                    r= ((MultiPoint)geom).getPointAt(0).getBounds2D();
360
                                    for (int i=1;i<num;i++){
361
                                            org.gvsig.fmap.geom.primitive.Point fp=((MultiPoint)geom).getPointAt(i);
362
                                            r.add(new Point2D.Double(fp.getX(),fp.getY()));
363
                                    }
364
                            }
365
                            if (r!=null)
366
                            return new Point2D.Double(r.getCenterX(),r.getCenterY());
367
                            break;
368

    
369
                } // switch
370
                                
371
                return null;
372
        }
373
        public void setTypeFont(String t) {
374
                font=Font.getFont(t,font);
375
        }
376
        public int getStyle() {
377
                return m_Style;
378
        }
379

    
380
        public void setBoundBox(Rectangle2D boundBox) {
381
                this.boundBox=boundBox;
382

    
383
        }
384
        public Rectangle2D getBoundBox(){
385
                return boundBox;
386
        }
387

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

    
392
        public void setColor(int i) {
393
                color=new Color(i);
394
        }
395

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

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

    
427

    
428
        }
429

    
430
}