Statistics
| Revision:

svn-document-layout / tags / 2059 / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / fframes / FFrameLegend.java @ 46

History | View | Annotate | Download (30.5 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.legend.ILegend;
49
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
50
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
51
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
52
import org.gvsig.tools.ToolsLocator;
53
import org.gvsig.tools.dynobject.DynStruct;
54
import org.gvsig.tools.persistence.PersistenceManager;
55
import org.gvsig.tools.persistence.PersistentState;
56
import org.gvsig.tools.persistence.exception.PersistenceException;
57

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

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

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

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

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

    
91
    public FFrameLegend() {
92

    
93
    }
94

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

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

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

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

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

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

    
158
        m_max = 0;
159
        m_numLayers = 0;
160

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

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

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

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

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

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

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

    
228
                    double dX = 0;
229
                    double dY = n[0] * h;
230
                    double xl = (re.getX() + dX);
231
                    double yl = (re.getY() + dY);
232
                    if (layoutContext != null) {
233
                        toFFrameText(layoutContext, layer.getName(), re, sizefont,
234
                            (xl - (re.getWidth() / 5)), yl, h);
235
                    } else {
236
                        drawNameLegend(g, layer.getName(), re, sizefont,
237
                            (xl - (re.getWidth() / 5)), yl, h);
238
                    }
239
                    n[0]++;
240
                    drawLegendOrToFFrame(g, re, h, (FLayers) layer, n, layoutContext);
241
                    n[0]++;
242
                } else {
243
                    
244
                    if (layer instanceof Classifiable) {
245
                        Classifiable cla_layer = (Classifiable) layer;
246
                        ILegend cla_legend = cla_layer.getLegend();
247
                        
248
                        if (cla_legend instanceof IHasImageLegend) {
249
                            
250
                            // =============================================
251
                            // classifiable AND image legend
252

    
253
                            Image image =
254
                                ((IHasImageLegend) cla_legend).getImageLegend();
255
                            String path =
256
                                ((IHasImageLegend) cla_legend).getPathImage();
257
                            
258
                            if (image != null) {
259
                                FFramePicture picture =
260
                                    (FFramePicture) layoutManager
261
                                        .createFrame(FFramePicture.PERSISTENCE_DEFINITION_NAME);
262

    
263
                                BufferedImage bi =
264
                                    new BufferedImage(image.getWidth(null),
265
                                        image.getHeight(null),
266
                                        BufferedImage.TYPE_INT_ARGB);
267
                                Graphics2D biContext = bi.createGraphics();
268
                                biContext.drawImage(image, 0, 0, null);
269
                                picture.setImage(bi);
270
                                double dY = n[0] * h;
271
                                Rectangle2D rectImage =
272
                                    new Rectangle2D.Double(re.getX(), re.getY()
273
                                        + dY, re.getWidth(), h);
274
                                if (path != null && layoutContext != null) {
275
                                    picture
276
                                        .setBoundBox(FLayoutUtilities
277
                                            .toSheetRect(rectImage,
278
                                                layoutContext.getAT()));
279
                                    picture.setPath(path);
280
                                    layoutContext.addFFrame(picture, false, true);
281
                                } else {
282
                                    picture.setBoundBox(FLayoutUtilities
283
                                        .toSheetRect(rectImage,
284
                                            new AffineTransform()));
285
                                    picture.draw(g, new AffineTransform(), re,
286
                                        bi);
287
                                }
288
                            } else {
289
                                // ========================
290
                                // no image was returned
291
                                double dX = 0;
292
                                double dY = n[0] * h;
293
                                double xl = (re.getX() + dX);
294
                                double yl = (re.getY() + dY);
295
                                if (layoutContext != null) {
296
                                    toFFrameText(layoutContext, layer.getName(), re, sizefont, xl, yl, h);
297
                                } else {
298
                                    drawNameLegend(g, layer.getName(), re, sizefont, xl, yl, h);
299
                                }
300
                                // ========================
301
                            }
302
                            n[0]++;
303
                            
304
                            // classifiable AND image legend (end)
305
                            // =============================================
306

    
307
                        } else {
308
                            
309
                            // =============================================
310
                            // classifiable and NOT image legend
311

    
312
                            if (cla_legend instanceof IClassifiedLegend) {
313

    
314
                                IClassifiedLegend cli = (IClassifiedLegend) cla_legend;
315
                                double dX = 0;
316
                                double dY = n[0] * h;
317

    
318
                                double xl = (re.getX() + dX);
319
                                double yl = (re.getY() + dY);
320
                                if (layoutContext != null) {
321
                                    toFFrameText(layoutContext, layer.getName(), re,
322
                                        sizefont, (xl - (re.getWidth() / 5)), yl, h);
323
                                } else {
324
                                    drawNameLegend(g, layer.getName(), re,
325
                                        sizefont, (xl - (re.getWidth() / 5)), yl, h);
326
                                }
327
                                n[0]++;
328
                                String[] descriptions = cli.getDescriptions();
329
                                ISymbol[] symbols = cli.getSymbols();
330
                                for (int j = 0; j < descriptions.length; j++) {
331
                                    dY = n[0] * h;
332

    
333
                                    xl = (re.getX() + dX);
334
                                    yl = (re.getY() + dY);
335

    
336
                                    String s = descriptions[j];
337
                                    if (layoutContext != null) {
338
                                        toFFrameText(layoutContext, s, re, sizefont, xl,
339
                                            yl, h);
340
                                    } else {
341
                                        drawNameLegend(g, s, re, sizefont, xl, yl,
342
                                            h);
343
                                    }
344
                                    ISymbol fs2d = symbols[j];
345
                                    if (layoutContext != null) {
346
                                        try {
347
                                            toFFrameSymbol(layoutContext, re, xl, yl,
348
                                                fs2d, sizefont, h,
349
                                                cla_layer.getShapeType());
350
                                        } catch (ReadException e) {
351
                                            e.printStackTrace();
352
                                        }
353
                                    } else {
354
                                        drawSymbolLegend(g, re, xl, yl, fs2d,
355
                                            sizefont, h);
356
                                    }
357
                                    n[0]++;
358
                                }
359
                            } else {
360
                                double dX = 0;
361
                                double dY = n[0] * h;
362

    
363
                                double xl = (re.getX() + dX);
364
                                double yl = (re.getY() + dY);
365
                                if (layoutContext != null) {
366
                                    toFFrameText(layoutContext, layer.getName(), re,
367
                                        sizefont, xl, yl, h);
368
                                } else {
369
                                    drawNameLegend(g, layer.getName(), re,
370
                                        sizefont, xl, yl, h);
371
                                }
372
                                // TO DO: CAMBIAR TO_DO ESTO PARA QUE ACEPTE ISYMBOL
373
                                // TODO: comprovar que no es trenca res
374
                                if (cla_legend != null) {
375
                                    ISymbol fs2d = cla_legend.getDefaultSymbol();
376

    
377
                                    if (layoutContext != null) {
378
                                        try {
379
                                            toFFrameSymbol(layoutContext, re, xl, yl,
380
                                                fs2d, sizefont, h,
381
                                                cla_layer.getShapeType());
382
                                        } catch (ReadException e) {
383
                                            e.printStackTrace();
384
                                        }
385
                                    } else {
386
                                        drawSymbolLegend(g, re, xl, yl, fs2d,
387
                                            sizefont, h);
388
                                    }
389
                                }
390
                                n[0]++;
391
                            }
392

    
393
                            // classifiable and NOT image legend (end)
394
                            // =============================================
395

    
396
                        }
397
                        
398
                    } else {
399
                        // ===================================
400
                        // NOT classifiable
401
                        double dX = 0;
402
                        double dY = n[0] * h;
403
                        double xl = (re.getX() + dX);
404
                        double yl = (re.getY() + dY);
405
                        if (layoutContext != null) {
406
                            toFFrameText(layoutContext, layer.getName(), re, sizefont, xl, yl, h);
407
                        } else {
408
                            drawNameLegend(g, layer.getName(), re, sizefont, xl, yl, h);
409
                        }
410
                        n[0]++;
411
                        // NOT classifiable (end)
412
                        // ===================================
413
                    }
414
                }
415
            }
416
        }
417
    }
418

    
419
    /**
420
     * Dibuja sobre el graphics el nombre de la capa que se pasa como
421
     * par?metro.
422
     * 
423
     * @param g
424
     *            Graphics2D sobre el que dibujar.
425
     * @param name
426
     *            Nombre de la capa.
427
     * @param re
428
     *            Rect?ngulo a ocupar por el nombre.
429
     * @param sizefont
430
     *            tama?o de la fuente.
431
     * @param x
432
     *            Posici?n X.
433
     * @param y
434
     *            Posici?n Y.
435
     * @param h
436
     *            Altura.
437
     */
438
    private void drawNameLegend(Graphics2D g, String name, Rectangle2D re,
439
        float sizefont, double x, double y, double h) {
440
        Font f = getFont(sizefont);
441
        g.setFont(f);
442
        g.setColor(Color.black);
443
        if (name != null)
444
            g.drawString(name, (float) (x + (re.getWidth() / 4)),
445
                (float) (y + (h / 2)));
446
    }
447

    
448
    private Font getFont(float sizefont) {
449
        Font f = getFont();
450
        return new Font(f.getName(), f.getStyle(), (int) sizefont);
451
    }
452

    
453
    /**
454
     * A?ade al Layout un nuevo FFrameText a partir de los par?metros de
455
     * entrada.
456
     * 
457
     * @param layoutContext
458
     *            Layout sobre el que se crea el nuevo FFrame.
459
     * @param name
460
     *            Texto.
461
     * @param sizefont
462
     *            tama?o de la fuente.
463
     * @param x
464
     *            Posici?n X.
465
     * @param y
466
     *            Posici?n Y.
467
     * @param h
468
     *            Altura.
469
     * @param wT
470
     *            Rect?ngulo del FFrame.
471
     * @param hT
472
     *            DOCUMENT ME!
473
     */
474
    private void toFFrameText(LayoutContext layoutContext, String name,
475
        Rectangle2D re, float sizefont, double x, double y, double h) {
476
        // Font f = getFont(sizefont);
477
        Rectangle2D rAux =
478
            new Rectangle2D.Double(x + re.getWidth() / 4, ((y + (h / 2)) - h),
479
                re.getWidth() - re.getWidth() / 4, h);
480
        FFrameText text =
481
            (FFrameText) layoutManager
482
                .createFrame(FFrameText.PERSISTENCE_DEFINITION_NAME);
483

    
484
        text.setFixedFontSize(true);
485
        double myScale = layoutContext.getAT().getScaleX() * 0.0234;
486
        text.setFontSize((int) (sizefont / myScale));
487
        text.setBoundBox(FLayoutUtilities.toSheetRect(rAux, layoutContext.getAT()));
488
        text.addText(name);
489
        layoutContext.addFFrame(text, false, true);
490
    }
491

    
492
    /**
493
     * Dibuja sobre el Graphics2D el s?mbolo.
494
     * 
495
     * @param g
496
     *            Graphics2D.
497
     * @param re
498
     *            Rect?ngulo a cubrir por el s?mbolo.
499
     * @param x
500
     *            Posici?n X.
501
     * @param y
502
     *            Posici?n Y.
503
     * @param symbol2d
504
     *            S?mbolo a dibujar.
505
     * @param sizefont
506
     *            Tama?o de la fuente.
507
     * @param h
508
     *            Altura.
509
     */
510
    private void drawSymbolLegend(Graphics2D g, Rectangle2D re, double x,
511
        double y, ISymbol symbol2d, float sizefont, double h) {
512
        double pW = 5;
513
        double wl = (re.getWidth() / pW);
514
        double haux = (sizefont * 0.7);
515
        Font font = null;
516

    
517
        if (symbol2d instanceof ITextSymbol
518
            && (font = ((ITextSymbol) symbol2d).getFont()) != null) {
519
            ((ITextSymbol) symbol2d).setFont(new Font(font.getFontName(), font
520
                .getStyle(), (int) (wl / 6)));
521
        }
522

    
523
        Rectangle rectangle =
524
            new Rectangle((int) x, (int) ((y + (h / 2)) - haux), (int) wl,
525
                (int) haux);
526
        try {
527
            symbol2d.drawInsideRectangle(g, new AffineTransform(), rectangle,
528
                properties);
529
        } catch (SymbolDrawingException e) {
530
            if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
531
                try {
532
                    mapContextManager
533
                        .getSymbolManager()
534
                        .getWarningSymbol(
535
                            SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
536
                            symbol2d.getDescription(),
537
                            SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
538
                        .drawInsideRectangle(g, null, rectangle, null);
539
                } catch (SymbolDrawingException e1) {
540
                    // IMPOSSIBLE TO REACH THIS
541
                }
542
            } else {
543
                // should be unreachable code
544
                throw new Error(PluginServices.getText(this,
545
                    "symbol_shapetype_mismatch"));
546
            }
547
        }
548
    }
549

    
550
    /**
551
     * A?ade al Layout un nuevo FFrameSymbol.
552
     * 
553
     * @param layoutContext
554
     *            Layout sobe el que se a?ade el FFrame.
555
     * @param x
556
     *            Posici?n X.
557
     * @param y
558
     *            Posici?n Y.
559
     * @param fs2d
560
     *            S?mbolo a a?adir.
561
     * @param h
562
     *            Altura.
563
     * @param shapeType
564
     * @param wT
565
     *            Rect?ngulo del FFrame.
566
     * @param hT
567
     *            tama?o de la fuente.
568
     */
569
    private void toFFrameSymbol(LayoutContext layoutContext, Rectangle2D re, double x,
570
        double y, ISymbol fs2d, float sizefont, double h, int shapeType) {
571
        double pW = 5;
572
        double wl = (re.getWidth() / pW);
573
        double haux = (sizefont * 0.7);
574
        Font font = null;
575
        if (fs2d instanceof ITextSymbol
576
            && (font = ((ITextSymbol) fs2d).getFont()) != null) {
577
            ((ITextSymbol) fs2d).setFont(new Font(font.getFontName(), font
578
                .getStyle(), (int) (wl / 6)));
579
        }
580

    
581
        Rectangle2D rAux3 =
582
            new Rectangle2D.Double(x, ((y + (h / 2)) - haux), wl, haux);
583
        FFrameSymbol symbol =
584
            (FFrameSymbol) layoutManager
585
                .createFrame(FFrameSymbol.PERSISTENCE_DEFINITION_NAME);
586

    
587
        // symbol.setLayout(getLayout());
588
        symbol.setBoundBox(FLayoutUtilities.toSheetRect(rAux3, layoutContext.getAT()));
589
        symbol.setSymbol(fs2d);
590
        symbol.setShapeType(shapeType);
591
        layoutContext.addFFrame(symbol, false, true);
592
    }
593

    
594
    /**
595
     * Devuelve el n?mero total de capas incluyendo las subcapas.
596
     * 
597
     * @param layers
598
     *            Capa a contar.
599
     * 
600
     * @return N?mero de capas y subcapas.
601
     */
602
    private int getSizeNum(FLayers layers) {
603
        int n = 0;
604

    
605
        // ///Aqu? hay que calcular cuantos layers y sublayers hay, para saber
606
        // que distancias dejar entre uno y otro.
607
        // /y adem?s el tama?o de cada uno de ellos para saber que anchura
608
        // dejar.
609
        int l = 0;
610
        for (int i = layers.getLayersCount() - 1; i >= 0; i--) {
611
            FLayer layer = layers.getLayer(i);
612
            boolean b = false;
613
            if (nameLayers.size() > l
614
                && nameLayers.get(l).equals(layer.getName())) {
615
                b = ((Boolean) areVisible.get(l)).booleanValue();
616
            } else {
617
                b = layer.isVisible();
618
            }
619
            l++;
620
            if (b) {
621
                // if (layer.isVisible()) {
622
                if (layer.getName().length() > m_max) {
623
                    m_max = layer.getName().length();
624
                }
625

    
626
                if (layer instanceof FLayers) {
627
                    // n++;
628
                    n = n + 3;
629
                    n += getSizeNum((FLayers) layer); // m_numLayers +=
630
                                                      // getNumInLyrGroup((FLayers)
631
                                                      // layer);
632
                } else {
633
                    if (layer instanceof Classifiable) {
634
                        Classifiable cO = (Classifiable) layer;
635
                        n++;
636

    
637
                        if (cO.getLegend() instanceof IClassifiedLegend) {// &&
638
                                                                          // !(cO
639
                                                                          // instanceof
640
                                                                          // FLyrAnnotation))
641
                                                                          // {
642
                            IClassifiedLegend cli =
643
                                (IClassifiedLegend) cO.getLegend();
644

    
645
                            for (int j = 0; j < cli.getValues().length; j++) {
646
                                String s = cli.getDescriptions()[j];
647

    
648
                                if (s != null && s.length() > m_max) {
649
                                    m_max = s.length();
650
                                }
651

    
652
                                n++;
653
                            }
654
                        }
655
                    } else {
656
                        String s = layer.getName();
657

    
658
                        if (s != null && s.length() > m_max) {
659
                            m_max = s.length();
660
                        }
661

    
662
                        n++;
663
                    }
664

    
665
                }
666
            }
667
        }
668

    
669
        return n;
670
    }
671

    
672
    /**
673
     * Transforma el FFrameLegend en diferentes FFrameSymbol y FFrameText.
674
     * 
675
     * @param layout
676
     *            Layout sobre el que a?adir los FFrame nuevos y sobre el
677
     *            que elimnar el FFrameLegend anterior.
678
     */
679
    public void toFFrames(LayoutContext layout) {
680
        Rectangle2D rectangle = getBoundingBox(null);
681
        double h = rectangle.getHeight() / m_numLayers;
682
        FLayers lays = layers;
683

    
684
        // layout.getEFS().startComplexCommand();
685
        // toFFrames(layout, lays, rectangle, r.getWidth(), r.getHeight(), h,
686
        // 0);
687
        int[] n = new int[1];
688
        n[0] = 0;
689
        drawLegendOrToFFrame(null, rectangle, h, lays, n, layout);
690
        layout.delFFrame(this);
691

    
692
        // /layout.getFFrames().remove(this);
693
        // layout.getEFS().endComplexCommand();
694
    }
695

    
696
    /**
697
     * Rellena la fuente a utilizar al dibujar los String sobre el graphics.
698
     * 
699
     * @param f
700
     *            Font.
701
     */
702
    public void setFont(Font f) {
703
        m_font = f;
704
    }
705

    
706
    /**
707
     * Devuelve la fuente que esta utilizando.
708
     * 
709
     * @return Font.
710
     */
711
    public Font getFont() {
712
        if (m_font != null) {
713
            return new Font(m_font.getName(), m_font.getStyle(), 9);
714
        }
715
        return new Font("SansSerif", Font.PLAIN, 9);
716
    }
717

    
718
    public String getNameFFrame() {
719
        return PluginServices.getText(this, "leyenda") + num;
720
    }
721

    
722
    public String getName() {
723
        return PERSISTENCE_DEFINITION_NAME;
724
    }
725

    
726
    public void print(Graphics2D g, AffineTransform at, Geometry geom,
727
        PrintAttributes properties) {
728
        this.properties = properties;
729
        draw(g, at, null, null);
730
        this.properties = null;
731
    }
732

    
733
    public void initialize() {
734

    
735
    }
736

    
737
    public void setNameLayers(List nameLayers) {
738
        this.nameLayers = nameLayers;
739
    }
740

    
741
    public void setAreVisible(List areVisible) {
742
        this.areVisible = areVisible;
743
    }
744

    
745
    public List getNameLayers() {
746
        return nameLayers;
747
    }
748

    
749
    public List getAreVisible() {
750
        return areVisible;
751
    }
752

    
753
    public static void registerPersistent() {
754
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
755
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
756
            DynStruct definition =
757
                manager.addDefinition(FFrameLegend.class,
758
                    PERSISTENCE_DEFINITION_NAME,
759
                    "FFrameLegend persistence definition", null, null);
760

    
761
            definition
762
                .extend(manager
763
                    .getDefinition(AbstractFFrameViewDependence.PERSISTENCE_DEFINITION_NAME));
764

    
765
            definition.addDynFieldInt(NUMLAYERS_FIELD).setMandatory(true);
766
            definition.addDynFieldInt(MAX_FIELD).setMandatory(true);
767
            definition.addDynFieldInt(QUALITY_FIELD).setMandatory(true);
768
            definition.addDynFieldInt(VIEWING_FIELD).setMandatory(true);
769
            definition.addDynFieldObject(FONT_FIELD)
770
                .setClassOfValue(Font.class).setMandatory(true);
771
            definition.addDynFieldList(NAMELAYERS_FIELD)
772
                .setClassOfItems(String.class).setMandatory(true);
773
            definition.addDynFieldList(AREVISIBLE_FIELD)
774
                .setClassOfItems(Boolean.class).setMandatory(true);
775
        }
776
    }
777

    
778
    @Override
779
    public void loadFromState(PersistentState state)
780
        throws PersistenceException {
781
        super.loadFromState(state);
782
        m_numLayers = state.getInt(NUMLAYERS_FIELD);
783
        m_max = state.getInt(MAX_FIELD);
784
        m_quality = state.getInt(QUALITY_FIELD);
785
        m_viewing = state.getInt(VIEWING_FIELD);
786
        m_font = (Font) state.get(FONT_FIELD);
787
        nameLayers = (List<String>) state.getList(NAMELAYERS_FIELD);
788
        areVisible = (List<Boolean>) state.getList(AREVISIBLE_FIELD);
789
    }
790

    
791
    @Override
792
    public void saveToState(PersistentState state) throws PersistenceException {
793
        super.saveToState(state);
794
        state.set(NUMLAYERS_FIELD, m_numLayers);
795
        state.set(MAX_FIELD, m_max);
796
        state.set(QUALITY_FIELD, m_quality);
797
        state.set(VIEWING_FIELD, m_viewing);
798
        state.set(FONT_FIELD, m_font);
799
        state.set(NAMELAYERS_FIELD, nameLayers);
800
        state.set(AREVISIBLE_FIELD, areVisible);
801
    }
802
}