Statistics
| Revision:

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

History | View | Annotate | Download (20.4 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
                                        }
518
                                        keys.add(auxValue);
519
                                }
520

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

    
533
                                        symbols.put(auxValue2, sym);
534
                                }
535
                                if (!foundNullValue && useDefaultSymbol){
536
                                        auxValue2 = new NullUniqueValue();
537
                                        sym = getDefaultSymbol();
538
                                        symbols.put(auxValue2, sym);
539
                                }
540
                        } else {
541

    
542

    
543
                                for (int i = 0; i < numKeys; i++) {
544
                                        auxValue = getValue(sk[i]);
545
                                        if ( auxValue  == null ) { //Default
546
                                                auxValue = new NullUniqueValue();
547
                                        }
548
                                        keys.add(auxValue);
549
                                }
550

    
551
                                boolean foundNullValue = false;
552
                                for (int i = 0; i < sv.length; i++) {
553
                                        auxValue2 = getValue(sv[i]);
554
                                        if ( auxValue2 == null ) { //Default
555
                                                foundNullValue = true;
556
                                                auxValue2 = new NullUniqueValue();
557
                                                sym = getDefaultSymbol();
558
                                        } else {
559
                                                sym = SymbologyFactory.createSymbolFromXML(xml
560
                                                                .getChild(i+1), null);
561
                                        }
562

    
563
                                        symbols.put(auxValue2, sym);
564
                                }
565
                                if (!foundNullValue && useDefaultSymbol){
566
                                        auxValue2 = new NullUniqueValue();
567
                                        sym = getDefaultSymbol();
568
                                        symbols.put(auxValue2, sym);
569
                                }
570
                        }
571
                }
572

    
573
                XMLEntity zSortXML = xml.firstChild("id", "zSort");
574
                if (zSortXML != null) {
575
                        ZSort zSort = new ZSort(this);
576
                        zSort.setXMLEntity(zSortXML);
577
                        addLegendListener(zSort);
578
                        setZSort(zSort);
579
                }
580
        }
581

    
582
        public void setDefaultSymbol(ISymbol s) {
583
                ISymbol mySymbol = defaultSymbol;
584

    
585
                if (s == null)
586
                        throw new NullPointerException("Default symbol cannot be null");
587

    
588
                ISymbol old = mySymbol;
589
                defaultSymbol = s;
590
                fireDefaultSymbolChangedEvent(new SymbolLegendEvent(old, s));
591
        }
592

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

    
611
                                return val;
612
                        } catch (NumberFormatException e) {
613
                        }
614

    
615
                        try {
616
                                val = ValueFactory.createValueByType(s, Types.BIGINT);
617

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

    
622
                        try {
623
                                val = ValueFactory.createValueByType(s, Types.FLOAT);
624

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

    
629
                        try {
630
                                val = ValueFactory.createValueByType(s, Types.DOUBLE);
631

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

    
636
                        val = ValueFactory.createValueByType(s, Types.LONGVARCHAR);
637

    
638
                } catch (ParseException e) {
639
                        log.warn("parse exception", e);
640
                }
641

    
642
                return val;
643
        }
644

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

    
665
        public ILegend cloneLegend() throws XMLException {
666
                return LegendFactory.createFromXML(getXMLEntity());
667
        }
668

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

    
683
        /*
684
         * (non-Javadoc)
685
         *
686
         * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getSymbolByValue(com.hardcode.gdbms.engine.values.Value)
687
         */
688
        public ISymbol getSymbolByValue(Value key) {
689

    
690
//                ISymbol symbol = symbols.get(key);
691
                
692
                //PARCHE
693
                ISymbol symbol = getSymbolByKey(key);
694
                //FIN DEL PARCHE
695
                
696
                if (symbol != null) {
697
                        return symbol;
698
                } else if (useDefaultSymbol) {
699
                        return getDefaultSymbol();
700
                }
701
                return null;
702

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

    
729
        public int getShapeType() {
730
                return shapeType;
731
        }
732

    
733
        public void useDefaultSymbol(boolean b) {
734
                useDefaultSymbol = b;
735
        }
736

    
737
        /**
738
         * Devuelve si se utiliza o no el resto de valores para representarse.
739
         *
740
         * @return True si se utiliza el resto de valores.
741
         */
742
        public boolean isUseDefaultSymbol() {
743
                return useDefaultSymbol;
744
        }
745

    
746
        public void delSymbol(Object key) {
747
                keys.remove(key);
748
                ISymbol removedSymbol = symbols.remove(key);
749
                if (removedSymbol != null){
750
                        fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(removedSymbol, null));
751
                }
752
        }
753

    
754
        public String getClassName() {
755
                return getClass().getName();
756
        }
757

    
758
        public void replace(ISymbol oldSymbol, ISymbol newSymbol) {
759
                if (symbols.containsValue(oldSymbol)) {
760
                        Iterator<Entry<Value, ISymbol>> it = symbols.entrySet().iterator();
761
                        while (it.hasNext()) {
762
                                Entry<Value, ISymbol> entry = it.next();
763
                                if (entry.getValue().equals(oldSymbol)) {
764
                                        entry.setValue(newSymbol);
765
                                        fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(
766
                                        oldSymbol, newSymbol));
767
                                        break;
768
                                }
769
                        }
770
                        
771
                }
772
        }
773

    
774
        public Color[] getColorScheme() {
775
                return selectedColors;
776
        }
777

    
778
        public void setColorScheme(Color[] cc) {
779
                this.selectedColors = cc;
780
        }
781

    
782
        public boolean isOwnOrder() {
783
                return ownOrder;
784
        }
785

    
786
        public void setOwnOrder(boolean ownOrder) {
787
                this.ownOrder = ownOrder;
788
        }
789

    
790
        public ArrayList getOrders() {
791
                return orders;
792
        }
793

    
794
        public void setOrders(ArrayList orders) {
795
                this.orders = orders;
796
        }
797
}