Statistics
| Revision:

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

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 java.awt.Color;
44
import java.sql.Types;
45
import java.text.ParseException;
46
import java.util.ArrayList;
47
import java.util.Comparator;
48
import java.util.Iterator;
49
import java.util.Set;
50
import java.util.TreeMap;
51
import java.util.Map.Entry;
52

    
53
import org.apache.log4j.Logger;
54

    
55
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
56
import com.hardcode.gdbms.engine.data.DataSource;
57
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
58
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
59
import com.hardcode.gdbms.engine.instruction.SemanticException;
60
import com.hardcode.gdbms.engine.values.BooleanValue;
61
import com.hardcode.gdbms.engine.values.NullValue;
62
import com.hardcode.gdbms.engine.values.StringValue;
63
import com.hardcode.gdbms.engine.values.Value;
64
import com.hardcode.gdbms.engine.values.ValueFactory;
65
import com.iver.cit.gvsig.fmap.core.IFeature;
66
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
67
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
68
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
69
import com.iver.cit.gvsig.fmap.layers.XMLException;
70
import com.iver.utiles.StringUtilities;
71
import com.iver.utiles.XMLEntity;
72

    
73
/**
74
 * Vectorial legend for unique values
75
 *
76
 * @author Vicente Caballero Navarro
77
 */
78
// public class VectorialUniqueValueLegend implements
79
// IVectorialUniqueValueLegend {
80
public class VectorialUniqueValueLegend extends AbstractClassifiedVectorLegend
81
                implements IVectorialUniqueValueLegend {
82
        private static final Logger log = Logger
83
                        .getLogger(VectorialUniqueValueLegend.class);
84

    
85

    
86
        protected int fieldId;
87

    
88
        protected DataSource dataSource;
89

    
90
        private boolean ownOrder = false;
91

    
92
        private ArrayList orders = new ArrayList();
93

    
94
        private TreeMap<Value, ISymbol> symbols = new TreeMap<Value, ISymbol>(
95
                        new Comparator<Object>() {
96
                                public int compare(Object o1, Object o2) {
97
                                        if (ownOrder) {
98
                                                try {
99
                                                        if (((BooleanValue)((Value)o1).equals((Value)o2)).getValue()) {
100
                                                                return 0;
101
                                                        }
102
                                                } catch (IncompatibleTypesException e) {
103
                                                        log.info("Cannot compare the values: "+o1.toString()+" - "+o2.toString(), e);
104
                                                }
105
                                                if (orders.indexOf(o1.toString()) < orders.indexOf(o2.toString())) {
106
                                                        return -1;
107
                                                } else if (orders.indexOf(o1.toString()) > orders.indexOf(o2.toString())) {
108
                                                        return 1;
109
                                                }
110
                                                return 0;
111
                                        }
112
                                        if ((o1 != null) && (o2 != null)) {
113
                                                Value v2 = (Value) o2;
114
                                                Value v1 = (Value) o1;
115
                                                BooleanValue boolVal;
116

    
117
                                                // TODO estas dos comprobaciones son por evitar un bug
118
                                                // en el gdbms, cuando se solucione se puede eliminar.
119
                                                if (v1 instanceof NullValue && v2 instanceof NullValue) {
120
                                                        return 0;
121
                                                }
122

    
123
                                                if (v1 instanceof NullValue) {
124
                                                        return -1;
125
                                                }
126

    
127
                                                if (v2 instanceof NullValue) {
128
                                                        return 1;
129
                                                }
130

    
131
                                                try {
132
                                                        boolVal = (BooleanValue) (v1.greater(v2));
133

    
134
                                                        if (boolVal.getValue()) {
135
                                                                return 1;
136
                                                        }
137

    
138
                                                        boolVal = (BooleanValue) (v1.less(v2));
139

    
140
                                                        if (boolVal.getValue()) {
141
                                                                return -1;
142
                                                        }
143
                                                } catch (IncompatibleTypesException e) {
144
                                                        // TODO Auto-generated catch block
145
                                                        // e.printStackTrace();
146
                                                }
147

    
148
                                                try {
149
                                                        if (((BooleanValue) v1.equals(v2)).getValue()) {
150
                                                                return 0;
151
                                                        }
152
                                                } catch (IncompatibleTypesException e) {
153
                                                        // TODO Auto-generated catch block
154
                                                        // e.printStackTrace();
155
                                                }
156

    
157
                                                if (v1 instanceof StringValue) {
158
                                                        return -1;
159
                                                }
160

    
161
                                                if (v2 instanceof StringValue) {
162
                                                        return 1;
163
                                                }
164
                                        }
165

    
166
                                        return 0;
167
                                }
168
                        }); // Para poder ordenar
169

    
170
        private ArrayList<Value> keys = new ArrayList<Value>(); // En lugar de un
171
                                                                                                                        // HashSet, para
172
                                                                                                                        // tener acceso por
173
                                                                                                                        // ?ndice
174

    
175
        private String labelFieldName;
176

    
177
        private String labelFieldHeight;
178

    
179
        private String labelFieldRotation;
180

    
181
        private ISymbol defaultSymbol;
182

    
183
        private int shapeType;
184

    
185
        private String valueType = NullValue.class.getName();
186

    
187
        private boolean useDefaultSymbol = false;
188

    
189
        private Color[] selectedColors = null;
190

    
191
        /**
192
         * Constructor method
193
         */
194
        public VectorialUniqueValueLegend() {
195
        }
196

    
197
        /**
198
         * Constructor method
199
         *
200
         * @param shapeType
201
         *            Type of the shape.
202
         */
203
        public VectorialUniqueValueLegend(int shapeType) {
204
                setShapeType(shapeType);
205
        }
206

    
207
        public void setShapeType(int shapeType) {
208
                if (this.shapeType != shapeType) {
209
                        ISymbol old = defaultSymbol;
210
                        defaultSymbol = SymbologyFactory
211
                                        .createDefaultSymbolByShapeType(shapeType);
212
                        fireDefaultSymbolChangedEvent(new SymbolLegendEvent(old,
213
                                        defaultSymbol));
214
                        this.shapeType = shapeType;
215
                }
216
        }
217

    
218
        public void setValueSymbolByID(int id, ISymbol symbol) {
219
                ISymbol old = symbols.put(keys.get(id), symbol);
220
                fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(old, symbol));
221
        }
222

    
223
        /**
224
         * Used in the table that shows the legend
225
         *
226
         * @deprecated use setValueSymbolByID(int id, ISymbol symbol);
227
         * @param id
228
         * @param symbol
229
         */
230
        public void setValueSymbol(int id, ISymbol symbol) {
231
                ISymbol old = symbols.put(keys.get(id), symbol);
232
                fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(old, symbol));
233
        }
234

    
235
        public Object[] getValues() {
236
                return symbols.keySet().toArray(new Object[0]);
237
        }
238

    
239
        public void addSymbol(Object key, ISymbol symbol) {
240
                ISymbol resul;
241
                resul = symbols.put((Value) key, symbol);
242

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

    
251
                        if (!key.getClass().equals(NullValue.class)) {
252
                                valueType = key.getClass().getName();
253
                        }
254
                }
255
                fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(resul, symbol));
256

    
257

    
258
        }
259

    
260
        public void clear() {
261
                keys.clear();
262
                ISymbol[] olds = symbols.values().toArray(new ISymbol[0]);
263
                symbols.clear();
264
                removeLegendListener(getZSort());
265
                setZSort(null);
266

    
267
                fireLegendClearEvent(new LegendClearEvent(olds));
268
        }
269

    
270
        public String[] getDescriptions() {
271
                String[] descriptions = new String[symbols.size()];
272
                ISymbol[] auxSym = getSymbols();
273

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

    
277
                return descriptions;
278
        }
279

    
280
        public ISymbol[] getSymbols() {
281
                return symbols.values().toArray(new ISymbol[0]);
282
        }
283

    
284
        @Override
285
        public void setClassifyingFieldNames(String[] fNames) {
286
                super.setClassifyingFieldNames(fNames);
287
                try {
288
                        fieldId = dataSource
289
                                        .getFieldIndexByName(getClassifyingFieldNames()[0]);
290
                } catch (NullPointerException e) {
291
                        log.warn("data source not set");
292
                } catch (ReadDriverException e) {
293
                        log.warn("failed setting field id");
294
                }
295
        }
296

    
297
        /*
298
         * @see com.iver.cit.gvsig.fmap.rendering.IVectorialLegend#getSymbol(int)
299
         */
300
        public ISymbol getSymbol(int recordIndex) throws ReadDriverException {
301
                Value val = dataSource.getFieldValue(recordIndex, fieldId);
302
                ISymbol theSymbol = getSymbolByValue(val);
303

    
304
                return theSymbol;
305
        }
306

    
307
        /**
308
         * Devuelve un s?mbolo a partir de una IFeature. OJO!! Cuando usamos un
309
         * feature iterator de base de datos el ?nico campo que vendr? rellenado es
310
         * el de fieldID. Los dem?s vendr?n a nulos para ahorra tiempo de creaci?n.
311
         *
312
         * @param feat
313
         *            IFeature
314
         *
315
         * @return S?mbolo.
316
         */
317
        public ISymbol getSymbolByFeature(IFeature feat) {
318
                // Value val =
319
                // feat.getAttribute(FLyrVect.forTestOnlyVariableUseIterators_REMOVE_THIS_FIELD
320
                // ? 0 :fieldId);
321
                Value val = feat.getAttribute(0);
322
                ISymbol theSymbol = getSymbolByValue(val);
323

    
324
                if (theSymbol != null) {
325
                        return theSymbol;
326
                }
327
                return null;
328
        }
329

    
330
        public ISymbol getDefaultSymbol() {
331

    
332
                if (defaultSymbol == null) {
333
                        defaultSymbol = SymbologyFactory
334
                                        .createDefaultSymbolByShapeType(shapeType);
335
                        fireDefaultSymbolChangedEvent(new SymbolLegendEvent(null,
336
                                        defaultSymbol));
337
                }
338
                return defaultSymbol;
339
        }
340

    
341
        public XMLEntity getXMLEntity() {
342
                XMLEntity xml = new XMLEntity();
343
                xml.putProperty("className", this.getClass().getName());
344
                xml.putProperty("fieldNames", getClassifyingFieldNames()[0]);
345
                if (getClassifyingFieldTypes()!=null)
346
                        xml.putProperty("fieldTypes", getClassifyingFieldTypes()[0]);
347
                xml.putProperty("ownOrder", isOwnOrder());
348

    
349
                xml.putProperty("orders",getOrders().toArray());
350

    
351
                if (selectedColors != null) {
352
                        String[] strColors = new String[selectedColors.length];
353
                        for (int i = 0; i < strColors.length; i++) {
354
                                strColors[i] = StringUtilities.color2String(selectedColors[i]);
355
                        }
356
                        xml.putProperty("colorScheme", strColors);
357
                }
358

    
359
                xml.putProperty("labelfield", labelFieldName);
360
                xml.putProperty("labelFieldHeight", labelFieldHeight);
361
                xml.putProperty("labelFieldRotation", labelFieldRotation);
362

    
363
                xml.putProperty("useDefaultSymbol", useDefaultSymbol);
364
                xml.addChild(getDefaultSymbol().getXMLEntity());
365
                xml.putProperty("numKeys", keys.size());
366

    
367
                if (keys.size() > 0) {
368
                        xml.putProperty("tipoValueKeys", valueType);
369

    
370
                        String[] sk = new String[keys.size()];
371
                        int[] stk = new int[keys.size()];
372
                        Object[] values = getValues();
373
                        String[] sv = new String[values.length];
374
                        int[] stv = new int[values.length];
375

    
376
                        for (int i = 0; i < keys.size(); i++) {
377
                                Value key = keys.get(i);
378
                                sk[i] = key.toString();
379
                                stk[i] = key.getSQLType();
380
                        }
381

    
382
                        for (int i=0; i < values.length; i++){
383
                                Value value = (Value) values[i];
384
                                if( value instanceof NullUniqueValue){
385
                                        sv[i] = ((NullUniqueValue)value).toString();
386
                                        stv[i] = ((NullUniqueValue)value).getSQLType();
387
                                } else {
388
                                        sv[i] = value.toString();
389
                                        stv[i] = value.getSQLType();
390
                                }
391
                                
392
//                                ISymbol symbol = symbols.get(value);
393
                                //PARCHE
394
                                ISymbol symbol = getSymbolByKey(value);
395
                                //FIN DEL PARCHE
396
                                
397
                                if(symbol != null){
398
                                        xml.addChild(symbol.getXMLEntity());
399
                                }
400
                        }
401

    
402
                        xml.putProperty("keys", sk);
403
                        xml.putProperty("values", sv);
404
                        xml.putProperty("typeKeys", stk);
405
                        xml.putProperty("typeValues", stv);
406
                }
407

    
408
                if (getZSort() != null) {
409
                        XMLEntity xmlZSort = getZSort().getXMLEntity();
410
                        xmlZSort.putProperty("id", "zSort");
411
                        xml.addChild(xmlZSort);
412
                }
413
                return xml;
414
        }
415

    
416
        public void setXMLEntity03(XMLEntity xml) {
417
                clear();
418
                setClassifyingFieldNames(new String[] { xml
419
                                .getStringProperty("fieldName") });
420

    
421
                int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
422

    
423
                if (useDefaultSymbol == 1) {
424
                        setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
425
                } else {
426
                        setDefaultSymbol(null);
427
                }
428

    
429
                int numKeys = xml.getIntProperty("numKeys");
430

    
431
                if (numKeys > 0) {
432
                        String className = xml.getStringProperty("tipoValueKeys");
433
                        String[] sk = xml.getStringArrayProperty("keys");
434
                        String[] sv = xml.getStringArrayProperty("values");
435
                        Value auxValue;
436
                        Value auxValue2;
437

    
438
                        for (int i = 0; i < numKeys; i++) {
439
                                try {
440
                                        auxValue = ValueFactory.createValue(sk[i], className);
441
                                        auxValue2 = ValueFactory.createValue(sv[i], className);
442

    
443
                                        ISymbol sym = FSymbol.createFromXML03(xml.getChild(i
444
                                                        + useDefaultSymbol));
445

    
446
                                        symbols.put(auxValue2, sym);
447
                                        keys.add(auxValue);
448

    
449
                                } catch (SemanticException e) {
450
                                        log.error("Exception", e);
451
                                        e.printStackTrace();
452
                                }
453
                        }
454
                }
455
        }
456

    
457
        public void setXMLEntity(XMLEntity xml) {
458
                clear();
459
                if (xml.contains("fieldName"))
460
                        setClassifyingFieldNames(new String[] { xml
461
                                        .getStringProperty("fieldName") });
462
                else
463
                        setClassifyingFieldNames(xml.getStringArrayProperty("fieldNames"));
464

    
465
                if (xml.contains("fieldTypes"))
466
                        setClassifyingFieldTypes(new int[] { xml
467
                                        .getIntProperty("fieldTypes") });
468

    
469
                if (xml.contains("colorScheme")) {
470
                        String[] strColors = xml.getStringArrayProperty("colorScheme");
471

    
472
                        Color[] cc = new Color[strColors.length];
473
                        for (int i = 0; i < cc.length; i++) {
474
                                cc[i] = StringUtilities.string2Color(strColors[i]);
475
                        }
476
                        setColorScheme(cc);
477
                }
478

    
479
                if (xml.contains("ownOrder"))
480
                        setOwnOrder(xml.getBooleanProperty("ownOrder"));
481
                if(xml.contains("orders")){
482
                        String[] ord = xml.getStringArrayProperty("orders");
483
                        ArrayList arrayOrd = new ArrayList();
484
                        for (int i = 0; i < ord.length; i++) {
485
                                arrayOrd.add(ord[i]);
486
                        }
487
                        setOrders(arrayOrd);
488
                }
489

    
490
                useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbol");
491
                setDefaultSymbol(SymbologyFactory.createSymbolFromXML(xml.getChild(0),
492
                                null));
493

    
494
                int numKeys = xml.getIntProperty("numKeys");
495

    
496
                if (numKeys > 0) {
497
                        String className = xml.getStringProperty("tipoValueKeys");
498
                        String[] sk = xml.getStringArrayProperty("keys");
499
                        if(sk.length == 0){
500
                                sk = new String[]{""};
501
                        }
502
                        String[] sv = xml.getStringArrayProperty("values");
503
                        if(sv.length == 0){
504
                                sv = new String[]{""};
505
                        }
506
                        Value auxValue = null;
507
                        Value auxValue2 = null;
508
                        ISymbol sym;
509
                        int[] stk = null;
510
                        if (xml.contains("typeKeys")) {
511
                                stk = xml.getIntArrayProperty("typeKeys");
512
                                int[] stv = xml.getIntArrayProperty("typeValues");
513
                                for (int i = 0; i < numKeys; i++) {
514
                                        auxValue = getValue(sk[i], stk[i]);
515
                                        if ( auxValue instanceof NullValue ) {
516
                                                auxValue = new NullUniqueValue();
517
                                        } else {
518
                                        keys.add(auxValue);
519
                                        }
520
                                }
521

    
522
                                boolean foundNullValue = false;
523
                                for (int i = 0; i < sv.length; i++) {
524
                                        auxValue2 = getValue(sv[i], stv[i]);
525
                                        if ( auxValue2 instanceof NullValue ) {
526
                                                foundNullValue = true;
527
                                                auxValue2 = new NullUniqueValue();
528
                                                sym = getDefaultSymbol();
529
                                        } else {
530
                                                if(foundNullValue){
531
                                                sym = SymbologyFactory.createSymbolFromXML(xml
532
                                                                .getChild(i), null);
533
                                                } else {
534
                                                        sym = SymbologyFactory.createSymbolFromXML(xml
535
                                                                        .getChild(i+1), null);
536
                                                }
537
                                        }
538

    
539
                                        symbols.put(auxValue2, sym);
540
                                }
541
                                if (!foundNullValue && useDefaultSymbol){
542
                                        auxValue2 = new NullUniqueValue();
543
                                        sym = getDefaultSymbol();
544
                                        symbols.put(auxValue2, sym);
545
                                }
546
                        } else {
547

    
548

    
549
                                for (int i = 0; i < numKeys; i++) {
550
                                        auxValue = getValue(sk[i]);
551
                                        if ( auxValue  == null ) { //Default
552
                                                auxValue = new NullUniqueValue();
553
                                        }
554
                                        keys.add(auxValue);
555
                                }
556

    
557
                                boolean foundNullValue = false;
558
                                for (int i = 0; i < sv.length; i++) {
559
                                        auxValue2 = getValue(sv[i]);
560
                                        if ( auxValue2 == null ) { //Default
561
                                                foundNullValue = true;
562
                                                auxValue2 = new NullUniqueValue();
563
                                                sym = getDefaultSymbol();
564
                                        } else {
565
                                                sym = SymbologyFactory.createSymbolFromXML(xml
566
                                                                .getChild(i+1), null);
567
                                        }
568

    
569
                                        symbols.put(auxValue2, sym);
570
                                }
571
                                if (!foundNullValue && useDefaultSymbol){
572
                                        auxValue2 = new NullUniqueValue();
573
                                        sym = getDefaultSymbol();
574
                                        symbols.put(auxValue2, sym);
575
                                }
576
                        }
577
                }
578

    
579
                XMLEntity zSortXML = xml.firstChild("id", "zSort");
580
                if (zSortXML != null) {
581
                        ZSort zSort = new ZSort(this);
582
                        zSort.setXMLEntity(zSortXML);
583
                        addLegendListener(zSort);
584
                        setZSort(zSort);
585
                }
586
        }
587

    
588
        public void setDefaultSymbol(ISymbol s) {
589
                ISymbol mySymbol = defaultSymbol;
590

    
591
                if (s == null)
592
                        throw new NullPointerException("Default symbol cannot be null");
593

    
594
                ISymbol old = mySymbol;
595
                defaultSymbol = s;
596
                fireDefaultSymbolChangedEvent(new SymbolLegendEvent(old, s));
597
        }
598

    
599
        /**
600
         * Returns the value using the its value in a string.
601
         *
602
         *
603
         * @param s
604
         *            String with the value.
605
         * @deprecated Method used until 1.0 alpha 855 You should use
606
         *             getValue(String s,int type);
607
         * @return Value.
608
         */
609
        private Value getValue(String s) {
610
                Value val = new NullUniqueValue();
611
                if (s.equals("Resto de Valores"))
612
                        return val;
613
                try {
614
                        try {
615
                                val = ValueFactory.createValueByType(s, Types.INTEGER);
616

    
617
                                return val;
618
                        } catch (NumberFormatException e) {
619
                        }
620

    
621
                        try {
622
                                val = ValueFactory.createValueByType(s, Types.BIGINT);
623

    
624
                                return val;
625
                        } catch (NumberFormatException e) {
626
                        }
627

    
628
                        try {
629
                                val = ValueFactory.createValueByType(s, Types.FLOAT);
630

    
631
                                return val;
632
                        } catch (NumberFormatException e) {
633
                        }
634

    
635
                        try {
636
                                val = ValueFactory.createValueByType(s, Types.DOUBLE);
637

    
638
                                return val;
639
                        } catch (NumberFormatException e) {
640
                        }
641

    
642
                        val = ValueFactory.createValueByType(s, Types.LONGVARCHAR);
643

    
644
                } catch (ParseException e) {
645
                        log.warn("parse exception", e);
646
                }
647

    
648
                return val;
649
        }
650

    
651
        /**
652
         * Devuelve el valor a partir de su valor en un string.
653
         *
654
         * @param s
655
         *            String con el valor.
656
         *
657
         * @return Value.
658
         */
659
        private Value getValue(String s, int type) {
660
                Value val = new NullUniqueValue();
661
                if (type == Types.OTHER)
662
                        return val;
663
                try {
664
                        val = ValueFactory.createValueByType(s, type);
665
                } catch (ParseException e) {
666
                        e.printStackTrace();
667
                }
668
                return val;
669
        }
670

    
671
        public ILegend cloneLegend() throws XMLException {
672
                return LegendFactory.createFromXML(getXMLEntity());
673
        }
674

    
675
        /*
676
         * (non-Javadoc)
677
         *
678
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
679
         */
680
        public void setDataSource(DataSource ds) throws FieldNotFoundException,
681
                        ReadDriverException {
682
                dataSource = ds;
683
                ds.start();
684
                if (getClassifyingFieldNames() != null)
685
                        fieldId = ds.getFieldIndexByName(getClassifyingFieldNames()[0]);
686
                ds.stop();
687
        }
688

    
689
        /*
690
         * (non-Javadoc)
691
         *
692
         * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getSymbolByValue(com.hardcode.gdbms.engine.values.Value)
693
         */
694
        public ISymbol getSymbolByValue(Value key) {
695

    
696
//                ISymbol symbol = symbols.get(key);
697
                
698
                //PARCHE
699
                ISymbol symbol = getSymbolByKey(key);
700
                //FIN DEL PARCHE
701
                
702
                if (symbol != null) {
703
                        return symbol;
704
                } else if (useDefaultSymbol) {
705
                        return getDefaultSymbol();
706
                }
707
                return null;
708

    
709
        }
710
        
711
        private ISymbol getSymbolByKey(Value key) {
712
                //FIXME: Esto es un parche para sustituir symbols.get(key)
713
                // porque parece que no funciona bien el metodo get sobre un 
714
                // TreeMap cuyas claves son Values. Si se consigue que funcione
715
                // correctamente, eliminar este metodo.
716
                ISymbol symbol = null;
717
                Set<Entry<Value, ISymbol>> entrySet = symbols.entrySet();
718
                Iterator<Entry<Value, ISymbol>> it = entrySet.iterator();
719
                while(it.hasNext()){
720
                        Entry<Value, ISymbol> entry = it.next();
721
                        try {
722
                                if (((BooleanValue)key.equals(entry.getKey())).getValue()) {
723
                                        symbol=entry.getValue();
724
                                }
725
                        } catch (IncompatibleTypesException e) {
726
                                log.info("Cannot compare the values: "+key.toString()+" - "+entry.getKey().toString(), e);
727
                        }
728
                }
729
                if (symbol != null) {
730
                        return symbol;
731
                }
732
                return null;
733
        }
734

    
735
        public int getShapeType() {
736
                return shapeType;
737
        }
738

    
739
        public void useDefaultSymbol(boolean b) {
740
                useDefaultSymbol = b;
741
        }
742

    
743
        /**
744
         * Devuelve si se utiliza o no el resto de valores para representarse.
745
         *
746
         * @return True si se utiliza el resto de valores.
747
         */
748
        public boolean isUseDefaultSymbol() {
749
                return useDefaultSymbol;
750
        }
751

    
752
        public void delSymbol(Object key) {
753
                keys.remove(key);
754
                ISymbol removedSymbol = symbols.remove(key);
755
                if (removedSymbol != null){
756
                        fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(removedSymbol, null));
757
                }
758
        }
759

    
760
        public String getClassName() {
761
                return getClass().getName();
762
        }
763

    
764
        public void replace(ISymbol oldSymbol, ISymbol newSymbol) {
765
//                if (symbols.containsValue(oldSymbol)) {
766
                        Iterator<Entry<Value, ISymbol>> it = symbols.entrySet().iterator();
767
                        while (it.hasNext()) {
768
                                Entry<Value, ISymbol> entry = it.next();
769
                                if (entry.getValue().equals(oldSymbol)) {
770
                                        entry.setValue(newSymbol);
771
                                        fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(
772
                                        oldSymbol, newSymbol));
773
                                        break;
774
                                }
775
                        }
776
                        if(oldSymbol.equals(this.getDefaultSymbol())) {
777
                                this.setDefaultSymbol(newSymbol);
778
                        }
779
                        
780
//                }
781
        }
782

    
783
        public Color[] getColorScheme() {
784
                return selectedColors;
785
        }
786

    
787
        public void setColorScheme(Color[] cc) {
788
                this.selectedColors = cc;
789
        }
790

    
791
        public boolean isOwnOrder() {
792
                return ownOrder;
793
        }
794

    
795
        public void setOwnOrder(boolean ownOrder) {
796
                this.ownOrder = ownOrder;
797
        }
798

    
799
        public ArrayList getOrders() {
800
                return orders;
801
        }
802

    
803
        public void setOrders(ArrayList orders) {
804
                this.orders = orders;
805
        }
806
}