Statistics
| Revision:

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

History | View | Annotate | Download (20.6 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
        if (xml.contains("isBWithHeightText")) {
476
            setBWithHeightText(xml.getBooleanProperty("isBWithHeightText"));
477
        }
478

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

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

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

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

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

    
507
                FSymbol sym = FSymbol.createFromXML(xml.getChild(i + 1));
508

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

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

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

    
536
                return val;
537
            } catch (NumberFormatException e) {
538
            }
539

    
540
            try {
541
                val = ValueFactory.createValueByType(s, Types.BIGINT);
542

    
543
                return val;
544
            } catch (NumberFormatException e) {
545
            }
546

    
547
            try {
548
                val = ValueFactory.createValueByType(s, Types.FLOAT);
549

    
550
                return val;
551
            } catch (NumberFormatException e) {
552
            }
553

    
554
            try {
555
                val = ValueFactory.createValueByType(s, Types.DOUBLE);
556

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

    
567
        return val;
568
    }
569

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

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

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

    
602
        return null;
603
    }
604

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

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

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

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

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

    
644
    /**
645
     * Introduce si se tiene que representar el resto de valores o no.
646
     *
647
     * @param b True si se utiliza el resto de valores.
648
     */
649
    public void useDefaultSymbol(boolean b) {
650
        useDefaultSymbol = b;
651
    }
652
    /**
653
     * Devuelve si se utiliza o no el resto de valores para representarse.
654
     *
655
     * @return True si se utiliza el resto de valores.
656
     */
657
    public boolean isUseDefaultSymbol() {
658
        return useDefaultSymbol;
659
    }
660
    /**
661
     * Devuelve true si el etiquetado de la capa se ha modificado.
662
     *
663
     * @return True si se ha modificado el etiquetado de la capa.
664
     */
665
    public boolean isBWithHeightText() {
666
        return bWithHeightText;
667
    }
668

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

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