Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / v02 / FLabel.java @ 4123

History | View | Annotate | Download (8.13 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
import com.iver.cit.gvsig.fmap.core.FPoint2D;
50 757 fjp
import com.iver.cit.gvsig.fmap.core.FShape;
51 1005 vcaballero
52 452 fjp
import com.iver.utiles.XMLEntity;
53 1005 vcaballero
54 757 fjp
import com.vividsolutions.jts.geom.Geometry;
55
import com.vividsolutions.jts.geom.Point;
56 452 fjp
57 1005 vcaballero
import org.apache.batik.ext.awt.geom.PathLength;
58
59
import java.awt.geom.Point2D;
60
61
62 452 fjp
/**
63 1005 vcaballero
 * Se utiliza para etiquetar. Las capas vectoriales tienen un arrayList
64
 * (m_labels) de FLabel, un FLabel por cada registro.
65
 *
66 452 fjp
 * @author FJP
67
 */
68
public class FLabel implements Cloneable {
69
        public final static byte LEFT_TOP = 0;
70
        public final static byte LEFT_CENTER = 1;
71
        public final static byte LEFT_BOTTOM = 2;
72
        public final static byte CENTER_TOP = 6;
73
        public final static byte CENTER_CENTER = 7;
74
        public final static byte CENTER_BOTTOM = 8;
75
        public final static byte RIGHT_TOP = 12;
76
        public final static byte RIGHT_CENTER = 13;
77
        public final static byte RIGHT_BOTTOM = 14;
78
        private String m_Str = null;
79
        private Point2D m_Orig = null;
80
        private double m_Height;
81 1005 vcaballero
82 452 fjp
        /**
83 1005 vcaballero
         * <code>m_rotation</code> en grados, NO en radianes. Se convierte en
84
         * radianes al dibujar.
85 452 fjp
         */
86
        private double m_rotation;
87 1005 vcaballero
88 452 fjp
        /**
89 1005 vcaballero
         * Valores posibles para <code>m_Justification</code>: Left/Top = 0
90
         * Center/Top = 6                Right/Top = 12 Left/Center = 1
91
         * Center/Center = 7            Right/Center = 13 Left/Bottom = 2
92
         * Center/Bottom = 8            Right/Bottom = 14
93 452 fjp
         */
94
        private byte m_Justification = LEFT_BOTTOM; // Por defecto
95 1005 vcaballero
96
        /**
97
         * Crea un nuevo FLabel.
98
         */
99
        public FLabel() {
100 452 fjp
        }
101 1005 vcaballero
102
        /**
103
         * Crea un nuevo FLabel.
104
         *
105
         * @param str DOCUMENT ME!
106
         */
107
        public FLabel(String str) {
108
                m_Str = str;
109 452 fjp
        }
110 1005 vcaballero
111
        /**
112
         * Crea un nuevo FLabel.
113
         *
114
         * @param str
115
         * @param pOrig
116
         * @param heightText
117
         * @param rotation
118
         */
119
        public FLabel(String str, Point2D pOrig, double heightText, double rotation) {
120 452 fjp
                m_Str = str;
121
                m_Orig = pOrig;
122
                m_Height = heightText;
123
                m_rotation = rotation;
124
        }
125 1005 vcaballero
126
        /**
127
         * Introduce un nuevo FLabel al ya existente.
128
         *
129
         * @param label
130
         */
131
        public void setFLabel(FLabel label) {
132 452 fjp
                m_Str = label.m_Str;
133
                m_Orig = label.m_Orig;
134
                m_Height = label.m_Height;
135
                m_rotation = label.m_rotation;
136
        }
137 1005 vcaballero
138
        /**
139
         * Clona el FLabel.
140
         *
141
         * @return Object clonado.
142
         */
143
        public Object clone() {
144
                return new FLabel(m_Str, m_Orig, m_Height, m_rotation);
145 452 fjp
        }
146
147
        /**
148 1005 vcaballero
         * Devuelve la altura del Label.
149
         *
150 452 fjp
         * @return Returns the m_Height.
151
         */
152
        public double getHeight() {
153
                return m_Height;
154
        }
155 1005 vcaballero
156 452 fjp
        /**
157 1005 vcaballero
         * Devuelve el punto de origen.
158
         *
159 452 fjp
         * @return Returns the m_Orig.
160
         */
161
        public Point2D getOrig() {
162
                return m_Orig;
163
        }
164 1005 vcaballero
165 452 fjp
        /**
166 1005 vcaballero
         * Devuelve la rotaci?n.
167
         *
168 452 fjp
         * @return Returns the m_rotation.
169
         */
170
        public double getRotation() {
171
                return m_rotation;
172
        }
173 1005 vcaballero
174 452 fjp
        /**
175 1005 vcaballero
         * Devuelve un String con el texto del Label.
176
         *
177 452 fjp
         * @return Returns the m_Str.
178
         */
179
        public String getString() {
180
                return m_Str;
181
        }
182 1005 vcaballero
183 452 fjp
        /**
184 1005 vcaballero
         * Introduce la altura del Label.
185
         *
186 452 fjp
         * @param height The m_Height to set.
187
         */
188
        public void setHeight(double height) {
189
                m_Height = height;
190
        }
191 1005 vcaballero
192 452 fjp
        /**
193 1005 vcaballero
         * Introduce el punto de origen del Label.
194
         *
195 452 fjp
         * @param orig The m_Orig to set.
196
         */
197
        public void setOrig(Point2D orig) {
198
                m_Orig = orig;
199
        }
200 1005 vcaballero
201 452 fjp
        /**
202 1005 vcaballero
         * Introduce la rotaci?n a aplicar al Label.
203
         *
204 452 fjp
         * @param m_rotation The m_rotation to set.
205
         */
206
        public void setRotation(double m_rotation) {
207
                this.m_rotation = m_rotation;
208
        }
209 1005 vcaballero
210 452 fjp
        /**
211 1005 vcaballero
         * Introduce el texto del Label.
212
         *
213 452 fjp
         * @param str The m_Str to set.
214
         */
215
        public void setString(String str) {
216
                m_Str = str;
217
        }
218 1005 vcaballero
219 452 fjp
        /**
220 1005 vcaballero
         * Valores posibles para <code>m_Justification</code>: Left/Top = 0
221
         * Center/Top = 6                Right/Top = 12 Left/Center = 1
222
         * Center/Center = 7            Right/Center = 13 Left/Bottom = 2
223
         * Center/Bottom = 8            Right/Bottom = 14
224
         *
225
         * @return byte.
226 452 fjp
         */
227
        public byte getJustification() {
228
                return m_Justification;
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
         * @param justification byte
238 452 fjp
         */
239
        public void setJustification(byte justification) {
240
                m_Justification = justification;
241
        }
242 1005 vcaballero
243 452 fjp
        /**
244 1005 vcaballero
         * Devuelve un Objeto XMLEntity con la informaci?n los atributos necesarios
245
         * para poder despu?s volver a crear el objeto original.
246
         *
247
         * @return XMLEntity.
248
         */
249
        public XMLEntity getXMLEntity() {
250
                XMLEntity xml = new XMLEntity();
251 1094 vcaballero
                xml.putProperty("className",this.getClass().getName());
252 1005 vcaballero
                xml.setName("flabel");
253
                xml.putProperty("m_Height", m_Height);
254
                xml.putProperty("m_Justification", (int) m_Justification);
255
                xml.putProperty("m_OrigX", m_Orig.getX());
256
                xml.putProperty("m_OrigY", m_Orig.getY());
257
                xml.putProperty("m_rotation", m_rotation);
258
                xml.putProperty("m_Str", m_Str);
259 452 fjp
260 1005 vcaballero
                return xml;
261
        }
262 452 fjp
263 1005 vcaballero
        /**
264
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
265
         *
266
         * @param xml XMLEntity
267
         *
268
         * @return Objeto de esta clase.
269
         */
270
        public static FLabel createFLabel(XMLEntity xml) {
271
                FLabel label = new FLabel();
272
                label.setHeight(xml.getDoubleProperty("m_Height"));
273
                label.setJustification((byte) xml.getIntProperty("m_Justification"));
274
                label.setOrig(new Point2D.Double(xml.getDoubleProperty("m_OrigX"),
275
                                xml.getDoubleProperty("m_OrigY")));
276
                label.setString("m_Str");
277 757 fjp
278 1005 vcaballero
                return label;
279
        }
280
281
        /**
282
         * M?todo est?tico que crea un FLabel a partir de un FShape.
283
         *
284
         * @param shp FShape.
285
         *
286
         * @return nuevo FLabel creado.
287
         */
288
        public static FLabel createFLabel(FShape shp) {
289
                float angle;
290
                float x;
291
                float y;
292
                Point2D pAux = null;
293
294
                FLabel label = new FLabel();
295
296
                switch (shp.getShapeType()) {
297
                        case FShape.POINT:
298 2354 fjp
            case FShape.POINT + FShape.Z:
299 1005 vcaballero
                                pAux = new Point2D.Double(((FPoint2D) shp).getX(),
300
                                                ((FPoint2D) shp).getY());
301
                                label.setOrig(pAux);
302
303
                                break;
304
305
                        case FShape.LINE:
306 2354 fjp
            case FShape.LINE + FShape.Z:
307 1005 vcaballero
                                PathLength pathLen = new PathLength(shp);
308
309
                                // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
310
                                float midDistance = pathLen.lengthOfPath() / 2;
311
                                pAux = pathLen.pointAtLength(midDistance);
312
                                angle = pathLen.angleAtLength(midDistance);
313
314
                                if (angle < 0) {
315
                                        angle = angle + (float) (2 * Math.PI);
316
                                }
317
318
                                if ((angle > (Math.PI / 2)) && (angle < ((3 * Math.PI) / 2))) {
319
                                        angle = angle - (float) Math.PI;
320
                                }
321
322
                                label.setRotation(Math.toDegrees(angle));
323
                                label.setOrig(pAux);
324
325
                                break;
326
327
                        case FShape.POLYGON:
328 2354 fjp
            case FShape.POLYGON + FShape.Z:
329 1005 vcaballero
                                Geometry geo = FConverter.java2d_to_jts(shp);
330
331
                                if (geo == null) {
332
                                        return null;
333
                                }
334
335
                                Point pJTS = geo.getCentroid();
336
337
                                if (pJTS == null) {
338
                                        label = null;
339
                                } else {
340
                                        FShape pLabel = FConverter.jts_to_java2d(pJTS);
341
                                        label.setRotation(0);
342
                                        label.setOrig(new Point2D.Double(
343
                                                        ((FPoint2D) pLabel).getX(),
344
                                                        ((FPoint2D) pLabel).getY()));
345
                                }
346
347
                                break;
348
                } // switch
349
350
                return label;
351
        }
352 452 fjp
}