Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / fframes / FFrameLegend.java @ 36648

History | View | Annotate | Download (29.3 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.app.project.documents.layout.fframes;
23

    
24
import java.awt.Color;
25
import java.awt.Font;
26
import java.awt.Graphics2D;
27
import java.awt.Image;
28
import java.awt.Rectangle;
29
import java.awt.geom.AffineTransform;
30
import java.awt.geom.Rectangle2D;
31
import java.awt.image.BufferedImage;
32
import java.util.ArrayList;
33
import java.util.List;
34

    
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
37
import org.gvsig.app.project.documents.layout.LayoutContext;
38
import org.gvsig.compat.print.PrintAttributes;
39
import org.gvsig.fmap.dal.exception.ReadException;
40
import org.gvsig.fmap.geom.Geometry;
41
import org.gvsig.fmap.mapcontext.MapContextLocator;
42
import org.gvsig.fmap.mapcontext.MapContextManager;
43
import org.gvsig.fmap.mapcontext.layers.FLayer;
44
import org.gvsig.fmap.mapcontext.layers.FLayers;
45
import org.gvsig.fmap.mapcontext.layers.operations.Classifiable;
46
import org.gvsig.fmap.mapcontext.layers.operations.IHasImageLegend;
47
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedLegend;
48
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
49
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
50
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
51
import org.gvsig.tools.ToolsLocator;
52
import org.gvsig.tools.dynobject.DynStruct;
53
import org.gvsig.tools.persistence.PersistenceManager;
54
import org.gvsig.tools.persistence.PersistentState;
55
import org.gvsig.tools.persistence.exception.PersistenceException;
56

    
57
/**
58
 * FFrame para introducir una leyenda en el Layout.
59
 * 
60
 * @author Vicente Caballero Navarro
61
 */
62
public class FFrameLegend extends AbstractFFrameViewDependence implements
63
    IFFrameViewDependence {
64

    
65
    public static final String PERSISTENCE_DEFINITION_NAME = "FFrameLegend";
66

    
67
    private static final String NUMLAYERS_FIELD = "numLayers";
68
    private static final String MAX_FIELD = "max";
69
    private static final String QUALITY_FIELD = "quality";
70
    private static final String VIEWING_FIELD = "viewing";
71
    private static final String FONT_FIELD = "font";
72
    private static final String NAMELAYERS_FIELD = "nameLayers";
73
    private static final String AREVISIBLE_FIELD = "areVisible";
74

    
75
    private static final int PRESENTACION = 0;
76
    private static final int BORRADOR = 1;
77
    private int m_quality = 0;
78
    private int m_viewing = 0;
79
    private Font m_font = new Font("SansSerif", Font.PLAIN, 9);
80
    private int m_max;
81
    private int m_numLayers;
82
    private FLayers layers = null;
83
    private List<String> nameLayers = new ArrayList<String>();
84
    private List<Boolean> areVisible = new ArrayList<Boolean>();
85
    private PrintAttributes properties;
86

    
87
    private MapContextManager mapContextManager = MapContextLocator
88
        .getMapContextManager();
89

    
90
    public FFrameLegend() {
91

    
92
    }
93

    
94
    /**
95
     * Rellena la calidad que se quiere aplicar.
96
     * 
97
     * @param q
98
     *            entero que representa la calidad a aplicar.
99
     */
100
    public void setQuality(int q) {
101
        m_quality = q;
102
    }
103

    
104
    /**
105
     * Devuelve un entero que representa la calidad que est? seleccionada.
106
     * 
107
     * @return tipo de calidad selccionada.
108
     */
109
    public int getQuality() {
110
        return m_quality;
111
    }
112

    
113
    /**
114
     * Devuelve un entero que representa la forma en que se actualiza la vista.
115
     * 
116
     * @return forma que se actualiza la vista.
117
     */
118
    public int getViewing() {
119
        return m_viewing;
120
    }
121

    
122
    /**
123
     * Rellena la forma de actualizar la vista.
124
     * 
125
     * @param v
126
     *            entero que representa la forma de actualizar la vista.
127
     */
128
    public void setViewing(int v) {
129
        m_viewing = v;
130
    }
131

    
132
    /**
133
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
134
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
135
     * de dibujar.
136
     * 
137
     * @param g
138
     *            Graphics
139
     * @param at
140
     *            Transformada afin.
141
     * @param rv
142
     *            rect?ngulo sobre el que hacer un clip.
143
     * @param imgBase
144
     *            Imagen para acelerar el dibujado.
145
     */
146
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
147
        BufferedImage imgBase) {
148
        Rectangle2D.Double re = getBoundingBox(at);
149
        g.rotate(Math.toRadians(getRotation()), re.x + (re.width / 2), re.y
150
            + (re.height / 2));
151

    
152
        if ((fframeViewDependence != null)
153
            && (fframeViewDependence.getMapContext() != null)) {
154
            layers = fframeViewDependence.getMapContext().getLayers();
155
        }
156

    
157
        m_max = 0;
158
        m_numLayers = 0;
159

    
160
        if (intersects(rv, re)) {
161
            if (layers == null) { // Si no se ha seleccionado ninguna vista para
162
                                  // crear la leyenda.
163
                drawEmpty(g);
164
            } else
165
                if ((rv == null) || (getQuality() == PRESENTACION)) {
166
                    m_numLayers = getSizeNum(layers);
167

    
168
                    double h = re.getHeight() / m_numLayers;
169
                    int[] n = new int[1];
170
                    n[0] = 0;
171
                    drawLegendOrToFFrame(g, re, h, layers, n, null);
172
                } else
173
                    if (getQuality() == BORRADOR) { // Si se selecciona la
174
                                                    // calidad BORRADOR.
175
                        drawDraft(g);
176
                    }
177
        }
178

    
179
        g.rotate(Math.toRadians(-getRotation()), re.x + (re.width / 2), re.y
180
            + (re.height / 2));
181
    }
182

    
183
    /**
184
     * Dibuja el nombre u s?mbolo de la capa que se pasa como par?metro.
185
     * 
186
     * @param g
187
     *            Graphics2D sobre el que se dibuja.
188
     * @param re
189
     *            Rectangle a rellenar.
190
     * @param h
191
     *            Altura en pixels.
192
     * @param layers
193
     *            Capa a representar.
194
     * @param n
195
     *            ?ndice de la capa a dibujar.
196
     * @throws ReadDriverException
197
     *             TODO
198
     */
199
    private void drawLegendOrToFFrame(Graphics2D g, Rectangle2D re, double h,
200
        FLayers layers, int[] n, LayoutContext layout) {
201
        float sizefont = 0;
202

    
203
        if ((re.getHeight() / m_numLayers) < (re.getWidth() / (m_max * 0.7))) {
204
            sizefont = (float) (re.getHeight() / m_numLayers);
205
        } else {
206
            sizefont = (float) (re.getWidth() / (m_max * 0.7));
207
        }
208

    
209
        int l = 0;
210
        // ////Se recorren los layers dibujando el s?mbolo y su nombre sobre el
211
        // graphics
212
        for (int i = layers.getLayersCount() - 1; i >= 0; i--) {
213
            FLayer layer = layers.getLayer(i);
214
            boolean b = false;
215

    
216
            if (nameLayers.size() > l
217
                && nameLayers.get(l).equals(layer.getName())) {
218
                b = ((Boolean) areVisible.get(l)).booleanValue();
219
            } else {
220
                b = layer.isVisible();
221
            }
222
            l++;
223
            if (b) {
224
                if (layer instanceof FLayers) {
225
                    n[0]++;
226

    
227
                    double dX = 0;
228
                    double dY = n[0] * h;
229
                    double xl = (re.getX() + dX);
230
                    double yl = (re.getY() + dY);
231
                    if (layout != null) {
232
                        toFFrameText(layout, layer.getName(), re, sizefont,
233
                            (xl - (re.getWidth() / 5)), yl, h);
234
                    } else {
235
                        drawNameLegend(g, layer.getName(), re, sizefont,
236
                            (xl - (re.getWidth() / 5)), yl, h);
237
                    }
238
                    n[0]++;
239
                    drawLegendOrToFFrame(g, re, h, (FLayers) layer, n, layout);
240
                    n[0]++;
241
                } else
242
                    if (layer instanceof Classifiable
243
                        && !(layer instanceof IHasImageLegend)) {
244
                        Classifiable cO = (Classifiable) layer;
245

    
246
                        if (cO.getLegend() instanceof IClassifiedLegend) {// &&
247
                                                                          // !(cO
248
                                                                          // instanceof
249
                                                                          // FLyrAnnotation))
250
                                                                          // {
251
                            IClassifiedLegend cli =
252
                                (IClassifiedLegend) cO.getLegend();
253
                            double dX = 0;
254
                            double dY = n[0] * h;
255

    
256
                            double xl = (re.getX() + dX);
257
                            double yl = (re.getY() + dY);
258
                            if (layout != null) {
259
                                toFFrameText(layout, layer.getName(), re,
260
                                    sizefont, (xl - (re.getWidth() / 5)), yl, h);
261
                            } else {
262
                                drawNameLegend(g, layer.getName(), re,
263
                                    sizefont, (xl - (re.getWidth() / 5)), yl, h);
264
                            }
265
                            n[0]++;
266
                            String[] descriptions = cli.getDescriptions();
267
                            ISymbol[] symbols = cli.getSymbols();
268
                            for (int j = 0; j < descriptions.length; j++) {
269
                                dY = n[0] * h;
270

    
271
                                xl = (re.getX() + dX);
272
                                yl = (re.getY() + dY);
273

    
274
                                String s = descriptions[j];
275
                                if (layout != null) {
276
                                    toFFrameText(layout, s, re, sizefont, xl,
277
                                        yl, h);
278
                                } else {
279
                                    drawNameLegend(g, s, re, sizefont, xl, yl,
280
                                        h);
281
                                }
282
                                ISymbol fs2d = symbols[j];
283
                                if (layout != null) {
284
                                    try {
285
                                        toFFrameSymbol(layout, re, xl, yl,
286
                                            fs2d, sizefont, h,
287
                                            cO.getShapeType());
288
                                    } catch (ReadException e) {
289
                                        e.printStackTrace();
290
                                    }
291
                                } else {
292
                                    drawSymbolLegend(g, re, xl, yl, fs2d,
293
                                        sizefont, h);
294
                                }
295
                                n[0]++;
296
                            }
297
                        } else {
298
                            double dX = 0;
299
                            double dY = n[0] * h;
300

    
301
                            double xl = (re.getX() + dX);
302
                            double yl = (re.getY() + dY);
303
                            if (layout != null) {
304
                                toFFrameText(layout, layer.getName(), re,
305
                                    sizefont, xl, yl, h);
306
                            } else {
307
                                drawNameLegend(g, layer.getName(), re,
308
                                    sizefont, xl, yl, h);
309
                            }
310
                            // TO DO: CAMBIAR TO_DO ESTO PARA QUE ACEPTE ISYMBOL
311
                            // TODO: comprovar que no es trenca res
312
                            if (cO.getLegend() != null) {
313
                                ISymbol fs2d =
314
                                    cO.getLegend().getDefaultSymbol();
315

    
316
                                if (layout != null) {
317
                                    try {
318
                                        toFFrameSymbol(layout, re, xl, yl,
319
                                            fs2d, sizefont, h,
320
                                            cO.getShapeType());
321
                                    } catch (ReadException e) {
322
                                        e.printStackTrace();
323
                                    }
324
                                } else {
325
                                    drawSymbolLegend(g, re, xl, yl, fs2d,
326
                                        sizefont, h);
327
                                }
328
                            }
329
                            n[0]++;
330
                        }
331
                    } else
332
                        if (layer instanceof IHasImageLegend) {
333
                            Image image =
334
                                ((IHasImageLegend) layer).getImageLegend();
335
                            String path =
336
                                ((IHasImageLegend) layer).getPathImage();
337
                            if (image != null) {
338
                                FFramePicture picture =
339
                                    (FFramePicture) layoutManager
340
                                        .createFrame(FFramePicture.PERSISTENCE_DEFINITION_NAME);
341

    
342
                                // picture.setLayout(getLayout());
343
                                BufferedImage bi =
344
                                    new BufferedImage(image.getWidth(null),
345
                                        image.getHeight(null),
346
                                        BufferedImage.TYPE_INT_ARGB);
347
                                Graphics2D biContext = bi.createGraphics();
348
                                biContext.drawImage(image, 0, 0, null);
349
                                picture.setImage(bi);
350
                                double dY = n[0] * h;
351
                                Rectangle2D rectImage =
352
                                    new Rectangle2D.Double(re.getX(), re.getY()
353
                                        + dY, re.getWidth(), h);
354
                                if (layout != null) {
355
                                    picture
356
                                        .setBoundBox(FLayoutUtilities
357
                                            .toSheetRect(rectImage,
358
                                                layout.getAT()));
359
                                    picture.setPath(path);
360
                                    layout.addFFrame(picture, false, true);
361
                                } else {
362
                                    picture.setBoundBox(FLayoutUtilities
363
                                        .toSheetRect(rectImage,
364
                                            new AffineTransform()));
365
                                    picture.draw(g, new AffineTransform(), re,
366
                                        bi);
367
                                }
368
                            }
369
                            n[0]++;
370
                        } else {
371
                            double dX = 0;
372
                            double dY = n[0] * h;
373

    
374
                            double xl = (re.getX() + dX);
375
                            double yl = (re.getY() + dY);
376
                            if (layout != null) {
377
                                toFFrameText(layout, layer.getName(), re,
378
                                    sizefont, xl, yl, h);
379
                            } else {
380
                                drawNameLegend(g, layer.getName(), re,
381
                                    sizefont, xl, yl, h);
382
                            }
383
                            n[0]++;
384
                        }
385
            }
386
        }
387
    }
388

    
389
    /**
390
     * Dibuja sobre el graphics el nombre de la capa que se pasa como
391
     * par?metro.
392
     * 
393
     * @param g
394
     *            Graphics2D sobre el que dibujar.
395
     * @param name
396
     *            Nombre de la capa.
397
     * @param re
398
     *            Rect?ngulo a ocupar por el nombre.
399
     * @param sizefont
400
     *            tama?o de la fuente.
401
     * @param x
402
     *            Posici?n X.
403
     * @param y
404
     *            Posici?n Y.
405
     * @param h
406
     *            Altura.
407
     */
408
    private void drawNameLegend(Graphics2D g, String name, Rectangle2D re,
409
        float sizefont, double x, double y, double h) {
410
        Font f = getFont(sizefont);
411
        g.setFont(f);
412
        g.setColor(Color.black);
413
        if (name != null)
414
            g.drawString(name, (float) (x + (re.getWidth() / 4)),
415
                (float) (y + (h / 2)));
416
    }
417

    
418
    private Font getFont(float sizefont) {
419
        Font f = getFont();
420
        return new Font(f.getName(), f.getStyle(), (int) sizefont);
421
    }
422

    
423
    /**
424
     * A?ade al Layout un nuevo FFrameText a partir de los par?metros de
425
     * entrada.
426
     * 
427
     * @param layout
428
     *            Layout sobre el que se crea el nuevo FFrame.
429
     * @param name
430
     *            Texto.
431
     * @param sizefont
432
     *            tama?o de la fuente.
433
     * @param x
434
     *            Posici?n X.
435
     * @param y
436
     *            Posici?n Y.
437
     * @param h
438
     *            Altura.
439
     * @param wT
440
     *            Rect?ngulo del FFrame.
441
     * @param hT
442
     *            DOCUMENT ME!
443
     */
444
    private void toFFrameText(LayoutContext layout, String name,
445
        Rectangle2D re, float sizefont, double x, double y, double h) {
446
        // Font f = getFont(sizefont);
447
        Rectangle2D rAux =
448
            new Rectangle2D.Double(x + re.getWidth() / 4, ((y + (h / 2)) - h),
449
                re.getWidth() - re.getWidth() / 4, h);
450
        FFrameText text =
451
            (FFrameText) layoutManager
452
                .createFrame(FFrameText.PERSISTENCE_DEFINITION_NAME);
453

    
454
        // text.setLayout(getLayout());
455
        text.setFixedFontSize(true);
456
        // text.setFontSize((int) (sizefont*1.4));
457
        double myScale = layout.getAT().getScaleX() * 0.0234;
458
        text.setFontSize((int) (sizefont / myScale));
459
        text.setBoundBox(FLayoutUtilities.toSheetRect(rAux, layout.getAT()));
460
        text.addText(name);
461
        layout.addFFrame(text, false, true);
462
    }
463

    
464
    /**
465
     * Dibuja sobre el Graphics2D el s?mbolo.
466
     * 
467
     * @param g
468
     *            Graphics2D.
469
     * @param re
470
     *            Rect?ngulo a cubrir por el s?mbolo.
471
     * @param x
472
     *            Posici?n X.
473
     * @param y
474
     *            Posici?n Y.
475
     * @param symbol2d
476
     *            S?mbolo a dibujar.
477
     * @param sizefont
478
     *            Tama?o de la fuente.
479
     * @param h
480
     *            Altura.
481
     */
482
    private void drawSymbolLegend(Graphics2D g, Rectangle2D re, double x,
483
        double y, ISymbol symbol2d, float sizefont, double h) {
484
        double pW = 5;
485
        double wl = (re.getWidth() / pW);
486
        double haux = (sizefont * 0.7);
487
        Font font = null;
488

    
489
        if (symbol2d instanceof ITextSymbol
490
            && (font = ((ITextSymbol) symbol2d).getFont()) != null) {
491
            ((ITextSymbol) symbol2d).setFont(new Font(font.getFontName(), font
492
                .getStyle(), (int) (wl / 6)));
493
        }
494

    
495
        Rectangle rectangle =
496
            new Rectangle((int) x, (int) ((y + (h / 2)) - haux), (int) wl,
497
                (int) haux);
498
        try {
499
            symbol2d.drawInsideRectangle(g, new AffineTransform(), rectangle,
500
                properties);
501
        } catch (SymbolDrawingException e) {
502
            if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
503
                try {
504
                    mapContextManager
505
                        .getSymbolManager()
506
                        .getWarningSymbol(
507
                            SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
508
                            symbol2d.getDescription(),
509
                            SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
510
                        .drawInsideRectangle(g, null, rectangle, null);
511
                } catch (SymbolDrawingException e1) {
512
                    // IMPOSSIBLE TO REACH THIS
513
                }
514
            } else {
515
                // should be unreachable code
516
                throw new Error(PluginServices.getText(this,
517
                    "symbol_shapetype_mismatch"));
518
            }
519
        }
520
    }
521

    
522
    /**
523
     * A?ade al Layout un nuevo FFrameSymbol.
524
     * 
525
     * @param layout
526
     *            Layout sobe el que se a?ade el FFrame.
527
     * @param x
528
     *            Posici?n X.
529
     * @param y
530
     *            Posici?n Y.
531
     * @param fs2d
532
     *            S?mbolo a a?adir.
533
     * @param h
534
     *            Altura.
535
     * @param shapeType
536
     * @param wT
537
     *            Rect?ngulo del FFrame.
538
     * @param hT
539
     *            tama?o de la fuente.
540
     */
541
    private void toFFrameSymbol(LayoutContext layout, Rectangle2D re, double x,
542
        double y, ISymbol fs2d, float sizefont, double h, int shapeType) {
543
        double pW = 5;
544
        double wl = (re.getWidth() / pW);
545
        double haux = (sizefont * 0.7);
546
        Font font = null;
547
        if (fs2d instanceof ITextSymbol
548
            && (font = ((ITextSymbol) fs2d).getFont()) != null) {
549
            ((ITextSymbol) fs2d).setFont(new Font(font.getFontName(), font
550
                .getStyle(), (int) (wl / 6)));
551
        }
552

    
553
        Rectangle2D rAux3 =
554
            new Rectangle2D.Double(x, ((y + (h / 2)) - haux), wl, haux);
555
        FFrameSymbol symbol =
556
            (FFrameSymbol) layoutManager
557
                .createFrame(FFrameSymbol.PERSISTENCE_DEFINITION_NAME);
558

    
559
        // symbol.setLayout(getLayout());
560
        symbol.setBoundBox(FLayoutUtilities.toSheetRect(rAux3, layout.getAT()));
561
        symbol.setSymbol(fs2d);
562
        symbol.setShapeType(shapeType);
563
        layout.addFFrame(symbol, false, true);
564
    }
565

    
566
    /**
567
     * Devuelve el n?mero total de capas incluyendo las subcapas.
568
     * 
569
     * @param layers
570
     *            Capa a contar.
571
     * 
572
     * @return N?mero de capas y subcapas.
573
     */
574
    private int getSizeNum(FLayers layers) {
575
        int n = 0;
576

    
577
        // ///Aqu? hay que calcular cuantos layers y sublayers hay, para saber
578
        // que distancias dejar entre uno y otro.
579
        // /y adem?s el tama?o de cada uno de ellos para saber que anchura
580
        // dejar.
581
        int l = 0;
582
        for (int i = layers.getLayersCount() - 1; i >= 0; i--) {
583
            FLayer layer = layers.getLayer(i);
584
            boolean b = false;
585
            if (nameLayers.size() > l
586
                && nameLayers.get(l).equals(layer.getName())) {
587
                b = ((Boolean) areVisible.get(l)).booleanValue();
588
            } else {
589
                b = layer.isVisible();
590
            }
591
            l++;
592
            if (b) {
593
                // if (layer.isVisible()) {
594
                if (layer.getName().length() > m_max) {
595
                    m_max = layer.getName().length();
596
                }
597

    
598
                if (layer instanceof FLayers) {
599
                    // n++;
600
                    n = n + 3;
601
                    n += getSizeNum((FLayers) layer); // m_numLayers +=
602
                                                      // getNumInLyrGroup((FLayers)
603
                                                      // layer);
604
                } else {
605
                    if (layer instanceof Classifiable) {
606
                        Classifiable cO = (Classifiable) layer;
607
                        n++;
608

    
609
                        if (cO.getLegend() instanceof IClassifiedLegend) {// &&
610
                                                                          // !(cO
611
                                                                          // instanceof
612
                                                                          // FLyrAnnotation))
613
                                                                          // {
614
                            IClassifiedLegend cli =
615
                                (IClassifiedLegend) cO.getLegend();
616

    
617
                            for (int j = 0; j < cli.getValues().length; j++) {
618
                                String s = cli.getDescriptions()[j];
619

    
620
                                if (s != null && s.length() > m_max) {
621
                                    m_max = s.length();
622
                                }
623

    
624
                                n++;
625
                            }
626
                        }
627
                    } else {
628
                        String s = layer.getName();
629

    
630
                        if (s != null && s.length() > m_max) {
631
                            m_max = s.length();
632
                        }
633

    
634
                        n++;
635
                    }
636

    
637
                }
638
            }
639
        }
640

    
641
        return n;
642
    }
643

    
644
    /**
645
     * Transforma el FFrameLegend en diferentes FFrameSymbol y FFrameText.
646
     * 
647
     * @param layout
648
     *            Layout sobre el que a?adir los FFrame nuevos y sobre el
649
     *            que elimnar el FFrameLegend anterior.
650
     */
651
    public void toFFrames(LayoutContext layout) {
652
        Rectangle2D rectangle = getBoundingBox(null);
653
        // Rectangle2D r = getBoundBox();
654
        double h = rectangle.getHeight() / m_numLayers;
655
        FLayers lays = layers;
656

    
657
        // layout.getEFS().startComplexCommand();
658
        // toFFrames(layout, lays, rectangle, r.getWidth(), r.getHeight(), h,
659
        // 0);
660
        int[] n = new int[1];
661
        n[0] = 0;
662
        drawLegendOrToFFrame(null, rectangle, h, lays, n, layout);
663
        layout.delFFrame(this);
664

    
665
        // /layout.getFFrames().remove(this);
666
        // layout.getEFS().endComplexCommand();
667
    }
668

    
669
    /**
670
     * Rellena la fuente a utilizar al dibujar los String sobre el graphics.
671
     * 
672
     * @param f
673
     *            Font.
674
     */
675
    public void setFont(Font f) {
676
        m_font = f;
677
    }
678

    
679
    /**
680
     * Devuelve la fuente que esta utilizando.
681
     * 
682
     * @return Font.
683
     */
684
    public Font getFont() {
685
        if (m_font != null) {
686
            return new Font(m_font.getName(), m_font.getStyle(), 9);
687
            // return new Font(m_f.getFontName(), m_f.getStyle(), 9);
688
        }
689
        return new Font("SansSerif", Font.PLAIN, 9);
690
    }
691

    
692
    /*
693
     * @see
694
     * com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#getNameFFrame
695
     * ()
696
     */
697
    public String getNameFFrame() {
698
        return PluginServices.getText(this, "leyenda") + num;
699
    }
700

    
701
    public String getName() {
702
        return PERSISTENCE_DEFINITION_NAME;
703
    }
704

    
705
    /*
706
     * @see
707
     * com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#print(java
708
     * .awt.Graphics2D,
709
     * java.awt.geom.AffineTransform)
710
     */
711
    public void print(Graphics2D g, AffineTransform at, Geometry geom,
712
        PrintAttributes properties) {
713
        this.properties = properties;
714
        draw(g, at, null, null);
715
        this.properties = null;
716
    }
717

    
718
    public void initialize() {
719
        // TODO Auto-generated method stub
720

    
721
    }
722

    
723
    public void setNameLayers(List nameLayers) {
724
        this.nameLayers = nameLayers;
725
    }
726

    
727
    public void setAreVisible(List areVisible) {
728
        this.areVisible = areVisible;
729
    }
730

    
731
    public List getNameLayers() {
732
        return nameLayers;
733
    }
734

    
735
    public List getAreVisible() {
736
        return areVisible;
737
    }
738

    
739
    public static void registerPersistent() {
740
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
741
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
742
            DynStruct definition =
743
                manager.addDefinition(FFrameLegend.class,
744
                    PERSISTENCE_DEFINITION_NAME,
745
                    "FFrameLegend persistence definition", null, null);
746

    
747
            definition
748
                .extend(manager
749
                    .getDefinition(AbstractFFrameViewDependence.PERSISTENCE_DEFINITION_NAME));
750

    
751
            definition.addDynFieldInt(NUMLAYERS_FIELD).setMandatory(true);
752
            definition.addDynFieldInt(MAX_FIELD).setMandatory(true);
753
            definition.addDynFieldInt(QUALITY_FIELD).setMandatory(true);
754
            definition.addDynFieldInt(VIEWING_FIELD).setMandatory(true);
755
            definition.addDynFieldObject(FONT_FIELD)
756
                .setClassOfValue(Font.class).setMandatory(true);
757
            definition.addDynFieldList(NAMELAYERS_FIELD)
758
                .setClassOfItems(String.class).setMandatory(true);
759
            definition.addDynFieldList(AREVISIBLE_FIELD)
760
                .setClassOfItems(Boolean.class).setMandatory(true);
761
        }
762
    }
763

    
764
    @Override
765
    public void loadFromState(PersistentState state)
766
        throws PersistenceException {
767
        super.loadFromState(state);
768
        m_numLayers = state.getInt(NUMLAYERS_FIELD);
769
        m_max = state.getInt(MAX_FIELD);
770
        m_quality = state.getInt(QUALITY_FIELD);
771
        m_viewing = state.getInt(VIEWING_FIELD);
772
        m_font = (Font) state.get(FONT_FIELD);
773
        nameLayers = (List<String>) state.getList(NAMELAYERS_FIELD);
774
        areVisible = (List<Boolean>) state.getList(AREVISIBLE_FIELD);
775
    }
776

    
777
    @Override
778
    public void saveToState(PersistentState state) throws PersistenceException {
779
        super.saveToState(state);
780
        state.set(NUMLAYERS_FIELD, m_numLayers);
781
        state.set(MAX_FIELD, m_max);
782
        state.set(QUALITY_FIELD, m_quality);
783
        state.set(VIEWING_FIELD, m_viewing);
784
        state.set(FONT_FIELD, m_font);
785
        state.set(NAMELAYERS_FIELD, nameLayers);
786
        state.set(AREVISIBLE_FIELD, areVisible);
787
    }
788
}