Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_897 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / VectorialUniqueValueLegend.java @ 10444

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.ISymbol;
75
import com.iver.cit.gvsig.fmap.core.SLDTags;
76
import com.iver.cit.gvsig.fmap.core.SLDUtils;
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
            NullUniqueValue nuv=new NullUniqueValue();
377
            if (symbols.containsKey(nuv))
378
                    return (ISymbol)symbols.get(nuv);
379
        return defaultSymbol;
380
    }
381

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

    
398
                        FeatureTypeStyle featStyle = styleFactory.createFeatureTypeStyle();
399
                        featStyle.setFeatureTypeName(fieldName);
400

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

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

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

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

    
453
                        ISymbol[] symbols = this.getSymbols();
454
                        Object[] values = this.getValues();
455
                        String valueStr = null;
456

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

    
476
                                xmlBuilder.closeTag();
477
                        }
478

    
479
                        xmlBuilder.closeTag();
480
                        xmlBuilder.closeTag();
481
                        xmlBuilder.closeTag();
482
                        xmlBuilder.closeTag();
483
                        return xmlBuilder.getXML();
484

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

    
502
        xml.putProperty("useDefaultSymbol", useDefaultSymbol);
503
        xml.addChild(getDefaultSymbol().getXMLEntity());
504
        // xml.putProperty("isBWithHeightText", isBWithHeightText());
505
        xml.putProperty("numKeys", keys.size());
506

    
507
        if (keys.size() > 0) {
508
            xml.putProperty("tipoValueKeys", valueType);
509

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

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

    
532
                ///System.out.println("get-----------"+sk[i]+"--"+fsymbols[i].getDescription()+"---"+fsymbols[i].getColor());
533
            }
534

    
535
            xml.putProperty("keys", sk);
536
            xml.putProperty("values", sv);
537
            xml.putProperty("typeKeys",stk);
538
            xml.putProperty("typeValues",stv);
539
        }
540

    
541
        return xml;
542
    }
543

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

    
554
        int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
555

    
556
        if (useDefaultSymbol == 1) {
557
            setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
558
        } else {
559
            setDefaultSymbol(null);
560
        }
561

    
562
        int numKeys = xml.getIntProperty("numKeys");
563

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

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

    
576
                    ISymbol sym = FSymbol.createFromXML03(xml.getChild(i +
577
                                useDefaultSymbol));
578

    
579
                    ///addSymbol(auxValue, sym);
580
                    symbols.put(auxValue2, sym);
581
                    keys.add(auxValue);
582

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

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

    
603
        if (xml.contains("labelFieldHeight")) {
604
            setLabelHeightField(xml.getStringProperty("labelFieldHeight"));
605
        }
606

    
607
        if (xml.contains("labelFieldRotation")) {
608
            setLabelRotationField(xml.getStringProperty("labelFieldRotation"));
609
        }
610

    
611
        useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbol");
612
        setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
613

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

    
620
        //addSymbol(new NullUniqueValue(),getDefaultSymbol());
621
        int numKeys = xml.getIntProperty("numKeys");
622

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
778
        return null;
779
    }
780

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

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

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

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

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

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

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

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

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

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

    
880
    }
881
}