Statistics
| Revision:

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

History | View | Annotate | Download (21 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
                // Para evitar que salte un error cuando hemos borrado un campo en el que estaba basada una leyenda
323
                if (val==null)
324
                        return getDefaultSymbol();
325
                // Fin
326
                ISymbol theSymbol = getSymbolByValue(val);
327

    
328
                if (theSymbol != null) {
329
                        return theSymbol;
330
                }
331
                return null;
332
        }
333

    
334
        public ISymbol getDefaultSymbol() {
335

    
336
                if (defaultSymbol == null) {
337
                        defaultSymbol = SymbologyFactory
338
                                        .createDefaultSymbolByShapeType(shapeType);
339
                        fireDefaultSymbolChangedEvent(new SymbolLegendEvent(null,
340
                                        defaultSymbol));
341
                }
342
                return defaultSymbol;
343
        }
344

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

    
353
                xml.putProperty("orders",getOrders().toArray());
354

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

    
363
                xml.putProperty("labelfield", labelFieldName);
364
                xml.putProperty("labelFieldHeight", labelFieldHeight);
365
                xml.putProperty("labelFieldRotation", labelFieldRotation);
366

    
367
                xml.putProperty("useDefaultSymbol", useDefaultSymbol);
368
                xml.addChild(getDefaultSymbol().getXMLEntity());
369
                xml.putProperty("numKeys", keys.size());
370

    
371
                if (keys.size() > 0) {
372
                        xml.putProperty("tipoValueKeys", valueType);
373

    
374
                        String[] sk = new String[keys.size()];
375
                        int[] stk = new int[keys.size()];
376
                        Object[] values = getValues();
377
                        String[] sv = new String[values.length];
378
                        int[] stv = new int[values.length];
379

    
380
                        for (int i = 0; i < keys.size(); i++) {
381
                                Value key = keys.get(i);
382
                                sk[i] = key.toString();
383
                                stk[i] = key.getSQLType();
384
                        }
385

    
386
                        for (int i=0; i < values.length; i++){
387
                                Value value = (Value) values[i];
388
                                if( value instanceof NullUniqueValue){
389
                                        sv[i] = ((NullUniqueValue)value).toString();
390
                                        stv[i] = ((NullUniqueValue)value).getSQLType();
391
                                } else {
392
                                        sv[i] = value.toString();
393
                                        stv[i] = value.getSQLType();
394
                                }
395

    
396
//                                ISymbol symbol = symbols.get(value);
397
                                //PARCHE
398
                                ISymbol symbol = getSymbolByKey(value);
399
                                //FIN DEL PARCHE
400

    
401
                                if(symbol != null){
402
                                        xml.addChild(symbol.getXMLEntity());
403
                                }
404
                        }
405

    
406
                        xml.putProperty("keys", sk);
407
                        xml.putProperty("values", sv);
408
                        xml.putProperty("typeKeys", stk);
409
                        xml.putProperty("typeValues", stv);
410
                }
411

    
412
                if (getZSort() != null) {
413
                        XMLEntity xmlZSort = getZSort().getXMLEntity();
414
                        xmlZSort.putProperty("id", "zSort");
415
                        xml.addChild(xmlZSort);
416
                }
417
                return xml;
418
        }
419

    
420
        public void setXMLEntity03(XMLEntity xml) {
421
                clear();
422
                setClassifyingFieldNames(new String[] { xml
423
                                .getStringProperty("fieldName") });
424

    
425
                int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
426

    
427
                if (useDefaultSymbol == 1) {
428
                        setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
429
                } else {
430
                        setDefaultSymbol(null);
431
                }
432

    
433
                int numKeys = xml.getIntProperty("numKeys");
434

    
435
                if (numKeys > 0) {
436
                        String className = xml.getStringProperty("tipoValueKeys");
437
                        String[] sk = xml.getStringArrayProperty("keys");
438
                        String[] sv = xml.getStringArrayProperty("values");
439
                        Value auxValue;
440
                        Value auxValue2;
441

    
442
                        for (int i = 0; i < numKeys; i++) {
443
                                try {
444
                                        auxValue = ValueFactory.createValue(sk[i], className);
445
                                        auxValue2 = ValueFactory.createValue(sv[i], className);
446

    
447
                                        ISymbol sym = FSymbol.createFromXML03(xml.getChild(i
448
                                                        + useDefaultSymbol));
449

    
450
                                        symbols.put(auxValue2, sym);
451
                                        keys.add(auxValue);
452

    
453
                                } catch (SemanticException e) {
454
                                        log.error("Exception", e);
455
                                        e.printStackTrace();
456
                                }
457
                        }
458
                }
459
        }
460

    
461
        public void setXMLEntity(XMLEntity xml) {
462
                clear();
463
                if (xml.contains("fieldName"))
464
                        setClassifyingFieldNames(new String[] { xml
465
                                        .getStringProperty("fieldName") });
466
                else
467
                        setClassifyingFieldNames(xml.getStringArrayProperty("fieldNames"));
468

    
469
                if (xml.contains("fieldTypes"))
470
                        setClassifyingFieldTypes(new int[] { xml
471
                                        .getIntProperty("fieldTypes") });
472

    
473
                if (xml.contains("colorScheme")) {
474
                        String[] strColors = xml.getStringArrayProperty("colorScheme");
475

    
476
                        Color[] cc = new Color[strColors.length];
477
                        for (int i = 0; i < cc.length; i++) {
478
                                cc[i] = StringUtilities.string2Color(strColors[i]);
479
                        }
480
                        setColorScheme(cc);
481
                }
482

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

    
494
                useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbol");
495
                setDefaultSymbol(SymbologyFactory.createSymbolFromXML(xml.getChild(0),
496
                                null));
497

    
498
                int numKeys = xml.getIntProperty("numKeys");
499

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

    
526
                                boolean foundNullValue = false;
527
                                for (int i = 0; i < sv.length; i++) {
528
                                        auxValue2 = getValue(sv[i], stv[i]);
529
                                        if ( auxValue2 instanceof NullValue ) {
530
                                                foundNullValue = true;
531
                                                auxValue2 = new NullUniqueValue();
532
                                                sym = getDefaultSymbol();
533
                                        } else {
534
                                                if(foundNullValue){
535
                                                        if(stv.length == stk.length && xml.getChildrenCount()>stv.length){
536
                                                                sym = SymbologyFactory.createSymbolFromXML(xml
537
                                                                                .getChild(i+1), null);
538
                                                        } else {
539
                                                                sym = SymbologyFactory.createSymbolFromXML(xml
540
                                                                                .getChild(i), null);
541
                                                        }
542
                                                } else {
543
                                                        sym = SymbologyFactory.createSymbolFromXML(xml
544
                                                                        .getChild(i+1), null);
545
                                                }
546
                                        }
547

    
548
                                        symbols.put(auxValue2, sym);
549
                                }
550
                                if (!foundNullValue && useDefaultSymbol){
551
                                        auxValue2 = new NullUniqueValue();
552
                                        sym = getDefaultSymbol();
553
                                        symbols.put(auxValue2, sym);
554
                                }
555
                        } else {
556

    
557

    
558
                                for (int i = 0; i < numKeys; i++) {
559
                                        auxValue = getValue(sk[i]);
560
                                        if ( auxValue  == null ) { //Default
561
                                                auxValue = new NullUniqueValue();
562
                                        }
563
                                        keys.add(auxValue);
564
                                }
565

    
566
                                boolean foundNullValue = false;
567
                                for (int i = 0; i < sv.length; i++) {
568
                                        auxValue2 = getValue(sv[i]);
569
                                        if ( auxValue2 == null ) { //Default
570
                                                foundNullValue = true;
571
                                                auxValue2 = new NullUniqueValue();
572
                                                sym = getDefaultSymbol();
573
                                        } else {
574
                                                sym = SymbologyFactory.createSymbolFromXML(xml
575
                                                                .getChild(i+1), null);
576
                                        }
577

    
578
                                        symbols.put(auxValue2, sym);
579
                                }
580
                                if (!foundNullValue && useDefaultSymbol){
581
                                        auxValue2 = new NullUniqueValue();
582
                                        sym = getDefaultSymbol();
583
                                        symbols.put(auxValue2, sym);
584
                                }
585
                        }
586
                }
587

    
588
                XMLEntity zSortXML = xml.firstChild("id", "zSort");
589
                if (zSortXML != null) {
590
                        ZSort zSort = new ZSort(this);
591
                        zSort.setXMLEntity(zSortXML);
592
                        addLegendListener(zSort);
593
                        setZSort(zSort);
594
                }
595
        }
596

    
597
        public void setDefaultSymbol(ISymbol s) {
598
                ISymbol mySymbol = defaultSymbol;
599

    
600
                if (s == null)
601
                        throw new NullPointerException("Default symbol cannot be null");
602

    
603
                ISymbol old = mySymbol;
604
                defaultSymbol = s;
605
                fireDefaultSymbolChangedEvent(new SymbolLegendEvent(old, s));
606
        }
607

    
608
        /**
609
         * Returns the value using the its value in a string.
610
         *
611
         *
612
         * @param s
613
         *            String with the value.
614
         * @deprecated Method used until 1.0 alpha 855 You should use
615
         *             getValue(String s,int type);
616
         * @return Value.
617
         */
618
        private Value getValue(String s) {
619
                Value val = new NullUniqueValue();
620
                if (s.equals("Resto de Valores"))
621
                        return val;
622
                try {
623
                        try {
624
                                val = ValueFactory.createValueByType(s, Types.INTEGER);
625

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

    
630
                        try {
631
                                val = ValueFactory.createValueByType(s, Types.BIGINT);
632

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

    
637
                        try {
638
                                val = ValueFactory.createValueByType(s, Types.FLOAT);
639

    
640
                                return val;
641
                        } catch (NumberFormatException e) {
642
                        }
643

    
644
                        try {
645
                                val = ValueFactory.createValueByType(s, Types.DOUBLE);
646

    
647
                                return val;
648
                        } catch (NumberFormatException e) {
649
                        }
650

    
651
                        val = ValueFactory.createValueByType(s, Types.LONGVARCHAR);
652

    
653
                } catch (ParseException e) {
654
                        log.warn("parse exception", e);
655
                }
656

    
657
                return val;
658
        }
659

    
660
        /**
661
         * Devuelve el valor a partir de su valor en un string.
662
         *
663
         * @param s
664
         *            String con el valor.
665
         *
666
         * @return Value.
667
         */
668
        private Value getValue(String s, int type) {
669
                Value val = new NullUniqueValue();
670
                if (type == Types.OTHER)
671
                        return val;
672
                try {
673
                        val = ValueFactory.createValueByType(s, type);
674
                } catch (ParseException e) {
675
                        e.printStackTrace();
676
                }
677
                return val;
678
        }
679

    
680
        public ILegend cloneLegend() throws XMLException {
681
                return LegendFactory.createFromXML(getXMLEntity());
682
        }
683

    
684
        /*
685
         * (non-Javadoc)
686
         *
687
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
688
         */
689
        public void setDataSource(DataSource ds) throws FieldNotFoundException,
690
                        ReadDriverException {
691
                dataSource = ds;
692
                ds.start();
693
                if (getClassifyingFieldNames() != null && getClassifyingFieldNames().length>0)
694
                        fieldId = ds.getFieldIndexByName(getClassifyingFieldNames()[0]);
695
                ds.stop();
696
        }
697

    
698
        /*
699
         * (non-Javadoc)
700
         *
701
         * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getSymbolByValue(com.hardcode.gdbms.engine.values.Value)
702
         */
703
        public ISymbol getSymbolByValue(Value key) {
704

    
705
                ISymbol symbol = symbols.get(key);
706

    
707
                //PARCHE
708
//                ISymbol symbol = getSymbolByKey(key);
709
                //FIN DEL PARCHE
710

    
711
                if (symbol != null) {
712
                        return symbol;
713
                } else if (useDefaultSymbol) {
714
                        return getDefaultSymbol();
715
                }
716
                return null;
717

    
718
        }
719

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

    
746
        public int getShapeType() {
747
                return shapeType;
748
        }
749

    
750
        public void useDefaultSymbol(boolean b) {
751
                useDefaultSymbol = b;
752
        }
753

    
754
        /**
755
         * Devuelve si se utiliza o no el resto de valores para representarse.
756
         *
757
         * @return True si se utiliza el resto de valores.
758
         */
759
        public boolean isUseDefaultSymbol() {
760
                return useDefaultSymbol;
761
        }
762

    
763
        public void delSymbol(Object key) {
764
                keys.remove(key);
765
                ISymbol removedSymbol = symbols.remove(key);
766
                if (removedSymbol != null){
767
                        fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(removedSymbol, null));
768
                }
769
        }
770

    
771
        public String getClassName() {
772
                return getClass().getName();
773
        }
774

    
775
        public void replace(ISymbol oldSymbol, ISymbol newSymbol) {
776
//                if (symbols.containsValue(oldSymbol)) {
777
                        Iterator<Entry<Value, ISymbol>> it = symbols.entrySet().iterator();
778
                        while (it.hasNext()) {
779
                                Entry<Value, ISymbol> entry = it.next();
780
                                if (entry.getValue().equals(oldSymbol)) {
781
                                        entry.setValue(newSymbol);
782
                                        fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(
783
                                        oldSymbol, newSymbol));
784
                                        break;
785
                                }
786
                        }
787
                        if(oldSymbol.equals(this.getDefaultSymbol())) {
788
                                this.setDefaultSymbol(newSymbol);
789
                        }
790

    
791
//                }
792
        }
793

    
794
        public Color[] getColorScheme() {
795
                return selectedColors;
796
        }
797

    
798
        public void setColorScheme(Color[] cc) {
799
                this.selectedColors = cc;
800
        }
801

    
802
        public boolean isOwnOrder() {
803
                return ownOrder;
804
        }
805

    
806
        public void setOwnOrder(boolean ownOrder) {
807
                this.ownOrder = ownOrder;
808
        }
809

    
810
        public ArrayList getOrders() {
811
                return orders;
812
        }
813

    
814
        public void setOrders(ArrayList orders) {
815
                this.orders = orders;
816
        }
817
}