Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / VectorialIntervalLegend.java @ 3035

History | View | Annotate | Download (20.2 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("labelfield", labelFieldName);
349
        xml.putProperty("labelFieldHeight", labelFieldHeight);
350
        xml.putProperty("labelFieldRotation", labelFieldRotation);
351
        
352
        xml.putProperty("intervalType", intervalType);
353
        xml.putProperty("numKeys", keys.size());
354

    
355
        if (keys.size() > 0) {
356
            xml.putProperty("tipoValueKeys", keys.get(0).getClass().getName());
357

    
358
            String[] sk = new String[keys.size()];
359

    
360
            for (int i = 0; i < keys.size(); i++) {
361
                sk[i] = ((IInterval) keys.get(i)).toString();
362
            }
363

    
364
            xml.putProperty("keys", getValues());
365

    
366
            for (int i = 0; i < keys.size(); i++) {
367
                xml.addChild(getSymbols()[i].getXMLEntity());
368
            }
369
        }
370

    
371
        xml.putProperty("startColor", StringUtilities.color2String(startColor));
372
        xml.putProperty("endColor", StringUtilities.color2String(endColor));
373

    
374
        ///xml.putProperty("numSymbols", symbols.size());
375
        ///xml.putProperty("indexs", getIndexs());
376
        ///xml.putProperty("values", getValues());
377
        return xml;
378
    }
379

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

    
390
        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.
391
            intervalType = xml.getIntProperty("intervalType");
392
        }
393

    
394
        int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
395

    
396
        if (useDefaultSymbol == 1) {
397
            setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
398
        } else {
399
            setDefaultSymbol(null);
400
        }
401

    
402
        int numKeys = xml.getIntProperty("numKeys");
403

    
404
        if (numKeys > 0) {
405
            String className = xml.getStringProperty("tipoValueKeys");
406
            String[] sk = xml.getStringArrayProperty("keys");
407
            IInterval auxInterval;
408

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

    
420
        startColor = StringUtilities.string2Color(xml.getStringProperty(
421
                    "startColor"));
422
        endColor = StringUtilities.string2Color(xml.getStringProperty(
423
                    "endColor"));
424
    }
425

    
426
    /**
427
     * Inserta los atributos del XMLEntity.
428
     *
429
     * @param xml XMLEntity.
430
     */
431
    public void setXMLEntity(XMLEntity xml) {
432
        fieldName = xml.getStringProperty("fieldName");
433
        index = xml.getIntProperty("index");
434
        labelFieldName = xml.getStringProperty("labelfield");
435
        if (xml.contains("labelFieldHeight")) {
436
            setLabelHeightField(xml.getStringProperty("labelFieldHeight"));
437
        }
438

    
439
        if (xml.contains("labelFieldRotation")) {
440
            setLabelRotationField(xml.getStringProperty("labelFieldRotation"));
441
        }
442

    
443
        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.
444
            intervalType = xml.getIntProperty("intervalType");
445
        }
446

    
447
        int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
448

    
449
        if (useDefaultSymbol == 1) {
450
            setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
451
            this.useDefaultSymbol=true;
452
        } else {
453
            setDefaultSymbol(null);
454
            this.useDefaultSymbol=false;
455
        }
456

    
457
        int numKeys = xml.getIntProperty("numKeys");
458

    
459
        if (numKeys > 0) {
460
            String className = xml.getStringProperty("tipoValueKeys");
461
            String[] sk = xml.getStringArrayProperty("keys");
462
            IInterval auxInterval;
463

    
464
            for (int i = 0; i < numKeys; i++) {
465
                auxInterval = FInterval.create(sk[i]);
466
                symbols.put(auxInterval,
467
                    FSymbol.createFromXML(xml.getChild(i + useDefaultSymbol)));
468
                keys.add(auxInterval);
469
                System.out.println("auxInterval =" + auxInterval + "Symbol =" +
470
                    FSymbol.createFromXML(xml.getChild(i + useDefaultSymbol))
471
                           .getDescription());
472
            }
473
        }
474

    
475
        startColor = StringUtilities.string2Color(xml.getStringProperty(
476
                    "startColor"));
477
        endColor = StringUtilities.string2Color(xml.getStringProperty(
478
                    "endColor"));
479
    }
480

    
481
    /**
482
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
483
     */
484
    public Legend cloneLegend() throws XMLException {
485
        return (Legend) LegendFactory.createFromXML(getXMLEntity());
486
    }
487

    
488
    /* (non-Javadoc)
489
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
490
     */
491
    public void setDataSource(DataSource ds)
492
        throws FieldNotFoundException, DriverException {
493
        try {
494
            dataSource = ds;
495
            ds.start();
496
            fieldId = ds.getFieldIndexByName(fieldName);
497
            ds.stop();
498
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
499
            throw new DriverException(e);
500
        }
501
    }
502

    
503
    /**
504
     * Devuelve el intervalo a partir del valor.
505
     *
506
     * @param v valor.
507
     *
508
     * @return intervalo.
509
     */
510
    public IInterval getInterval(Value v) {
511
        /*if (v instanceof NullValue){
512
           System.out.println("Si");
513
           }*/
514
        for (int i = 0; i < keys.size(); i++) {
515
            if (((IInterval) keys.get(i)).isInInterval(v)) {
516
                return (IInterval) keys.get(i);
517
            }
518
        }
519

    
520
        return null;
521
    }
522

    
523
    /**
524
     * Devuelve el color final.
525
     *
526
     * @return color final.
527
     */
528
    public Color getEndColor() {
529
        return endColor;
530
    }
531

    
532
    /**
533
     * Inserta el color final.
534
     *
535
     * @param endColor Color final.
536
     */
537
    public void setEndColor(Color endColor) {
538
        this.endColor = endColor;
539
    }
540

    
541
    /**
542
     * Devuelve el color inicial.
543
     *
544
     * @return Color inicial.
545
     */
546
    public Color getStartColor() {
547
        return startColor;
548
    }
549

    
550
    /**
551
     * Inserta el color inicial.
552
     *
553
     * @param startColor Color inicial.
554
     */
555
    public void setStartColor(Color startColor) {
556
        this.startColor = startColor;
557
    }
558

    
559
    /* (non-Javadoc)
560
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getShapeType()
561
     */
562
    public int getShapeType() {
563
        return shapeType;
564
    }
565

    
566
    /* (non-Javadoc)
567
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setShapeType(int)
568
     */
569
    public void setShapeType(int shapeType) {
570
        if (this.shapeType != shapeType) {
571
            switch (shapeType) {
572
                case FShape.POINT:
573
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
574

    
575
                    break;
576

    
577
                case FShape.LINE:
578
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
579

    
580
                    break;
581

    
582
                case FShape.POLYGON:
583
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
584

    
585
                    break;
586

    
587
                default:
588
                    defaultSymbol = new FSymbol(shapeType);
589
            }
590

    
591
            this.shapeType = shapeType;
592
        }
593
    }
594

    
595
    /* (non-Javadoc)
596
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
597
     */
598
    public String getLabelHeightField() {
599
        return labelFieldHeight;
600
    }
601

    
602
    /**
603
     * Inserta el alto del campo.
604
     *
605
     * @param str alto.
606
     */
607
    public void setLabelHeightField(String str) {
608
        labelFieldHeight = str;
609
    }
610

    
611
    /* (non-Javadoc)
612
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
613
     */
614
    public String getLabelRotationField() {
615
        return labelFieldRotation;
616
    }
617

    
618
    /**
619
     * Inserta la rotaci?n del campo.
620
     *
621
     * @param str Rotaci?n.
622
     */
623
    public void setLabelRotationField(String str) {
624
        labelFieldRotation = str;
625
    }
626

    
627
    /**
628
     * Inserta el tipo de clasificaci?n de los intervalos.
629
     *
630
     * @param tipoClasificacion Tipo de clasificaci?n.
631
     */
632
    public void setIntervalType(int tipoClasificacion) {
633
        intervalType = tipoClasificacion;
634
    }
635

    
636
    /**
637
     * Devuelve el tipo de clasificaci?n de los intervalos.
638
     *
639
     * @return Tipo de clasificaci?n.
640
     */
641
    public int getIntervalType() {
642
        return intervalType;
643
    }
644

    
645
    /**
646
     * Inserta si se representan el resto de valores o no.
647
     *
648
     * @param b True si se tienen que representar el resto de valores.
649
     */
650
    public void useDefaultSymbol(boolean b) {
651
        useDefaultSymbol = b;
652
    }
653

    
654
    /**
655
     * Devuelve si se utiliza o no el resto de valores para representarse.
656
     *
657
     * @return True si se utiliza el resto de valores.
658
     */
659
    public boolean isUseDefaultSymbol() {
660
        return useDefaultSymbol;
661
    }
662

    
663
    /**
664
     * Devuelve si ha sido modificado el etiquetado de la capa.
665
     *
666
     * @return True si el etiquetado de la capa ha sido modificado.
667
     */
668
    /* public boolean isBWithHeightText() {
669
        return bWithHeightText;
670
    } */
671

    
672
    /**
673
     * Introduce si el etiquetado de la capa ha sido modificado.
674
     *
675
     * @param withHeightText Boolean que indica si el etiquetado de la capa ha
676
     *        sido modificado.
677
     */
678
    /* public void setBWithHeightText(boolean withHeightText) {
679
        bWithHeightText = withHeightText;
680
    } */
681

    
682
    /**
683
     * Elimina un s?mbolo a partir de su clave.
684
     *
685
     * @param obj clave.
686
     */
687
    public void delSymbol(Object obj) {
688
        keys.remove(obj);
689
        symbols.remove(obj);
690
    }
691
}