svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / 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 @ 43156
History | View | Annotate | Download (11.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.Font; |
28 |
import java.awt.Graphics2D; |
29 |
import java.awt.Shape; |
30 |
import java.awt.geom.AffineTransform; |
31 |
import java.awt.geom.Point2D; |
32 |
import java.awt.geom.Rectangle2D; |
33 |
|
34 |
import org.apache.batik.ext.awt.geom.DefaultPathLength; |
35 |
import org.slf4j.Logger; |
36 |
import org.slf4j.LoggerFactory; |
37 |
|
38 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
39 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
40 |
import org.gvsig.fmap.geom.primitive.Point; |
41 |
import org.gvsig.fmap.mapcontext.ViewPort; |
42 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
43 |
import org.gvsig.symbology.PathLength; |
44 |
import org.gvsig.utils.XMLEntity; |
45 |
|
46 |
/**
|
47 |
* Se utiliza para etiquetar. Las capas vectoriales tienen un arrayList
|
48 |
* (m_labels) de FLabel, un FLabel por cada registro.
|
49 |
* @deprecated
|
50 |
*/
|
51 |
public class FLabel implements Cloneable { |
52 |
|
53 |
private static final Logger logger = LoggerFactory.getLogger(FLabel.class); |
54 |
|
55 |
|
56 |
public final static byte LEFT_TOP = 0; |
57 |
public final static byte LEFT_CENTER = 1; |
58 |
public final static byte LEFT_BOTTOM = 2; |
59 |
public final static byte CENTER_TOP = 6; |
60 |
public final static byte CENTER_CENTER = 7; |
61 |
public final static byte CENTER_BOTTOM = 8; |
62 |
public final static byte RIGHT_TOP = 12; |
63 |
public final static byte RIGHT_CENTER = 13; |
64 |
public final static byte RIGHT_BOTTOM = 14; |
65 |
private String m_Str = null; |
66 |
private Point2D m_Orig = null; |
67 |
private double m_Height; |
68 |
public static final double SQUARE = Math.sqrt(2.0); |
69 |
/**
|
70 |
* <code>m_rotation</code> en grados, NO en radianes. Se convierte en
|
71 |
* radianes al dibujar.
|
72 |
*/
|
73 |
private double m_rotation; |
74 |
|
75 |
/**
|
76 |
* Valores posibles para <code>m_Justification</code>: Left/Top = 0
|
77 |
* Center/Top = 6 Right/Top = 12 Left/Center = 1
|
78 |
* Center/Center = 7 Right/Center = 13 Left/Bottom = 2
|
79 |
* Center/Bottom = 8 Right/Bottom = 14
|
80 |
*/
|
81 |
private byte m_Justification = LEFT_BOTTOM; // Por defecto |
82 |
private int m_Style; |
83 |
private Rectangle2D boundBox; |
84 |
private Color color; |
85 |
private static Font font=new Font("Dialog",Font.PLAIN,12); |
86 |
/**
|
87 |
* Crea un nuevo FLabel.
|
88 |
*/
|
89 |
public FLabel() {
|
90 |
// nothing to do
|
91 |
} |
92 |
|
93 |
/**
|
94 |
* Crea un nuevo FLabel.
|
95 |
*
|
96 |
* @param str DOCUMENT ME!
|
97 |
*/
|
98 |
public FLabel(String str) { |
99 |
m_Str = str; |
100 |
} |
101 |
|
102 |
/**
|
103 |
* Crea un nuevo FLabel.
|
104 |
*
|
105 |
* @param str
|
106 |
* @param pOrig
|
107 |
* @param heightText
|
108 |
* @param rotation
|
109 |
*/
|
110 |
public FLabel(String str, Point2D pOrig, double heightText, double rotation) { |
111 |
m_Str = str; |
112 |
m_Orig = pOrig; |
113 |
m_Height = heightText; |
114 |
m_rotation = rotation; |
115 |
} |
116 |
|
117 |
/**
|
118 |
* Introduce un nuevo FLabel al ya existente.
|
119 |
*
|
120 |
* @param label
|
121 |
*/
|
122 |
public void setFLabel(FLabel label) { |
123 |
m_Str = label.m_Str; |
124 |
m_Orig = label.m_Orig; |
125 |
m_Height = label.m_Height; |
126 |
m_rotation = label.m_rotation; |
127 |
} |
128 |
|
129 |
/**
|
130 |
* Clona el FLabel.
|
131 |
*
|
132 |
* @return Object clonado.
|
133 |
*/
|
134 |
public Object clone() { |
135 |
FLabel label=new FLabel(m_Str, m_Orig, m_Height, m_rotation);
|
136 |
label.boundBox=(Rectangle2D)boundBox.clone();
|
137 |
return label;
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* Devuelve la altura del Label.
|
142 |
*
|
143 |
* @return Returns the m_Height.
|
144 |
*/
|
145 |
public double getHeight() { |
146 |
return m_Height;
|
147 |
} |
148 |
|
149 |
/**
|
150 |
* Devuelve el punto de origen.
|
151 |
*
|
152 |
* @return Returns the m_Orig.
|
153 |
*/
|
154 |
public Point2D getOrig() { |
155 |
return m_Orig;
|
156 |
} |
157 |
|
158 |
/**
|
159 |
* Devuelve la rotaci?n.
|
160 |
*
|
161 |
* @return Returns the m_rotation.
|
162 |
*/
|
163 |
public double getRotation() { |
164 |
return m_rotation;
|
165 |
} |
166 |
|
167 |
/**
|
168 |
* Devuelve un String con el texto del Label.
|
169 |
*
|
170 |
* @return Returns the m_Str.
|
171 |
*/
|
172 |
public String getString() { |
173 |
return m_Str;
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* Introduce la altura del Label.
|
178 |
*
|
179 |
* @param height The m_Height to set.
|
180 |
*/
|
181 |
public void setHeight(double height) { |
182 |
m_Height = height; |
183 |
} |
184 |
|
185 |
/**
|
186 |
* Introduce el punto de origen del Label.
|
187 |
*
|
188 |
* @param orig The m_Orig to set.
|
189 |
*/
|
190 |
public void setOrig(Point2D orig) { |
191 |
m_Orig = orig; |
192 |
} |
193 |
|
194 |
/**
|
195 |
* Introduce la rotaci?n a aplicar al Label.
|
196 |
*
|
197 |
* @param m_rotation The m_rotation to set.
|
198 |
*/
|
199 |
public void setRotation(double m_rotation) { |
200 |
this.m_rotation = Math.toRadians(-m_rotation); |
201 |
} |
202 |
|
203 |
/**
|
204 |
* Introduce el texto del Label.
|
205 |
*
|
206 |
* @param str The m_Str to set.
|
207 |
*/
|
208 |
public void setString(String str) { |
209 |
m_Str = str; |
210 |
} |
211 |
|
212 |
/**
|
213 |
* Valores posibles para <code>m_Justification</code>: Left/Top = 0
|
214 |
* Center/Top = 6 Right/Top = 12 Left/Center = 1
|
215 |
* Center/Center = 7 Right/Center = 13 Left/Bottom = 2
|
216 |
* Center/Bottom = 8 Right/Bottom = 14
|
217 |
*
|
218 |
* @return byte.
|
219 |
*/
|
220 |
public byte getJustification() { |
221 |
return m_Justification;
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* Valores posibles para <code>m_Justification</code>: Left/Top = 0
|
226 |
* Center/Top = 6 Right/Top = 12 Left/Center = 1
|
227 |
* Center/Center = 7 Right/Center = 13 Left/Bottom = 2
|
228 |
* Center/Bottom = 8 Right/Bottom = 14
|
229 |
*
|
230 |
* @param justification byte
|
231 |
*/
|
232 |
public void setJustification(byte justification) { |
233 |
m_Justification = justification; |
234 |
} |
235 |
|
236 |
/**
|
237 |
* Devuelve un Objeto XMLEntity con la informaci?n los atributos necesarios
|
238 |
* para poder despu?s volver a crear el objeto original.
|
239 |
*
|
240 |
* @return XMLEntity.
|
241 |
*/
|
242 |
public XMLEntity getXMLEntity() {
|
243 |
XMLEntity xml = new XMLEntity();
|
244 |
xml.putProperty("className",this.getClass().getName()); |
245 |
xml.setName("flabel");
|
246 |
xml.putProperty("m_Height", m_Height);
|
247 |
xml.putProperty("m_Justification", (int) m_Justification); |
248 |
xml.putProperty("m_OrigX", m_Orig.getX());
|
249 |
xml.putProperty("m_OrigY", m_Orig.getY());
|
250 |
xml.putProperty("m_rotation", m_rotation);
|
251 |
xml.putProperty("m_Str", m_Str);
|
252 |
|
253 |
return xml;
|
254 |
} |
255 |
|
256 |
/**
|
257 |
* Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
|
258 |
*
|
259 |
* @param xml XMLEntity
|
260 |
*
|
261 |
* @return Objeto de esta clase.
|
262 |
*/
|
263 |
public static FLabel createFLabel(XMLEntity xml) { |
264 |
FLabel label = new FLabel();
|
265 |
label.setHeight(xml.getDoubleProperty("m_Height"));
|
266 |
label.setJustification((byte) xml.getIntProperty("m_Justification")); |
267 |
label.setOrig(new Point2D.Double(xml.getDoubleProperty("m_OrigX"), |
268 |
xml.getDoubleProperty("m_OrigY")));
|
269 |
label.setString("m_Str");
|
270 |
|
271 |
return label;
|
272 |
} |
273 |
|
274 |
/**
|
275 |
* M?todo est?tico que crea un FLabel a partir de una Geometry.
|
276 |
*
|
277 |
* @param geom Geometry.
|
278 |
*
|
279 |
* @return nuevo FLabel creado.
|
280 |
* @throws CreateGeometryException
|
281 |
*/
|
282 |
public static FLabel createFLabel(org.gvsig.fmap.geom.Geometry geom) throws CreateGeometryException { |
283 |
float angle;
|
284 |
Point2D pAux = createLabelPoint(geom);
|
285 |
|
286 |
FLabel label = new FLabel();
|
287 |
label.setOrig(pAux); |
288 |
switch (geom.getType()) {
|
289 |
case org.gvsig.fmap.geom.Geometry.TYPES.CURVE:
|
290 |
PathLength pathLen = new DefaultPathLength(geom.getShape());
|
291 |
float midDistance = pathLen.lengthOfPath() / 2; |
292 |
angle = pathLen.angleAtLength(midDistance); |
293 |
|
294 |
if (angle < 0) { |
295 |
angle = angle + (float) (2 * Math.PI); |
296 |
} |
297 |
if ((angle > (Math.PI / 2)) && (angle < ((3 * Math.PI) / 2))) { |
298 |
angle = angle - (float) Math.PI; |
299 |
} |
300 |
label.setRotation(Math.toDegrees(angle));
|
301 |
break;
|
302 |
} // switch
|
303 |
|
304 |
return label;
|
305 |
} |
306 |
public static Point2D createLabelPoint(org.gvsig.fmap.geom.Geometry geom) throws CreateGeometryException { |
307 |
Point2D pAux = null; |
308 |
switch (geom.getType()) {
|
309 |
case org.gvsig.fmap.geom.Geometry.TYPES.POINT:
|
310 |
pAux = new Point2D.Double(((org.gvsig.fmap.geom.primitive.Point) geom).getX(), |
311 |
((org.gvsig.fmap.geom.primitive.Point) geom).getY()); |
312 |
return pAux;
|
313 |
|
314 |
case org.gvsig.fmap.geom.Geometry.TYPES.CURVE:
|
315 |
DefaultPathLength pathLen = new DefaultPathLength(geom.getShape());
|
316 |
|
317 |
// if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
|
318 |
float midDistance = pathLen.lengthOfPath() / 2; |
319 |
pAux = pathLen.pointAtLength(midDistance); |
320 |
return pAux;
|
321 |
|
322 |
case org.gvsig.fmap.geom.Geometry.TYPES.SURFACE:
|
323 |
|
324 |
try {
|
325 |
Point pLabel = geom.centroid();
|
326 |
if(pLabel!=null){ |
327 |
return new Point2D.Double(pLabel.getX(), pLabel.getY()); |
328 |
} |
329 |
} catch (Exception ex) { |
330 |
logger.info("Error while computing centroid. ", ex);
|
331 |
} |
332 |
break;
|
333 |
case org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT:
|
334 |
int num=((MultiPoint)geom).getPrimitivesNumber();
|
335 |
Rectangle2D r=null; |
336 |
if (num>0){ |
337 |
r= ((MultiPoint)geom).getPointAt(0).getBounds2D();
|
338 |
for (int i=1;i<num;i++){ |
339 |
org.gvsig.fmap.geom.primitive.Point fp=((MultiPoint)geom).getPointAt(i); |
340 |
r.add(new Point2D.Double(fp.getX(),fp.getY())); |
341 |
} |
342 |
} |
343 |
if (r!=null) |
344 |
return new Point2D.Double(r.getCenterX(),r.getCenterY()); |
345 |
break;
|
346 |
|
347 |
} // switch
|
348 |
|
349 |
return null; |
350 |
} |
351 |
public void setTypeFont(String t) { |
352 |
font=Font.getFont(t,font);
|
353 |
} |
354 |
public int getStyle() { |
355 |
return m_Style;
|
356 |
} |
357 |
|
358 |
public void setBoundBox(Rectangle2D boundBox) { |
359 |
this.boundBox=boundBox;
|
360 |
|
361 |
} |
362 |
public Rectangle2D getBoundBox(){ |
363 |
return boundBox;
|
364 |
} |
365 |
|
366 |
public Rectangle2D getBoundingBox(ViewPort vp) { |
367 |
return vp.fromMapRectangle(boundBox);
|
368 |
} |
369 |
|
370 |
public void setColor(int i) { |
371 |
color=new Color(i); |
372 |
} |
373 |
|
374 |
public Color getColor() { |
375 |
return color;
|
376 |
} |
377 |
/*public Font getFont(AffineTransform at,boolean isInPixels){
|
378 |
if (!isInPixels) {
|
379 |
float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE);
|
380 |
//Font nuevaFuente = font.deriveFont(alturaPixels);//new Font(getTypeFont(),getStyle(),(int)alturaPixels);
|
381 |
return font.deriveFont(alturaPixels);
|
382 |
}
|
383 |
//Font nuevaFuente = font.deriveFont((float)(getHeight()*SQUARE));//new Font(getTypeFont(),getStyle(),(int)(getHeight()*SQUARE));
|
384 |
return font;//font.deriveFont((float)(getHeight()*SQUARE));
|
385 |
}*/
|
386 |
public Font getFont(AffineTransform at,Font font){ |
387 |
float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE); |
388 |
//return font.deriveFont(alturaPixels);
|
389 |
return font.deriveFont(font.getStyle(),alturaPixels);
|
390 |
} |
391 |
|
392 |
/**
|
393 |
* Por ahora, para extender el renderizado de etiquetas, habr?a que
|
394 |
* crear otro FLabel y reimplementar este m?todo para que haga caso
|
395 |
* a otros s?mbolos. Es decir, NO usar FGraphicUtilities
|
396 |
* @param g
|
397 |
* @param affineTransform
|
398 |
* @param theShape
|
399 |
* @param theSymbol
|
400 |
*/
|
401 |
public void draw(Graphics2D g, AffineTransform affineTransform, Shape theShape, ISymbol theSymbol) { |
402 |
FGraphicUtilities.DrawLabel(g, affineTransform, |
403 |
(org.gvsig.fmap.geom.Geometry) theShape, (FSymbol) theSymbol, this);
|
404 |
|
405 |
|
406 |
} |
407 |
|
408 |
} |