Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / fframes / FFrameLegend.java @ 32704

History | View | Annotate | Download (27 KB)

1 9392 caballero
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.project.documents.layout.fframes;
42
43
import java.awt.Color;
44
import java.awt.Font;
45
import java.awt.Graphics2D;
46
import java.awt.Image;
47
import java.awt.Rectangle;
48
import java.awt.geom.AffineTransform;
49
import java.awt.geom.Rectangle2D;
50
import java.awt.image.BufferedImage;
51
import java.util.ArrayList;
52
53 10911 jaume
import javax.print.attribute.PrintRequestAttributeSet;
54
55 10626 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
56 9392 caballero
import com.iver.andami.PluginServices;
57 9593 caballero
import com.iver.cit.gvsig.fmap.core.FShape;
58 17187 jdominguez
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
59 9640 jaume
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
60 10850 jaume
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
61 18623 jdominguez
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
62 9392 caballero
import com.iver.cit.gvsig.fmap.layers.FLayer;
63
import com.iver.cit.gvsig.fmap.layers.FLayers;
64
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation;
65
import com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable;
66
import com.iver.cit.gvsig.fmap.layers.layerOperations.IHasImageLegend;
67 11558 jaume
import com.iver.cit.gvsig.fmap.rendering.IClassifiedLegend;
68 9392 caballero
import com.iver.cit.gvsig.project.documents.exceptions.SaveException;
69
import com.iver.cit.gvsig.project.documents.layout.FLayoutUtilities;
70 9808 caballero
import com.iver.cit.gvsig.project.documents.layout.LayoutContext;
71 9392 caballero
import com.iver.cit.gvsig.project.documents.layout.fframes.gui.dialogs.FFrameLegendDialog;
72
import com.iver.cit.gvsig.project.documents.layout.fframes.gui.dialogs.IFFrameDialog;
73
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
74
import com.iver.utiles.XMLEntity;
75
76
77
/**
78
 * FFrame para introducir una leyenda en el Layout.
79
 *
80
 * @author Vicente Caballero Navarro
81
 */
82
public class FFrameLegend extends FFrame implements IFFrameViewDependence {
83
    private static final int PRESENTACION = 0;
84
    private static final int BORRADOR = 1;
85
    private int m_quality = 0;
86
    private int m_viewing = 0;
87
    private Font m_f = new Font("SansSerif", Font.PLAIN, 9);
88
    private FFrameView fframeview = null;
89
    private int m_max;
90
    private int m_numLayers;
91
    private FLayers layers = null;
92
    private int dependenceIndex = -1;
93
        private ArrayList nameLayers=new ArrayList();
94
        private ArrayList areVisible=new ArrayList();
95 28368 vcaballero
        private PrintRequestAttributeSet properties;
96 9392 caballero
97 9516 caballero
        public FFrameLegend() {
98
99
        }
100 9392 caballero
    /**
101
     * Rellena la calidad que se quiere aplicar.
102
     *
103
     * @param q entero que representa la calidad a aplicar.
104
     */
105
    public void setQuality(int q) {
106
        m_quality = q;
107
    }
108
109
    /**
110
     * Devuelve un entero que representa la calidad que est? seleccionada.
111
     *
112
     * @return tipo de calidad selccionada.
113
     */
114
    public int getQuality() {
115
        return m_quality;
116
    }
117
118
    /**
119
     * Devuelve un entero que representa la forma en que se actualiza la vista.
120
     *
121
     * @return forma que se actualiza la vista.
122
     */
123
    public int getViewing() {
124
        return m_viewing;
125
    }
126
127
    /**
128
     * Inserta una FFrameView de donde se obtiene la informaci?n de las capas
129
     * para generar la leyenda.
130
     *
131
     * @param f FFrameView para obtener los nombres de las capas.
132
     */
133
    public void setFFrameDependence(IFFrame f) {
134
        fframeview = (FFrameView) f;
135
    }
136
137 11800 caballero
    public void refreshDependence(IFFrame fant, IFFrame fnew) {
138
            if ((fframeview != null) &&
139
                fframeview.equals(fant)) {
140
            fframeview=(FFrameView)fnew;
141
            }
142
    }
143 9392 caballero
    /**
144
     * Devuelve el FFrameView utilizado para obtener la leyenda.
145
     *
146
     * @return FFrameView utilizado.
147
     */
148 11800 caballero
    public IFFrame[] getFFrameDependence() {
149
        return new IFFrame[]{fframeview};
150 9392 caballero
    }
151
152
    /**
153
     * Rellena la forma de actualizar la vista.
154
     *
155
     * @param v entero que representa la forma de actualizar la vista.
156
     */
157
    public void setViewing(int v) {
158
        m_viewing = v;
159
    }
160
161
    /**
162
     * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
163
     * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
164
     * de dibujar.
165
     *
166
     * @param g Graphics
167
     * @param at Transformada afin.
168
     * @param rv rect?ngulo sobre el que hacer un clip.
169
     * @param imgBase Imagen para acelerar el dibujado.
170
     */
171
    public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
172 28368 vcaballero
        BufferedImage imgBase) {
173 9392 caballero
        Rectangle2D.Double re = getBoundingBox(at);
174
        g.rotate(Math.toRadians(getRotation()), re.x + (re.width / 2),
175
            re.y + (re.height / 2));
176
177
        if ((fframeview != null) && (fframeview.getMapContext() != null)) {
178
            layers = fframeview.getMapContext().getLayers();
179
        }
180
181
        m_max = 0;
182
        m_numLayers = 0;
183
184
        if (intersects(rv, re)) {
185
            if (layers == null) { //Si no se ha seleccionado ninguna vista para crear la leyenda.
186
                drawEmpty(g);
187
            } else if ((rv == null) || (getQuality() == PRESENTACION)) {
188
                m_numLayers = getSizeNum(layers);
189
190
                double h = re.getHeight() / m_numLayers;
191
                int[] n = new int[1];
192
                n[0] = 0;
193
                drawLegendOrToFFrame(g, re, h, layers, n, null);
194
            } else if (getQuality() == BORRADOR) { //Si se selecciona la calidad BORRADOR.
195
                drawDraft(g);
196
            }
197
        }
198
199
        g.rotate(Math.toRadians(-getRotation()), re.x + (re.width / 2),
200
            re.y + (re.height / 2));
201
    }
202
203
    /**
204
     * Dibuja el nombre u s?mbolo de la capa que se pasa como par?metro.
205
     *
206
     * @param g Graphics2D sobre el que se dibuja.
207
     * @param re Rectangle a rellenar.
208
     * @param h Altura en pixels.
209
     * @param layers Capa a representar.
210
     * @param n ?ndice de la capa a dibujar.
211 10626 caballero
     * @throws ReadDriverException TODO
212 9392 caballero
     */
213
    private void drawLegendOrToFFrame(Graphics2D g, Rectangle2D re, double h,
214 28368 vcaballero
        FLayers layers, int[] n, LayoutContext layout)  {
215 9392 caballero
        float sizefont = 0;
216
217
        if ((re.getHeight() / m_numLayers) < (re.getWidth() / (m_max * 0.7))) {
218
            sizefont = (float) (re.getHeight() / m_numLayers);
219
        } else {
220
            sizefont = (float) (re.getWidth() / (m_max * 0.7));
221
        }
222
223
        int l=0;
224
        //////Se recorren los layers dibujando el s?mbolo y su nombre sobre el graphics
225
        for (int i = layers.getLayersCount() - 1; i >= 0; i--) {
226
            FLayer layer = layers.getLayer(i);
227
            boolean b=false;
228
229
            if (nameLayers.size()>l && nameLayers.get(l).equals(layer.getName())) {
230
                    b=((Boolean)areVisible.get(l)).booleanValue();
231
            }else {
232
                    b=layer.isVisible();
233
            }
234
            l++;
235
            if (b) {
236
                if (layer instanceof FLayers) {
237
                    n[0]++;
238
239
                    double dX = 0;
240
                    double dY = n[0] * h;
241
                    double xl = (re.getX() + dX);
242
                    double yl = (re.getY() + dY);
243
                    if (layout!=null) {
244
                            toFFrameText(layout,layer.getName(),re,sizefont,(xl - (re.getWidth() / 5)),yl,h);
245
                    }else {
246
                            drawNameLegend(g, layer.getName(), re, sizefont,
247
                                            (xl - (re.getWidth() / 5)), yl, h);
248
                    }
249
                            n[0]++;
250
                    drawLegendOrToFFrame(g, re, h, (FLayers) layer, n, layout);
251
                    n[0]++;
252 13799 caballero
                } else if (layer instanceof Classifiable && !(layer instanceof IHasImageLegend) ) {
253
                    Classifiable cO = (Classifiable) layer;
254 9392 caballero
255 13799 caballero
                    if (cO.getLegend() instanceof IClassifiedLegend && !(cO instanceof FLyrAnnotation)) {
256
                        IClassifiedLegend cli = (IClassifiedLegend) cO.getLegend();
257 9392 caballero
                        double dX = 0;
258
                        double dY = n[0] * h;
259
260
                        double xl = (re.getX() + dX);
261
                        double yl = (re.getY() + dY);
262
                        if (layout!=null) {
263
                                toFFrameText(layout,layer.getName(),re,sizefont,(xl - (re.getWidth() / 5)),yl,h);
264
                        }else {
265
                                drawNameLegend(g, layer.getName(), re, sizefont,
266
                                                (xl - (re.getWidth() / 5)), yl, h);
267
                        }
268
                        n[0]++;
269 13799 caballero
                        String[] descriptions=cli.getDescriptions();
270
                        ISymbol[] symbols=cli.getSymbols();
271
                        for (int j = 0; j < descriptions.length; j++) {
272 9392 caballero
                            dY = n[0] * h;
273
274
                            xl = (re.getX() + dX);
275
                            yl = (re.getY() + dY);
276
277 13799 caballero
                            String s = descriptions[j];
278 9392 caballero
                            if (layout!=null) {
279
                                    toFFrameText(layout,s,re,sizefont,xl,yl,h);
280
                            }else {
281
                                    drawNameLegend(g, s, re, sizefont, xl, yl, h);
282
                            }
283 13799 caballero
                            ISymbol fs2d = symbols[j];
284 9392 caballero
                            if (layout!=null) {
285 29459 vcaballero
                                    try {
286
                                                                        toFFrameSymbol(layout,re,xl,yl,fs2d,sizefont, h, cO.getShapeType());
287
                                                                } catch (ReadDriverException e) {
288
                                                                        e.printStackTrace();
289
                                                                }
290 9392 caballero
                            }else {
291
                                    drawSymbolLegend(g, re, xl, yl, fs2d, sizefont,
292
                                                    h);
293
                            }
294
                            n[0]++;
295
                        }
296
                    } else {
297
                        double dX = 0;
298
                        double dY = n[0] * h;
299
300
                        double xl = (re.getX() + dX);
301
                        double yl = (re.getY() + dY);
302
                        if (layout!=null) {
303
                                toFFrameText(layout,layer.getName(),re,sizefont,xl,yl,h);
304
                        }else {
305
                                drawNameLegend(g, layer.getName(), re, sizefont, xl,
306
                                                yl, h);
307
                        }
308 10850 jaume
                        // TO DO: CAMBIAR TO_DO ESTO PARA QUE ACEPTE ISYMBOL
309
                        // TODO: comprovar que no es trenca res
310 14368 vcaballero
                        if (cO.getLegend() != null) {
311
                                                        ISymbol fs2d = cO.getLegend().getDefaultSymbol();
312 9392 caballero
313 14368 vcaballero
                                                        if (layout != null) {
314 29459 vcaballero
                                                                try {
315
                                                                        toFFrameSymbol(layout, re, xl, yl, fs2d,
316
                                                                                        sizefont, h, cO.getShapeType());
317
                                                                } catch (ReadDriverException e) {
318
                                                                        e.printStackTrace();
319
                                                                }
320 14368 vcaballero
                                                        } else {
321
                                                                drawSymbolLegend(g, re, xl, yl, fs2d, sizefont,
322
                                                                                h);
323
                                                        }
324
                                                }
325 9392 caballero
                        n[0]++;
326
                    }
327
                }else if (layer instanceof IHasImageLegend){
328
                        Image image=((IHasImageLegend)layer).getImageLegend();
329
                        String path=((IHasImageLegend)layer).getPathImage();
330
                        if (image!=null) {
331 9516 caballero
                        FFramePicture picture =(FFramePicture)FrameFactory.createFrameFromName(FFramePictureFactory.registerName);
332
333 9392 caballero
                        picture.setLayout(getLayout());
334
                        BufferedImage bi = new BufferedImage(image.getWidth(null),
335
                            image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
336
                    Graphics2D biContext = bi.createGraphics();
337
                    biContext.drawImage(image, 0, 0, null);
338
                    picture.setImage(bi);
339
                    double dY = n[0] * h;
340
                    Rectangle2D rectImage=new Rectangle2D.Double(re.getX(),re.getY()+dY,re.getWidth(),h);
341
                   if (layout!=null) {
342 9808 caballero
                            picture.setBoundBox(FLayoutUtilities.toSheetRect(rectImage, layout.getAT()));
343 9392 caballero
                            picture.setPath(path);
344 9808 caballero
                            layout.addFFrame(picture, false, true);
345 9392 caballero
                    }else {
346
                            picture.setBoundBox(FLayoutUtilities.toSheetRect(rectImage,new AffineTransform()));
347
                        picture.draw(g,new AffineTransform(),re,bi);
348
                    }
349
                        }
350
                    n[0]++;
351
                }else {
352
                        double dX = 0;
353
                    double dY = n[0] * h;
354
355
                    double xl = (re.getX() + dX);
356
                    double yl = (re.getY() + dY);
357
                    if (layout!=null) {
358
                            toFFrameText(layout,layer.getName(),re,sizefont,xl,yl,h);
359
                    }else {
360
                            drawNameLegend(g, layer.getName(), re, sizefont, xl,
361
                                            yl, h);
362
                    }
363
                    n[0]++;
364
                }
365
            }
366
        }
367
    }
368
369
    /**
370
     * Dibuja sobre el graphics el nombre de la capa que se pasa como
371
     * par?metro.
372
     *
373
     * @param g Graphics2D sobre el que dibujar.
374
     * @param name Nombre de la capa.
375
     * @param re Rect?ngulo a ocupar por el nombre.
376
     * @param sizefont tama?o de la fuente.
377
     * @param x Posici?n X.
378
     * @param y Posici?n Y.
379
     * @param h Altura.
380
     */
381
    private void drawNameLegend(Graphics2D g, String name, Rectangle2D re,
382
        float sizefont, double x, double y, double h) {
383
        Font f = getFont(sizefont);
384
        g.setFont(f);
385
        g.setColor(Color.black);
386 30363 vcaballero
        if (name!=null)
387
                g.drawString(name, (float) (x + (re.getWidth() / 4)),
388
                                (float) (y + (h / 2)));
389 9392 caballero
    }
390
391
    private Font getFont(float sizefont) {
392
                Font f=getFont();
393
                return new Font(f.getName(),f.getStyle(),(int)sizefont);
394 18623 jdominguez
  }
395 9392 caballero
396
        /**
397
     * A?ade al Layout un nuevo FFrameText a partir de los par?metros de
398
     * entrada.
399
     *
400
     * @param layout Layout sobre el que se crea el nuevo FFrame.
401
         * @param name Texto.
402
         * @param sizefont tama?o de la fuente.
403
         * @param x Posici?n X.
404
         * @param y Posici?n Y.
405
         * @param h Altura.
406
         * @param wT Rect?ngulo del FFrame.
407
         * @param hT DOCUMENT ME!
408
     */
409 9808 caballero
    private void toFFrameText(LayoutContext layout, String name, Rectangle2D re,
410 9392 caballero
            float sizefont, double x, double y, double h) {
411
            //Font f = getFont(sizefont);
412
            Rectangle2D rAux = new Rectangle2D.Double(x+re.getWidth()/4, ((y + (h / 2)) - h),
413
                    re.getWidth()-re.getWidth()/4, h);
414 9516 caballero
            FFrameText text =(FFrameText)FrameFactory.createFrameFromName(FFrameTextFactory.registerName);
415
416 9392 caballero
            text.setLayout(getLayout());
417
            text.setFixedFontSize(true);
418
            //text.setFontSize((int) (sizefont*1.4));
419 9808 caballero
            double myScale = layout.getAT().getScaleX() * 0.0234;
420 9392 caballero
            text.setFontSize((int)(sizefont/myScale));
421 9808 caballero
            text.setBoundBox(FLayoutUtilities.toSheetRect(rAux, layout.getAT()));
422 9392 caballero
            text.addText(name);
423 9808 caballero
            layout.addFFrame(text, false, true);
424 9392 caballero
        }
425
426
    /**
427
     * Dibuja sobre el Graphics2D el s?mbolo.
428
     *
429
     * @param g Graphics2D.
430
     * @param re Rect?ngulo a cubrir por el s?mbolo.
431
     * @param x Posici?n X.
432
     * @param y Posici?n Y.
433 17187 jdominguez
     * @param symbol2d S?mbolo a dibujar.
434 9392 caballero
     * @param sizefont Tama?o de la fuente.
435
     * @param h Altura.
436
     */
437
    private void drawSymbolLegend(Graphics2D g, Rectangle2D re, double x,
438 18623 jdominguez
            double y, ISymbol symbol2d, float sizefont, double h) {
439 17187 jdominguez
            double pW = 5;
440
            double wl = (re.getWidth() / pW);
441
            double haux = (sizefont * 0.7);
442
            Font font=null;
443 10850 jaume
444 17187 jdominguez
            if (symbol2d instanceof ITextSymbol && (font=((ITextSymbol)symbol2d).getFont()) != null) {
445
                    ((ITextSymbol)symbol2d).setFont(new Font(font.getFontName(), font.getStyle(),
446
                                    (int) (wl / 6)));
447
            }
448
449
                Rectangle rectangle = new Rectangle((int) x, (int) ((y + (h / 2)) - haux), (int) wl,
450
                                (int) haux);
451 18623 jdominguez
                try {
452 17187 jdominguez
                        symbol2d.drawInsideRectangle(g, new AffineTransform(),
453 28368 vcaballero
                                        rectangle, properties);
454 18623 jdominguez
                } catch (SymbolDrawingException e) {
455
                        if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
456
                                try {
457
                                        SymbologyFactory.getWarningSymbol(
458
                                                        SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
459
                                                        symbol2d.getDescription(),
460 28368 vcaballero
                                                        SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS).drawInsideRectangle(g, null, rectangle, null);
461 18623 jdominguez
                                } catch (SymbolDrawingException e1) {
462
                                        // IMPOSSIBLE TO REACH THIS
463
                                }
464
                        } else {
465
                                // should be unreachable code
466
                                throw new Error(PluginServices.getText(this, "symbol_shapetype_mismatch"));
467
                        }
468
                }
469 9392 caballero
    }
470 28368 vcaballero
471 9392 caballero
    /**
472
     * A?ade al Layout un nuevo FFrameSymbol.
473
     *
474
     * @param layout Layout sobe el que se a?ade el FFrame.
475
     * @param x Posici?n X.
476
     * @param y Posici?n Y.
477
     * @param fs2d S?mbolo a a?adir.
478
     * @param h Altura.
479
     * @param wT Rect?ngulo del FFrame.
480
     * @param hT tama?o de la fuente.
481
     */
482 9808 caballero
    private void toFFrameSymbol(LayoutContext layout, Rectangle2D re, double x,
483 29459 vcaballero
            double y, ISymbol fs2d, float sizefont, double h, int shapeType) {
484 9392 caballero
            double pW = 5;
485
            double wl = (re.getWidth() / pW);
486
            double haux = (sizefont * 0.7);
487
            Font font=null;
488 10850 jaume
            if (fs2d instanceof ITextSymbol && (font=((ITextSymbol)fs2d).getFont()) != null) {
489
              ((ITextSymbol)fs2d).setFont(new Font(font.getFontName(), font.getStyle(),
490
                      (int) (wl / 6)));
491 9392 caballero
            }
492
493
            Rectangle2D rAux3 = new Rectangle2D.Double(x, ((y + (h / 2)) - haux),
494
                    wl, haux);
495 9516 caballero
            FFrameSymbol symbol =(FFrameSymbol)FrameFactory.createFrameFromName(FFrameSymbolFactory.registerName);
496
497 9392 caballero
            symbol.setLayout(getLayout());
498 9808 caballero
            symbol.setBoundBox(FLayoutUtilities.toSheetRect(rAux3, layout.getAT()));
499 9392 caballero
            symbol.setFSymbol(fs2d);
500 29459 vcaballero
            symbol.setShapeType(shapeType);
501 9808 caballero
            layout.addFFrame(symbol, false, true);
502 9392 caballero
        }
503
504
    /**
505
     * Devuelve el n?mero total de capas incluyendo las subcapas.
506
     *
507
     * @param layers Capa a contar.
508
     *
509
     * @return N?mero de capas y subcapas.
510
     */
511
    private int getSizeNum(FLayers layers) {
512
        int n = 0;
513
514
        /////Aqu? hay que calcular cuantos layers y sublayers hay, para saber que distancias dejar entre uno y otro.
515
        ///y adem?s el tama?o de cada uno de ellos para saber que anchura dejar.
516
        int l=0;
517
        for (int i = layers.getLayersCount()-1; i>=0; i--) {
518
            FLayer layer = layers.getLayer(i);
519
            boolean b=false;
520
            if (nameLayers.size()>l && nameLayers.get(l).equals(layer.getName())) {
521
                    b=((Boolean)areVisible.get(l)).booleanValue();
522
            }else {
523
                    b=layer.isVisible();
524
            }
525
            l++;
526
            if (b) {
527
            //if (layer.isVisible()) {
528
                if (layer.getName().length() > m_max) {
529
                    m_max = layer.getName().length();
530
                }
531
532
                if (layer instanceof FLayers) {
533
                    //n++;
534
                        n = n + 3;
535
                    n += getSizeNum((FLayers) layer); //m_numLayers += getNumInLyrGroup((FLayers) layer);
536
                } else {
537 13799 caballero
                        if (layer instanceof Classifiable) {
538
                    Classifiable cO = (Classifiable) layer;
539 9392 caballero
                    n++;
540
541 13799 caballero
                    if (cO.getLegend() instanceof IClassifiedLegend && !(cO instanceof FLyrAnnotation)) {
542
                        IClassifiedLegend cli = (IClassifiedLegend) cO.getLegend();
543 9392 caballero
544
                        for (int j = 0; j < cli.getValues().length; j++) {
545
                            String s = cli.getDescriptions()[j];
546
547 30131 jmvivo
                            if (s != null && s.length() > m_max) {
548 9392 caballero
                                m_max = s.length();
549
                            }
550
551
                            n++;
552
                        }
553
                    }
554
                        }else{
555
                                  String s = layer.getName();
556
557 30131 jmvivo
                          if (s != null && s.length() > m_max) {
558 9392 caballero
                              m_max = s.length();
559
                          }
560
561
                          n++;
562
                        }
563
564
                }
565
            }
566
        }
567
568
        return n;
569
    }
570
571
    /**
572
     * Transforma el FFrameLegend en diferentes FFrameSymbol y FFrameText.
573
     *
574
     * @param layout Layout sobre el que a?adir los FFrame nuevos y sobre el
575
     *        que elimnar el FFrameLegend anterior.
576
     */
577 9808 caballero
    public void toFFrames(LayoutContext layout) {
578 9392 caballero
        Rectangle2D rectangle = getBoundingBox(null);
579
        //Rectangle2D r = getBoundBox();
580
        double h = rectangle.getHeight() / m_numLayers;
581
        FLayers lays = layers;
582
583
        //layout.getEFS().startComplexCommand();
584
        //toFFrames(layout, lays, rectangle, r.getWidth(), r.getHeight(), h, 0);
585
        int[] n = new int[1];
586
        n[0] = 0;
587 28368 vcaballero
              drawLegendOrToFFrame(null,rectangle,h,lays,n,layout);
588
                layout.delFFrame(this);
589 9392 caballero
590
        ///layout.getFFrames().remove(this);
591
        //layout.getEFS().endComplexCommand();
592
    }
593
594
    /**
595
     * Rellena la fuente a utilizar al dibujar los String sobre el graphics.
596
     *
597
     * @param f Font.
598
     */
599
    public void setFont(Font f) {
600
        m_f = f;
601
    }
602
603
    /**
604
     * Devuelve la fuente que esta utilizando.
605
     *
606
     * @return Font.
607
     */
608
    public Font getFont() {
609
        if (m_f != null) {
610
                return new Font(m_f.getName(),m_f.getStyle(),9);
611
        //    return new Font(m_f.getFontName(), m_f.getStyle(), 9);
612
        }
613
        return new Font("SansSerif",Font.PLAIN,9);
614
    }
615
616
    /**
617
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#getXMLEntity()
618
     */
619
    public XMLEntity getXMLEntity() throws SaveException {
620
        XMLEntity xml = super.getXMLEntity();
621
622
        try {
623
//            xml.putProperty("type", Layout.RECTANGLELEGEND);
624
            xml.putProperty("m_numLayers", m_numLayers);
625
            xml.putProperty("m_max", m_max);
626
            xml.putProperty("m_quality", m_quality);
627
            xml.putProperty("m_viewing", m_viewing);
628
            xml.putProperty("fontName", m_f.getFontName());
629
            xml.putProperty("fontStyle", m_f.getStyle());
630
631
            if (fframeview != null) {
632
                Layout layout = fframeview.getLayout();
633
                IFFrame[] fframes = layout.getLayoutContext().getAllFFrames();
634
635
                for (int i = 0; i < fframes.length; i++) {
636
                    if (fframeview.equals(fframes[i])) {
637
                        xml.putProperty("index", i);
638
                        break;
639
                    }
640
                }
641
            }
642
643
            String[] s=new String[nameLayers.size()];
644
            boolean[] b=new boolean[nameLayers.size()];
645
            for (int i=0;i<nameLayers.size();i++) {
646
                    String k=(String)nameLayers.get(i);
647
                    s[i]=k;
648
                    b[i]=((Boolean)areVisible.get(i)).booleanValue();
649
            }
650
            xml.putProperty("nameLayers",s);
651
            xml.putProperty("areVisible",b);
652
        } catch (Exception e) {
653
            throw new SaveException(e, this.getClass().getName());
654
        }
655
656
        return xml;
657
    }
658
659
    /**
660
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
661
     *      com.iver.cit.gvsig.project.Project)
662
     */
663
    public void setXMLEntity03(XMLEntity xml, Layout l) {
664
        if (xml.getIntProperty("m_Selected") != 0) {
665
            this.setSelected(true);
666
        } else {
667
            this.setSelected(false);
668
        }
669
670
        this.m_numLayers = xml.getIntProperty("m_numLayers");
671
        this.m_max = xml.getIntProperty("m_max");
672
        this.m_quality = xml.getIntProperty("m_quality");
673
        this.m_viewing = xml.getIntProperty("m_viewing");
674
        this.m_f = new Font(xml.getStringProperty("fontName"),
675
                xml.getIntProperty("fontStyle"), 9);
676
677
        if (xml.contains("index")) {
678
            fframeview = (FFrameView) l.getLayoutContext().getFFrame(xml.getIntProperty("index"));
679
        }
680
    }
681
682
    /**
683
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
684
     *      com.iver.cit.gvsig.project.Project)
685
     */
686
    public void setXMLEntity(XMLEntity xml) {
687
        if (xml.getIntProperty("m_Selected") != 0) {
688
            this.setSelected(true);
689
        } else {
690
            this.setSelected(false);
691
        }
692
693
        this.m_numLayers = xml.getIntProperty("m_numLayers");
694
        this.m_max = xml.getIntProperty("m_max");
695
        this.m_quality = xml.getIntProperty("m_quality");
696
        this.m_viewing = xml.getIntProperty("m_viewing");
697
        this.m_f = new Font(xml.getStringProperty("fontName"),
698
                xml.getIntProperty("fontStyle"), 9);
699
        setRotation(xml.getDoubleProperty("m_rotation"));
700
701
        if (xml.contains("index")) {
702
            dependenceIndex = xml.getIntProperty("index");
703
        }
704
        if (xml.contains("nameLayers")) {
705
                String[] s=xml.getStringArrayProperty("nameLayers");
706
                boolean[] b=xml.getBooleanArrayProperty("areVisible");
707
                for (int i=0;i<s.length;i++) {
708
                        nameLayers.add(s[i]);
709
                        areVisible.add(new Boolean(b[i]));
710
                }
711
        }
712
    }
713
714 17187 jdominguez
    /*
715 9392 caballero
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#getNameFFrame()
716
     */
717
    public String getNameFFrame() {
718
        return PluginServices.getText(this, "leyenda")+ num;
719
    }
720
721 17187 jdominguez
    /*
722 9392 caballero
     * @see com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame#print(java.awt.Graphics2D,
723
     *      java.awt.geom.AffineTransform)
724
     */
725 28368 vcaballero
    public void print(Graphics2D g, AffineTransform at,FShape shape, PrintRequestAttributeSet properties) {
726
            this.properties=properties;
727 28568 vcaballero
        draw(g, at, null, null);
728 28368 vcaballero
        this.properties=null;
729 9392 caballero
    }
730
731
    /**
732
     * Actualiza las dependencias que tenga este FFrame con el resto.
733
     *
734
     * @param fframes Resto de FFrames.
735
     */
736
    public void initDependence(IFFrame[] fframes) {
737
        if ((dependenceIndex != -1) &&
738
                fframes[dependenceIndex] instanceof FFrameView) {
739
            fframeview = (FFrameView) fframes[dependenceIndex];
740
        }
741
    }
742
743
        public void initialize() {
744
                // TODO Auto-generated method stub
745
746
        }
747
748
        public void setNameLayers(ArrayList nameLayers) {
749
                this.nameLayers=nameLayers;
750
        }
751
752
        public void setAreVisible(ArrayList areVisible) {
753
                this.areVisible=areVisible;
754
        }
755
756
        public ArrayList getNameLayers() {
757
                return nameLayers;
758
        }
759
760
        public ArrayList getAreVisible() {
761
                return areVisible;
762
        }
763
764
        public void cloneActions(IFFrame frame) {
765
                // TODO Auto-generated method stub
766
767
        }
768
769
        public IFFrameDialog getPropertyDialog() {
770
                return new FFrameLegendDialog(getLayout(),this);
771
        }
772
}