Statistics
| Revision:

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

History | View | Annotate | Download (25.2 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 com.hardcode.gdbms.engine.data.DataSource;
44
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
45
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
46
import com.hardcode.gdbms.engine.instruction.SemanticException;
47
import com.hardcode.gdbms.engine.values.BooleanValue;
48
import com.hardcode.gdbms.engine.values.NullValue;
49
import com.hardcode.gdbms.engine.values.StringValue;
50
import com.hardcode.gdbms.engine.values.Value;
51
import com.hardcode.gdbms.engine.values.ValueFactory;
52

    
53
import com.iver.cit.gvsig.fmap.DriverException;
54
import com.iver.cit.gvsig.fmap.core.FShape;
55
import com.iver.cit.gvsig.fmap.core.IFeature;
56
import com.iver.cit.gvsig.fmap.core.SLDTags;
57
import com.iver.cit.gvsig.fmap.core.SLDUtils;
58
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
59
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
60
import com.iver.cit.gvsig.fmap.layers.XMLException;
61

    
62
import com.iver.utiles.XMLEntity;
63

    
64
import java.sql.Types;
65

    
66
import java.text.ParseException;
67

    
68
import java.util.ArrayList;
69
import java.util.Comparator;
70
import java.util.TreeMap;
71

    
72
import org.geotools.filter.ExpressionBuilder;
73
import org.geotools.filter.Filter;
74
import org.geotools.styling.FeatureTypeStyle;
75
import org.geotools.styling.NamedLayer;
76
import org.geotools.styling.Rule;
77
import org.geotools.styling.SLDTransformer;
78
import org.geotools.styling.Style;
79
import org.geotools.styling.StyleBuilder;
80
import org.geotools.styling.StyleFactory;
81
import org.geotools.styling.StyledLayerDescriptor;
82
import org.geotools.styling.Symbolizer;
83

    
84

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

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

    
104
                        if (v1 instanceof NullValue) {
105
                            return -1;
106
                        }
107

    
108
                        if (v2 instanceof NullValue) {
109
                            return 1;
110
                        }
111

    
112
                        try {
113
                            boolVal = (BooleanValue) (v1.greater(v2));
114

    
115
                            if (boolVal.getValue()) {
116
                                return 1;
117
                            }
118

    
119
                            boolVal = (BooleanValue) (v1.less(v2));
120

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

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

    
138
                        if (v1 instanceof StringValue) {
139
                            return -1;
140
                        }
141

    
142
                        if (v2 instanceof StringValue) {
143
                            return 1;
144
                        }
145
                    }
146

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

    
163
    /**
164
     * Crea un nuevo VectorialUniqueValueLegend.
165
     */
166
    public VectorialUniqueValueLegend() {
167
        //        defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
168
    }
169

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

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

    
190
                    break;
191

    
192
                case FShape.LINE:
193
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
194

    
195
                    break;
196

    
197
                case FShape.POLYGON:
198
                    defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
199

    
200
                    break;
201

    
202
                default:
203
                    defaultSymbol = new FSymbol(shapeType);
204
            }
205

    
206
            this.shapeType = shapeType;
207
        }
208
    }
209

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

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

    
226
    /*
227
       public FSymbol getSymbolByID(int ID) {
228
           return (FSymbol) symbols.get(keys.get(ID));
229
       }
230
     */
231

    
232
    /**
233
     * Se usa en la tabla que muestra una leyenda.
234
     *
235
     * @param id
236
     * @param symbol
237
     */
238
    public void setValueSymbol(int id, FSymbol symbol) {
239
        symbols.put(keys.get(id), symbol);
240
    }
241

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

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

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

    
265
            if (!key.getClass().equals(NullValue.class)) {
266
                valueType = key.getClass().getName();
267
            }
268
        }
269
    }
270

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

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

    
286
        for (int i = 0; i < descriptions.length; i++)
287
            descriptions[i] = auxSym[i].getDescription();
288

    
289
        return descriptions;
290
    }
291

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

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

    
306
    /**
307
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDefaultSymbol(com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D)
308
     */
309
    public void setDefaultSymbol(FSymbol s) {
310
        defaultSymbol = s;
311
    }
312

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

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

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

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

    
342
            //if (theSymbol != null) {
343
            return theSymbol;
344

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

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

    
367
        //if (theSymbol != null) {
368
        return theSymbol;
369

    
370
        //} else {
371
        //    return getDefaultSymbol();
372
        //}
373
    }
374

    
375
    /**
376
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
377
     */
378
    public FSymbol getDefaultSymbol() {
379
        return defaultSymbol;
380
    }
381

    
382
        /**
383
         * Writes and SLD using GEOTOOLS objetcs.
384
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getSLDString()
385
         */
386
        public String getSLDString_()
387
        {                                             
388
            try{
389
                        StyledLayerDescriptor sld = new StyledLayerDescriptor();
390
                        StyleFactory styleFactory = StyleFactory.createStyleFactory();                
391
                        StyleBuilder sb = new StyleBuilder();
392
                Style style = sb.createStyle();
393
                style.setName("default");
394
                Filter filter = null;
395
                Rule rule = null;                
396
                
397
                        FeatureTypeStyle featStyle = styleFactory.createFeatureTypeStyle();
398
                        featStyle.setFeatureTypeName(fieldName);
399
                
400
                        FSymbol[] symbols = this.getSymbols(); 
401
                        Symbolizer[] theSymbolizers = new Symbolizer[symbols.length];
402
                        Object[] values = this.getValues(); 
403
                        String valueStr = null;
404
                        String condition = null;
405
                        
406
                        for(int i = 0; i < symbols.length; i++ )
407
                        {
408
                                valueStr = values[i].toString();
409
                                //if(this.valueType == "")
410
                                        condition = fieldName +"='"+valueStr+"'";
411
                                //else
412
                                //        condition = fieldName +"="+values[i];
413
                                filter = (Filter)ExpressionBuilder.parse(condition);
414
                                theSymbolizers[0] = SLDUtils.toGeotoolsSymbol(symbols[i]);
415
                                rule = styleFactory.createRule();
416
                                rule.setName(valueStr);
417
                                rule.setTitle(valueStr);
418
                                rule.setFilter(filter);
419
                                rule.setSymbolizers((Symbolizer[])theSymbolizers.clone());
420
                                featStyle.addRule(rule);
421
                        }
422
                        style.addFeatureTypeStyle(featStyle);
423
                        SLDTransformer st = new SLDTransformer();
424
                        NamedLayer namedLayer = new NamedLayer();
425
                        namedLayer.setName("comunidades");
426
                        namedLayer.addStyle(style);
427
                        sld.addStyledLayer(namedLayer);
428
                        return st.transform(style);                        
429
                               
430
            }catch(Exception e)
431
            {
432
                    e.printStackTrace(); 
433
                    return null;
434
            }
435
        }
436
        public String getSLDString(String name)
437
        {                                             
438
            try{
439
                    
440
                        XmlBuilder xmlBuilder = new XmlBuilder();
441
                        xmlBuilder.writeHeader();
442
                        xmlBuilder.openTag(SLDTags.SLD_ROOT, SLDTags.VERSION_ATTR, SLDTags.VERSION_1_0_0);
443
                        xmlBuilder.openTag(SLDTags.NAMEDLAYER);
444
                        xmlBuilder.writeTag(SLDTags.NAME,name);
445
                        xmlBuilder.openTag(SLDTags.USERSTYLE);
446
                        xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE);
447
                        xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"fieldName");
448
                        
449
                        FSymbol[] symbols = this.getSymbols(); 
450
                        Object[] values = this.getValues(); 
451
                        String valueStr = null;
452
                        
453
                        for(int i = 0; i < symbols.length; i++ )
454
                        {
455
                                valueStr = values[i].toString();
456
                                xmlBuilder.openTag(SLDTags.RULE);
457
                                xmlBuilder.openTag(SLDTags.FILTER);
458
                                xmlBuilder.openTag(SLDTags.PROPERTYISEQUALTO);
459
                                xmlBuilder.writeTag(SLDTags.PROPERTYNAME,fieldName);
460
                                xmlBuilder.writeTag(SLDTags.LITERAL, valueStr);
461
                                xmlBuilder.closeTag();
462
                                xmlBuilder.closeTag();                                
463
                                                
464
                                xmlBuilder.writeRaw(symbols[i].toSLD());
465
                                
466
                                xmlBuilder.closeTag();
467
                        }
468
                        
469
                        xmlBuilder.closeTag();
470
                        xmlBuilder.closeTag();
471
                        xmlBuilder.closeTag();
472
                        xmlBuilder.closeTag();                        
473
                        return xmlBuilder.getXML();                                                     
474
                               
475
            }catch(Exception e)
476
            {
477
                    e.printStackTrace(); 
478
                    return null;
479
            }
480
        }
481
    /**
482
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
483
     */
484
    public XMLEntity getXMLEntity() {
485
        XMLEntity xml = new XMLEntity();
486
        xml.putProperty("className", this.getClass().getName());
487
        xml.putProperty("fieldName", fieldName);
488
        xml.putProperty("labelfield", labelFieldName);
489
        xml.putProperty("labelFieldHeight", labelFieldHeight);
490
        xml.putProperty("labelFieldRotation", labelFieldRotation);
491

    
492
        xml.putProperty("useDefaultSymbol", useDefaultSymbol);
493
        xml.addChild(getDefaultSymbol().getXMLEntity());
494
        // xml.putProperty("isBWithHeightText", isBWithHeightText());
495
        xml.putProperty("numKeys", keys.size());
496

    
497
        if (keys.size() > 0) {
498
            xml.putProperty("tipoValueKeys", valueType);
499

    
500
            String[] sk = new String[keys.size()];
501
            String[] sv = new String[keys.size()];
502

    
503
            FSymbol[] fsymbols = getSymbols();
504
            Object[] values = (Object[]) getValues();
505

    
506
            for (int i = 0; i < keys.size(); i++) {
507
                sk[i] = ((Value) keys.get(i)).toString();
508
                sv[i] = ((Value) values[i]).toString();
509
                xml.addChild(fsymbols[i].getXMLEntity());
510

    
511
                ///System.out.println("get-----------"+sk[i]+"--"+fsymbols[i].getDescription()+"---"+fsymbols[i].getColor());
512
            }
513

    
514
            xml.putProperty("keys", sk);
515
            xml.putProperty("values", sv);
516
        }
517

    
518
        return xml;
519
    }
520

    
521
    /**
522
     * Inserta el XMLEntity.
523
     *
524
     * @param xml XMLEntity.
525
     */
526
    public void setXMLEntity03(XMLEntity xml) {
527
        clear();
528
        setFieldName(xml.getStringProperty("fieldName"));
529
        setLabelField(xml.getStringProperty("labelfield"));
530

    
531
        int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
532

    
533
        if (useDefaultSymbol == 1) {
534
            setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
535
        } else {
536
            setDefaultSymbol(null);
537
        }
538

    
539
        int numKeys = xml.getIntProperty("numKeys");
540

    
541
        if (numKeys > 0) {
542
            String className = xml.getStringProperty("tipoValueKeys");
543
            String[] sk = xml.getStringArrayProperty("keys");
544
            String[] sv = xml.getStringArrayProperty("values");
545
            Value auxValue;
546
            Value auxValue2;
547

    
548
            for (int i = 0; i < numKeys; i++) {
549
                try {
550
                    auxValue = ValueFactory.createValue(sk[i], className);
551
                    auxValue2 = ValueFactory.createValue(sv[i], className);
552

    
553
                    FSymbol sym = FSymbol.createFromXML03(xml.getChild(i +
554
                                useDefaultSymbol));
555

    
556
                    ///addSymbol(auxValue, sym);
557
                    symbols.put(auxValue2, sym);
558
                    keys.add(auxValue);
559

    
560
                    ///System.out.println("---set------"+auxValue.toString());
561
                    /// System.out.println("set-----------"+sk[i]+"--"+sym.getDescription()+"---"+sym.getColor());
562
                } catch (SemanticException e) {
563
                    // TODO Auto-generated catch block
564
                    e.printStackTrace();
565
                }
566
            }
567
        }
568
    }
569

    
570
    /**
571
     * Inserta el XMLEntity.
572
     *
573
     * @param xml XMLEntity.
574
     */
575
    public void setXMLEntity(XMLEntity xml) {
576
        clear();
577
        setFieldName(xml.getStringProperty("fieldName"));
578
        setLabelField(xml.getStringProperty("labelfield"));
579

    
580
        if (xml.contains("labelFieldHeight")) {
581
            setLabelHeightField(xml.getStringProperty("labelFieldHeight"));
582
        }
583

    
584
        if (xml.contains("labelFieldRotation")) {
585
            setLabelRotationField(xml.getStringProperty("labelFieldRotation"));
586
        }
587

    
588
        useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbol");
589
        setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
590

    
591
        // FJP: Esto no es necesario ya. Para comprobar si tenemos un campo de altura, miramos
592
        // si getLabelHeightField devuelve null o no.
593
        /* if (xml.contains("isBWithHeightText")) {
594
            setBWithHeightText(xml.getBooleanProperty("isBWithHeightText"));
595
        } */
596

    
597
        //addSymbol(new NullUniqueValue(),getDefaultSymbol());
598
        int numKeys = xml.getIntProperty("numKeys");
599

    
600
        if (numKeys > 0) {
601
            String className = xml.getStringProperty("tipoValueKeys");
602
            String[] sk = xml.getStringArrayProperty("keys");
603
            String[] sv = xml.getStringArrayProperty("values");
604
            Value auxValue = null;
605
            Value auxValue2 = null;
606

    
607
            for (int i = 0; i < numKeys; i++) {
608
                //try {
609
                boolean isDefault = false;
610

    
611
                if (getValue(sk[i]) == null) {
612
                    isDefault = true;
613
                }
614

    
615
                if (className.equals(
616
                            "com.hardcode.gdbms.engine.values.NullUniqueValue") ||
617
                        isDefault) {
618
                    auxValue = new NullUniqueValue();
619
                    auxValue2 = ValueFactory.createNullValue();
620
                } else {
621
                    auxValue = getValue(sk[i]); //ValueFactory.createValue(sk[i], className);
622
                    auxValue2 = getValue(sv[i]); // ValueFactory.createValue(sv[i], className);
623
                }
624

    
625
                FSymbol sym = FSymbol.createFromXML(xml.getChild(i + 1));
626

    
627
                ///addSymbol(auxValue, sym);
628
                symbols.put(auxValue2, sym);
629
                keys.add(auxValue);
630

    
631
                ///System.out.println("---set------"+auxValue.toString());
632
                /// System.out.println("set-----------"+sk[i]+"--"+sym.getDescription()+"---"+sym.getColor());
633
                //} catch (SemanticException e) {
634
                //        e.printStackTrace();
635
                //}
636
            }
637
        }
638
    }
639

    
640
    /**
641
     * Devuelve el valor a partir de su valor en un string.
642
     *
643
     * @param s String con el valor.
644
     *
645
     * @return Value.
646
     */
647
    private Value getValue(String s) {
648
        Value val = new NullUniqueValue();
649
        if (s.equals("Resto de Valores"))return val;
650
        try {
651
            try {
652
                val = ValueFactory.createValueByType(s, Types.INTEGER);
653

    
654
                return val;
655
            } catch (NumberFormatException e) {
656
            }
657

    
658
            try {
659
                val = ValueFactory.createValueByType(s, Types.BIGINT);
660

    
661
                return val;
662
            } catch (NumberFormatException e) {
663
            }
664

    
665
            try {
666
                val = ValueFactory.createValueByType(s, Types.FLOAT);
667

    
668
                return val;
669
            } catch (NumberFormatException e) {
670
            }
671

    
672
            try {
673
                val = ValueFactory.createValueByType(s, Types.DOUBLE);
674

    
675
                return val;
676
            } catch (NumberFormatException e) {
677
            }
678
            
679
            val = ValueFactory.createValueByType(s, Types.LONGVARCHAR);
680
           
681
        } catch (ParseException e) {
682
            e.printStackTrace();
683
        }
684

    
685
        return val;
686
    }
687

    
688
    /**
689
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
690
     */
691
    public Legend cloneLegend() throws XMLException {
692
        return (Legend) LegendFactory.createFromXML(getXMLEntity());
693
    }
694

    
695
    /* (non-Javadoc)
696
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
697
     */
698
    public void setDataSource(DataSource ds)
699
        throws FieldNotFoundException, DriverException {
700
        try {
701
            dataSource = ds;
702
            ds.start();
703
            fieldId = ds.getFieldIndexByName(fieldName);
704
            ds.stop();
705
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
706
            throw new DriverException(e);
707
        }
708
    }
709

    
710
    /* (non-Javadoc)
711
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getSymbolByValue(com.hardcode.gdbms.engine.values.Value)
712
     */
713
    public FSymbol getSymbolByValue(Value key) {
714
        if (symbols.containsKey(key)) {
715
            return (FSymbol) symbols.get(key);
716
        } else if (useDefaultSymbol) {
717
            return getDefaultSymbol();
718
        }
719

    
720
        return null;
721
    }
722

    
723
    /* (non-Javadoc)
724
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getShapeType()
725
     */
726
    public int getShapeType() {
727
        return shapeType;
728
    }
729

    
730
    /* (non-Javadoc)
731
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
732
     */
733
    public String getLabelHeightField() {
734
        return labelFieldHeight;
735
    }
736

    
737
    /**
738
     * Inserta el alto de campo.
739
     *
740
     * @param str alto.
741
     */
742
    public void setLabelHeightField(String str) {
743
        labelFieldHeight = str;
744
    }
745

    
746
    /* (non-Javadoc)
747
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
748
     */
749
    public String getLabelRotationField() {
750
        return labelFieldRotation;
751
    }
752

    
753
    /**
754
     * Inserta rotaci?n.
755
     *
756
     * @param str Rotaci?n.
757
     */
758
    public void setLabelRotationField(String str) {
759
        labelFieldRotation = str;
760
    }
761

    
762
    /**
763
     * Introduce si se tiene que representar el resto de valores o no.
764
     *
765
     * @param b True si se utiliza el resto de valores.
766
     */
767
    public void useDefaultSymbol(boolean b) {
768
        useDefaultSymbol = b;
769
    }
770
    /**
771
     * Devuelve si se utiliza o no el resto de valores para representarse.
772
     *
773
     * @return True si se utiliza el resto de valores.
774
     */
775
    public boolean isUseDefaultSymbol() {
776
        return useDefaultSymbol;
777
    }
778
    /**
779
     * Devuelve true si el etiquetado de la capa se ha modificado.
780
     *
781
     * @return True si se ha modificado el etiquetado de la capa.
782
     */
783
    /* public boolean isBWithHeightText() {
784
        return bWithHeightText;
785
    } */
786

    
787
    /**
788
     * Introduce si se ha modificado el etiquetado de la capa.
789
     *
790
     * @param withHeightText True si se ha modificado el etiquetado de la capa.
791
     */
792
    /* public void setBWithHeightText(boolean withHeightText) {
793
        bWithHeightText = withHeightText;
794
    } */
795

    
796
    /**
797
     * Elimina el s?mbolo que tiene como clave el valor que se pasa como par?metro.
798
     *
799
     * @param key clave.
800
     */
801
    public void delSymbol(Object key) {
802
        keys.remove(key);
803
        symbols.remove(key);
804
    }
805

    
806
    /* (non-Javadoc)
807
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getUsedFields()
808
     */
809
    public String[] getUsedFields() {
810
        ArrayList usedFields = new ArrayList();
811
        if (getFieldName() != null)
812
            usedFields.add(getFieldName());        
813
        if (getLabelField() != null)
814
            usedFields.add(getLabelField());
815
        if (getLabelHeightField() != null)
816
            usedFields.add(getLabelHeightField());
817
        if (getLabelRotationField() != null)
818
            usedFields.add(getLabelRotationField());
819
                
820
        return (String[]) usedFields.toArray(new String[0]);
821

    
822
    }
823
}