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