Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / VectorialIntervalLegend.java @ 2796

History | View | Annotate | Download (19.7 KB)

1
/* 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.fmap.rendering;
42

    
43
import com.hardcode.gdbms.engine.data.DataSource;
44
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
45
import com.hardcode.gdbms.engine.values.Value;
46

    
47
import com.iver.cit.gvsig.fmap.DriverException;
48
import com.iver.cit.gvsig.fmap.core.FShape;
49
import com.iver.cit.gvsig.fmap.core.IFeature;
50
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
51
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
52
import com.iver.cit.gvsig.fmap.layers.XMLException;
53

    
54
import com.iver.utiles.StringUtilities;
55
import com.iver.utiles.XMLEntity;
56

    
57
import java.awt.Color;
58

    
59
import java.util.ArrayList;
60
import java.util.Comparator;
61
import java.util.TreeMap;
62

    
63

    
64
/**
65
 * Leyenda Vectorial por intervalos.
66
 *
67
 * @author Vicente Caballero Navarro
68
 */
69
public class VectorialIntervalLegend implements IntervalLegend, VectorialLegend {
70
    public final static int EQUAL_INTERVALS = 0;
71
    public final static int NATURAL_INTERVALS = 1;
72
    public final static int QUANTILE_INTERVALS = 2;
73
    private TreeMap symbols = new TreeMap(new Comparator() {
74
                public int compare(Object o1, Object o2) {
75
                    if ((o1 != null) && (o2 != null)) {
76
                       if (o1 instanceof NullIntervalValue &&
77
                                o2 instanceof NullIntervalValue) {
78
                            return 0;
79
                        }
80

    
81
                        if (o2 instanceof NullIntervalValue) {
82
                            return 1;
83
                        }
84

    
85
                        if (o1 instanceof NullIntervalValue) {
86
                            return -1;
87
                        }
88

    
89
                        FInterval i2 = (FInterval) o2;
90
                        FInterval i1 = (FInterval) o1;
91

    
92
                        if (i1.getMin() > i2.getMin()) {
93
                            return 1;
94
                        }
95

    
96
                        if (i1.getMin() < i2.getMin()) {
97
                            return -1;
98
                        }
99
                    }
100

    
101
                    return 0;
102
                }
103
            }); // Para poder ordenar
104
    private ArrayList keys = new ArrayList(); // En lugar de un HashSet, para tener acceso por ?ndice
105
    private int index = 0;
106
    private String fieldName;
107
    private int fieldId;
108
    private String labelFieldName;
109
    private String labelFieldHeight;
110
    private String labelFieldRotation;
111
    private FSymbol defaultSymbol;
112
    private DataSource dataSource;
113
    private Color startColor = Color.red;
114
    private Color endColor = Color.blue;
115
    private int shapeType;
116
    private int intervalType = NATURAL_INTERVALS;
117
    private boolean useDefaultSymbol = false;
118
    private boolean bWithHeightText;
119

    
120
    /**
121
     * Crea un nuevo VectorialIntervalLegend.
122
     */
123
    public VectorialIntervalLegend() {
124
        //defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
125
    }
126

    
127
    /**
128
     * Crea un nuevo VectorialIntervalLegend.
129
     *
130
     * @param type tipo de shape.
131
     */
132
    public VectorialIntervalLegend(int type) {
133
        setShapeType(type);
134
    }
135

    
136
    /**
137
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#addSymbol(java.lang.Object,
138
     *      FSymbol)
139
     */
140
    public void addSymbol(Object key, FSymbol symbol) {
141
        //TODO guardar los intervalos.
142
        Object resul;
143
        resul = symbols.put(key, symbol);
144

    
145
        /*if (resul != null) {
146
           System.err.println("Error: la clave " + key +
147
                   " ya exist?a. Resul = " + resul);
148
           System.err.println("symbol nuevo:" + symbol.m_Descrip +
149
                   " Sviejo= " + ((FSymbol) resul).m_Descrip);
150
           } else {
151
         */
152
        keys.add(key);
153

    
154
        //}
155
    }
156

    
157
    /*
158
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getSymbol(java.lang.Object)
159
     *
160
                   public FStyle2D getSymbol(Object value) {
161
                       return (FStyle2D) symbols.get(value);
162
                   }
163
                   // TODO transformar la funci?n anterior en la siguiente
164
     *
165
     */
166

    
167
    /**
168
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getSymbol(int)
169
     */
170
    public FSymbol getSymbol(int recordIndex) throws DriverException {
171
        try {
172
            Value val = dataSource.getFieldValue(recordIndex, fieldId);
173
            IInterval interval = getInterval(val);
174
            FSymbol theSymbol = getSymbolByInterval(interval);
175

    
176
            if (theSymbol != null) {
177
                return theSymbol;
178
            } else if (useDefaultSymbol) {
179
                return getDefaultSymbol();
180
            }
181

    
182
            return null;
183
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
184
            throw new DriverException(e);
185
        }
186
    }
187

    
188
    /**
189
     * Devuelve un s?mbolo a partir de una IFeature.
190
     *
191
     * @param feat IFeature.
192
     *
193
     * @return S?mbolo.
194
     */
195
    public FSymbol getSymbolByFeature(IFeature feat) {
196
        Value val = feat.getAttribute(fieldId);
197
        IInterval interval = getInterval(val);
198
        FSymbol theSymbol = getSymbolByInterval(interval);
199

    
200
        if (theSymbol != null) {
201
            return theSymbol;
202
        } else {
203
            return getDefaultSymbol();
204
        }
205
    }
206

    
207
    /**
208
     * Devuelve el s?mbolo a partir del intervalo.
209
     *
210
     * @param key intervalo.
211
     *
212
     * @return s?mbolo.
213
     */
214
    public FSymbol getSymbolByInterval(IInterval key) {
215
        if (key == null) {
216
            return null;
217
        }
218

    
219
        if (symbols.containsKey(key)) {
220
            return (FSymbol) symbols.get(key);
221
        }
222

    
223
        return null;
224
    }
225

    
226
    /**
227
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo#getDescriptions()
228
     */
229
    public String[] getDescriptions() {
230
        String[] descriptions = new String[symbols.size()];
231
        FSymbol[] auxSym = getSymbols();
232

    
233
        for (int i = 0; i < descriptions.length; i++)
234
            descriptions[i] = auxSym[i].getDescription();
235

    
236
        return descriptions;
237
    }
238

    
239
    /**
240
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo#getValues()
241
     */
242
    public Object[] getValues() {
243
        return (Object[]) symbols.keySet().toArray(new Object[0]);
244
    }
245

    
246
    /**
247
     * @see com.iver.cit.gvsig.fmap.rendering.IntervalLegend#setIntervalSymbol(com.iver.cit.gvsig.fmap.rendering.FInterval,
248
     *      org.geotools.renderer.style.Style2D)
249
     */
250
    public void setIntervalSymbol(IInterval interval, FSymbol symbol) {
251
        /*symbols.put(interval, symbol);
252
           values.put(new Integer(index), interval);
253
           index++;
254
         */
255
    }
256

    
257
    /**
258
     * @see com.iver.cit.gvsig.fmap.rendering.IntervalLegend#changeInterval(int,
259
     *      com.iver.cit.gvsig.fmap.rendering.FInterval)
260
     */
261
    public void changeInterval(int index, IInterval newInterval) {
262
        /*Object value = values.remove(new Integer(index));
263
           Object symbol = symbols.remove(value);
264
           values.put(new Integer(index), newInterval);
265
           symbols.put(newInterval, symbol);
266
         */
267
    }
268

    
269
    /**
270
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#clear()
271
     */
272
    public void clear() {
273
        index = 0;
274
        keys.clear();
275
        symbols.clear();
276
    }
277

    
278
    /**
279
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getSymbols()
280
     */
281
    public FSymbol[] getSymbols() {
282
        return (FSymbol[]) symbols.values().toArray(new FSymbol[0]);
283
    }
284

    
285
    /**
286
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getFieldName()
287
     */
288
    public String getFieldName() {
289
        return fieldName;
290
    }
291

    
292
    /**
293
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDefaultSymbol(com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D)
294
     */
295
    public void setDefaultSymbol(FSymbol s) {
296
        defaultSymbol = s;
297
    }
298

    
299
    /**
300
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#setFieldName(String)
301
     */
302
    public void setFieldName(String str) {
303
        fieldName = str;
304
    }
305

    
306
    /**
307
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#setLabelField(int)
308
     */
309
    public void setLabelField(String fieldName) {
310
        labelFieldName = fieldName;
311
    }
312

    
313
    /**
314
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getLabelField()
315
     */
316
    public String getLabelField() {
317
        return labelFieldName;
318
    }
319

    
320
    /**
321
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
322
     */
323
    public FSymbol getDefaultSymbol() {
324
        return defaultSymbol;
325
    }
326

    
327
    /**
328
     * DOCUMENT ME!
329
     *
330
     * @return DOCUMENT ME!
331
     *
332
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
333
     */
334
    public XMLEntity getXMLEntity() {
335
        XMLEntity xml = new XMLEntity();
336
        xml.putProperty("className", this.getClass().getName());
337

    
338
        if (getDefaultSymbol() == null) {
339
            xml.putProperty("useDefaultSymbol", 0);
340
        } else {
341
            xml.putProperty("useDefaultSymbol", 1);
342
            xml.addChild(getDefaultSymbol().getXMLEntity());
343
        }
344

    
345
        xml.putProperty("fieldName", fieldName);
346
        xml.putProperty("index", index);
347
        xml.putProperty("labelfield", labelFieldName);
348
        xml.putProperty("intervalType", intervalType);
349
        xml.putProperty("numKeys", keys.size());
350

    
351
        if (keys.size() > 0) {
352
            xml.putProperty("tipoValueKeys", keys.get(0).getClass().getName());
353

    
354
            String[] sk = new String[keys.size()];
355

    
356
            for (int i = 0; i < keys.size(); i++) {
357
                sk[i] = ((IInterval) keys.get(i)).toString();
358
            }
359

    
360
            xml.putProperty("keys", getValues());
361

    
362
            for (int i = 0; i < keys.size(); i++) {
363
                xml.addChild(getSymbols()[i].getXMLEntity());
364
            }
365
        }
366

    
367
        xml.putProperty("startColor", StringUtilities.color2String(startColor));
368
        xml.putProperty("endColor", StringUtilities.color2String(endColor));
369

    
370
        ///xml.putProperty("numSymbols", symbols.size());
371
        ///xml.putProperty("indexs", getIndexs());
372
        ///xml.putProperty("values", getValues());
373
        return xml;
374
    }
375

    
376
    /**
377
     * Inserta los atributos del XMLEntity.
378
     *
379
     * @param xml XMLEntity.
380
     */
381
    public void setXMLEntity03(XMLEntity xml) {
382
        fieldName = xml.getStringProperty("fieldName");
383
        index = xml.getIntProperty("index");
384
        labelFieldName = xml.getStringProperty("labelfield");
385

    
386
        if (xml.contains("intervalType")) { //TODO Esta condici?n es para poder cargar la versi?n 0.3, se puede eliminar cuando ya no queramos soportar esta versi?n.
387
            intervalType = xml.getIntProperty("intervalType");
388
        }
389

    
390
        int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
391

    
392
        if (useDefaultSymbol == 1) {
393
            setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
394
        } else {
395
            setDefaultSymbol(null);
396
        }
397

    
398
        int numKeys = xml.getIntProperty("numKeys");
399

    
400
        if (numKeys > 0) {
401
            String className = xml.getStringProperty("tipoValueKeys");
402
            String[] sk = xml.getStringArrayProperty("keys");
403
            IInterval auxInterval;
404

    
405
            for (int i = 0; i < numKeys; i++) {
406
                auxInterval = FInterval.create(sk[i]);
407
                symbols.put(auxInterval,
408
                    FSymbol.createFromXML03(xml.getChild(i + useDefaultSymbol)));
409
                keys.add(auxInterval);
410
                System.out.println("auxInterval =" + auxInterval + "Symbol =" +
411
                    FSymbol.createFromXML03(xml.getChild(i + useDefaultSymbol))
412
                           .getDescription());
413
            }
414
        }
415

    
416
        startColor = StringUtilities.string2Color(xml.getStringProperty(
417
                    "startColor"));
418
        endColor = StringUtilities.string2Color(xml.getStringProperty(
419
                    "endColor"));
420
    }
421

    
422
    /**
423
     * Inserta los atributos del XMLEntity.
424
     *
425
     * @param xml XMLEntity.
426
     */
427
    public void setXMLEntity(XMLEntity xml) {
428
        fieldName = xml.getStringProperty("fieldName");
429
        index = xml.getIntProperty("index");
430
        labelFieldName = xml.getStringProperty("labelfield");
431

    
432
        if (xml.contains("intervalType")) { //TODO Esta condici?n es para poder cargar la versi?n 0.3, se puede eliminar cuando ya no queramos soportar esta versi?n.
433
            intervalType = xml.getIntProperty("intervalType");
434
        }
435

    
436
        int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
437

    
438
        if (useDefaultSymbol == 1) {
439
            setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
440
            this.useDefaultSymbol=true;
441
        } else {
442
            setDefaultSymbol(null);
443
            this.useDefaultSymbol=false;
444
        }
445

    
446
        int numKeys = xml.getIntProperty("numKeys");
447

    
448
        if (numKeys > 0) {
449
            String className = xml.getStringProperty("tipoValueKeys");
450
            String[] sk = xml.getStringArrayProperty("keys");
451
            IInterval auxInterval;
452

    
453
            for (int i = 0; i < numKeys; i++) {
454
                auxInterval = FInterval.create(sk[i]);
455
                symbols.put(auxInterval,
456
                    FSymbol.createFromXML(xml.getChild(i + useDefaultSymbol)));
457
                keys.add(auxInterval);
458
                System.out.println("auxInterval =" + auxInterval + "Symbol =" +
459
                    FSymbol.createFromXML(xml.getChild(i + useDefaultSymbol))
460
                           .getDescription());
461
            }
462
        }
463

    
464
        startColor = StringUtilities.string2Color(xml.getStringProperty(
465
                    "startColor"));
466
        endColor = StringUtilities.string2Color(xml.getStringProperty(
467
                    "endColor"));
468
    }
469

    
470
    /**
471
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
472
     */
473
    public Legend cloneLegend() throws XMLException {
474
        return (Legend) LegendFactory.createFromXML(getXMLEntity());
475
    }
476

    
477
    /* (non-Javadoc)
478
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
479
     */
480
    public void setDataSource(DataSource ds)
481
        throws FieldNotFoundException, DriverException {
482
        try {
483
            dataSource = ds;
484
            ds.start();
485
            fieldId = ds.getFieldIndexByName(fieldName);
486
            ds.stop();
487
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
488
            throw new DriverException(e);
489
        }
490
    }
491

    
492
    /**
493
     * Devuelve el intervalo a partir del valor.
494
     *
495
     * @param v valor.
496
     *
497
     * @return intervalo.
498
     */
499
    public IInterval getInterval(Value v) {
500
        /*if (v instanceof NullValue){
501
           System.out.println("Si");
502
           }*/
503
        for (int i = 0; i < keys.size(); i++) {
504
            if (((IInterval) keys.get(i)).isInInterval(v)) {
505
                return (IInterval) keys.get(i);
506
            }
507
        }
508

    
509
        return null;
510
    }
511

    
512
    /**
513
     * Devuelve el color final.
514
     *
515
     * @return color final.
516
     */
517
    public Color getEndColor() {
518
        return endColor;
519
    }
520

    
521
    /**
522
     * Inserta el color final.
523
     *
524
     * @param endColor Color final.
525
     */
526
    public void setEndColor(Color endColor) {
527
        this.endColor = endColor;
528
    }
529

    
530
    /**
531
     * Devuelve el color inicial.
532
     *
533
     * @return Color inicial.
534
     */
535
    public Color getStartColor() {
536
        return startColor;
537
    }
538

    
539
    /**
540
     * Inserta el color inicial.
541
     *
542
     * @param startColor Color inicial.
543
     */
544
    public void setStartColor(Color startColor) {
545
        this.startColor = startColor;
546
    }
547

    
548
    /* (non-Javadoc)
549
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getShapeType()
550
     */
551
    public int getShapeType() {
552
        return shapeType;
553
    }
554

    
555
    /* (non-Javadoc)
556
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setShapeType(int)
557
     */
558
    public void setShapeType(int shapeType) {
559
        if (this.shapeType != shapeType) {
560
            switch (shapeType) {
561
                case FShape.POINT:
562
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
563

    
564
                    break;
565

    
566
                case FShape.LINE:
567
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
568

    
569
                    break;
570

    
571
                case FShape.POLYGON:
572
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
573

    
574
                    break;
575

    
576
                default:
577
                    defaultSymbol = new FSymbol(shapeType);
578
            }
579

    
580
            this.shapeType = shapeType;
581
        }
582
    }
583

    
584
    /* (non-Javadoc)
585
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
586
     */
587
    public String getLabelHeightField() {
588
        return labelFieldHeight;
589
    }
590

    
591
    /**
592
     * Inserta el alto del campo.
593
     *
594
     * @param str alto.
595
     */
596
    public void setLabelHeightField(String str) {
597
        labelFieldHeight = str;
598
    }
599

    
600
    /* (non-Javadoc)
601
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
602
     */
603
    public String getLabelRotationField() {
604
        return labelFieldRotation;
605
    }
606

    
607
    /**
608
     * Inserta la rotaci?n del campo.
609
     *
610
     * @param str Rotaci?n.
611
     */
612
    public void setLabelRotationField(String str) {
613
        labelFieldRotation = str;
614
    }
615

    
616
    /**
617
     * Inserta el tipo de clasificaci?n de los intervalos.
618
     *
619
     * @param tipoClasificacion Tipo de clasificaci?n.
620
     */
621
    public void setIntervalType(int tipoClasificacion) {
622
        intervalType = tipoClasificacion;
623
    }
624

    
625
    /**
626
     * Devuelve el tipo de clasificaci?n de los intervalos.
627
     *
628
     * @return Tipo de clasificaci?n.
629
     */
630
    public int getIntervalType() {
631
        return intervalType;
632
    }
633

    
634
    /**
635
     * Inserta si se representan el resto de valores o no.
636
     *
637
     * @param b True si se tienen que representar el resto de valores.
638
     */
639
    public void useDefaultSymbol(boolean b) {
640
        useDefaultSymbol = b;
641
    }
642

    
643
    /**
644
     * Devuelve si se utiliza o no el resto de valores para representarse.
645
     *
646
     * @return True si se utiliza el resto de valores.
647
     */
648
    public boolean isUseDefaultSymbol() {
649
        return useDefaultSymbol;
650
    }
651

    
652
    /**
653
     * Devuelve si ha sido modificado el etiquetado de la capa.
654
     *
655
     * @return True si el etiquetado de la capa ha sido modificado.
656
     */
657
    public boolean isBWithHeightText() {
658
        return bWithHeightText;
659
    }
660

    
661
    /**
662
     * Introduce si el etiquetado de la capa ha sido modificado.
663
     *
664
     * @param withHeightText Boolean que indica si el etiquetado de la capa ha
665
     *        sido modificado.
666
     */
667
    public void setBWithHeightText(boolean withHeightText) {
668
        bWithHeightText = withHeightText;
669
    }
670

    
671
    /**
672
     * Elimina un s?mbolo a partir de su clave.
673
     *
674
     * @param obj clave.
675
     */
676
    public void delSymbol(Object obj) {
677
        keys.remove(obj);
678
        symbols.remove(obj);
679
    }
680
}