Statistics
| Revision:

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

History | View | Annotate | Download (20.8 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.instruction.IncompatibleTypesException;
46
import com.hardcode.gdbms.engine.instruction.SemanticException;
47
import com.hardcode.gdbms.engine.values.BooleanValue;
48
import com.hardcode.gdbms.engine.values.NullValue;
49
import com.hardcode.gdbms.engine.values.StringValue;
50
import com.hardcode.gdbms.engine.values.Value;
51
import com.hardcode.gdbms.engine.values.ValueFactory;
52

    
53
import com.iver.cit.gvsig.fmap.DriverException;
54
import com.iver.cit.gvsig.fmap.core.FShape;
55
import com.iver.cit.gvsig.fmap.core.IFeature;
56
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
57
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
58
import com.iver.cit.gvsig.fmap.layers.XMLException;
59

    
60
import com.iver.utiles.XMLEntity;
61

    
62
import java.sql.Types;
63

    
64
import java.text.ParseException;
65

    
66
import java.util.ArrayList;
67
import java.util.Comparator;
68
import java.util.TreeMap;
69

    
70

    
71
/**
72
 * Leyenda vectorial por valores ?nicos.
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
76
public class VectorialUniqueValueLegend implements UniqueValueLegend,
77
    VectorialLegend {
78
    private TreeMap symbols = new TreeMap(new Comparator() {
79
                public int compare(Object o1, Object o2) {
80
                    if ((o1 != null) && (o2 != null)) {
81
                        Value v2 = (Value) o2;
82
                        Value v1 = (Value) o1;
83
                        BooleanValue boolVal;
84

    
85
                        //                                                TODO estas dos comprobaciones son por evitar un bug en el gdbms, cuando se solucione se puede eliminar.
86
                        if (v1 instanceof NullValue && v2 instanceof NullValue) {
87
                            return 0;
88
                        }
89

    
90
                        if (v1 instanceof NullValue) {
91
                            return -1;
92
                        }
93

    
94
                        if (v2 instanceof NullValue) {
95
                            return 1;
96
                        }
97

    
98
                        try {
99
                            boolVal = (BooleanValue) (v1.greater(v2));
100

    
101
                            if (boolVal.getValue()) {
102
                                return 1;
103
                            }
104

    
105
                            boolVal = (BooleanValue) (v1.less(v2));
106

    
107
                            if (boolVal.getValue()) {
108
                                return -1;
109
                            }
110
                        } catch (IncompatibleTypesException e) {
111
                            // TODO Auto-generated catch block
112
                            //e.printStackTrace();
113
                        }
114

    
115
                        try {
116
                            if (((BooleanValue) v1.equals(v2)).getValue()) {
117
                                return 0;
118
                            }
119
                        } catch (IncompatibleTypesException e) {
120
                            // TODO Auto-generated catch block
121
                            //e.printStackTrace();
122
                        }
123

    
124
                        if (v1 instanceof StringValue) {
125
                            return -1;
126
                        }
127

    
128
                        if (v2 instanceof StringValue) {
129
                            return 1;
130
                        }
131
                    }
132

    
133
                    return 0;
134
                }
135
            }); // Para poder ordenar
136
    private ArrayList keys = new ArrayList(); // En lugar de un HashSet, para tener acceso por ?ndice
137
    private String fieldName;
138
    private int fieldId = -1;
139
    private String labelFieldName;
140
    private String labelFieldHeight;
141
    private String labelFieldRotation;
142
    private FSymbol defaultSymbol;
143
    private DataSource dataSource;
144
    private int shapeType;
145
    private String valueType = NullValue.class.getName();
146
    private boolean useDefaultSymbol = false;
147
    // private boolean bWithHeightText = false;
148

    
149
    /**
150
     * Crea un nuevo VectorialUniqueValueLegend.
151
     */
152
    public VectorialUniqueValueLegend() {
153
        //        defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
154
    }
155

    
156
    /**
157
     * Crea un nuevo VectorialUniqueValueLegend.
158
     *
159
     * @param shapeType Tipo de shape.
160
     */
161
    public VectorialUniqueValueLegend(int shapeType) {
162
        setShapeType(shapeType);
163
    }
164

    
165
    /**
166
     * Inserta el tipo de shape.
167
     *
168
     * @param shapeType Tipo de shape.
169
     */
170
    public void setShapeType(int shapeType) {
171
        if (this.shapeType != shapeType) {
172
            switch (shapeType) {
173
                case FShape.POINT:
174
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
175

    
176
                    break;
177

    
178
                case FShape.LINE:
179
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
180

    
181
                    break;
182

    
183
                case FShape.POLYGON:
184
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
185

    
186
                    break;
187

    
188
                default:
189
                    defaultSymbol = new FSymbol(shapeType);
190
            }
191

    
192
            this.shapeType = shapeType;
193
        }
194
    }
195

    
196
    /**
197
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#setValueSymbolByID(int,
198
     *      FSymbol)
199
     */
200
    public void setValueSymbolByID(int id, FSymbol symbol) {
201
        symbols.put(keys.get(id), symbol);
202
    }
203

    
204
    /**
205
     * Devuelve un s?mbolo a partir del ID. Mira en el m_ArrayKeys  el elemento
206
     * ID, y con esa clave recupera el FSymbol por valor
207
     *
208
     * @param id ID.
209
     * @param symbol DOCUMENT ME!
210
     */
211

    
212
    /*
213
       public FSymbol getSymbolByID(int ID) {
214
           return (FSymbol) symbols.get(keys.get(ID));
215
       }
216
     */
217

    
218
    /**
219
     * Se usa en la tabla que muestra una leyenda.
220
     *
221
     * @param id
222
     * @param symbol
223
     */
224
    public void setValueSymbol(int id, FSymbol symbol) {
225
        symbols.put(keys.get(id), symbol);
226
    }
227

    
228
    /**
229
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getValues()
230
     */
231
    public Object[] getValues() {
232
        return symbols.keySet().toArray(new Object[0]);
233
    }
234

    
235
    /**
236
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#addSymbol(java.lang.Object,
237
     *      FSymbol)
238
     */
239
    public void addSymbol(Object key, FSymbol symbol) {
240
        Object resul;
241
        resul = symbols.put(key, symbol);
242

    
243
        if (resul != null) {
244
            System.err.println("Error: la clave " + key +
245
                " ya exist?a. Resul = " + resul);
246
            System.err.println("symbol nuevo:" + symbol.getDescription() +
247
                " Sviejo= " + ((FSymbol) resul).getDescription());
248
        } else {
249
            keys.add(key);
250

    
251
            if (!key.getClass().equals(NullValue.class)) {
252
                valueType = key.getClass().getName();
253
            }
254
        }
255
    }
256

    
257
    /**
258
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#clear()
259
     */
260
    public void clear() {
261
        keys.clear();
262
        symbols.clear();
263
    }
264

    
265
    /**
266
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getDescriptions()
267
     */
268
    public String[] getDescriptions() {
269
        String[] descriptions = new String[symbols.size()];
270
        FSymbol[] auxSym = getSymbols();
271

    
272
        for (int i = 0; i < descriptions.length; i++)
273
            descriptions[i] = auxSym[i].getDescription();
274

    
275
        return descriptions;
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
    /* (non-Javadoc)
300
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelField()
301
     */
302
    public String getLabelField() {
303
        return labelFieldName;
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.ClassifiedLegend#setField()
315
     */
316
    public void setFieldName(String str) {
317
        fieldName = str;
318
    }
319

    
320
    /**
321
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getSymbol(int)
322
     */
323
    public FSymbol getSymbol(int recordIndex) throws DriverException {
324
        try {
325
            Value val = dataSource.getFieldValue(recordIndex, fieldId);
326
            FSymbol theSymbol = getSymbolByValue(val);
327

    
328
            //if (theSymbol != null) {
329
            return theSymbol;
330

    
331
            //} else {
332
            //        return getDefaultSymbol();
333
            //}
334
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
335
            throw new DriverException(e);
336
        }
337
    }
338

    
339
    /**
340
     * Devuelve un s?mbolo a partir de una IFeature.
341
     *
342
     * @param feat IFeature
343
     *
344
     * @return S?mbolo.
345
     */
346
    public FSymbol getSymbolByFeature(IFeature feat) {
347
        Value val = feat.getAttribute(fieldId);
348
        FSymbol theSymbol = getSymbolByValue(val);
349

    
350
        //if (theSymbol != null) {
351
        return theSymbol;
352

    
353
        //} else {
354
        //    return getDefaultSymbol();
355
        //}
356
    }
357

    
358
    /**
359
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
360
     */
361
    public FSymbol getDefaultSymbol() {
362
        return defaultSymbol;
363
    }
364

    
365
    /**
366
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
367
     */
368
    public XMLEntity getXMLEntity() {
369
        XMLEntity xml = new XMLEntity();
370
        xml.putProperty("className", this.getClass().getName());
371
        xml.putProperty("fieldName", fieldName);
372
        xml.putProperty("labelfield", labelFieldName);
373
        xml.putProperty("labelFieldHeight", labelFieldHeight);
374
        xml.putProperty("labelFieldRotation", labelFieldRotation);
375

    
376
        xml.putProperty("useDefaultSymbol", useDefaultSymbol);
377
        xml.addChild(getDefaultSymbol().getXMLEntity());
378
        // xml.putProperty("isBWithHeightText", isBWithHeightText());
379
        xml.putProperty("numKeys", keys.size());
380

    
381
        if (keys.size() > 0) {
382
            xml.putProperty("tipoValueKeys", valueType);
383

    
384
            String[] sk = new String[keys.size()];
385
            String[] sv = new String[keys.size()];
386

    
387
            FSymbol[] fsymbols = getSymbols();
388
            Object[] values = (Object[]) getValues();
389

    
390
            for (int i = 0; i < keys.size(); i++) {
391
                sk[i] = ((Value) keys.get(i)).toString();
392
                sv[i] = ((Value) values[i]).toString();
393
                xml.addChild(fsymbols[i].getXMLEntity());
394

    
395
                ///System.out.println("get-----------"+sk[i]+"--"+fsymbols[i].getDescription()+"---"+fsymbols[i].getColor());
396
            }
397

    
398
            xml.putProperty("keys", sk);
399
            xml.putProperty("values", sv);
400
        }
401

    
402
        return xml;
403
    }
404

    
405
    /**
406
     * Inserta el XMLEntity.
407
     *
408
     * @param xml XMLEntity.
409
     */
410
    public void setXMLEntity03(XMLEntity xml) {
411
        clear();
412
        setFieldName(xml.getStringProperty("fieldName"));
413
        setLabelField(xml.getStringProperty("labelfield"));
414

    
415
        int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
416

    
417
        if (useDefaultSymbol == 1) {
418
            setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
419
        } else {
420
            setDefaultSymbol(null);
421
        }
422

    
423
        int numKeys = xml.getIntProperty("numKeys");
424

    
425
        if (numKeys > 0) {
426
            String className = xml.getStringProperty("tipoValueKeys");
427
            String[] sk = xml.getStringArrayProperty("keys");
428
            String[] sv = xml.getStringArrayProperty("values");
429
            Value auxValue;
430
            Value auxValue2;
431

    
432
            for (int i = 0; i < numKeys; i++) {
433
                try {
434
                    auxValue = ValueFactory.createValue(sk[i], className);
435
                    auxValue2 = ValueFactory.createValue(sv[i], className);
436

    
437
                    FSymbol sym = FSymbol.createFromXML03(xml.getChild(i +
438
                                useDefaultSymbol));
439

    
440
                    ///addSymbol(auxValue, sym);
441
                    symbols.put(auxValue2, sym);
442
                    keys.add(auxValue);
443

    
444
                    ///System.out.println("---set------"+auxValue.toString());
445
                    /// System.out.println("set-----------"+sk[i]+"--"+sym.getDescription()+"---"+sym.getColor());
446
                } catch (SemanticException e) {
447
                    // TODO Auto-generated catch block
448
                    e.printStackTrace();
449
                }
450
            }
451
        }
452
    }
453

    
454
    /**
455
     * Inserta el XMLEntity.
456
     *
457
     * @param xml XMLEntity.
458
     */
459
    public void setXMLEntity(XMLEntity xml) {
460
        clear();
461
        setFieldName(xml.getStringProperty("fieldName"));
462
        setLabelField(xml.getStringProperty("labelfield"));
463

    
464
        if (xml.contains("labelFieldHeight")) {
465
            setLabelHeightField(xml.getStringProperty("labelFieldHeight"));
466
        }
467

    
468
        if (xml.contains("labelFieldRotation")) {
469
            setLabelRotationField(xml.getStringProperty("labelFieldRotation"));
470
        }
471

    
472
        useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbol");
473
        setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
474

    
475
        // FJP: Esto no es necesario ya. Para comprobar si tenemos un campo de altura, miramos
476
        // si getLabelHeightField devuelve null o no.
477
        /* if (xml.contains("isBWithHeightText")) {
478
            setBWithHeightText(xml.getBooleanProperty("isBWithHeightText"));
479
        } */
480

    
481
        //addSymbol(new NullUniqueValue(),getDefaultSymbol());
482
        int numKeys = xml.getIntProperty("numKeys");
483

    
484
        if (numKeys > 0) {
485
            String className = xml.getStringProperty("tipoValueKeys");
486
            String[] sk = xml.getStringArrayProperty("keys");
487
            String[] sv = xml.getStringArrayProperty("values");
488
            Value auxValue = null;
489
            Value auxValue2 = null;
490

    
491
            for (int i = 0; i < numKeys; i++) {
492
                //try {
493
                boolean isDefault = false;
494

    
495
                if (getValue(sk[i]) == null) {
496
                    isDefault = true;
497
                }
498

    
499
                if (className.equals(
500
                            "com.hardcode.gdbms.engine.values.NullUniqueValue") ||
501
                        isDefault) {
502
                    auxValue = new NullUniqueValue();
503
                    auxValue2 = ValueFactory.createNullValue();
504
                } else {
505
                    auxValue = getValue(sk[i]); //ValueFactory.createValue(sk[i], className);
506
                    auxValue2 = getValue(sv[i]); // ValueFactory.createValue(sv[i], className);
507
                }
508

    
509
                FSymbol sym = FSymbol.createFromXML(xml.getChild(i + 1));
510

    
511
                ///addSymbol(auxValue, sym);
512
                symbols.put(auxValue2, sym);
513
                keys.add(auxValue);
514

    
515
                ///System.out.println("---set------"+auxValue.toString());
516
                /// System.out.println("set-----------"+sk[i]+"--"+sym.getDescription()+"---"+sym.getColor());
517
                //} catch (SemanticException e) {
518
                //        e.printStackTrace();
519
                //}
520
            }
521
        }
522
    }
523

    
524
    /**
525
     * Devuelve el valor a partir de su valor en un string.
526
     *
527
     * @param s String con el valor.
528
     *
529
     * @return Value.
530
     */
531
    private Value getValue(String s) {
532
        Value val = new NullUniqueValue();
533
        if (s.equals("Resto de Valores"))return val;
534
        try {
535
            try {
536
                val = ValueFactory.createValueByType(s, Types.INTEGER);
537

    
538
                return val;
539
            } catch (NumberFormatException e) {
540
            }
541

    
542
            try {
543
                val = ValueFactory.createValueByType(s, Types.BIGINT);
544

    
545
                return val;
546
            } catch (NumberFormatException e) {
547
            }
548

    
549
            try {
550
                val = ValueFactory.createValueByType(s, Types.FLOAT);
551

    
552
                return val;
553
            } catch (NumberFormatException e) {
554
            }
555

    
556
            try {
557
                val = ValueFactory.createValueByType(s, Types.DOUBLE);
558

    
559
                return val;
560
            } catch (NumberFormatException e) {
561
            }
562
            
563
            val = ValueFactory.createValueByType(s, Types.LONGVARCHAR);
564
           
565
        } catch (ParseException e) {
566
            e.printStackTrace();
567
        }
568

    
569
        return val;
570
    }
571

    
572
    /**
573
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
574
     */
575
    public Legend cloneLegend() throws XMLException {
576
        return (Legend) LegendFactory.createFromXML(getXMLEntity());
577
    }
578

    
579
    /* (non-Javadoc)
580
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
581
     */
582
    public void setDataSource(DataSource ds)
583
        throws FieldNotFoundException, DriverException {
584
        try {
585
            dataSource = ds;
586
            ds.start();
587
            fieldId = ds.getFieldIndexByName(fieldName);
588
            ds.stop();
589
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
590
            throw new DriverException(e);
591
        }
592
    }
593

    
594
    /* (non-Javadoc)
595
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getSymbolByValue(com.hardcode.gdbms.engine.values.Value)
596
     */
597
    public FSymbol getSymbolByValue(Value key) {
598
        if (symbols.containsKey(key)) {
599
            return (FSymbol) symbols.get(key);
600
        } else if (useDefaultSymbol) {
601
            return getDefaultSymbol();
602
        }
603

    
604
        return null;
605
    }
606

    
607
    /* (non-Javadoc)
608
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getShapeType()
609
     */
610
    public int getShapeType() {
611
        return shapeType;
612
    }
613

    
614
    /* (non-Javadoc)
615
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
616
     */
617
    public String getLabelHeightField() {
618
        return labelFieldHeight;
619
    }
620

    
621
    /**
622
     * Inserta el alto de campo.
623
     *
624
     * @param str alto.
625
     */
626
    public void setLabelHeightField(String str) {
627
        labelFieldHeight = str;
628
    }
629

    
630
    /* (non-Javadoc)
631
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
632
     */
633
    public String getLabelRotationField() {
634
        return labelFieldRotation;
635
    }
636

    
637
    /**
638
     * Inserta rotaci?n.
639
     *
640
     * @param str Rotaci?n.
641
     */
642
    public void setLabelRotationField(String str) {
643
        labelFieldRotation = str;
644
    }
645

    
646
    /**
647
     * Introduce si se tiene que representar el resto de valores o no.
648
     *
649
     * @param b True si se utiliza el resto de valores.
650
     */
651
    public void useDefaultSymbol(boolean b) {
652
        useDefaultSymbol = b;
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
     * Devuelve true si el etiquetado de la capa se ha modificado.
664
     *
665
     * @return True si se ha modificado el etiquetado de la capa.
666
     */
667
    /* public boolean isBWithHeightText() {
668
        return bWithHeightText;
669
    } */
670

    
671
    /**
672
     * Introduce si se ha modificado el etiquetado de la capa.
673
     *
674
     * @param withHeightText True si se ha modificado el etiquetado de la capa.
675
     */
676
    /* public void setBWithHeightText(boolean withHeightText) {
677
        bWithHeightText = withHeightText;
678
    } */
679

    
680
    /**
681
     * Elimina el s?mbolo que tiene como clave el valor que se pasa como par?metro.
682
     *
683
     * @param key clave.
684
     */
685
    public void delSymbol(Object key) {
686
        keys.remove(key);
687
        symbols.remove(key);
688
    }
689
}