svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_mapcontext / src / org / gvsig / fmap / geom / utils / FLabel.java @ 29097
History | View | Annotate | Download (11.9 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.fmap.geom.utils; |
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.gvsig.fmap.geom.aggregate.MultiPoint; |
59 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
60 |
import org.gvsig.fmap.geom.util.Converter; |
61 |
import org.gvsig.fmap.mapcontext.ViewPort; |
62 |
import org.gvsig.fmap.mapcontext.rendering.symbols.FGraphicUtilities; |
63 |
import org.gvsig.fmap.mapcontext.rendering.symbols.FSymbol; |
64 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
65 |
|
66 |
import com.iver.utiles.XMLEntity; |
67 |
import com.vividsolutions.jts.geom.Geometry; |
68 |
import com.vividsolutions.jts.geom.Point; |
69 |
|
70 |
|
71 |
/**
|
72 |
* Se utiliza para etiquetar. Las capas vectoriales tienen un arrayList
|
73 |
* (m_labels) de FLabel, un FLabel por cada registro.
|
74 |
*
|
75 |
* @author FJP
|
76 |
*/
|
77 |
public class FLabel implements Cloneable { |
78 |
public final static byte LEFT_TOP = 0; |
79 |
public final static byte LEFT_CENTER = 1; |
80 |
public final static byte LEFT_BOTTOM = 2; |
81 |
public final static byte CENTER_TOP = 6; |
82 |
public final static byte CENTER_CENTER = 7; |
83 |
public final static byte CENTER_BOTTOM = 8; |
84 |
public final static byte RIGHT_TOP = 12; |
85 |
public final static byte RIGHT_CENTER = 13; |
86 |
public final static byte RIGHT_BOTTOM = 14; |
87 |
private String m_Str = null; |
88 |
private Point2D m_Orig = null; |
89 |
private double m_Height; |
90 |
public static final double SQUARE = Math.sqrt(2.0); |
91 |
/**
|
92 |
* <code>m_rotation</code> en grados, NO en radianes. Se convierte en
|
93 |
* radianes al dibujar.
|
94 |
*/
|
95 |
private double m_rotation; |
96 |
|
97 |
/**
|
98 |
* Valores posibles para <code>m_Justification</code>: Left/Top = 0
|
99 |
* Center/Top = 6 Right/Top = 12 Left/Center = 1
|
100 |
* Center/Center = 7 Right/Center = 13 Left/Bottom = 2
|
101 |
* Center/Bottom = 8 Right/Bottom = 14
|
102 |
*/
|
103 |
private byte m_Justification = LEFT_BOTTOM; // Por defecto |
104 |
private int m_Style; |
105 |
private Rectangle2D boundBox; |
106 |
private Color color; |
107 |
private static Font font=new Font("Dialog",Font.PLAIN,12); |
108 |
/**
|
109 |
* Crea un nuevo FLabel.
|
110 |
*/
|
111 |
public FLabel() {
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* Crea un nuevo FLabel.
|
116 |
*
|
117 |
* @param str DOCUMENT ME!
|
118 |
*/
|
119 |
public FLabel(String str) { |
120 |
m_Str = str; |
121 |
} |
122 |
|
123 |
/**
|
124 |
* Crea un nuevo FLabel.
|
125 |
*
|
126 |
* @param str
|
127 |
* @param pOrig
|
128 |
* @param heightText
|
129 |
* @param rotation
|
130 |
*/
|
131 |
public FLabel(String str, Point2D pOrig, double heightText, double rotation) { |
132 |
m_Str = str; |
133 |
m_Orig = pOrig; |
134 |
m_Height = heightText; |
135 |
m_rotation = rotation; |
136 |
} |
137 |
|
138 |
/**
|
139 |
* Introduce un nuevo FLabel al ya existente.
|
140 |
*
|
141 |
* @param label
|
142 |
*/
|
143 |
public void setFLabel(FLabel label) { |
144 |
m_Str = label.m_Str; |
145 |
m_Orig = label.m_Orig; |
146 |
m_Height = label.m_Height; |
147 |
m_rotation = label.m_rotation; |
148 |
} |
149 |
|
150 |
/**
|
151 |
* Clona el FLabel.
|
152 |
*
|
153 |
* @return Object clonado.
|
154 |
*/
|
155 |
public Object clone() { |
156 |
FLabel label=new FLabel(m_Str, m_Orig, m_Height, m_rotation);
|
157 |
label.boundBox=(Rectangle2D)boundBox.clone();
|
158 |
return label;
|
159 |
} |
160 |
|
161 |
/**
|
162 |
* Devuelve la altura del Label.
|
163 |
*
|
164 |
* @return Returns the m_Height.
|
165 |
*/
|
166 |
public double getHeight() { |
167 |
return m_Height;
|
168 |
} |
169 |
|
170 |
/**
|
171 |
* Devuelve el punto de origen.
|
172 |
*
|
173 |
* @return Returns the m_Orig.
|
174 |
*/
|
175 |
public Point2D getOrig() { |
176 |
return m_Orig;
|
177 |
} |
178 |
|
179 |
/**
|
180 |
* Devuelve la rotaci?n.
|
181 |
*
|
182 |
* @return Returns the m_rotation.
|
183 |
*/
|
184 |
public double getRotation() { |
185 |
return m_rotation;
|
186 |
} |
187 |
|
188 |
/**
|
189 |
* Devuelve un String con el texto del Label.
|
190 |
*
|
191 |
* @return Returns the m_Str.
|
192 |
*/
|
193 |
public String getString() { |
194 |
return m_Str;
|
195 |
} |
196 |
|
197 |
/**
|
198 |
* Introduce la altura del Label.
|
199 |
*
|
200 |
* @param height The m_Height to set.
|
201 |
*/
|
202 |
public void setHeight(double height) { |
203 |
m_Height = height; |
204 |
} |
205 |
|
206 |
/**
|
207 |
* Introduce el punto de origen del Label.
|
208 |
*
|
209 |
* @param orig The m_Orig to set.
|
210 |
*/
|
211 |
public void setOrig(Point2D orig) { |
212 |
m_Orig = orig; |
213 |
} |
214 |
|
215 |
/**
|
216 |
* Introduce la rotaci?n a aplicar al Label.
|
217 |
*
|
218 |
* @param m_rotation The m_rotation to set.
|
219 |
*/
|
220 |
public void setRotation(double m_rotation) { |
221 |
this.m_rotation = Math.toRadians(-m_rotation); |
222 |
} |
223 |
|
224 |
/**
|
225 |
* Introduce el texto del Label.
|
226 |
*
|
227 |
* @param str The m_Str to set.
|
228 |
*/
|
229 |
public void setString(String str) { |
230 |
m_Str = str; |
231 |
} |
232 |
|
233 |
/**
|
234 |
* Valores posibles para <code>m_Justification</code>: Left/Top = 0
|
235 |
* Center/Top = 6 Right/Top = 12 Left/Center = 1
|
236 |
* Center/Center = 7 Right/Center = 13 Left/Bottom = 2
|
237 |
* Center/Bottom = 8 Right/Bottom = 14
|
238 |
*
|
239 |
* @return byte.
|
240 |
*/
|
241 |
public byte getJustification() { |
242 |
return m_Justification;
|
243 |
} |
244 |
|
245 |
/**
|
246 |
* Valores posibles para <code>m_Justification</code>: Left/Top = 0
|
247 |
* Center/Top = 6 Right/Top = 12 Left/Center = 1
|
248 |
* Center/Center = 7 Right/Center = 13 Left/Bottom = 2
|
249 |
* Center/Bottom = 8 Right/Bottom = 14
|
250 |
*
|
251 |
* @param justification byte
|
252 |
*/
|
253 |
public void setJustification(byte justification) { |
254 |
m_Justification = justification; |
255 |
} |
256 |
|
257 |
/**
|
258 |
* Devuelve un Objeto XMLEntity con la informaci?n los atributos necesarios
|
259 |
* para poder despu?s volver a crear el objeto original.
|
260 |
*
|
261 |
* @return XMLEntity.
|
262 |
*/
|
263 |
public XMLEntity getXMLEntity() {
|
264 |
XMLEntity xml = new XMLEntity();
|
265 |
xml.putProperty("className",this.getClass().getName()); |
266 |
xml.setName("flabel");
|
267 |
xml.putProperty("m_Height", m_Height);
|
268 |
xml.putProperty("m_Justification", (int) m_Justification); |
269 |
xml.putProperty("m_OrigX", m_Orig.getX());
|
270 |
xml.putProperty("m_OrigY", m_Orig.getY());
|
271 |
xml.putProperty("m_rotation", m_rotation);
|
272 |
xml.putProperty("m_Str", m_Str);
|
273 |
|
274 |
return xml;
|
275 |
} |
276 |
|
277 |
/**
|
278 |
* Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
|
279 |
*
|
280 |
* @param xml XMLEntity
|
281 |
*
|
282 |
* @return Objeto de esta clase.
|
283 |
*/
|
284 |
public static FLabel createFLabel(XMLEntity xml) { |
285 |
FLabel label = new FLabel();
|
286 |
label.setHeight(xml.getDoubleProperty("m_Height"));
|
287 |
label.setJustification((byte) xml.getIntProperty("m_Justification")); |
288 |
label.setOrig(new Point2D.Double(xml.getDoubleProperty("m_OrigX"), |
289 |
xml.getDoubleProperty("m_OrigY")));
|
290 |
label.setString("m_Str");
|
291 |
|
292 |
return label;
|
293 |
} |
294 |
|
295 |
/**
|
296 |
* M?todo est?tico que crea un FLabel a partir de una Geometry.
|
297 |
*
|
298 |
* @param geom Geometry.
|
299 |
*
|
300 |
* @return nuevo FLabel creado.
|
301 |
* @throws CreateGeometryException
|
302 |
*/
|
303 |
public static FLabel createFLabel(org.gvsig.fmap.geom.Geometry geom) throws CreateGeometryException { |
304 |
float angle;
|
305 |
Point2D pAux = createLabelPoint(geom);
|
306 |
|
307 |
FLabel label = new FLabel();
|
308 |
label.setOrig(pAux); |
309 |
switch (geom.getType()) {
|
310 |
case org.gvsig.fmap.geom.Geometry.TYPES.CURVE:
|
311 |
PathLength pathLen = new PathLength(geom);
|
312 |
float midDistance = pathLen.lengthOfPath() / 2; |
313 |
angle = pathLen.angleAtLength(midDistance); |
314 |
|
315 |
if (angle < 0) { |
316 |
angle = angle + (float) (2 * Math.PI); |
317 |
} |
318 |
if ((angle > (Math.PI / 2)) && (angle < ((3 * Math.PI) / 2))) { |
319 |
angle = angle - (float) Math.PI; |
320 |
} |
321 |
label.setRotation(Math.toDegrees(angle));
|
322 |
break;
|
323 |
} // switch
|
324 |
|
325 |
return label;
|
326 |
} |
327 |
public static Point2D createLabelPoint(org.gvsig.fmap.geom.Geometry geom) throws CreateGeometryException { |
328 |
Point2D pAux = null; |
329 |
switch (geom.getType()) {
|
330 |
case org.gvsig.fmap.geom.Geometry.TYPES.POINT:
|
331 |
pAux = new Point2D.Double(((org.gvsig.fmap.geom.primitive.Point) geom).getX(), |
332 |
((org.gvsig.fmap.geom.primitive.Point) geom).getY()); |
333 |
return pAux;
|
334 |
|
335 |
case org.gvsig.fmap.geom.Geometry.TYPES.CURVE:
|
336 |
PathLength pathLen = new PathLength(geom);
|
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 org.gvsig.fmap.geom.Geometry.TYPES.SURFACE:
|
344 |
Geometry geo = Converter.geometryToJts(geom); |
345 |
|
346 |
if (geo == null) { |
347 |
return null; |
348 |
} |
349 |
|
350 |
Point pJTS = geo.getCentroid();
|
351 |
|
352 |
if (pJTS != null) { |
353 |
org.gvsig.fmap.geom.Geometry pLabel = Converter.jtsToGeometry(pJTS); |
354 |
return new Point2D.Double(((org.gvsig.fmap.geom.primitive.Point) pLabel).getX(), |
355 |
((org.gvsig.fmap.geom.primitive.Point) pLabel).getY()); |
356 |
} |
357 |
break;
|
358 |
case org.gvsig.fmap.geom.Geometry.TYPES.MULTIPOINT:
|
359 |
int num=((MultiPoint)geom).getPrimitivesNumber();
|
360 |
Rectangle2D r=null; |
361 |
if (num>0){ |
362 |
r= ((MultiPoint)geom).getPointAt(0).getBounds2D();
|
363 |
for (int i=1;i<num;i++){ |
364 |
org.gvsig.fmap.geom.primitive.Point fp=((MultiPoint)geom).getPointAt(i); |
365 |
r.add(new Point2D.Double(fp.getX(),fp.getY())); |
366 |
} |
367 |
} |
368 |
if (r!=null) |
369 |
return new Point2D.Double(r.getCenterX(),r.getCenterY()); |
370 |
break;
|
371 |
|
372 |
} // switch
|
373 |
return null; |
374 |
} |
375 |
public void setTypeFont(String t) { |
376 |
font=Font.getFont(t,font);
|
377 |
} |
378 |
public int getStyle() { |
379 |
return m_Style;
|
380 |
} |
381 |
|
382 |
public void setBoundBox(Rectangle2D boundBox) { |
383 |
this.boundBox=boundBox;
|
384 |
|
385 |
} |
386 |
public Rectangle2D getBoundBox(){ |
387 |
return boundBox;
|
388 |
} |
389 |
|
390 |
public Rectangle2D getBoundingBox(ViewPort vp) { |
391 |
return vp.fromMapRectangle(boundBox);
|
392 |
} |
393 |
|
394 |
public void setColor(int i) { |
395 |
color=new Color(i); |
396 |
} |
397 |
|
398 |
public Color getColor() { |
399 |
return color;
|
400 |
} |
401 |
/*public Font getFont(AffineTransform at,boolean isInPixels){
|
402 |
if (!isInPixels) {
|
403 |
float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE);
|
404 |
//Font nuevaFuente = font.deriveFont(alturaPixels);//new Font(getTypeFont(),getStyle(),(int)alturaPixels);
|
405 |
return font.deriveFont(alturaPixels);
|
406 |
}
|
407 |
//Font nuevaFuente = font.deriveFont((float)(getHeight()*SQUARE));//new Font(getTypeFont(),getStyle(),(int)(getHeight()*SQUARE));
|
408 |
return font;//font.deriveFont((float)(getHeight()*SQUARE));
|
409 |
}*/
|
410 |
public Font getFont(AffineTransform at,Font font){ |
411 |
float alturaPixels = (float) ((getHeight() * at.getScaleX())*SQUARE); |
412 |
//return font.deriveFont(alturaPixels);
|
413 |
return font.deriveFont(font.getStyle(),alturaPixels);
|
414 |
} |
415 |
|
416 |
/**
|
417 |
* Por ahora, para extender el renderizado de etiquetas, habr?a que
|
418 |
* crear otro FLabel y reimplementar este m?todo para que haga caso
|
419 |
* a otros s?mbolos. Es decir, NO usar FGraphicUtilities
|
420 |
* @param g
|
421 |
* @param affineTransform
|
422 |
* @param theShape
|
423 |
* @param theSymbol
|
424 |
*/
|
425 |
public void draw(Graphics2D g, AffineTransform affineTransform, Shape theShape, ISymbol theSymbol) { |
426 |
FGraphicUtilities.DrawLabel(g, affineTransform, |
427 |
(org.gvsig.fmap.geom.Geometry) theShape, (FSymbol) theSymbol, this);
|
428 |
|
429 |
|
430 |
} |
431 |
|
432 |
} |