Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / fframes / FFrameLegend.java @ 1634

History | View | Annotate | Download (32.1 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
import org.gvsig.andami.PluginServices;
35
import org.gvsig.app.project.documents.layout.FLayoutUtilities;
36
import org.gvsig.app.project.documents.layout.LayoutContext;
37
import org.gvsig.compat.print.PrintAttributes;
38
import org.gvsig.fmap.dal.exception.ReadException;
39
import org.gvsig.fmap.geom.Geometry;
40
import org.gvsig.fmap.mapcontext.MapContextLocator;
41
import org.gvsig.fmap.mapcontext.MapContextManager;
42
import org.gvsig.fmap.mapcontext.ViewPort;
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.layers.vectorial.FLyrVect;
48
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedLegend;
49
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
50
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
51
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
52
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol;
53
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
54
import org.gvsig.tools.ToolsLocator;
55
import org.gvsig.tools.dynobject.DynStruct;
56
import org.gvsig.tools.persistence.PersistenceManager;
57
import org.gvsig.tools.persistence.PersistentState;
58
import org.gvsig.tools.persistence.exception.PersistenceException;
59

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

    
68
    public static final String PERSISTENCE_DEFINITION_NAME = "FFrameLegend";
69

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

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

    
90
    private final MapContextManager mapContextManager = MapContextLocator
91
        .getMapContextManager();
92

    
93
    public FFrameLegend() {
94

    
95
    }
96

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

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

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

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

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

    
156
        if ((fframeViewDependence != null)
157
            && (fframeViewDependence.getMapContext() != null)) {
158
            layers = fframeViewDependence.getMapContext().getLayers();
159
        }
160

    
161
        m_max = 0;
162
        m_numLayers = 0;
163

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

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

    
183
        g.rotate(Math.toRadians(-getRotation()), re.x + (re.width / 2), re.y
184
            + (re.height / 2));
185
    }
186

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

    
207
        if ((re.getHeight() / m_numLayers) < (re.getWidth() / (m_max * 0.7))) {
208
            sizefont = (float) (re.getHeight() / m_numLayers);
209
        } else {
210
            sizefont = (float) (re.getWidth() / (m_max * 0.7));
211
        }
212
        
213
        ViewPort viewPort = layers.getMapContext().getViewPort();
214

    
215
        int l = 0;
216
        // ////Se recorren los layers dibujando el s?mbolo y su nombre sobre el
217
        // graphics
218
        for (int i = layers.getLayersCount() - 1; i >= 0; i--) {
219
            FLayer layer = layers.getLayer(i);
220
            boolean b;// = false;
221

    
222
            if (nameLayers.size() > l
223
                && nameLayers.get(l).equals(layer.getName())) {
224
                b = (areVisible.get(l));
225
            } else {
226
                b = layer.isVisible();
227
            }
228
            l++;
229
            if (b) {
230
                if (layer instanceof FLayers) {
231
                    n[0]++;
232

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

    
258
                            Image image =
259
                                ((IHasImageLegend) cla_legend).getImageLegend();
260
                            String path =
261
                                ((IHasImageLegend) cla_legend).getPathImage();
262
                            
263
                            if (image != null) {
264
                                FFramePicture picture =
265
                                    (FFramePicture) layoutManager
266
                                        .createFrame(FFramePicture.PERSISTENCE_DEFINITION_NAME);
267

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

    
312
                        } else {
313
                            
314
                            // =============================================
315
                            // classifiable and NOT image legend
316

    
317
                            if (cla_legend instanceof IClassifiedLegend) {
318

    
319
                                IClassifiedLegend cli = (IClassifiedLegend) cla_legend;
320
                                double dX = 0;
321
                                double dY = n[0] * h;
322

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

    
338
                                    xl = (re.getX() + dX);
339
                                    yl = (re.getY() + dY);
340

    
341
                                    String desc = descriptions[j];
342
                                    if (layoutContext != null) {
343
                                        toFFrameText(layoutContext, desc, re, sizefont, xl,
344
                                            yl, h);
345
                                    } else {
346
                                        drawNameLegend(g, desc, re, sizefont, xl, yl,
347
                                            h);
348
                                    }
349
                                    ISymbol fs2d = symbols[j];
350
                                    if (layoutContext != null) {
351
                                        try {
352
                                            toFFrameSymbol(layoutContext, re, xl, yl,
353
                                                fs2d, sizefont, h,
354
                                                cla_layer.getShapeType());
355
                                        } catch (ReadException ex) {
356
                                            ex.printStackTrace();
357
                                        }
358
                                    } else {
359
                                        if(fs2d instanceof CartographicSupport){
360
                                            Geometry geom = null;
361
                                            if(layer instanceof FLyrVect){
362
                                                geom = ((FLyrVect) layer).getFeatureStore().getSampleFeature().getDefaultGeometry();
363
                                            }
364
                                            ((CartographicSupport) fs2d).setCartographicContext(viewPort, properties, geom);
365
                                        }
366
                                        drawSymbolLegend(g, re, xl, yl, fs2d,
367
                                            sizefont, h);
368
                                    }
369
                                    n[0]++;
370
                                }
371
                            } else {
372
                                double dX = 0;
373
                                double dY = n[0] * h;
374

    
375
                                double xl = (re.getX() + dX);
376
                                double yl = (re.getY() + dY);
377
                                if (layoutContext != null) {
378
                                    toFFrameText(layoutContext, layer.getName(), re,
379
                                        sizefont, xl, yl, h);
380
                                } else {
381
                                    drawNameLegend(g, layer.getName(), re,
382
                                        sizefont, xl, yl, h);
383
                                }
384
                                // TO DO: CAMBIAR TO_DO ESTO PARA QUE ACEPTE ISYMBOL
385
                                // TODO: comprovar que no es trenca res
386
                                if (cla_legend != null) {
387
                                    ISymbol fs2d = cla_legend.getDefaultSymbol();
388

    
389
                                    if (layoutContext != null) {
390
                                        try {
391
                                            toFFrameSymbol(layoutContext, re, xl, yl,
392
                                                fs2d, sizefont, h,
393
                                                cla_layer.getShapeType());
394
                                        } catch (ReadException e) {
395
                                            e.printStackTrace();
396
                                        }
397
                                    } else {
398
                                        if(fs2d instanceof CartographicSupport){
399
                                            Geometry geom = null;
400
                                            if(layer instanceof FLyrVect){
401
                                                geom = ((FLyrVect) layer).getFeatureStore().getSampleFeature().getDefaultGeometry();
402
                                            }
403
                                            ((CartographicSupport) fs2d).setCartographicContext(viewPort, properties, geom);
404
                                        }
405
                                        drawSymbolLegend(g, re, xl, yl, fs2d,
406
                                            sizefont, h);
407
                                    }
408
                                }
409
                                n[0]++;
410
                            }
411

    
412
                            // classifiable and NOT image legend (end)
413
                            // =============================================
414

    
415
                        }
416
                        
417
                    } else {
418
                        // ===================================
419
                        // NOT classifiable
420
                        double dX = 0;
421
                        double dY = n[0] * h;
422
                        double xl = (re.getX() + dX);
423
                        double yl = (re.getY() + dY);
424
                        if (layoutContext != null) {
425
                            toFFrameText(layoutContext, layer.getName(), re, sizefont, xl, yl, h);
426
                        } else {
427
                            drawNameLegend(g, layer.getName(), re, sizefont, xl, yl, h);
428
                        }
429
                        n[0]++;
430
                        // NOT classifiable (end)
431
                        // ===================================
432
                    }
433
                }
434
            }
435
        }
436
    }
437

    
438
    /**
439
     * Dibuja sobre el graphics el nombre de la capa que se pasa como
440
     * par?metro.
441
     * 
442
     * @param g
443
     *            Graphics2D sobre el que dibujar.
444
     * @param name
445
     *            Nombre de la capa.
446
     * @param re
447
     *            Rect?ngulo a ocupar por el nombre.
448
     * @param sizefont
449
     *            tama?o de la fuente.
450
     * @param x
451
     *            Posici?n X.
452
     * @param y
453
     *            Posici?n Y.
454
     * @param h
455
     *            Altura.
456
     */
457
    private void drawNameLegend(Graphics2D g, String name, Rectangle2D re,
458
        float sizefont, double x, double y, double h) {
459
        Font f = getFont(sizefont);
460
        g.setFont(f);
461
        g.setColor(Color.black);
462
        if (name != null)
463
            g.drawString(name, (float) (x + (re.getWidth() / 4)),
464
                (float) (y + (h / 2)));
465
    }
466

    
467
    private Font getFont(float sizefont) {
468
        Font f = getFont();
469
        return new Font(f.getName(), f.getStyle(), (int) sizefont);
470
    }
471

    
472
    /**
473
     * A?ade al Layout un nuevo FFrameText a partir de los par?metros de
474
     * entrada.
475
     * 
476
     * @param layoutContext
477
     *            Layout sobre el que se crea el nuevo FFrame.
478
     * @param name
479
     *            Texto.
480
     * @param sizefont
481
     *            tama?o de la fuente.
482
     * @param x
483
     *            Posici?n X.
484
     * @param y
485
     *            Posici?n Y.
486
     * @param h
487
     *            Altura.
488
     * @param wT
489
     *            Rect?ngulo del FFrame.
490
     * @param hT
491
     *            DOCUMENT ME!
492
     */
493
    private void toFFrameText(LayoutContext layoutContext, String name,
494
        Rectangle2D re, float sizefont, double x, double y, double h) {
495
        // Font f = getFont(sizefont);
496
        Rectangle2D rAux =
497
            new Rectangle2D.Double(x + re.getWidth() / 4, ((y + (h / 2)) - h),
498
                re.getWidth() - re.getWidth() / 4, h);
499
        FFrameText text =
500
            (FFrameText) layoutManager
501
                .createFrame(FFrameText.PERSISTENCE_DEFINITION_NAME);
502

    
503
        text.setFixedFontSize(true);
504
        double myScale = layoutContext.getAT().getScaleX() * 0.0234;
505
        text.setFontSize((int) (sizefont / myScale));
506
        text.setBoundBox(FLayoutUtilities.toSheetRect(rAux, layoutContext.getAT()));
507
        text.addText(name);
508
        layoutContext.addFFrame(text, false, true);
509
    }
510

    
511
    /**
512
     * Dibuja sobre el Graphics2D el s?mbolo.
513
     * 
514
     * @param g
515
     *            Graphics2D.
516
     * @param re
517
     *            Rect?ngulo a cubrir por el s?mbolo.
518
     * @param x
519
     *            Posici?n X.
520
     * @param y
521
     *            Posici?n Y.
522
     * @param symbol2d
523
     *            S?mbolo a dibujar.
524
     * @param sizefont
525
     *            Tama?o de la fuente.
526
     * @param h
527
     *            Altura.
528
     */
529
    private void drawSymbolLegend(Graphics2D g, Rectangle2D re, double x,
530
        double y, ISymbol symbol2d, float sizefont, double h) {
531
        double pW = 5;
532
        double wl = (re.getWidth() / pW);
533
        double haux = (sizefont * 0.7);
534
        Font font = null;
535

    
536
        if (symbol2d instanceof ITextSymbol
537
            && (font = ((ITextSymbol) symbol2d).getFont()) != null) {
538
            ((ITextSymbol) symbol2d).setFont(new Font(font.getFontName(), font
539
                .getStyle(), (int) (wl / 6)));
540
        }
541

    
542
        Rectangle rectangle =
543
            new Rectangle((int) x, (int) ((y + (h / 2)) - haux), (int) wl,
544
                (int) haux);
545
        try {
546
//            if(symbol2d instanceof CartographicSupport){
547
//                ((CartographicSupport)symbol2d).setCartographicContext(properties);
548
//            }
549
            symbol2d.drawInsideRectangle(g, new AffineTransform(), rectangle); //,properties);
550
        } catch (SymbolDrawingException ex) {
551
            if (ex.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
552
                try {
553
                    mapContextManager
554
                        .getSymbolManager()
555
                        .getWarningSymbol(
556
                            SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
557
                            symbol2d.getDescription(),
558
                            SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
559
                        .drawInsideRectangle(g, null, rectangle);
560
                } catch (SymbolDrawingException e1) {
561
                    // IMPOSSIBLE TO REACH THIS
562
                }
563
            } else {
564
                // should be unreachable code
565
                throw new Error(PluginServices.getText(this,
566
                    "symbol_shapetype_mismatch"));
567
            }
568
        }
569
    }
570

    
571
    /**
572
     * A?ade al Layout un nuevo FFrameSymbol.
573
     * 
574
     * @param layoutContext
575
     *            Layout sobe el que se a?ade el FFrame.
576
     * @param x
577
     *            Posici?n X.
578
     * @param y
579
     *            Posici?n Y.
580
     * @param fs2d
581
     *            S?mbolo a a?adir.
582
     * @param h
583
     *            Altura.
584
     * @param shapeType
585
     * @param wT
586
     *            Rect?ngulo del FFrame.
587
     * @param hT
588
     *            tama?o de la fuente.
589
     */
590
    private void toFFrameSymbol(LayoutContext layoutContext, Rectangle2D re, double x,
591
        double y, ISymbol fs2d, float sizefont, double h, int shapeType) {
592
        double pW = 5;
593
        double wl = (re.getWidth() / pW);
594
        double haux = (sizefont * 0.7);
595
        Font font = null;
596
        if (fs2d instanceof ITextSymbol
597
            && (font = ((ITextSymbol) fs2d).getFont()) != null) {
598
            ((ITextSymbol) fs2d).setFont(new Font(font.getFontName(), font
599
                .getStyle(), (int) (wl / 6)));
600
        }
601

    
602
        Rectangle2D rAux3 =
603
            new Rectangle2D.Double(x, ((y + (h / 2)) - haux), wl, haux);
604
        FFrameSymbol symbol =
605
            (FFrameSymbol) layoutManager
606
                .createFrame(FFrameSymbol.PERSISTENCE_DEFINITION_NAME);
607

    
608
        // symbol.setLayout(getLayout());
609
        symbol.setBoundBox(FLayoutUtilities.toSheetRect(rAux3, layoutContext.getAT()));
610
        symbol.setSymbol(fs2d);
611
        symbol.setShapeType(shapeType);
612
        layoutContext.addFFrame(symbol, false, true);
613
    }
614

    
615
    /**
616
     * Devuelve el n?mero total de capas incluyendo las subcapas.
617
     * 
618
     * @param layers
619
     *            Capa a contar.
620
     * 
621
     * @return N?mero de capas y subcapas.
622
     */
623
    private int getSizeNum(FLayers layers) {
624
        int count = 0;
625

    
626
        // ///Aqu? hay que calcular cuantos layers y sublayers hay, para saber
627
        // que distancias dejar entre uno y otro.
628
        // /y adem?s el tama?o de cada uno de ellos para saber que anchura
629
        // dejar.
630
        int l = 0;
631
        for (int i = layers.getLayersCount() - 1; i >= 0; i--) {
632
            FLayer layer = layers.getLayer(i);
633
            boolean b; // = false;
634
            if (nameLayers.size() > l
635
                && nameLayers.get(l).equals(layer.getName())) {
636
                b = areVisible.get(l);
637
            } else {
638
                b = layer.isVisible();
639
            }
640
            l++;
641
            if (b) {
642
                // if (layer.isVisible()) {
643
                if (layer.getName().length() > m_max) {
644
                    m_max = layer.getName().length();
645
                }
646

    
647
                if (layer instanceof FLayers) {
648
                    // n++;
649
                    count = count + 3;
650
                    count += getSizeNum((FLayers) layer); // m_numLayers +=
651
                                                      // getNumInLyrGroup((FLayers)
652
                                                      // layer);
653
                } else {
654
                    if (layer instanceof Classifiable) {
655
                        Classifiable cO = (Classifiable) layer;
656
                        count++;
657

    
658
                        if (cO.getLegend() instanceof IClassifiedLegend) {// &&
659
                                                                          // !(cO
660
                                                                          // instanceof
661
                                                                          // FLyrAnnotation))
662
                                                                          // {
663
                            IClassifiedLegend cli =
664
                                (IClassifiedLegend) cO.getLegend();
665

    
666
                            for (int j = 0; j < cli.getValues().length; j++) {
667
                                String desc = cli.getDescriptions()[j];
668

    
669
                                if (desc != null && desc.length() > m_max) {
670
                                    m_max = desc.length();
671
                                }
672

    
673
                                count++;
674
                            }
675
                        }
676
                    } else {
677
                        String layerName = layer.getName();
678

    
679
                        if (layerName != null && layerName.length() > m_max) {
680
                            m_max = layerName.length();
681
                        }
682

    
683
                        count++;
684
                    }
685

    
686
                }
687
            }
688
        }
689

    
690
        return count;
691
    }
692

    
693
    /**
694
     * Transforma el FFrameLegend en diferentes FFrameSymbol y FFrameText.
695
     * 
696
     * @param layout
697
     *            Layout sobre el que a?adir los FFrame nuevos y sobre el
698
     *            que elimnar el FFrameLegend anterior.
699
     */
700
    public void toFFrames(LayoutContext layout) {
701
        Rectangle2D rectangle = getBoundingBox(null);
702
        double h = rectangle.getHeight() / m_numLayers;
703
        FLayers lays = layers;
704

    
705
        // layout.getEFS().startComplexCommand();
706
        // toFFrames(layout, lays, rectangle, r.getWidth(), r.getHeight(), h,
707
        // 0);
708
        int[] n = new int[1];
709
        n[0] = 0;
710
        drawLegendOrToFFrame(null, rectangle, h, lays, n, layout);
711
        layout.delFFrame(this);
712

    
713
        // /layout.getFFrames().remove(this);
714
        // layout.getEFS().endComplexCommand();
715
    }
716

    
717
    /**
718
     * Rellena la fuente a utilizar al dibujar los String sobre el graphics.
719
     * 
720
     * @param f
721
     *            Font.
722
     */
723
    public void setFont(Font f) {
724
        m_font = f;
725
    }
726

    
727
    /**
728
     * Devuelve la fuente que esta utilizando.
729
     * 
730
     * @return Font.
731
     */
732
    public Font getFont() {
733
        if (m_font != null) {
734
            return new Font(m_font.getName(), m_font.getStyle(), 9);
735
        }
736
        return new Font("SansSerif", Font.PLAIN, 9);
737
    }
738

    
739
    @Override
740
    public String getNameFFrame() {
741
        return PluginServices.getText(this, "leyenda") + num;
742
    }
743

    
744
    @Override
745
    public String getName() {
746
        return PERSISTENCE_DEFINITION_NAME;
747
    }
748

    
749
    @Override
750
    public void print(Graphics2D g, AffineTransform at, Geometry geom,
751
        PrintAttributes properties) {
752
        this.properties = properties;
753
        draw(g, at, null, null);
754
        this.properties = null;
755
    }
756

    
757
    public void initialize() {
758

    
759
    }
760

    
761
    public void setNameLayers(List nameLayers) {
762
        this.nameLayers = nameLayers;
763
    }
764

    
765
    public void setAreVisible(List areVisible) {
766
        this.areVisible = areVisible;
767
    }
768

    
769
    public List getNameLayers() {
770
        return nameLayers;
771
    }
772

    
773
    public List getAreVisible() {
774
        return areVisible;
775
    }
776

    
777
    public static void registerPersistent() {
778
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
779
        if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) {
780
            DynStruct definition =
781
                manager.addDefinition(FFrameLegend.class,
782
                    PERSISTENCE_DEFINITION_NAME,
783
                    "FFrameLegend persistence definition", null, null);
784

    
785
            definition
786
                .extend(manager
787
                    .getDefinition(AbstractFFrameViewDependence.PERSISTENCE_DEFINITION_NAME));
788

    
789
            definition.addDynFieldInt(NUMLAYERS_FIELD).setMandatory(true);
790
            definition.addDynFieldInt(MAX_FIELD).setMandatory(true);
791
            definition.addDynFieldInt(QUALITY_FIELD).setMandatory(true);
792
            definition.addDynFieldInt(VIEWING_FIELD).setMandatory(true);
793
            definition.addDynFieldObject(FONT_FIELD)
794
                .setClassOfValue(Font.class).setMandatory(true);
795
            definition.addDynFieldList(NAMELAYERS_FIELD)
796
                .setClassOfItems(String.class).setMandatory(true);
797
            definition.addDynFieldList(AREVISIBLE_FIELD)
798
                .setClassOfItems(Boolean.class).setMandatory(true);
799
        }
800
    }
801

    
802
    @Override
803
    public void loadFromState(PersistentState state)
804
        throws PersistenceException {
805
        super.loadFromState(state);
806
        m_numLayers = state.getInt(NUMLAYERS_FIELD);
807
        m_max = state.getInt(MAX_FIELD);
808
        m_quality = state.getInt(QUALITY_FIELD);
809
        m_viewing = state.getInt(VIEWING_FIELD);
810
        m_font = (Font) state.get(FONT_FIELD);
811
        nameLayers = (List<String>) state.getList(NAMELAYERS_FIELD);
812
        areVisible = (List<Boolean>) state.getList(AREVISIBLE_FIELD);
813
    }
814

    
815
    @Override
816
    public void saveToState(PersistentState state) throws PersistenceException {
817
        super.saveToState(state);
818
        state.set(NUMLAYERS_FIELD, m_numLayers);
819
        state.set(MAX_FIELD, m_max);
820
        state.set(QUALITY_FIELD, m_quality);
821
        state.set(VIEWING_FIELD, m_viewing);
822
        state.set(FONT_FIELD, getFont());
823
        state.set(NAMELAYERS_FIELD, nameLayers);
824
        state.set(AREVISIBLE_FIELD, areVisible);
825
    }
826
}