Statistics
| Revision:

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

History | View | Annotate | Download (21 KB)

1 1100 fjp
/* 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 322 fernando
package com.iver.cit.gvsig.fmap.rendering;
42
43 22727 vcaballero
import java.awt.Color;
44 7659 fjp
import java.sql.Types;
45
import java.text.ParseException;
46
import java.util.ArrayList;
47
import java.util.Comparator;
48 13606 jaume
import java.util.Iterator;
49 29870 vcaballero
import java.util.Set;
50 7659 fjp
import java.util.TreeMap;
51 29870 vcaballero
import java.util.Map.Entry;
52 7659 fjp
53 11853 jaume
import org.apache.log4j.Logger;
54
55 10627 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
56 470 fjp
import com.hardcode.gdbms.engine.data.DataSource;
57
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
58
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
59 479 fjp
import com.hardcode.gdbms.engine.instruction.SemanticException;
60 470 fjp
import com.hardcode.gdbms.engine.values.BooleanValue;
61 2361 vcaballero
import com.hardcode.gdbms.engine.values.NullValue;
62 2415 caballero
import com.hardcode.gdbms.engine.values.StringValue;
63 470 fjp
import com.hardcode.gdbms.engine.values.Value;
64 479 fjp
import com.hardcode.gdbms.engine.values.ValueFactory;
65 2183 fernando
import com.iver.cit.gvsig.fmap.core.IFeature;
66 10815 jaume
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
67 9641 jaume
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
68 458 fjp
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
69 490 fernando
import com.iver.cit.gvsig.fmap.layers.XMLException;
70 22727 vcaballero
import com.iver.utiles.StringUtilities;
71 435 vcaballero
import com.iver.utiles.XMLEntity;
72
73 322 fernando
/**
74 14426 jvidal
 * Vectorial legend for unique values
75 22712 vcaballero
 *
76 24680 vcaballero
 * @author Vicente Caballero Navarro
77 322 fernando
 */
78 24680 vcaballero
// 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 27224 vcaballero
86 13884 jaume
        protected int fieldId;
87 24680 vcaballero
88 12785 jaume
        protected DataSource dataSource;
89 12882 jvidal
90 24680 vcaballero
        private boolean ownOrder = false;
91
92
        private ArrayList orders = new ArrayList();
93
94 13884 jaume
        private TreeMap<Value, ISymbol> symbols = new TreeMap<Value, ISymbol>(
95
                        new Comparator<Object>() {
96 22750 vcaballero
                                public int compare(Object o1, Object o2) {
97 24680 vcaballero
                                        if (ownOrder) {
98 29870 vcaballero
                                                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 24680 vcaballero
                                                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 22750 vcaballero
                                        if ((o1 != null) && (o2 != null)) {
113
                                                Value v2 = (Value) o2;
114
                                                Value v1 = (Value) o1;
115
                                                BooleanValue boolVal;
116 10679 jaume
117 24680 vcaballero
                                                // TODO estas dos comprobaciones son por evitar un bug
118
                                                // en el gdbms, cuando se solucione se puede eliminar.
119 22750 vcaballero
                                                if (v1 instanceof NullValue && v2 instanceof NullValue) {
120
                                                        return 0;
121
                                                }
122 1034 vcaballero
123 22750 vcaballero
                                                if (v1 instanceof NullValue) {
124
                                                        return -1;
125
                                                }
126 1034 vcaballero
127 22750 vcaballero
                                                if (v2 instanceof NullValue) {
128
                                                        return 1;
129
                                                }
130 1034 vcaballero
131 22750 vcaballero
                                                try {
132
                                                        boolVal = (BooleanValue) (v1.greater(v2));
133 1034 vcaballero
134 22750 vcaballero
                                                        if (boolVal.getValue()) {
135
                                                                return 1;
136
                                                        }
137 1034 vcaballero
138 22750 vcaballero
                                                        boolVal = (BooleanValue) (v1.less(v2));
139 1034 vcaballero
140 22750 vcaballero
                                                        if (boolVal.getValue()) {
141
                                                                return -1;
142
                                                        }
143
                                                } catch (IncompatibleTypesException e) {
144
                                                        // TODO Auto-generated catch block
145 24680 vcaballero
                                                        // e.printStackTrace();
146 22750 vcaballero
                                                }
147 1034 vcaballero
148 22750 vcaballero
                                                try {
149
                                                        if (((BooleanValue) v1.equals(v2)).getValue()) {
150
                                                                return 0;
151
                                                        }
152
                                                } catch (IncompatibleTypesException e) {
153
                                                        // TODO Auto-generated catch block
154 24680 vcaballero
                                                        // e.printStackTrace();
155 22750 vcaballero
                                                }
156
157
                                                if (v1 instanceof StringValue) {
158
                                                        return -1;
159
                                                }
160
161
                                                if (v2 instanceof StringValue) {
162
                                                        return 1;
163
                                                }
164 10506 caballero
                                        }
165 1034 vcaballero
166 22750 vcaballero
                                        return 0;
167 10506 caballero
                                }
168 22750 vcaballero
                        }); // Para poder ordenar
169 24680 vcaballero
170
        private ArrayList<Value> keys = new ArrayList<Value>(); // En lugar de un
171
                                                                                                                        // HashSet, para
172
                                                                                                                        // tener acceso por
173
                                                                                                                        // ?ndice
174
175 22750 vcaballero
        private String labelFieldName;
176 24680 vcaballero
177 22750 vcaballero
        private String labelFieldHeight;
178 24680 vcaballero
179 22750 vcaballero
        private String labelFieldRotation;
180 24680 vcaballero
181 22750 vcaballero
        private ISymbol defaultSymbol;
182 24680 vcaballero
183 22750 vcaballero
        private int shapeType;
184 24680 vcaballero
185 22750 vcaballero
        private String valueType = NullValue.class.getName();
186 24680 vcaballero
187 22750 vcaballero
        private boolean useDefaultSymbol = false;
188 24680 vcaballero
189
        private Color[] selectedColors = null;
190
191 22750 vcaballero
        /**
192
         * Constructor method
193
         */
194
        public VectorialUniqueValueLegend() {
195
        }
196 10679 jaume
197 22750 vcaballero
        /**
198
         * Constructor method
199
         *
200 24680 vcaballero
         * @param shapeType
201
         *            Type of the shape.
202 22750 vcaballero
         */
203
        public VectorialUniqueValueLegend(int shapeType) {
204
                setShapeType(shapeType);
205
        }
206 10679 jaume
207 22750 vcaballero
        public void setShapeType(int shapeType) {
208
                if (this.shapeType != shapeType) {
209
                        ISymbol old = defaultSymbol;
210 24680 vcaballero
                        defaultSymbol = SymbologyFactory
211
                                        .createDefaultSymbolByShapeType(shapeType);
212
                        fireDefaultSymbolChangedEvent(new SymbolLegendEvent(old,
213
                                        defaultSymbol));
214 22750 vcaballero
                        this.shapeType = shapeType;
215 10506 caballero
                }
216 22750 vcaballero
        }
217 1034 vcaballero
218 22750 vcaballero
        public void setValueSymbolByID(int id, ISymbol symbol) {
219
                ISymbol old = symbols.put(keys.get(id), symbol);
220
                fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(old, symbol));
221
        }
222 1034 vcaballero
223 22750 vcaballero
        /**
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 322 fernando
235 22750 vcaballero
        public Object[] getValues() {
236
                return symbols.keySet().toArray(new Object[0]);
237
        }
238 322 fernando
239 22750 vcaballero
        public void addSymbol(Object key, ISymbol symbol) {
240
                ISymbol resul;
241
                resul = symbols.put((Value) key, symbol);
242 322 fernando
243 22750 vcaballero
                if (resul != null) {
244 24680 vcaballero
                        log.error("Error: la clave " + key + " ya exist?a. Resul = "
245
                                        + resul);
246
                        log.warn("symbol nuevo:" + symbol.getDescription() + " Sviejo= "
247
                                        + resul.getDescription());
248 22750 vcaballero
                } else {
249
                        keys.add((Value) key);
250 322 fernando
251 22750 vcaballero
                        if (!key.getClass().equals(NullValue.class)) {
252
                                valueType = key.getClass().getName();
253
                        }
254
                }
255
                fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(resul, symbol));
256 435 vcaballero
257 26364 vcaballero
258 22750 vcaballero
        }
259 22712 vcaballero
260 22750 vcaballero
        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 22712 vcaballero
267 22750 vcaballero
                fireLegendClearEvent(new LegendClearEvent(olds));
268
        }
269 2796 caballero
270 22750 vcaballero
        public String[] getDescriptions() {
271
                String[] descriptions = new String[symbols.size()];
272
                ISymbol[] auxSym = getSymbols();
273 22712 vcaballero
274 22750 vcaballero
                for (int i = 0; i < descriptions.length; i++)
275
                        descriptions[i] = auxSym[i].getDescription();
276 2796 caballero
277 22750 vcaballero
                return descriptions;
278
        }
279 2796 caballero
280 22750 vcaballero
        public ISymbol[] getSymbols() {
281
                return symbols.values().toArray(new ISymbol[0]);
282
        }
283 2796 caballero
284 22750 vcaballero
        @Override
285
        public void setClassifyingFieldNames(String[] fNames) {
286
                super.setClassifyingFieldNames(fNames);
287
                try {
288 24680 vcaballero
                        fieldId = dataSource
289
                                        .getFieldIndexByName(getClassifyingFieldNames()[0]);
290 22750 vcaballero
                } catch (NullPointerException e) {
291
                        log.warn("data source not set");
292
                } catch (ReadDriverException e) {
293
                        log.warn("failed setting field id");
294
                }
295
        }
296 24680 vcaballero
297 22750 vcaballero
        /*
298
         * @see com.iver.cit.gvsig.fmap.rendering.IVectorialLegend#getSymbol(int)
299
         */
300
        public ISymbol getSymbol(int recordIndex) throws ReadDriverException {
301 13884 jaume
                Value val = dataSource.getFieldValue(recordIndex, fieldId);
302 10627 caballero
                ISymbol theSymbol = getSymbolByValue(val);
303 2796 caballero
304 10627 caballero
                return theSymbol;
305
        }
306 2796 caballero
307 22750 vcaballero
        /**
308 10627 caballero
         * 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 22750 vcaballero
        public ISymbol getSymbolByFeature(IFeature feat) {
318 24680 vcaballero
                // Value val =
319
                // feat.getAttribute(FLyrVect.forTestOnlyVariableUseIterators_REMOVE_THIS_FIELD
320
                // ? 0 :fieldId);
321 24371 vcaballero
                Value val = feat.getAttribute(0);
322 31040 fpenarrubia
                // 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 22750 vcaballero
                ISymbol theSymbol = getSymbolByValue(val);
327 2183 fernando
328 22750 vcaballero
                if (theSymbol != null) {
329
                        return theSymbol;
330
                }
331
                return null;
332
        }
333 322 fernando
334 22750 vcaballero
        public ISymbol getDefaultSymbol() {
335 12882 jvidal
336 24680 vcaballero
                if (defaultSymbol == null) {
337
                        defaultSymbol = SymbologyFactory
338
                                        .createDefaultSymbolByShapeType(shapeType);
339
                        fireDefaultSymbolChangedEvent(new SymbolLegendEvent(null,
340
                                        defaultSymbol));
341 22750 vcaballero
                }
342
                return defaultSymbol;
343
        }
344 1034 vcaballero
345 22750 vcaballero
        public XMLEntity getXMLEntity() {
346
                XMLEntity xml = new XMLEntity();
347
                xml.putProperty("className", this.getClass().getName());
348
                xml.putProperty("fieldNames", getClassifyingFieldNames()[0]);
349 25743 vcaballero
                if (getClassifyingFieldTypes()!=null)
350
                        xml.putProperty("fieldTypes", getClassifyingFieldTypes()[0]);
351 24680 vcaballero
                xml.putProperty("ownOrder", isOwnOrder());
352 22712 vcaballero
353 24692 vcaballero
                xml.putProperty("orders",getOrders().toArray());
354
355 24680 vcaballero
                if (selectedColors != null) {
356 22750 vcaballero
                        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 22727 vcaballero
                }
362
363 22750 vcaballero
                xml.putProperty("labelfield", labelFieldName);
364
                xml.putProperty("labelFieldHeight", labelFieldHeight);
365
                xml.putProperty("labelFieldRotation", labelFieldRotation);
366 1034 vcaballero
367 22750 vcaballero
                xml.putProperty("useDefaultSymbol", useDefaultSymbol);
368
                xml.addChild(getDefaultSymbol().getXMLEntity());
369
                xml.putProperty("numKeys", keys.size());
370 435 vcaballero
371 22750 vcaballero
                if (keys.size() > 0) {
372
                        xml.putProperty("tipoValueKeys", valueType);
373 1034 vcaballero
374 22750 vcaballero
                        String[] sk = new String[keys.size()];
375
                        int[] stk = new int[keys.size()];
376
                        Object[] values = getValues();
377 28673 vcaballero
                        String[] sv = new String[values.length];
378
                        int[] stv = new int[values.length];
379 1034 vcaballero
380 22750 vcaballero
                        for (int i = 0; i < keys.size(); i++) {
381 28673 vcaballero
                                Value key = keys.get(i);
382
                                sk[i] = key.toString();
383
                                stk[i] = key.getSQLType();
384
                        }
385 1034 vcaballero
386 28673 vcaballero
                        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 31294 vcaballero
396 29870 vcaballero
//                                ISymbol symbol = symbols.get(value);
397
                                //PARCHE
398
                                ISymbol symbol = getSymbolByKey(value);
399
                                //FIN DEL PARCHE
400 31294 vcaballero
401 29870 vcaballero
                                if(symbol != null){
402
                                        xml.addChild(symbol.getXMLEntity());
403
                                }
404 22750 vcaballero
                        }
405 1034 vcaballero
406 22750 vcaballero
                        xml.putProperty("keys", sk);
407
                        xml.putProperty("values", sv);
408 24680 vcaballero
                        xml.putProperty("typeKeys", stk);
409
                        xml.putProperty("typeValues", stv);
410 22750 vcaballero
                }
411 22712 vcaballero
412 24680 vcaballero
                if (getZSort() != null) {
413 22750 vcaballero
                        XMLEntity xmlZSort = getZSort().getXMLEntity();
414
                        xmlZSort.putProperty("id", "zSort");
415
                        xml.addChild(xmlZSort);
416
                }
417
                return xml;
418
        }
419 2183 fernando
420 22750 vcaballero
        public void setXMLEntity03(XMLEntity xml) {
421
                clear();
422 24680 vcaballero
                setClassifyingFieldNames(new String[] { xml
423
                                .getStringProperty("fieldName") });
424 2183 fernando
425 22750 vcaballero
                int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
426 2183 fernando
427 22750 vcaballero
                if (useDefaultSymbol == 1) {
428
                        setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
429
                } else {
430
                        setDefaultSymbol(null);
431
                }
432 2183 fernando
433 22750 vcaballero
                int numKeys = xml.getIntProperty("numKeys");
434 2183 fernando
435 22750 vcaballero
                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 2183 fernando
442 22750 vcaballero
                        for (int i = 0; i < numKeys; i++) {
443
                                try {
444
                                        auxValue = ValueFactory.createValue(sk[i], className);
445
                                        auxValue2 = ValueFactory.createValue(sv[i], className);
446 2183 fernando
447 24680 vcaballero
                                        ISymbol sym = FSymbol.createFromXML03(xml.getChild(i
448
                                                        + useDefaultSymbol));
449 2183 fernando
450 22750 vcaballero
                                        symbols.put(auxValue2, sym);
451
                                        keys.add(auxValue);
452 2183 fernando
453 22750 vcaballero
                                } catch (SemanticException e) {
454
                                        log.error("Exception", e);
455
                                        e.printStackTrace();
456
                                }
457
                        }
458
                }
459
        }
460 2183 fernando
461 22750 vcaballero
        public void setXMLEntity(XMLEntity xml) {
462
                clear();
463
                if (xml.contains("fieldName"))
464 24680 vcaballero
                        setClassifyingFieldNames(new String[] { xml
465
                                        .getStringProperty("fieldName") });
466 22750 vcaballero
                else
467
                        setClassifyingFieldNames(xml.getStringArrayProperty("fieldNames"));
468 2796 caballero
469 22750 vcaballero
                if (xml.contains("fieldTypes"))
470 24680 vcaballero
                        setClassifyingFieldTypes(new int[] { xml
471
                                        .getIntProperty("fieldTypes") });
472 16203 jdominguez
473 24680 vcaballero
                if (xml.contains("colorScheme")) {
474 22750 vcaballero
                        String[] strColors = xml.getStringArrayProperty("colorScheme");
475 22727 vcaballero
476 22750 vcaballero
                        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 22727 vcaballero
                }
482 24680 vcaballero
483
                if (xml.contains("ownOrder"))
484
                        setOwnOrder(xml.getBooleanProperty("ownOrder"));
485 24692 vcaballero
                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 24680 vcaballero
494 22750 vcaballero
                useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbol");
495 24680 vcaballero
                setDefaultSymbol(SymbologyFactory.createSymbolFromXML(xml.getChild(0),
496
                                null));
497 22727 vcaballero
498 22750 vcaballero
                int numKeys = xml.getIntProperty("numKeys");
499 2796 caballero
500 22750 vcaballero
                if (numKeys > 0) {
501
                        String className = xml.getStringProperty("tipoValueKeys");
502
                        String[] sk = xml.getStringArrayProperty("keys");
503 28219 vcaballero
                        if(sk.length == 0){
504
                                sk = new String[]{""};
505
                        }
506 22750 vcaballero
                        String[] sv = xml.getStringArrayProperty("values");
507 28219 vcaballero
                        if(sv.length == 0){
508
                                sv = new String[]{""};
509
                        }
510 22750 vcaballero
                        Value auxValue = null;
511
                        Value auxValue2 = null;
512 28673 vcaballero
                        ISymbol sym;
513 24680 vcaballero
                        int[] stk = null;
514 22750 vcaballero
                        if (xml.contains("typeKeys")) {
515 6631 caballero
                                stk = xml.getIntArrayProperty("typeKeys");
516
                                int[] stv = xml.getIntArrayProperty("typeValues");
517
                                for (int i = 0; i < numKeys; i++) {
518 28673 vcaballero
                                        auxValue = getValue(sk[i], stk[i]);
519
                                        if ( auxValue instanceof NullValue ) {
520
                                                auxValue = new NullUniqueValue();
521 30041 vcaballero
                                        } else {
522
                                        keys.add(auxValue);
523 6631 caballero
                                        }
524 28673 vcaballero
                                }
525 1034 vcaballero
526 28673 vcaballero
                                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 6631 caballero
                                        } else {
534 30041 vcaballero
                                                if(foundNullValue){
535 30766 fdiaz
                                                        if(stv.length == stk.length && xml.getChildrenCount()>stv.length){
536 30603 fdiaz
                                                                sym = SymbologyFactory.createSymbolFromXML(xml
537
                                                                                .getChild(i+1), null);
538
                                                        } else {
539
                                                                sym = SymbologyFactory.createSymbolFromXML(xml
540
                                                                                .getChild(i), null);
541
                                                        }
542 30041 vcaballero
                                                } else {
543
                                                        sym = SymbologyFactory.createSymbolFromXML(xml
544
                                                                        .getChild(i+1), null);
545
                                                }
546 6631 caballero
                                        }
547 9558 jaume
548 6631 caballero
                                        symbols.put(auxValue2, sym);
549
                                }
550 28673 vcaballero
                                if (!foundNullValue && useDefaultSymbol){
551
                                        auxValue2 = new NullUniqueValue();
552
                                        sym = getDefaultSymbol();
553
                                        symbols.put(auxValue2, sym);
554
                                }
555 6631 caballero
                        } else {
556 28673 vcaballero
557
558 6631 caballero
                                for (int i = 0; i < numKeys; i++) {
559 28673 vcaballero
                                        auxValue = getValue(sk[i]);
560
                                        if ( auxValue  == null ) { //Default
561
                                                auxValue = new NullUniqueValue();
562 6631 caballero
                                        }
563 28673 vcaballero
                                        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 6631 caballero
                                        } else {
574 28673 vcaballero
                                                sym = SymbologyFactory.createSymbolFromXML(xml
575
                                                                .getChild(i+1), null);
576 6631 caballero
                                        }
577 28673 vcaballero
578 6631 caballero
                                        symbols.put(auxValue2, sym);
579
                                }
580 28673 vcaballero
                                if (!foundNullValue && useDefaultSymbol){
581
                                        auxValue2 = new NullUniqueValue();
582
                                        sym = getDefaultSymbol();
583
                                        symbols.put(auxValue2, sym);
584
                                }
585 6631 caballero
                        }
586 22750 vcaballero
                }
587 22712 vcaballero
588 22750 vcaballero
                XMLEntity zSortXML = xml.firstChild("id", "zSort");
589 14235 jdominguez
                if (zSortXML != null) {
590 16454 jdominguez
                        ZSort zSort = new ZSort(this);
591 14235 jdominguez
                        zSort.setXMLEntity(zSortXML);
592
                        addLegendListener(zSort);
593 16454 jdominguez
                        setZSort(zSort);
594 14235 jdominguez
                }
595 22750 vcaballero
        }
596 2415 caballero
597 22727 vcaballero
        public void setDefaultSymbol(ISymbol s) {
598 22750 vcaballero
                ISymbol mySymbol = defaultSymbol;
599 22712 vcaballero
600 22750 vcaballero
                if (s == null)
601
                        throw new NullPointerException("Default symbol cannot be null");
602 22712 vcaballero
603 22750 vcaballero
                ISymbol old = mySymbol;
604
                defaultSymbol = s;
605
                fireDefaultSymbolChangedEvent(new SymbolLegendEvent(old, s));
606
        }
607 14201 jvidal
608 22750 vcaballero
        /**
609
         * Returns the value using the its value in a string.
610 6631 caballero
         *
611 22712 vcaballero
         *
612 24680 vcaballero
         * @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 6631 caballero
         * @return Value.
617
         */
618 22750 vcaballero
        private Value getValue(String s) {
619
                Value val = new NullUniqueValue();
620 24680 vcaballero
                if (s.equals("Resto de Valores"))
621
                        return val;
622 22750 vcaballero
                try {
623
                        try {
624
                                val = ValueFactory.createValueByType(s, Types.INTEGER);
625 2415 caballero
626 22750 vcaballero
                                return val;
627
                        } catch (NumberFormatException e) {
628
                        }
629 2415 caballero
630 22750 vcaballero
                        try {
631
                                val = ValueFactory.createValueByType(s, Types.BIGINT);
632 2415 caballero
633 22750 vcaballero
                                return val;
634
                        } catch (NumberFormatException e) {
635
                        }
636 2415 caballero
637 22750 vcaballero
                        try {
638
                                val = ValueFactory.createValueByType(s, Types.FLOAT);
639 1034 vcaballero
640 22750 vcaballero
                                return val;
641
                        } catch (NumberFormatException e) {
642
                        }
643 470 fjp
644 22750 vcaballero
                        try {
645
                                val = ValueFactory.createValueByType(s, Types.DOUBLE);
646 1034 vcaballero
647 22750 vcaballero
                                return val;
648
                        } catch (NumberFormatException e) {
649
                        }
650 12882 jvidal
651 22750 vcaballero
                        val = ValueFactory.createValueByType(s, Types.LONGVARCHAR);
652 6368 caballero
653 22750 vcaballero
                } catch (ParseException e) {
654
                        log.warn("parse exception", e);
655
                }
656 1034 vcaballero
657 22750 vcaballero
                return val;
658
        }
659 10679 jaume
660 22750 vcaballero
        /**
661
         * Devuelve el valor a partir de su valor en un string.
662
         *
663 24680 vcaballero
         * @param s
664
         *            String con el valor.
665 22750 vcaballero
         *
666
         * @return Value.
667
         */
668 24680 vcaballero
        private Value getValue(String s, int type) {
669 22750 vcaballero
                Value val = new NullUniqueValue();
670 24680 vcaballero
                if (type == Types.OTHER)
671 22750 vcaballero
                        return val;
672
                try {
673
                        val = ValueFactory.createValueByType(s, type);
674
                } catch (ParseException e) {
675
                        e.printStackTrace();
676
                }
677
                return val;
678
        }
679 1034 vcaballero
680 22750 vcaballero
        public ILegend cloneLegend() throws XMLException {
681
                return LegendFactory.createFromXML(getXMLEntity());
682
        }
683 1034 vcaballero
684 24680 vcaballero
        /*
685
         * (non-Javadoc)
686
         *
687 22750 vcaballero
         * @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 24680 vcaballero
                        ReadDriverException {
691 10627 caballero
                dataSource = ds;
692
                ds.start();
693 31294 vcaballero
                if (getClassifyingFieldNames() != null && getClassifyingFieldNames().length>0)
694 21535 vcaballero
                        fieldId = ds.getFieldIndexByName(getClassifyingFieldNames()[0]);
695 10627 caballero
                ds.stop();
696
        }
697 801 fjp
698 22750 vcaballero
        /*
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 29870 vcaballero
705 37955 fpenarrubia
                ISymbol symbol = symbols.get(key);
706 31294 vcaballero
707 29870 vcaballero
                //PARCHE
708 37955 fpenarrubia
//                ISymbol symbol = getSymbolByKey(key);
709 29870 vcaballero
                //FIN DEL PARCHE
710 31294 vcaballero
711 24680 vcaballero
                if (symbol != null) {
712 22750 vcaballero
                        return symbol;
713
                } else if (useDefaultSymbol) {
714
                        return getDefaultSymbol();
715
                }
716
                return null;
717 2415 caballero
718 22750 vcaballero
        }
719 31294 vcaballero
720 29870 vcaballero
        private ISymbol getSymbolByKey(Value key) {
721
                //FIXME: Esto es un parche para sustituir symbols.get(key)
722 31294 vcaballero
                // porque parece que no funciona bien el metodo get sobre un
723 29870 vcaballero
                // TreeMap cuyas claves son Values. Si se consigue que funcione
724
                // correctamente, eliminar este metodo.
725 31807 vcaballero
                if (key==null)
726
                        return null;
727 29870 vcaballero
                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 2600 caballero
746 22750 vcaballero
        public int getShapeType() {
747
                return shapeType;
748
        }
749 2600 caballero
750 22750 vcaballero
        public void useDefaultSymbol(boolean b) {
751
                useDefaultSymbol = b;
752
        }
753 10679 jaume
754 22750 vcaballero
        /**
755 10679 jaume
         * Devuelve si se utiliza o no el resto de valores para representarse.
756 24680 vcaballero
         *
757
         * @return True si se utiliza el resto de valores.
758 10679 jaume
         */
759 22750 vcaballero
        public boolean isUseDefaultSymbol() {
760
                return useDefaultSymbol;
761
        }
762 2796 caballero
763 22750 vcaballero
        public void delSymbol(Object key) {
764
                keys.remove(key);
765 26364 vcaballero
                ISymbol removedSymbol = symbols.remove(key);
766
                if (removedSymbol != null){
767
                        fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(removedSymbol, null));
768
                }
769 22750 vcaballero
        }
770 3268 fjp
771 12882 jvidal
        public String getClassName() {
772
                return getClass().getName();
773
        }
774 22712 vcaballero
775 13606 jaume
        public void replace(ISymbol oldSymbol, ISymbol newSymbol) {
776 30082 vcaballero
//                if (symbols.containsValue(oldSymbol)) {
777 29870 vcaballero
                        Iterator<Entry<Value, ISymbol>> it = symbols.entrySet().iterator();
778 13606 jaume
                        while (it.hasNext()) {
779 29870 vcaballero
                                Entry<Value, ISymbol> entry = it.next();
780
                                if (entry.getValue().equals(oldSymbol)) {
781
                                        entry.setValue(newSymbol);
782 14201 jvidal
                                        fireClassifiedSymbolChangeEvent(new SymbolLegendEvent(
783 29870 vcaballero
                                        oldSymbol, newSymbol));
784
                                        break;
785 13606 jaume
                                }
786
                        }
787 30082 vcaballero
                        if(oldSymbol.equals(this.getDefaultSymbol())) {
788
                                this.setDefaultSymbol(newSymbol);
789
                        }
790 31294 vcaballero
791 30082 vcaballero
//                }
792 13606 jaume
        }
793 22727 vcaballero
794
        public Color[] getColorScheme() {
795
                return selectedColors;
796
        }
797
798
        public void setColorScheme(Color[] cc) {
799 22750 vcaballero
                this.selectedColors = cc;
800 22727 vcaballero
        }
801 24680 vcaballero
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 322 fernando
}