root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFrameLegend.java @ 1094
History | View | Annotate | Download (15.2 KB)
1 |
package com.iver.cit.gvsig.gui.layout.fframes; |
---|---|
2 |
|
3 |
import com.iver.andami.PluginServices; |
4 |
|
5 |
import com.iver.cit.gvsig.LayoutControls; |
6 |
import com.iver.cit.gvsig.fmap.DriverException; |
7 |
import com.iver.cit.gvsig.fmap.FMap; |
8 |
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities; |
9 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
10 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
11 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
12 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData; |
13 |
import com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable; |
14 |
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo; |
15 |
import com.iver.cit.gvsig.gui.layout.Layout; |
16 |
|
17 |
import com.iver.utiles.XMLEntity; |
18 |
|
19 |
import java.awt.Color; |
20 |
import java.awt.Font; |
21 |
import java.awt.Graphics2D; |
22 |
import java.awt.Rectangle; |
23 |
import java.awt.font.FontRenderContext; |
24 |
import java.awt.geom.AffineTransform; |
25 |
import java.awt.geom.Rectangle2D; |
26 |
import java.awt.image.BufferedImage; |
27 |
|
28 |
import java.util.ArrayList; |
29 |
|
30 |
|
31 |
/**
|
32 |
* FFrame para introducir una leyenda en el Layout.
|
33 |
*
|
34 |
* @author Vicente Caballero Navarro
|
35 |
*/
|
36 |
public class FFrameLegend extends FFrame { |
37 |
private static final int PRESENTACION = 0; |
38 |
private static final int BORRADOR = 1; |
39 |
private int m_quality = 0; |
40 |
private int m_viewing = 0; |
41 |
private FontRenderContext m_frc = null; |
42 |
private FMap m_fmap = null; |
43 |
private Font m_f = new Font("Monospaced", Font.PLAIN, 9); |
44 |
private FFrameView fframeview = null; |
45 |
private int num = 0; |
46 |
private int m_max; |
47 |
private int m_numLayers; |
48 |
|
49 |
/**
|
50 |
* Rellena la calidad que se quiere aplicar.
|
51 |
*
|
52 |
* @param q entero que representa la calidad a aplicar.
|
53 |
*/
|
54 |
public void setQuality(int q) { |
55 |
m_quality = q; |
56 |
} |
57 |
|
58 |
/**
|
59 |
* Devuelve un entero que representa la calidad que est? seleccionada.
|
60 |
*
|
61 |
* @return tipo de calidad selccionada.
|
62 |
*/
|
63 |
public int getQuality() { |
64 |
return m_quality;
|
65 |
} |
66 |
|
67 |
/**
|
68 |
* Devuelve un entero que representa la forma en que se actualiza la vista.
|
69 |
*
|
70 |
* @return forma que se actualiza la vista.
|
71 |
*/
|
72 |
public int getViewing() { |
73 |
return m_viewing;
|
74 |
} |
75 |
|
76 |
/**
|
77 |
* Inserta una FFrameView de donde se obtiene la informaci?n de las capas
|
78 |
* para generar la leyenda.
|
79 |
*
|
80 |
* @param f FFrameView para obtener los nombres de las capas.
|
81 |
*/
|
82 |
public void setFFrameView(FFrameView f) { |
83 |
fframeview = f; |
84 |
} |
85 |
|
86 |
/**
|
87 |
* Devuelve el FFrameView utilizado para obtener la leyenda.
|
88 |
*
|
89 |
* @return FFrameView utilizado.
|
90 |
*/
|
91 |
public FFrameView getFFrameView() {
|
92 |
return fframeview;
|
93 |
} |
94 |
|
95 |
/**
|
96 |
* Rellena la forma de actualizar la vista.
|
97 |
*
|
98 |
* @param v entero que representa la forma de actualizar la vista.
|
99 |
*/
|
100 |
public void setViewing(int v) { |
101 |
m_viewing = v; |
102 |
} |
103 |
|
104 |
/**
|
105 |
* M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
|
106 |
* la transformada afin que se debe de aplicar y el rect?ngulo que se debe
|
107 |
* de dibujar.
|
108 |
*
|
109 |
* @param g Graphics
|
110 |
* @param at Transformada afin.
|
111 |
* @param rv rect?ngulo sobre el que hacer un clip.
|
112 |
* @param imgBase Imagen para acelerar el dibujado.
|
113 |
*
|
114 |
* @throws DriverException
|
115 |
*/
|
116 |
public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv, |
117 |
BufferedImage imgBase) throws DriverException { |
118 |
Rectangle2D.Double re = getBoundingBox(at);
|
119 |
FLayers layers = null;
|
120 |
|
121 |
if ((fframeview != null) && (fframeview.getFMap() != null)) { |
122 |
layers = fframeview.getFMap().getLayers(); |
123 |
} |
124 |
|
125 |
m_max = 0;
|
126 |
m_numLayers = 0;
|
127 |
|
128 |
if (intersects(rv, re)) {
|
129 |
if (layers == null) { //Si no se ha seleccionado ninguna vista para crear la leyenda. |
130 |
drawEmpty(g); |
131 |
} else if ((rv == null) || (getQuality() == PRESENTACION)) { |
132 |
m_numLayers = getSizeNum(layers); |
133 |
m_frc = g.getFontRenderContext(); |
134 |
|
135 |
double h = re.getHeight() / m_numLayers;
|
136 |
int n = 0; |
137 |
drawTheme(g, re, h, layers, n); |
138 |
} else if (getQuality() == BORRADOR) { //Si se selecciona la calidad BORRADOR. |
139 |
drawDraft(g); |
140 |
} |
141 |
} |
142 |
} |
143 |
|
144 |
/**
|
145 |
* Dibuja el nombre u s?mbolo de la capa que se pasa como par?metro.
|
146 |
*
|
147 |
* @param g Graphics2D sobre el que se dibuja.
|
148 |
* @param re Rectangle a rellenar.
|
149 |
* @param h Altura en pixels.
|
150 |
* @param layers Capa a representar.
|
151 |
* @param n ?ndice de la capa a dibujar.
|
152 |
*
|
153 |
* @return ?ndice de la capa siguiente.
|
154 |
*
|
155 |
* @throws DriverException
|
156 |
*/
|
157 |
private int drawTheme(Graphics2D g, Rectangle2D re, double h, |
158 |
FLayers layers, int n) throws DriverException { |
159 |
//////Se recorren los layers dibujando el s?mbolo y su nombre sobre el graphics
|
160 |
for (int i = 0; i < layers.getLayersCount(); i++) { |
161 |
FLayer layer = (FLayer) layers.getLayer(i); |
162 |
|
163 |
if (layer.isVisible()) {
|
164 |
if (layer instanceof FLayers) { |
165 |
n += drawTheme(g, re, h, layers, n); |
166 |
} else if (layer instanceof AlphanumericData) { |
167 |
AlphanumericData cO = (AlphanumericData) layer; |
168 |
|
169 |
if (((Classifiable) cO).getLegend() instanceof ClassifiedLegendInfo) { |
170 |
ClassifiedLegendInfo cli = (ClassifiedLegendInfo) ((Classifiable) cO).getLegend(); |
171 |
double dX = 0; |
172 |
double dY = n * h;
|
173 |
|
174 |
int xl = (int) (re.getX() + dX); |
175 |
int yl = (int) (re.getY() + dY); |
176 |
int sizefont = 0; |
177 |
|
178 |
if ((re.getHeight() / m_numLayers) < (re.getWidth() / 8)) { |
179 |
sizefont = (int) (re.getHeight() / m_numLayers);
|
180 |
} else {
|
181 |
sizefont = (int) (re.getWidth() / 8); |
182 |
} |
183 |
|
184 |
drawNameLegend(g, layer.getName(), re, sizefont, |
185 |
(int) (xl - (re.getWidth() / 5)), yl, h); |
186 |
n++; |
187 |
|
188 |
for (int j = 0; j < cli.getValues().length; j++) { |
189 |
dY = n * h; |
190 |
|
191 |
xl = (int) (re.getX() + dX);
|
192 |
yl = (int) (re.getY() + dY);
|
193 |
|
194 |
String s = cli.getDescriptions()[j];
|
195 |
drawNameLegend(g, s, re, sizefont, xl, yl, h); |
196 |
|
197 |
FSymbol fs2d = cli.getSymbols()[j]; |
198 |
drawSymbolLegend(g, re, xl, yl, fs2d, sizefont, |
199 |
((Classifiable) cO).getShapeType(), h); |
200 |
|
201 |
n++; |
202 |
} |
203 |
} else {
|
204 |
double dX = 0; |
205 |
double dY = n * h;
|
206 |
|
207 |
int xl = (int) (re.getX() + dX); |
208 |
int yl = (int) (re.getY() + dY); |
209 |
int sizefont = 0; |
210 |
|
211 |
if ((re.getHeight() / m_numLayers) < (re.getWidth() / 8)) { |
212 |
sizefont = (int) (re.getHeight() / m_numLayers);
|
213 |
} else {
|
214 |
sizefont = (int) (re.getWidth() / 8); |
215 |
} |
216 |
|
217 |
drawNameLegend(g, layer.getName(), re, sizefont, xl, |
218 |
yl, h); |
219 |
|
220 |
//FStyle2D fs2d = cO.getLegendInfo().getSymbol();
|
221 |
//drawSymbolLegend(g, re, xl, yl, fs2d,sizefont,cO.getShapeType());
|
222 |
FSymbol fs2d = ((Classifiable) cO).getLegend() |
223 |
.getDefaultSymbol(); |
224 |
|
225 |
//System.out.println("sizefont = " + sizefont);
|
226 |
drawSymbolLegend(g, re, xl, yl, fs2d, sizefont, |
227 |
((Classifiable) cO).getShapeType(), h); |
228 |
|
229 |
//System.out.println("cO.getShapeType()= " +
|
230 |
// cO.getShapeType());
|
231 |
n++; |
232 |
} |
233 |
} |
234 |
} |
235 |
} |
236 |
|
237 |
return n;
|
238 |
} |
239 |
|
240 |
/*private void drawSymbolLegendFS2D(Graphics2D g, Rectangle2D re, int x,
|
241 |
int y, FStyle2D fs2d, int sizefont, int type) {
|
242 |
SymbolPreviewDrawer pSD = new SymbolPreviewDrawer(fs2d);
|
243 |
double pW = 1;
|
244 |
double pH = m_numLayers;
|
245 |
int wl = (int) (re.getWidth() / pW);
|
246 |
int hl = (int) (re.getHeight() / pH);
|
247 |
int haux = (int) (sizefont * 1.8);
|
248 |
int yaux = (int) (y + ((hl - haux) / 2));
|
249 |
pSD.draw(g, new Rectangle(x, yaux, wl, haux), type);
|
250 |
}
|
251 |
*/
|
252 |
|
253 |
/**
|
254 |
* Dibuja sobre el graphics el nombre de la capa que se pasa como
|
255 |
* par?metro.
|
256 |
*
|
257 |
* @param g Graphics2D sobre el que dibujar.
|
258 |
* @param name Nombre de la capa.
|
259 |
* @param re Rect?ngulo a ocupar por el nombre.
|
260 |
* @param sizefont tama?o de la fuente.
|
261 |
* @param x Posici?n X.
|
262 |
* @param y Posici?n Y.
|
263 |
* @param h Altura.
|
264 |
*/
|
265 |
private void drawNameLegend(Graphics2D g, String name, Rectangle2D re, |
266 |
int sizefont, int x, int y, double h) { |
267 |
Font f = new Font("SansSerif", Font.PLAIN, sizefont); |
268 |
g.setFont(f); |
269 |
g.setColor(Color.black);
|
270 |
g.drawString(name, (int) (x + (re.getWidth() / 4)), (int) (y + (h / 2))); |
271 |
} |
272 |
|
273 |
/**
|
274 |
* Dibuja sobre el Graphics2D el s?mbolo.
|
275 |
*
|
276 |
* @param g Graphics2D.
|
277 |
* @param re Rect?ngulo a cubrir por el s?mbolo.
|
278 |
* @param x Posici?n X.
|
279 |
* @param y Posici?n Y.
|
280 |
* @param fs2d S?mbolo a dibujar.
|
281 |
* @param sizefont Tama?o de la fuente.
|
282 |
* @param type Tipo de s?mbolo.
|
283 |
* @param h Altura.
|
284 |
*/
|
285 |
private void drawSymbolLegend(Graphics2D g, Rectangle2D re, int x, int y, |
286 |
FSymbol fs2d, int sizefont, int type, double h) { |
287 |
double pW = 5; |
288 |
double pH = m_numLayers;
|
289 |
int wl = (int) (re.getWidth() / pW); |
290 |
|
291 |
// int hl = (int) (re.getHeight() / (pH*1.1));
|
292 |
int haux = (int) (sizefont * 0.7); |
293 |
|
294 |
// int yaux=(int)(y+(hl-haux)/2);
|
295 |
// int ydef=(int)(re.getCenterY()-haux/2);
|
296 |
FGraphicUtilities.DrawSymbol(g, new AffineTransform(), |
297 |
new Rectangle(x, (int) ((y + (h / 2)) - haux), wl, haux), fs2d); |
298 |
} |
299 |
|
300 |
/*private void drawNameLegendFS2D(Graphics2D g, String name, Rectangle2D re,
|
301 |
int sizefont, int x, int y, double h) {
|
302 |
FStyle2D fsd = new FStyle2D();
|
303 |
TextStyle2D ts2d = new TextStyle2D();
|
304 |
ts2d.setLabel(name);
|
305 |
ts2d.setAnchorX(1);
|
306 |
ts2d.setAnchorY(0);
|
307 |
double pW = 1;
|
308 |
double pH = m_numLayers;
|
309 |
int wl = (int) (re.getWidth() / pW);
|
310 |
int hl = (int) (re.getHeight() / pH);
|
311 |
//ts2d.setAbsoluteLineDisplacement(true);
|
312 |
ts2d.setDisplacementX(re.getWidth() / 1.5); //re.getX());
|
313 |
ts2d.setDisplacementY(-re.getHeight() + (h / 1.5)); //re.getY());
|
314 |
ts2d.setFill(Color.black);
|
315 |
//fsd.setPointStyle2D(null);
|
316 |
//fsd.setLineStyle2D(null);
|
317 |
//fsd.setPolygonStyle2D(null);
|
318 |
ts2d.setFont(new Font("SansSerif", Font.PLAIN, sizefont));
|
319 |
fsd.setTextStyle2D(ts2d);
|
320 |
SymbolPreviewDrawer spd = new SymbolPreviewDrawer(fsd);
|
321 |
spd.drawText(g,
|
322 |
new Rectangle(x, y, (int) re.getWidth(), (int) re.getHeight()));
|
323 |
}
|
324 |
*/
|
325 |
|
326 |
/**
|
327 |
* Devuelve el n?mero total de capas incluyendo las subcapas.
|
328 |
*
|
329 |
* @param layers Capa a contar.
|
330 |
*
|
331 |
* @return N?mero de capas y subcapas.
|
332 |
*/
|
333 |
private int getSizeNum(FLayers layers) { |
334 |
int n = 0; |
335 |
|
336 |
/////Aqu? hay que calcular cuantos layers y sublayers hay, para saber que distancias dejar entre uno y otro.
|
337 |
///y adem?s el tama?o de cada uno de ellos para saber que anchura dejar.
|
338 |
for (int i = 0; i < layers.getLayersCount(); i++) { |
339 |
FLayer layer = (FLayer) layers.getLayer(i); |
340 |
|
341 |
if (layer.isVisible()) {
|
342 |
if (layer.getName().length() > m_max) {
|
343 |
m_max = layer.getName().length(); |
344 |
} |
345 |
|
346 |
if (layer instanceof FLayers) { |
347 |
n += getSizeNum((FLayers) layer); //m_numLayers += getNumInLyrGroup((FLayers) layer);
|
348 |
} else if (layer instanceof AlphanumericData) { |
349 |
AlphanumericData cO = (AlphanumericData) layer; |
350 |
n++; |
351 |
|
352 |
if (((Classifiable) cO).getLegend() instanceof ClassifiedLegendInfo) { |
353 |
ClassifiedLegendInfo cli = (ClassifiedLegendInfo) ((Classifiable) cO).getLegend(); |
354 |
|
355 |
for (int j = 0; j < cli.getValues().length; j++) { |
356 |
String s = cli.getDescriptions()[j];
|
357 |
|
358 |
if (s.length() > m_max) {
|
359 |
m_max = s.length(); |
360 |
} |
361 |
|
362 |
n++; |
363 |
} |
364 |
} |
365 |
} |
366 |
} |
367 |
} |
368 |
|
369 |
return n;
|
370 |
} |
371 |
|
372 |
/**
|
373 |
* Transforma el FFrameLegend en diferentes FFrameSymbol y FFrameText.
|
374 |
*
|
375 |
* @param layout Referencia al Layout.
|
376 |
*/
|
377 |
public void toFFrames(Layout layout) { |
378 |
//TODO Falta que implementar.
|
379 |
} |
380 |
|
381 |
/**
|
382 |
* Rellena la fuente a utilizar al dibujar los String sobre el graphics.
|
383 |
*
|
384 |
* @param f Font.
|
385 |
*/
|
386 |
public void setFont(Font f) { |
387 |
m_f = f; |
388 |
} |
389 |
|
390 |
/**
|
391 |
* Devuelve la fuente que esta utilizando.
|
392 |
*
|
393 |
* @return Font.
|
394 |
*/
|
395 |
public Font getFont() { |
396 |
if (m_f != null) { |
397 |
return new Font(m_f.getFontName(), m_f.getStyle(), 9); |
398 |
} |
399 |
|
400 |
return null; |
401 |
} |
402 |
|
403 |
/**
|
404 |
* @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
|
405 |
*/
|
406 |
public XMLEntity getXMLEntity() {
|
407 |
XMLEntity xml = new XMLEntity();
|
408 |
xml.putProperty("className",this.getClass().getName()); |
409 |
xml.putProperty("m_name", m_name);
|
410 |
xml.putProperty("x", getBoundBox().x);
|
411 |
xml.putProperty("y", getBoundBox().y);
|
412 |
xml.putProperty("w", getBoundBox().width);
|
413 |
xml.putProperty("h", getBoundBox().height);
|
414 |
xml.putProperty("m_Selected", m_Selected);
|
415 |
xml.putProperty("type", Layout.RECTANGLELEGEND);
|
416 |
xml.putProperty("m_numLayers", m_numLayers);
|
417 |
xml.putProperty("m_max", m_max);
|
418 |
xml.putProperty("m_quality", m_quality);
|
419 |
xml.putProperty("m_viewing", m_viewing);
|
420 |
xml.putProperty("fontName", m_f.getFontName());
|
421 |
xml.putProperty("fontStyle", m_f.getStyle());
|
422 |
xml.putProperty("tag", getTag());
|
423 |
|
424 |
///xml.addChild(fframeview.getXMLEntity());
|
425 |
LayoutControls lc = (LayoutControls) PluginServices.getExtension(LayoutControls.class); |
426 |
Layout layout = lc.getLayout(); |
427 |
ArrayList fframes = layout.getFFrames();
|
428 |
|
429 |
for (int i = 0; i < fframes.size(); i++) { |
430 |
if (fframes.get(i) instanceof FFrameView) { |
431 |
if (fframeview.getView().getName().equals(((FFrameView) fframes
|
432 |
.get(i)).getView() |
433 |
.getName())) { |
434 |
xml.putProperty("index", i);
|
435 |
} |
436 |
} |
437 |
} |
438 |
|
439 |
/*ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
|
440 |
ArrayList views = pe.getProject().getViews();
|
441 |
boolean hasIndex = false;
|
442 |
for (int i = 0; i < views.size(); i++) {
|
443 |
if (fframeview.getView() == ((ProjectView) views.get(i))) {
|
444 |
xml.putProperty("indice", i);
|
445 |
hasIndex = true;
|
446 |
}
|
447 |
}
|
448 |
if (!hasIndex) {
|
449 |
xml.putProperty("indice", -1);
|
450 |
}
|
451 |
*/
|
452 |
|
453 |
//De este ?ndice se debe de obtener el arrayList de layers.
|
454 |
|
455 |
/* ProjectExtension pe = (ProjectExtension) App.instance.getPc()
|
456 |
.getExtension(ProjectExtension.class);
|
457 |
ArrayList views = pe.getProject().getViews();
|
458 |
boolean hasIndex = false;
|
459 |
for (int i = 0; i < views.size(); i++) {
|
460 |
if (m_fmap == ((ProjectView) views.get(i)).getMapContext()) {
|
461 |
xml.putProperty("indice", i);
|
462 |
hasIndex = true;
|
463 |
}
|
464 |
}
|
465 |
if (!hasIndex) {
|
466 |
xml.putProperty("indice", -1);
|
467 |
}
|
468 |
*/
|
469 |
return xml;
|
470 |
} |
471 |
|
472 |
/**
|
473 |
* @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
|
474 |
* com.iver.cit.gvsig.project.Project)
|
475 |
*/
|
476 |
public void setXMLEntity(XMLEntity xml, Layout l) { |
477 |
if (xml.getIntProperty("m_Selected") != 0) { |
478 |
this.setSelected(true); |
479 |
} else {
|
480 |
this.setSelected(false); |
481 |
} |
482 |
|
483 |
this.m_numLayers = xml.getIntProperty("m_numLayers"); |
484 |
this.m_max = xml.getIntProperty("m_max"); |
485 |
this.m_quality = xml.getIntProperty("m_quality"); |
486 |
this.m_viewing = xml.getIntProperty("m_viewing"); |
487 |
this.m_f = new Font(xml.getStringProperty("fontName"), |
488 |
xml.getIntProperty("fontStyle"), 9); |
489 |
|
490 |
//this.setFFrameView((FFrameView) FFrameView.createFFrame(xml.getChild(0),
|
491 |
// p));
|
492 |
|
493 |
/* int indice = xml.getIntProperty("indice");
|
494 |
if (indice != -1) {
|
495 |
ProjectView view = (ProjectView) p.getViews().get(indice);
|
496 |
|
497 |
fframeview.setView((ProjectView) view);
|
498 |
}
|
499 |
*/
|
500 |
ArrayList fframes = l.getFFrames();
|
501 |
fframeview = (FFrameView) fframes.get(xml.getIntProperty("index"));
|
502 |
|
503 |
/* LayoutControls lc=(LayoutControls)PluginServices.getExtension(LayoutControls.class);
|
504 |
Layout layout=lc.getLayout();
|
505 |
ArrayList fframes=layout.getFFrames();
|
506 |
fframeview=(FFrameView)fframes.get(xml.getIntProperty("index"));
|
507 |
*/
|
508 |
|
509 |
//this.setFFrameView((FFrameView) FFrameView.createFFrame(xml.getChild(0),
|
510 |
// p));
|
511 |
} |
512 |
|
513 |
/**
|
514 |
* @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
|
515 |
*/
|
516 |
public String getNameFFrame() { |
517 |
return PluginServices.getText(this, "leyenda") + num; |
518 |
} |
519 |
|
520 |
/**
|
521 |
* @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
|
522 |
* java.awt.geom.AffineTransform)
|
523 |
*/
|
524 |
public void print(Graphics2D g, AffineTransform at) |
525 |
throws DriverException {
|
526 |
draw(g, at, null, null); |
527 |
} |
528 |
} |