Statistics
| Revision:

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

History | View | Annotate | Download (27 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.sql.Types;
44
import java.text.ParseException;
45
import java.util.ArrayList;
46
import java.util.Comparator;
47
import java.util.TreeMap;
48

    
49
import org.geotools.filter.ExpressionBuilder;
50
import org.geotools.filter.Filter;
51
import org.geotools.styling.FeatureTypeStyle;
52
import org.geotools.styling.NamedLayer;
53
import org.geotools.styling.Rule;
54
import org.geotools.styling.SLDTransformer;
55
import org.geotools.styling.Style;
56
import org.geotools.styling.StyleBuilder;
57
import org.geotools.styling.StyleFactory;
58
import org.geotools.styling.StyledLayerDescriptor;
59
import org.geotools.styling.Symbolizer;
60

    
61
import com.hardcode.gdbms.engine.data.DataSource;
62
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
63
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
64
import com.hardcode.gdbms.engine.instruction.SemanticException;
65
import com.hardcode.gdbms.engine.values.BooleanValue;
66
import com.hardcode.gdbms.engine.values.NullValue;
67
import com.hardcode.gdbms.engine.values.StringValue;
68
import com.hardcode.gdbms.engine.values.Value;
69
import com.hardcode.gdbms.engine.values.ValueFactory;
70
import com.iver.cit.gvsig.fmap.DriverException;
71
import com.iver.cit.gvsig.fmap.core.FShape;
72
import com.iver.cit.gvsig.fmap.core.IFeature;
73
import com.iver.cit.gvsig.fmap.core.ISLDCompatible;
74
import com.iver.cit.gvsig.fmap.core.SLDTags;
75
import com.iver.cit.gvsig.fmap.core.SLDUtils;
76
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
77
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
78
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
79
import com.iver.cit.gvsig.fmap.layers.XMLException;
80
import com.iver.utiles.XMLEntity;
81

    
82
/**
83
 * Leyenda vectorial por valores ?nicos.
84
 *
85
 * @author Vicente Caballero Navarro
86
 */
87
public class VectorialUniqueValueLegend implements UniqueValueLegend,
88
    VectorialLegend {
89
    private TreeMap symbols = new TreeMap(new Comparator() {
90
                public int compare(Object o1, Object o2) {
91
                    if ((o1 != null) && (o2 != null)) {
92
                        Value v2 = (Value) o2;
93
                        Value v1 = (Value) o1;
94
                        BooleanValue boolVal;
95

    
96
                        //                                                TODO estas dos comprobaciones son por evitar un bug en el gdbms, cuando se solucione se puede eliminar.
97
                        if (v1 instanceof NullValue && v2 instanceof NullValue) {
98
                            return 0;
99
                        }
100

    
101
                        if (v1 instanceof NullValue) {
102
                            return -1;
103
                        }
104

    
105
                        if (v2 instanceof NullValue) {
106
                            return 1;
107
                        }
108

    
109
                        try {
110
                            boolVal = (BooleanValue) (v1.greater(v2));
111

    
112
                            if (boolVal.getValue()) {
113
                                return 1;
114
                            }
115

    
116
                            boolVal = (BooleanValue) (v1.less(v2));
117

    
118
                            if (boolVal.getValue()) {
119
                                return -1;
120
                            }
121
                        } catch (IncompatibleTypesException e) {
122
                            // TODO Auto-generated catch block
123
                            //e.printStackTrace();
124
                        }
125

    
126
                        try {
127
                            if (((BooleanValue) v1.equals(v2)).getValue()) {
128
                                return 0;
129
                            }
130
                        } catch (IncompatibleTypesException e) {
131
                            // TODO Auto-generated catch block
132
                            //e.printStackTrace();
133
                        }
134

    
135
                        if (v1 instanceof StringValue) {
136
                            return -1;
137
                        }
138

    
139
                        if (v2 instanceof StringValue) {
140
                            return 1;
141
                        }
142
                    }
143

    
144
                    return 0;
145
                }
146
            }); // Para poder ordenar
147
    private ArrayList keys = new ArrayList(); // En lugar de un HashSet, para tener acceso por ?ndice
148
    private String fieldName;
149
    protected int fieldId = -1;
150
    private String labelFieldName;
151
    private String labelFieldHeight;
152
    private String labelFieldRotation;
153
    private ISymbol defaultSymbol;
154
    private DataSource dataSource;
155
    private int shapeType;
156
    private String valueType = NullValue.class.getName();
157
    private boolean useDefaultSymbol = false;
158
    // private boolean bWithHeightText = false;
159

    
160
    /**
161
     * Crea un nuevo VectorialUniqueValueLegend.
162
     */
163
    public VectorialUniqueValueLegend() {
164
        //        defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
165
    }
166

    
167
    /**
168
     * Crea un nuevo VectorialUniqueValueLegend.
169
     *
170
     * @param shapeType Tipo de shape.
171
     */
172
    public VectorialUniqueValueLegend(int shapeType) {
173
        setShapeType(shapeType);
174
    }
175

    
176
    /**
177
     * Inserta el tipo de shape.
178
     *
179
     * @param shapeType Tipo de shape.
180
     */
181
    public void setShapeType(int shapeType) {
182
        if (this.shapeType != shapeType) {
183
            switch (shapeType) {
184
                case FShape.POINT:
185
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
186

    
187
                    break;
188

    
189
                case FShape.LINE:
190
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
191

    
192
                    break;
193

    
194
                case FShape.POLYGON:
195
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
196

    
197
                    break;
198

    
199
                default:
200
                    defaultSymbol = new FSymbol(shapeType);
201
            }
202

    
203
            this.shapeType = shapeType;
204
        }
205
    }
206

    
207
    /**
208
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#setValueSymbolByID(int,
209
     *      ISymbol)
210
     */
211
    public void setValueSymbolByID(int id, ISymbol symbol) {
212
        symbols.put(keys.get(id), symbol);
213
    }
214

    
215
    /**
216
     * Devuelve un s?mbolo a partir del ID. Mira en el m_ArrayKeys  el elemento
217
     * ID, y con esa clave recupera el FSymbol por valor
218
     *
219
     * @param id ID.
220
     * @param symbol DOCUMENT ME!
221
     */
222

    
223
    /*
224
       public FSymbol getSymbolByID(int ID) {
225
           return (FSymbol) symbols.get(keys.get(ID));
226
       }
227
     */
228

    
229
    /**
230
     * Se usa en la tabla que muestra una leyenda.
231
     *        @deprecated use setValueSymbolByID(int id, ISymbol symbol);
232
     * @param id
233
     * @param symbol
234
     */
235
    public void setValueSymbol(int id, ISymbol symbol) {
236
        symbols.put(keys.get(id), symbol);
237
    }
238

    
239
    /**
240
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getValues()
241
     */
242
    public Object[] getValues() {
243
        return symbols.keySet().toArray(new Object[0]);
244
    }
245

    
246
    /**
247
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#addSymbol(java.lang.Object,
248
     *      ISymbol)
249
     */
250
    public void addSymbol(Object key, ISymbol symbol) {
251
        Object resul;
252
        resul = symbols.put(key, symbol);
253

    
254
        if (resul != null) {
255
            System.err.println("Error: la clave " + key +
256
                " ya exist?a. Resul = " + resul);
257
            System.err.println("symbol nuevo:" + symbol.getDescription() +
258
                " Sviejo= " + ((ISymbol) resul).getDescription());
259
        } else {
260
            keys.add(key);
261

    
262
            if (!key.getClass().equals(NullValue.class)) {
263
                valueType = key.getClass().getName();
264
            }
265
        }
266
    }
267

    
268
    /**
269
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#clear()
270
     */
271
    public void clear() {
272
        keys.clear();
273
        symbols.clear();
274
    }
275

    
276
    /**
277
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getDescriptions()
278
     */
279
    public String[] getDescriptions() {
280
        String[] descriptions = new String[symbols.size()];
281
        ISymbol[] auxSym = getSymbols();
282

    
283
        for (int i = 0; i < descriptions.length; i++)
284
            descriptions[i] = auxSym[i].getDescription();
285

    
286
        return descriptions;
287
    }
288

    
289
    /**
290
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getSymbols()
291
     */
292
    public ISymbol[] getSymbols() {
293
        return (ISymbol[]) symbols.values().toArray(new ISymbol[0]);
294
    }
295

    
296
    /**
297
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getFieldName()
298
     */
299
    public String getFieldName() {
300
        return fieldName;
301
    }
302

    
303
    /**
304
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDefaultSymbol(ISymbol)
305
     */
306
    public void setDefaultSymbol(ISymbol s) {
307
        defaultSymbol = s;
308
    }
309

    
310
    /* (non-Javadoc)
311
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelField()
312
     */
313
    public String getLabelField() {
314
        return labelFieldName;
315
    }
316

    
317
    /**
318
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#setLabelField(int)
319
     */
320
    public void setLabelField(String fieldName) {
321
        labelFieldName = fieldName;
322
    }
323

    
324
    /**
325
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#setField()
326
     */
327
    public void setFieldName(String str) {
328
        fieldName = str;
329
    }
330

    
331
    /**
332
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getSymbol(int)
333
     */
334
    public ISymbol getSymbol(int recordIndex) throws DriverException {
335
        try {
336
                 Value val = dataSource.getFieldValue(recordIndex, fieldId);
337
            ISymbol theSymbol = getSymbolByValue(val);
338

    
339
            //if (theSymbol != null) {
340
            return theSymbol;
341

    
342
            //} else {
343
            //        return getDefaultSymbol();
344
            //}
345
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
346
            throw new DriverException(e);
347
        }
348
    }
349

    
350
    /**
351
     * Devuelve un s?mbolo a partir de una IFeature.
352
     * OJO!! Cuando usamos un feature iterator de base de datos
353
     * el ?nico campo que vendr? rellenado es el de fieldID.
354
     * Los dem?s vendr?n a nulos para ahorra tiempo de creaci?n.
355
     * @param feat IFeature
356
     *
357
     * @return S?mbolo.
358
     */
359
    public ISymbol getSymbolByFeature(IFeature feat) {
360
        Value val = feat.getAttribute(fieldId);
361
        // Value val = feat.getAttribute(0);
362
        ISymbol theSymbol = getSymbolByValue(val);
363

    
364
        //if (theSymbol != null) {
365
        return theSymbol;
366

    
367
        //} else {
368
        //    return getDefaultSymbol();
369
        //}
370
    }
371

    
372
    /**
373
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
374
     */
375
    public ISymbol getDefaultSymbol() {
376
        return defaultSymbol;
377
    }
378

    
379
        /**
380
         * @deprecated
381
         * Writes and SLD using GEOTOOLS objetcs.
382
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getSLDString()
383
         */
384
        public String getSLDString_()
385
        {
386
            try{
387
                        StyledLayerDescriptor sld = new StyledLayerDescriptor();
388
                        StyleFactory styleFactory = StyleFactory.createStyleFactory();
389
                        StyleBuilder sb = new StyleBuilder();
390
                Style style = sb.createStyle();
391
                style.setName("default");
392
                Filter filter = null;
393
                Rule rule = null;
394

    
395
                        FeatureTypeStyle featStyle = styleFactory.createFeatureTypeStyle();
396
                        featStyle.setFeatureTypeName(fieldName);
397

    
398
                        ISymbol[] symbols = this.getSymbols();
399
                        Symbolizer[] theSymbolizers = new Symbolizer[symbols.length];
400
                        Object[] values = this.getValues();
401
                        String valueStr = null;
402
                        String condition = null;
403

    
404
                        for(int i = 0; i < symbols.length; i++ )
405
                        {
406
                                valueStr = values[i].toString();
407
                                //if(this.valueType == "")
408
                                        condition = fieldName +"='"+valueStr+"'";
409
                                //else
410
                                //        condition = fieldName +"="+values[i];
411
                                filter = (Filter)ExpressionBuilder.parse(condition);
412
                                theSymbolizers[0] = SLDUtils.toGeotoolsSymbol(symbols[i]);
413
                                rule = styleFactory.createRule();
414
                                rule.setName(valueStr);
415
                                rule.setTitle(valueStr);
416
                                rule.setFilter(filter);
417
                                rule.setSymbolizers((Symbolizer[])theSymbolizers.clone());
418
                                featStyle.addRule(rule);
419
                        }
420
                        style.addFeatureTypeStyle(featStyle);
421
                        SLDTransformer st = new SLDTransformer();
422
                        NamedLayer namedLayer = new NamedLayer();
423
                        namedLayer.setName("comunidades");
424
                        namedLayer.addStyle(style);
425
                        sld.addStyledLayer(namedLayer);
426
                        return st.transform(style);
427

    
428
            }catch(Exception e)
429
            {
430
                    e.printStackTrace();
431
                    return null;
432
            }
433
        }
434
        /**
435
         * creates the SLD String that defines this legend type.
436
         */
437
        public String getSLDString(String name)
438
        {
439
            try{
440

    
441
                        XmlBuilder xmlBuilder = new XmlBuilder();
442
                        xmlBuilder.writeHeader();
443
                        xmlBuilder.openTag(SLDTags.SLD_ROOT, SLDTags.VERSION_ATTR, SLDTags.VERSION_1_0_0);
444
                        xmlBuilder.openTag(SLDTags.NAMEDLAYER);
445
                        xmlBuilder.writeTag(SLDTags.NAME,name);
446
                        xmlBuilder.openTag(SLDTags.USERSTYLE);
447
                        xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE);
448
                        //xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"fieldName");
449

    
450
                        ISymbol[] symbols = this.getSymbols();
451
                        Object[] values = this.getValues();
452
                        String valueStr = null;
453

    
454
                        for(int i = 0; i < symbols.length; i++ )
455
                        {
456
                                valueStr = values[i].toString();
457
                                xmlBuilder.openTag(SLDTags.RULE);
458
                                xmlBuilder.writeTag(SLDTags.NAME, valueStr);
459
                                xmlBuilder.openTag(SLDTags.FILTER);
460
                                xmlBuilder.openTag(SLDTags.PROPERTYISEQUALTO);
461
                                xmlBuilder.writeTag(SLDTags.PROPERTYNAME,fieldName);
462
                                xmlBuilder.writeTag(SLDTags.LITERAL, valueStr);
463
                                xmlBuilder.closeTag();
464
                                xmlBuilder.closeTag();
465
                                if (symbols[i] instanceof ISLDCompatible)
466
                                {
467
                                        ISLDCompatible symSLD = (ISLDCompatible) symbols[i];
468
                                        xmlBuilder.writeRaw(symSLD.toSLD());
469
                                }
470
                                else
471
                                        throw new RuntimeException("Cannot convert Symbol " + i + " " + symbols[i].getDescription() + " to SLD");
472

    
473
                                xmlBuilder.closeTag();
474
                        }
475

    
476
                        xmlBuilder.closeTag();
477
                        xmlBuilder.closeTag();
478
                        xmlBuilder.closeTag();
479
                        xmlBuilder.closeTag();
480
                        return xmlBuilder.getXML();
481

    
482
            }catch(Exception e)
483
            {
484
                    e.printStackTrace();
485
                    return null;
486
            }
487
        }
488
    /**
489
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
490
     */
491
    public XMLEntity getXMLEntity() {
492
        XMLEntity xml = new XMLEntity();
493
        xml.putProperty("className", this.getClass().getName());
494
        xml.putProperty("fieldName", fieldName);
495
        xml.putProperty("labelfield", labelFieldName);
496
        xml.putProperty("labelFieldHeight", labelFieldHeight);
497
        xml.putProperty("labelFieldRotation", labelFieldRotation);
498

    
499
        xml.putProperty("useDefaultSymbol", useDefaultSymbol);
500
        xml.addChild(getDefaultSymbol().getXMLEntity());
501
        // xml.putProperty("isBWithHeightText", isBWithHeightText());
502
        xml.putProperty("numKeys", keys.size());
503

    
504
        if (keys.size() > 0) {
505
            xml.putProperty("tipoValueKeys", valueType);
506

    
507
            String[] sk = new String[keys.size()];
508
            String[] sv = new String[keys.size()];
509
            int[] stk = new int[keys.size()];
510
            int[] stv = new int[keys.size()];
511
            ISymbol[] fsymbols = getSymbols();
512
            Object[] values = getValues();
513

    
514
            for (int i = 0; i < keys.size(); i++) {
515
                    if (((Value) keys.get(i)).toString().equals("")) {
516
                            sk[i] =" ";
517
                    }else {
518
                            sk[i] = ((Value) keys.get(i)).toString();
519
                    }
520
                    if (((Value) values[i]).toString().equals("")) {
521
                            sv[i] =" ";
522
                    }else {
523
                            sv[i] = ((Value) values[i]).toString();
524
                    }
525
                    stk[i]= ((Value)keys.get(i)).getSQLType();
526
                    stv[i]= ((Value)values[i]).getSQLType();
527
                xml.addChild(fsymbols[i].getXMLEntity());
528

    
529
                ///System.out.println("get-----------"+sk[i]+"--"+fsymbols[i].getDescription()+"---"+fsymbols[i].getColor());
530
            }
531

    
532
            xml.putProperty("keys", sk);
533
            xml.putProperty("values", sv);
534
            xml.putProperty("typeKeys",stk);
535
            xml.putProperty("typeValues",stv);
536
        }
537

    
538
        return xml;
539
    }
540

    
541
    /**
542
     * Inserta el XMLEntity.
543
     *
544
     * @param xml XMLEntity.
545
     */
546
    public void setXMLEntity03(XMLEntity xml) {
547
        clear();
548
        setFieldName(xml.getStringProperty("fieldName"));
549
        setLabelField(xml.getStringProperty("labelfield"));
550

    
551
        int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
552

    
553
        if (useDefaultSymbol == 1) {
554
            setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
555
        } else {
556
            setDefaultSymbol(null);
557
        }
558

    
559
        int numKeys = xml.getIntProperty("numKeys");
560

    
561
        if (numKeys > 0) {
562
            String className = xml.getStringProperty("tipoValueKeys");
563
            String[] sk = xml.getStringArrayProperty("keys");
564
            String[] sv = xml.getStringArrayProperty("values");
565
            Value auxValue;
566
            Value auxValue2;
567

    
568
            for (int i = 0; i < numKeys; i++) {
569
                try {
570
                    auxValue = ValueFactory.createValue(sk[i], className);
571
                    auxValue2 = ValueFactory.createValue(sv[i], className);
572

    
573
                    ISymbol sym = FSymbol.createFromXML03(xml.getChild(i +
574
                                useDefaultSymbol));
575

    
576
                    ///addSymbol(auxValue, sym);
577
                    symbols.put(auxValue2, sym);
578
                    keys.add(auxValue);
579

    
580
                    ///System.out.println("---set------"+auxValue.toString());
581
                    /// System.out.println("set-----------"+sk[i]+"--"+sym.getDescription()+"---"+sym.getColor());
582
                } catch (SemanticException e) {
583
                    // TODO Auto-generated catch block
584
                    e.printStackTrace();
585
                }
586
            }
587
        }
588
    }
589

    
590
    /**
591
     * Inserta el XMLEntity.
592
     *
593
     * @param xml XMLEntity.
594
     */
595
    public void setXMLEntity(XMLEntity xml) {
596
        clear();
597
        setFieldName(xml.getStringProperty("fieldName"));
598
        setLabelField(xml.getStringProperty("labelfield"));
599

    
600
        if (xml.contains("labelFieldHeight")) {
601
            setLabelHeightField(xml.getStringProperty("labelFieldHeight"));
602
        }
603

    
604
        if (xml.contains("labelFieldRotation")) {
605
            setLabelRotationField(xml.getStringProperty("labelFieldRotation"));
606
        }
607

    
608
        useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbol");
609
        setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
610

    
611
        // FJP: Esto no es necesario ya. Para comprobar si tenemos un campo de altura, miramos
612
        // si getLabelHeightField devuelve null o no.
613
        /* if (xml.contains("isBWithHeightText")) {
614
            setBWithHeightText(xml.getBooleanProperty("isBWithHeightText"));
615
        } */
616

    
617
        //addSymbol(new NullUniqueValue(),getDefaultSymbol());
618
        int numKeys = xml.getIntProperty("numKeys");
619

    
620
        if (numKeys > 0) {
621
            String className = xml.getStringProperty("tipoValueKeys");
622
            String[] sk = xml.getStringArrayProperty("keys");
623
            String[] sv = xml.getStringArrayProperty("values");
624
            Value auxValue = null;
625
            Value auxValue2 = null;
626
            int[] stk=null;
627
            if (xml.contains("typeKeys")) {
628
                                stk = xml.getIntArrayProperty("typeKeys");
629
                                int[] stv = xml.getIntArrayProperty("typeValues");
630
                                for (int i = 0; i < numKeys; i++) {
631
                                        boolean isDefault = false;
632
                                        if (getValue(sk[i], stk[i]) == null) {
633
                                                isDefault = true;
634
                                        }
635

    
636
                                        if (className
637
                                                        .equals("com.hardcode.gdbms.engine.values.NullUniqueValue")
638
                                                        || isDefault) {
639
                                                auxValue = new NullUniqueValue();
640
                                                auxValue2 = ValueFactory.createNullValue();
641
                                        } else {
642
                                                auxValue = getValue(sk[i], stk[i]); // ValueFactory.createValue(sk[i],
643
                                                                                                                        // className);
644
                                                auxValue2 = getValue(sv[i], stv[i]); // ValueFactory.createValue(sv[i],
645
                                                                                                                                // className);
646
                                        }
647

    
648
                                        // (substituir la de baix per esta) ISymbol sym = SymbolFactory.createFromXML(xml.getChild(i + 1), null);
649
                                        ISymbol sym = FSymbol.createFromXML(xml.getChild(i + 1));
650
                                        symbols.put(auxValue2, sym);
651
                                        keys.add(auxValue);
652
                                }
653
                        } else {
654
                                for (int i = 0; i < numKeys; i++) {
655
                                        boolean isDefault = false;
656
                                        if (getValue(sk[i]) == null) {
657
                                                isDefault = true;
658
                                        }
659
                                        if (className
660
                                                        .equals("com.hardcode.gdbms.engine.values.NullUniqueValue")
661
                                                        || isDefault) {
662
                                                auxValue = new NullUniqueValue();
663
                                                auxValue2 = ValueFactory.createNullValue();
664
                                        } else {
665
                                                auxValue = getValue(sk[i]); // ValueFactory.createValue(sk[i],
666
                                                                                                        // className);
667
                                                auxValue2 = getValue(sv[i]); // ValueFactory.createValue(sv[i],
668
                                                                                                                // className);
669
                                        }
670
                                        ISymbol sym = FSymbol.createFromXML(xml.getChild(i + 1));
671
                                        symbols.put(auxValue2, sym);
672
                                        keys.add(auxValue);
673
                                }
674
                        }
675
        }
676
    }
677

    
678
    /**
679
         * Devuelve el valor a partir de su valor en un string.
680
         *
681
         * @param s
682
         *            String con el valor.
683
         * @deprecated M?todo utilizado hasta la 1.0 alpha 855 Debes utilizar a partir de ahora getValue(String s,int type);
684
         * @return Value.
685
         */
686
    private Value getValue(String s) {
687
        Value val = new NullUniqueValue();
688
        if (s.equals("Resto de Valores"))return val;
689
        try {
690
            try {
691
                val = ValueFactory.createValueByType(s, Types.INTEGER);
692

    
693
                return val;
694
            } catch (NumberFormatException e) {
695
            }
696

    
697
            try {
698
                val = ValueFactory.createValueByType(s, Types.BIGINT);
699

    
700
                return val;
701
            } catch (NumberFormatException e) {
702
            }
703

    
704
            try {
705
                val = ValueFactory.createValueByType(s, Types.FLOAT);
706

    
707
                return val;
708
            } catch (NumberFormatException e) {
709
            }
710

    
711
            try {
712
                val = ValueFactory.createValueByType(s, Types.DOUBLE);
713

    
714
                return val;
715
            } catch (NumberFormatException e) {
716
            }
717

    
718
            val = ValueFactory.createValueByType(s, Types.LONGVARCHAR);
719

    
720
        } catch (ParseException e) {
721
            e.printStackTrace();
722
        }
723

    
724
        return val;
725
    }
726
    /**
727
     * Devuelve el valor a partir de su valor en un string.
728
     *
729
     * @param s String con el valor.
730
     *
731
     * @return Value.
732
     */
733
    private Value getValue(String s,int type) {
734
        Value val = new NullUniqueValue();
735
        if (type==Types.OTHER)
736
                return val;
737
        try {
738
                val = ValueFactory.createValueByType(s, type);
739
        } catch (ParseException e) {
740
            e.printStackTrace();
741
        }
742
        return val;
743
    }
744

    
745
    /**
746
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
747
     */
748
    public Legend cloneLegend() throws XMLException {
749
        return LegendFactory.createFromXML(getXMLEntity());
750
    }
751

    
752
    /* (non-Javadoc)
753
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
754
     */
755
    public void setDataSource(DataSource ds)
756
        throws FieldNotFoundException, DriverException {
757
        try {
758
            dataSource = ds;
759
            ds.start();
760
            fieldId = ds.getFieldIndexByName(fieldName);
761
            ds.stop();
762
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
763
            throw new DriverException(e);
764
        }
765
    }
766

    
767
    /* (non-Javadoc)
768
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getSymbolByValue(com.hardcode.gdbms.engine.values.Value)
769
     */
770
    public ISymbol getSymbolByValue(Value key) {
771
        if (symbols.containsKey(key)) {
772
            return (ISymbol) symbols.get(key);
773
        } else if (useDefaultSymbol) {
774
            return getDefaultSymbol();
775
        }
776

    
777
        return null;
778
    }
779

    
780
    /* (non-Javadoc)
781
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getShapeType()
782
     */
783
    public int getShapeType() {
784
        return shapeType;
785
    }
786

    
787
    /* (non-Javadoc)
788
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
789
     */
790
    public String getLabelHeightField() {
791
        return labelFieldHeight;
792
    }
793

    
794
    /**
795
     * Inserta el alto de campo.
796
     *
797
     * @param str alto.
798
     */
799
    public void setLabelHeightField(String str) {
800
        labelFieldHeight = str;
801
    }
802

    
803
    /* (non-Javadoc)
804
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
805
     */
806
    public String getLabelRotationField() {
807
        return labelFieldRotation;
808
    }
809

    
810
    /**
811
     * Inserta rotaci?n.
812
     *
813
     * @param str Rotaci?n.
814
     */
815
    public void setLabelRotationField(String str) {
816
        labelFieldRotation = str;
817
    }
818

    
819
    /**
820
     * Introduce si se tiene que representar el resto de valores o no.
821
     *
822
     * @param b True si se utiliza el resto de valores.
823
     */
824
    public void useDefaultSymbol(boolean b) {
825
        useDefaultSymbol = b;
826
    }
827
    /**
828
     * Devuelve si se utiliza o no el resto de valores para representarse.
829
     *
830
     * @return True si se utiliza el resto de valores.
831
     */
832
    public boolean isUseDefaultSymbol() {
833
        return useDefaultSymbol;
834
    }
835
    /**
836
     * Devuelve true si el etiquetado de la capa se ha modificado.
837
     *
838
     * @return True si se ha modificado el etiquetado de la capa.
839
     */
840
    /* public boolean isBWithHeightText() {
841
        return bWithHeightText;
842
    } */
843

    
844
    /**
845
     * Introduce si se ha modificado el etiquetado de la capa.
846
     *
847
     * @param withHeightText True si se ha modificado el etiquetado de la capa.
848
     */
849
    /* public void setBWithHeightText(boolean withHeightText) {
850
        bWithHeightText = withHeightText;
851
    } */
852

    
853
    /**
854
     * Elimina el s?mbolo que tiene como clave el valor que se pasa como par?metro.
855
     *
856
     * @param key clave.
857
     */
858
    public void delSymbol(Object key) {
859
        keys.remove(key);
860
        symbols.remove(key);
861
    }
862

    
863
    /* (non-Javadoc)
864
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getUsedFields()
865
     */
866
    public String[] getUsedFields() {
867
        ArrayList usedFields = new ArrayList();
868
        if (getFieldName() != null)
869
            usedFields.add(getFieldName());
870
        if (getLabelField() != null)
871
            usedFields.add(getLabelField());
872
        if (getLabelHeightField() != null)
873
            usedFields.add(getLabelHeightField());
874
        if (getLabelRotationField() != null)
875
            usedFields.add(getLabelRotationField());
876

    
877
        return (String[]) usedFields.toArray(new String[0]);
878

    
879
    }
880
}