Statistics
| Revision:

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

History | View | Annotate | Download (27.4 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 boolean sorter=true;
90
    private TreeMap symbols = new TreeMap(new Comparator() {
91
                public int compare(Object o1, Object o2) {
92
                        if (sorter){
93
                                if ((o1 != null) && (o2 != null)) {
94
                                Value v2 = (Value) o2;
95
                                Value v1 = (Value) o1;
96
                                BooleanValue boolVal;
97

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

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

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

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

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

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

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

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

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

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

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

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

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

    
191
                    break;
192

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

    
196
                    break;
197

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

    
201
                    break;
202

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

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

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

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

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

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

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

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

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

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

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

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

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

    
290
        return descriptions;
291
    }
292

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

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

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

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

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

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

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

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

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

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

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

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

    
376
    /**
377
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
378
     */
379
    public ISymbol getDefaultSymbol() {
380
            NullUniqueValue nuv=new NullUniqueValue();
381
            if (symbols.containsKey(nuv))
382
                    return (ISymbol)symbols.get(nuv);
383
        return defaultSymbol;
384
    }
385

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

    
402
                        FeatureTypeStyle featStyle = styleFactory.createFeatureTypeStyle();
403
                        featStyle.setFeatureTypeName(fieldName);
404

    
405
                        ISymbol[] symbols = this.getSymbols();
406
                        Symbolizer[] theSymbolizers = new Symbolizer[symbols.length];
407
                        Object[] values = this.getValues();
408
                        String valueStr = null;
409
                        String condition = null;
410

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

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

    
448
                        XmlBuilder xmlBuilder = new XmlBuilder();
449
                        xmlBuilder.writeHeader();
450
                        xmlBuilder.openTag(SLDTags.SLD_ROOT, SLDTags.VERSION_ATTR, SLDTags.VERSION_1_0_0);
451
                        xmlBuilder.openTag(SLDTags.NAMEDLAYER);
452
                        xmlBuilder.writeTag(SLDTags.NAME,name);
453
                        xmlBuilder.openTag(SLDTags.USERSTYLE);
454
                        xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE);
455
                        //xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"fieldName");
456

    
457
                        ISymbol[] symbols = this.getSymbols();
458
                        Object[] values = this.getValues();
459
                        String valueStr = null;
460

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

    
480
                                xmlBuilder.closeTag();
481
                        }
482

    
483
                        xmlBuilder.closeTag();
484
                        xmlBuilder.closeTag();
485
                        xmlBuilder.closeTag();
486
                        xmlBuilder.closeTag();
487
                        return xmlBuilder.getXML();
488

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

    
506
        xml.putProperty("useDefaultSymbol", useDefaultSymbol);
507
        xml.putProperty("sorter",sorter);
508
        xml.addChild(getDefaultSymbol().getXMLEntity());
509
        // xml.putProperty("isBWithHeightText", isBWithHeightText());
510
        xml.putProperty("numKeys", keys.size());
511

    
512
        if (keys.size() > 0) {
513
            xml.putProperty("tipoValueKeys", valueType);
514

    
515
            String[] sk = new String[keys.size()];
516
            String[] sv = new String[keys.size()];
517
            int[] stk = new int[keys.size()];
518
            int[] stv = new int[keys.size()];
519
            ISymbol[] fsymbols = getSymbols();
520
            Object[] values = getValues();
521

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

    
537
                ///System.out.println("get-----------"+sk[i]+"--"+fsymbols[i].getDescription()+"---"+fsymbols[i].getColor());
538
            }
539

    
540
            xml.putProperty("keys", sk);
541
            xml.putProperty("values", sv);
542
            xml.putProperty("typeKeys",stk);
543
            xml.putProperty("typeValues",stv);
544
        }
545

    
546
        return xml;
547
    }
548

    
549
    /**
550
     * Inserta el XMLEntity.
551
     *
552
     * @param xml XMLEntity.
553
     */
554
    public void setXMLEntity03(XMLEntity xml) {
555
        clear();
556
        setFieldName(xml.getStringProperty("fieldName"));
557
        setLabelField(xml.getStringProperty("labelfield"));
558

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

    
561
        if (useDefaultSymbol == 1) {
562
            setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
563
        } else {
564
            setDefaultSymbol(null);
565
        }
566

    
567
        int numKeys = xml.getIntProperty("numKeys");
568

    
569
        if (numKeys > 0) {
570
            String className = xml.getStringProperty("tipoValueKeys");
571
            String[] sk = xml.getStringArrayProperty("keys");
572
            String[] sv = xml.getStringArrayProperty("values");
573
            Value auxValue;
574
            Value auxValue2;
575

    
576
            for (int i = 0; i < numKeys; i++) {
577
                try {
578
                    auxValue = ValueFactory.createValue(sk[i], className);
579
                    auxValue2 = ValueFactory.createValue(sv[i], className);
580

    
581
                    ISymbol sym = FSymbol.createFromXML03(xml.getChild(i +
582
                                useDefaultSymbol));
583

    
584
                    ///addSymbol(auxValue, sym);
585
                    symbols.put(auxValue2, sym);
586
                    keys.add(auxValue);
587

    
588
                    ///System.out.println("---set------"+auxValue.toString());
589
                    /// System.out.println("set-----------"+sk[i]+"--"+sym.getDescription()+"---"+sym.getColor());
590
                } catch (SemanticException e) {
591
                    // TODO Auto-generated catch block
592
                    e.printStackTrace();
593
                }
594
            }
595
        }
596
    }
597

    
598
    /**
599
     * Inserta el XMLEntity.
600
     *
601
     * @param xml XMLEntity.
602
     */
603
    public void setXMLEntity(XMLEntity xml) {
604
        clear();
605
        setFieldName(xml.getStringProperty("fieldName"));
606
        setLabelField(xml.getStringProperty("labelfield"));
607

    
608
        if (xml.contains("labelFieldHeight")) {
609
            setLabelHeightField(xml.getStringProperty("labelFieldHeight"));
610
        }
611

    
612
        if (xml.contains("labelFieldRotation")) {
613
            setLabelRotationField(xml.getStringProperty("labelFieldRotation"));
614
        }
615

    
616
        useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbol");
617
        if (xml.contains("sorter"))
618
                sorter = xml.getBooleanProperty("sorter");
619
        setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
620

    
621
        // FJP: Esto no es necesario ya. Para comprobar si tenemos un campo de altura, miramos
622
        // si getLabelHeightField devuelve null o no.
623
        /* if (xml.contains("isBWithHeightText")) {
624
            setBWithHeightText(xml.getBooleanProperty("isBWithHeightText"));
625
        } */
626

    
627
        //addSymbol(new NullUniqueValue(),getDefaultSymbol());
628
        int numKeys = xml.getIntProperty("numKeys");
629

    
630
        if (numKeys > 0) {
631
            String className = xml.getStringProperty("tipoValueKeys");
632
            String[] sk = xml.getStringArrayProperty("keys");
633
            String[] sv = xml.getStringArrayProperty("values");
634
            Value auxValue = null;
635
            Value auxValue2 = null;
636
            int[] stk=null;
637
            if (xml.contains("typeKeys")) {
638
                                stk = xml.getIntArrayProperty("typeKeys");
639
                                int[] stv = xml.getIntArrayProperty("typeValues");
640
                                for (int i = 0; i < numKeys; i++) {
641
                                        boolean isDefault = false;
642
                                        if (getValue(sk[i], stk[i]) == null) {
643
                                                isDefault = true;
644
                                        }
645

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

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

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

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

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

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

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

    
719
            try {
720
                val = ValueFactory.createValueByType(s, Types.DOUBLE);
721

    
722
                return val;
723
            } catch (NumberFormatException e) {
724
            }
725

    
726
            val = ValueFactory.createValueByType(s, Types.LONGVARCHAR);
727

    
728
        } catch (ParseException e) {
729
            e.printStackTrace();
730
        }
731

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

    
753
    /**
754
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
755
     */
756
    public Legend cloneLegend() throws XMLException {
757
        return LegendFactory.createFromXML(getXMLEntity());
758
    }
759

    
760
    /* (non-Javadoc)
761
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
762
     */
763
    public void setDataSource(DataSource ds)
764
        throws FieldNotFoundException, DriverException {
765
        try {
766
            dataSource = ds;
767
            ds.start();
768
            fieldId = ds.getFieldIndexByName(fieldName);
769
            ds.stop();
770
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
771
            throw new DriverException(e);
772
        }
773
    }
774

    
775
    /* (non-Javadoc)
776
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getSymbolByValue(com.hardcode.gdbms.engine.values.Value)
777
     */
778
    public ISymbol getSymbolByValue(Value key) {
779
        if (symbols.containsKey(key)) {
780
            return (ISymbol) symbols.get(key);
781
        } else if (useDefaultSymbol) {
782
            return getDefaultSymbol();
783
        }
784

    
785
        return null;
786
    }
787

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

    
795
    /* (non-Javadoc)
796
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
797
     */
798
    public String getLabelHeightField() {
799
        return labelFieldHeight;
800
    }
801

    
802
    /**
803
     * Inserta el alto de campo.
804
     *
805
     * @param str alto.
806
     */
807
    public void setLabelHeightField(String str) {
808
        labelFieldHeight = str;
809
    }
810

    
811
    /* (non-Javadoc)
812
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
813
     */
814
    public String getLabelRotationField() {
815
        return labelFieldRotation;
816
    }
817

    
818
    /**
819
     * Inserta rotaci?n.
820
     *
821
     * @param str Rotaci?n.
822
     */
823
    public void setLabelRotationField(String str) {
824
        labelFieldRotation = str;
825
    }
826

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

    
852
    /**
853
     * Introduce si se ha modificado el etiquetado de la capa.
854
     *
855
     * @param withHeightText True si se ha modificado el etiquetado de la capa.
856
     */
857
    /* public void setBWithHeightText(boolean withHeightText) {
858
        bWithHeightText = withHeightText;
859
    } */
860

    
861
    /**
862
     * Elimina el s?mbolo que tiene como clave el valor que se pasa como par?metro.
863
     *
864
     * @param key clave.
865
     */
866
    public void delSymbol(Object key) {
867
        keys.remove(key);
868
        symbols.remove(key);
869
    }
870

    
871
    /* (non-Javadoc)
872
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getUsedFields()
873
     */
874
    public String[] getUsedFields() {
875
        ArrayList usedFields = new ArrayList();
876
        if (getFieldName() != null)
877
            usedFields.add(getFieldName());
878
        if (getLabelField() != null)
879
            usedFields.add(getLabelField());
880
        if (getLabelHeightField() != null)
881
            usedFields.add(getLabelHeightField());
882
        if (getLabelRotationField() != null)
883
            usedFields.add(getLabelRotationField());
884

    
885
        return (String[]) usedFields.toArray(new String[0]);
886

    
887
    }
888

    
889
        public void setSorter(boolean b) {
890
                sorter=b;
891
        }
892

    
893
        public boolean isSorter() {
894
                return sorter;
895
        }
896
}