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 @ 1744

History | View | Annotate | Download (32.2 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(), viewPort);
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(), viewPort);
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, ViewPort viewPort) {
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
        symbol.setViewPort(viewPort);
613
        layoutContext.addFFrame(symbol, false, true);
614
    }
615

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

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

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

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

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

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

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

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

    
684
                        count++;
685
                    }
686

    
687
                }
688
            }
689
        }
690

    
691
        return count;
692
    }
693

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

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

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

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

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

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

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

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

    
758
    public void initialize() {
759

    
760
    }
761

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

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

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

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

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

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

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

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

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