Statistics
| Revision:

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

History | View | Annotate | Download (23.1 KB)

1 1100 fjp
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41 322 fernando
package com.iver.cit.gvsig.fmap.rendering;
42
43 7659 fjp
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 10627 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
62 470 fjp
import com.hardcode.gdbms.engine.data.DataSource;
63
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
64
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
65 479 fjp
import com.hardcode.gdbms.engine.instruction.SemanticException;
66 470 fjp
import com.hardcode.gdbms.engine.values.BooleanValue;
67 2361 vcaballero
import com.hardcode.gdbms.engine.values.NullValue;
68 2415 caballero
import com.hardcode.gdbms.engine.values.StringValue;
69 470 fjp
import com.hardcode.gdbms.engine.values.Value;
70 479 fjp
import com.hardcode.gdbms.engine.values.ValueFactory;
71 2183 fernando
import com.iver.cit.gvsig.fmap.core.IFeature;
72 7659 fjp
import com.iver.cit.gvsig.fmap.core.ISLDCompatible;
73 4111 ldiaz
import com.iver.cit.gvsig.fmap.core.SLDTags;
74
import com.iver.cit.gvsig.fmap.core.SLDUtils;
75 10815 jaume
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
76 9641 jaume
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
77 458 fjp
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
78 490 fernando
import com.iver.cit.gvsig.fmap.layers.XMLException;
79 435 vcaballero
import com.iver.utiles.XMLEntity;
80
81 322 fernando
/**
82 1034 vcaballero
 * Leyenda vectorial por valores ?nicos.
83 10679 jaume
 * @author   Vicente Caballero Navarro
84 322 fernando
 */
85 435 vcaballero
public class VectorialUniqueValueLegend implements UniqueValueLegend,
86 10679 jaume
VectorialLegend {
87 10506 caballero
        private boolean sorter=true;
88 10679 jaume
89
        private TreeMap symbols = new TreeMap(new Comparator() {
90 10506 caballero
                public int compare(Object o1, Object o2) {
91
                        if ((o1 != null) && (o2 != null)) {
92
                                Value v2 = (Value) o2;
93
                                Value v1 = (Value) o1;
94 10679 jaume
                                BooleanValue boolVal;
95
96 10739 jaume
                                // TODO estas dos comprobaciones son por evitar un bug en el gdbms, cuando se solucione se puede eliminar.
97 10506 caballero
                                if (v1 instanceof NullValue && v2 instanceof NullValue) {
98
                                        return 0;
99
                                }
100 1034 vcaballero
101 10506 caballero
                                if (v1 instanceof NullValue) {
102
                                        return -1;
103
                                }
104 1034 vcaballero
105 10506 caballero
                                if (v2 instanceof NullValue) {
106
                                        return 1;
107
                                }
108 1034 vcaballero
109 10679 jaume
                                try {
110
                                        boolVal = (BooleanValue) (v1.greater(v2));
111 1034 vcaballero
112 10679 jaume
                                        if (boolVal.getValue()) {
113
                                                return 1;
114 10506 caballero
                                        }
115 1034 vcaballero
116 10679 jaume
                                        boolVal = (BooleanValue) (v1.less(v2));
117 1034 vcaballero
118 10679 jaume
                                        if (boolVal.getValue()) {
119 10506 caballero
                                                return -1;
120
                                        }
121 10679 jaume
                                } catch (IncompatibleTypesException e) {
122
                                        // TODO Auto-generated catch block
123
                                        //e.printStackTrace();
124
                                }
125 1034 vcaballero
126 10679 jaume
                                try {
127
                                        if (((BooleanValue) v1.equals(v2)).getValue()) {
128
                                                return 0;
129 10506 caballero
                                        }
130 10679 jaume
                                } catch (IncompatibleTypesException e) {
131
                                        // TODO Auto-generated catch block
132
                                        //e.printStackTrace();
133
                                }
134 1034 vcaballero
135 10679 jaume
                                if (v1 instanceof StringValue) {
136
                                        return -1;
137 10506 caballero
                                }
138 10679 jaume
139
                                if (v2 instanceof StringValue) {
140
                                        return 1;
141
                                }
142 10506 caballero
                        }
143 10679 jaume
144 10506 caballero
                        return 0;
145
                }
146
        }); // Para poder ordenar
147 2796 caballero
    private ArrayList keys = new ArrayList(); // En lugar de un HashSet, para tener acceso por ?ndice
148
    private String fieldName;
149 8738 jmvivo
    protected int fieldId = -1;
150 2796 caballero
    private String labelFieldName;
151
    private String labelFieldHeight;
152
    private String labelFieldRotation;
153 7659 fjp
    private ISymbol defaultSymbol;
154 2796 caballero
    private DataSource dataSource;
155
    private int shapeType;
156
    private String valueType = NullValue.class.getName();
157
    private boolean useDefaultSymbol = false;
158 322 fernando
159 2796 caballero
    /**
160
     * Crea un nuevo VectorialUniqueValueLegend.
161
     */
162
    public VectorialUniqueValueLegend() {
163
        //        defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
164
    }
165 1034 vcaballero
166 2796 caballero
    /**
167
     * Crea un nuevo VectorialUniqueValueLegend.
168
     *
169
     * @param shapeType Tipo de shape.
170
     */
171
    public VectorialUniqueValueLegend(int shapeType) {
172
        setShapeType(shapeType);
173
    }
174 1034 vcaballero
175 2796 caballero
    /**
176 10679 jaume
         * @param shapeType
177
         * @uml.property  name="shapeType"
178
         */
179 2796 caballero
    public void setShapeType(int shapeType) {
180 10679 jaume
            if (this.shapeType != shapeType) {
181 10815 jaume
                    /*switch (shapeType) {
182 10679 jaume
                    case FShape.POINT:
183
                            defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
184 1034 vcaballero

185 10679 jaume
                            break;
186 1034 vcaballero

187 10679 jaume
                    case FShape.LINE:
188
                            defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
189 470 fjp

190 10679 jaume
                            break;
191 322 fernando

192 10679 jaume
                    case FShape.POLYGON:
193
                            defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
194 470 fjp

195 10679 jaume
                            break;
196 435 vcaballero

197 10679 jaume
                    default:
198
                            defaultSymbol = new FSymbol(shapeType);
199 10815 jaume
                    }*/
200
                    defaultSymbol = SymbologyFactory.createDefaultSymbolByShapeType(shapeType);
201 10679 jaume
                    this.shapeType = shapeType;
202
            }
203 2796 caballero
    }
204 322 fernando
205 7659 fjp
    public void setValueSymbolByID(int id, ISymbol symbol) {
206 2796 caballero
        symbols.put(keys.get(id), symbol);
207
    }
208 322 fernando
209 2796 caballero
    /**
210
     * Se usa en la tabla que muestra una leyenda.
211 7828 caballero
     *        @deprecated use setValueSymbolByID(int id, ISymbol symbol);
212 2796 caballero
     * @param id
213
     * @param symbol
214
     */
215 7828 caballero
    public void setValueSymbol(int id, ISymbol symbol) {
216 2796 caballero
        symbols.put(keys.get(id), symbol);
217
    }
218 322 fernando
219 2796 caballero
    public Object[] getValues() {
220
        return symbols.keySet().toArray(new Object[0]);
221
    }
222 322 fernando
223 7659 fjp
    public void addSymbol(Object key, ISymbol symbol) {
224 2796 caballero
        Object resul;
225
        resul = symbols.put(key, symbol);
226 322 fernando
227 2796 caballero
        if (resul != null) {
228
            System.err.println("Error: la clave " + key +
229
                " ya exist?a. Resul = " + resul);
230
            System.err.println("symbol nuevo:" + symbol.getDescription() +
231 7828 caballero
                " Sviejo= " + ((ISymbol) resul).getDescription());
232 2796 caballero
        } else {
233
            keys.add(key);
234 435 vcaballero
235 2796 caballero
            if (!key.getClass().equals(NullValue.class)) {
236
                valueType = key.getClass().getName();
237
            }
238
        }
239
    }
240
241
    public void clear() {
242
        keys.clear();
243
        symbols.clear();
244
    }
245
246
    public String[] getDescriptions() {
247
        String[] descriptions = new String[symbols.size()];
248 7659 fjp
        ISymbol[] auxSym = getSymbols();
249 2796 caballero
250
        for (int i = 0; i < descriptions.length; i++)
251
            descriptions[i] = auxSym[i].getDescription();
252
253
        return descriptions;
254
    }
255
256
    /**
257 10679 jaume
         * @return
258
         * @uml.property  name="symbols"
259
         */
260 7659 fjp
    public ISymbol[] getSymbols() {
261
        return (ISymbol[]) symbols.values().toArray(new ISymbol[0]);
262 2796 caballero
    }
263
264
    /**
265 10679 jaume
         * @return
266
         * @uml.property  name="fieldName"
267
         */
268 2796 caballero
    public String getFieldName() {
269
        return fieldName;
270
    }
271
272
    /**
273 10679 jaume
         * @param s
274
         * @uml.property  name="defaultSymbol"
275
         */
276 7659 fjp
    public void setDefaultSymbol(ISymbol s) {
277 2796 caballero
        defaultSymbol = s;
278
    }
279
280
    public String getLabelField() {
281
        return labelFieldName;
282
    }
283
284
    public void setLabelField(String fieldName) {
285
        labelFieldName = fieldName;
286
    }
287
288
    /**
289 10679 jaume
         * @param str
290
         * @uml.property  name="fieldName"
291
         */
292 2796 caballero
    public void setFieldName(String str) {
293
        fieldName = str;
294
    }
295
296
    /**
297
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getSymbol(int)
298
     */
299 10627 caballero
    public ISymbol getSymbol(int recordIndex) throws ReadDriverException {
300
                Value val = dataSource.getFieldValue(recordIndex, fieldId);
301
                ISymbol theSymbol = getSymbolByValue(val);
302 2796 caballero
303 10627 caballero
                // if (theSymbol != null) {
304
                return theSymbol;
305 2796 caballero
306 10627 caballero
                // } else {
307
                // return getDefaultSymbol();
308
                // }
309
        }
310 2796 caballero
311
    /**
312 10627 caballero
         * Devuelve un s?mbolo a partir de una IFeature. OJO!! Cuando usamos un
313
         * feature iterator de base de datos el ?nico campo que vendr? rellenado es
314
         * el de fieldID. Los dem?s vendr?n a nulos para ahorra tiempo de creaci?n.
315
         *
316
         * @param feat
317
         *            IFeature
318
         *
319
         * @return S?mbolo.
320
         */
321 7659 fjp
    public ISymbol getSymbolByFeature(IFeature feat) {
322 3347 fjp
        Value val = feat.getAttribute(fieldId);
323
        // Value val = feat.getAttribute(0);
324 7659 fjp
        ISymbol theSymbol = getSymbolByValue(val);
325 2183 fernando
326 2415 caballero
        //if (theSymbol != null) {
327 2796 caballero
        return theSymbol;
328
329 2415 caballero
        //} else {
330
        //    return getDefaultSymbol();
331
        //}
332 2183 fernando
    }
333 322 fernando
334 2796 caballero
    /**
335 10679 jaume
         * @return
336
         * @uml.property  name="defaultSymbol"
337
         */
338 7659 fjp
    public ISymbol getDefaultSymbol() {
339 9883 caballero
            NullUniqueValue nuv=new NullUniqueValue();
340
            if (symbols.containsKey(nuv))
341
                    return (ISymbol)symbols.get(nuv);
342 2796 caballero
        return defaultSymbol;
343
    }
344 1034 vcaballero
345 3771 ldiaz
        /**
346 4575 ldiaz
         * @deprecated
347 4111 ldiaz
         * Writes and SLD using GEOTOOLS objetcs.
348 3771 ldiaz
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getSLDString()
349
         */
350 4111 ldiaz
        public String getSLDString_()
351 6368 caballero
        {
352 3771 ldiaz
            try{
353 4111 ldiaz
                        StyledLayerDescriptor sld = new StyledLayerDescriptor();
354 6368 caballero
                        StyleFactory styleFactory = StyleFactory.createStyleFactory();
355 4111 ldiaz
                        StyleBuilder sb = new StyleBuilder();
356
                Style style = sb.createStyle();
357
                style.setName("default");
358
                Filter filter = null;
359 6368 caballero
                Rule rule = null;
360
361 3771 ldiaz
                        FeatureTypeStyle featStyle = styleFactory.createFeatureTypeStyle();
362
                        featStyle.setFeatureTypeName(fieldName);
363 6368 caballero
364 7659 fjp
                        ISymbol[] symbols = this.getSymbols();
365 3771 ldiaz
                        Symbolizer[] theSymbolizers = new Symbolizer[symbols.length];
366 6368 caballero
                        Object[] values = this.getValues();
367 4111 ldiaz
                        String valueStr = null;
368
                        String condition = null;
369 6368 caballero
370 3771 ldiaz
                        for(int i = 0; i < symbols.length; i++ )
371
                        {
372 4111 ldiaz
                                valueStr = values[i].toString();
373
                                //if(this.valueType == "")
374
                                        condition = fieldName +"='"+valueStr+"'";
375
                                //else
376
                                //        condition = fieldName +"="+values[i];
377
                                filter = (Filter)ExpressionBuilder.parse(condition);
378
                                theSymbolizers[0] = SLDUtils.toGeotoolsSymbol(symbols[i]);
379
                                rule = styleFactory.createRule();
380
                                rule.setName(valueStr);
381
                                rule.setTitle(valueStr);
382
                                rule.setFilter(filter);
383
                                rule.setSymbolizers((Symbolizer[])theSymbolizers.clone());
384
                                featStyle.addRule(rule);
385 3771 ldiaz
                        }
386 4111 ldiaz
                        style.addFeatureTypeStyle(featStyle);
387
                        SLDTransformer st = new SLDTransformer();
388
                        NamedLayer namedLayer = new NamedLayer();
389
                        namedLayer.setName("comunidades");
390
                        namedLayer.addStyle(style);
391
                        sld.addStyledLayer(namedLayer);
392 6368 caballero
                        return st.transform(style);
393
394 4111 ldiaz
            }catch(Exception e)
395
            {
396 6368 caballero
                    e.printStackTrace();
397 4111 ldiaz
                    return null;
398
            }
399
        }
400 10679 jaume
401 4567 ldiaz
        public String getSLDString(String name)
402 6368 caballero
        {
403 4111 ldiaz
            try{
404 6368 caballero
405 4111 ldiaz
                        XmlBuilder xmlBuilder = new XmlBuilder();
406
                        xmlBuilder.writeHeader();
407
                        xmlBuilder.openTag(SLDTags.SLD_ROOT, SLDTags.VERSION_ATTR, SLDTags.VERSION_1_0_0);
408
                        xmlBuilder.openTag(SLDTags.NAMEDLAYER);
409 4567 ldiaz
                        xmlBuilder.writeTag(SLDTags.NAME,name);
410 4111 ldiaz
                        xmlBuilder.openTag(SLDTags.USERSTYLE);
411
                        xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE);
412 4697 ldiaz
                        //xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"fieldName");
413 6368 caballero
414 7659 fjp
                        ISymbol[] symbols = this.getSymbols();
415 6368 caballero
                        Object[] values = this.getValues();
416 4111 ldiaz
                        String valueStr = null;
417 6368 caballero
418 4111 ldiaz
                        for(int i = 0; i < symbols.length; i++ )
419
                        {
420
                                valueStr = values[i].toString();
421
                                xmlBuilder.openTag(SLDTags.RULE);
422 7943 ldiaz
                                xmlBuilder.writeTag(SLDTags.NAME, valueStr);
423 4111 ldiaz
                                xmlBuilder.openTag(SLDTags.FILTER);
424
                                xmlBuilder.openTag(SLDTags.PROPERTYISEQUALTO);
425
                                xmlBuilder.writeTag(SLDTags.PROPERTYNAME,fieldName);
426
                                xmlBuilder.writeTag(SLDTags.LITERAL, valueStr);
427
                                xmlBuilder.closeTag();
428 6368 caballero
                                xmlBuilder.closeTag();
429 7659 fjp
                                if (symbols[i] instanceof ISLDCompatible)
430
                                {
431
                                        ISLDCompatible symSLD = (ISLDCompatible) symbols[i];
432
                                        xmlBuilder.writeRaw(symSLD.toSLD());
433
                                }
434
                                else
435
                                        throw new RuntimeException("Cannot convert Symbol " + i + " " + symbols[i].getDescription() + " to SLD");
436 6368 caballero
437 4111 ldiaz
                                xmlBuilder.closeTag();
438
                        }
439 6368 caballero
440 4111 ldiaz
                        xmlBuilder.closeTag();
441
                        xmlBuilder.closeTag();
442
                        xmlBuilder.closeTag();
443 6368 caballero
                        xmlBuilder.closeTag();
444
                        return xmlBuilder.getXML();
445
446 3771 ldiaz
            }catch(Exception e)
447
            {
448 6368 caballero
                    e.printStackTrace();
449 3771 ldiaz
                    return null;
450
            }
451
        }
452 10679 jaume
453 2796 caballero
    public XMLEntity getXMLEntity() {
454
        XMLEntity xml = new XMLEntity();
455
        xml.putProperty("className", this.getClass().getName());
456
        xml.putProperty("fieldName", fieldName);
457
        xml.putProperty("labelfield", labelFieldName);
458
        xml.putProperty("labelFieldHeight", labelFieldHeight);
459
        xml.putProperty("labelFieldRotation", labelFieldRotation);
460 1034 vcaballero
461 2796 caballero
        xml.putProperty("useDefaultSymbol", useDefaultSymbol);
462 10506 caballero
        xml.putProperty("sorter",sorter);
463 2796 caballero
        xml.addChild(getDefaultSymbol().getXMLEntity());
464 3035 fjp
        // xml.putProperty("isBWithHeightText", isBWithHeightText());
465 2796 caballero
        xml.putProperty("numKeys", keys.size());
466 435 vcaballero
467 2796 caballero
        if (keys.size() > 0) {
468
            xml.putProperty("tipoValueKeys", valueType);
469 1034 vcaballero
470 2796 caballero
            String[] sk = new String[keys.size()];
471
            String[] sv = new String[keys.size()];
472 6631 caballero
            int[] stk = new int[keys.size()];
473
            int[] stv = new int[keys.size()];
474 7659 fjp
            ISymbol[] fsymbols = getSymbols();
475 6631 caballero
            Object[] values = getValues();
476 1034 vcaballero
477 2796 caballero
            for (int i = 0; i < keys.size(); i++) {
478 6368 caballero
                    if (((Value) keys.get(i)).toString().equals("")) {
479
                            sk[i] =" ";
480
                    }else {
481
                            sk[i] = ((Value) keys.get(i)).toString();
482
                    }
483
                    if (((Value) values[i]).toString().equals("")) {
484
                            sv[i] =" ";
485
                    }else {
486
                            sv[i] = ((Value) values[i]).toString();
487
                    }
488 6631 caballero
                    stk[i]= ((Value)keys.get(i)).getSQLType();
489
                    stv[i]= ((Value)values[i]).getSQLType();
490 2796 caballero
                xml.addChild(fsymbols[i].getXMLEntity());
491 1034 vcaballero
492 2796 caballero
                ///System.out.println("get-----------"+sk[i]+"--"+fsymbols[i].getDescription()+"---"+fsymbols[i].getColor());
493
            }
494 1034 vcaballero
495 2796 caballero
            xml.putProperty("keys", sk);
496
            xml.putProperty("values", sv);
497 6631 caballero
            xml.putProperty("typeKeys",stk);
498
            xml.putProperty("typeValues",stv);
499 2796 caballero
        }
500 1034 vcaballero
501 2796 caballero
        return xml;
502
    }
503 2183 fernando
504 2796 caballero
    public void setXMLEntity03(XMLEntity xml) {
505
        clear();
506
        setFieldName(xml.getStringProperty("fieldName"));
507
        setLabelField(xml.getStringProperty("labelfield"));
508 2183 fernando
509 2796 caballero
        int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
510 2183 fernando
511 2796 caballero
        if (useDefaultSymbol == 1) {
512
            setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
513
        } else {
514
            setDefaultSymbol(null);
515
        }
516 2183 fernando
517 2796 caballero
        int numKeys = xml.getIntProperty("numKeys");
518 2183 fernando
519 2796 caballero
        if (numKeys > 0) {
520
            String className = xml.getStringProperty("tipoValueKeys");
521
            String[] sk = xml.getStringArrayProperty("keys");
522
            String[] sv = xml.getStringArrayProperty("values");
523
            Value auxValue;
524
            Value auxValue2;
525 2183 fernando
526 2796 caballero
            for (int i = 0; i < numKeys; i++) {
527
                try {
528
                    auxValue = ValueFactory.createValue(sk[i], className);
529
                    auxValue2 = ValueFactory.createValue(sv[i], className);
530 2183 fernando
531 7828 caballero
                    ISymbol sym = FSymbol.createFromXML03(xml.getChild(i +
532 2796 caballero
                                useDefaultSymbol));
533 2183 fernando
534 2796 caballero
                    ///addSymbol(auxValue, sym);
535
                    symbols.put(auxValue2, sym);
536
                    keys.add(auxValue);
537 2183 fernando
538 2796 caballero
                    ///System.out.println("---set------"+auxValue.toString());
539
                    /// System.out.println("set-----------"+sk[i]+"--"+sym.getDescription()+"---"+sym.getColor());
540
                } catch (SemanticException e) {
541 10679 jaume
                    // TODO Auto-generated catch block
542 2796 caballero
                    e.printStackTrace();
543
                }
544
            }
545
        }
546
    }
547 2183 fernando
548 2796 caballero
    public void setXMLEntity(XMLEntity xml) {
549
        clear();
550
        setFieldName(xml.getStringProperty("fieldName"));
551
        setLabelField(xml.getStringProperty("labelfield"));
552
553
        if (xml.contains("labelFieldHeight")) {
554 2673 fjp
            setLabelHeightField(xml.getStringProperty("labelFieldHeight"));
555 2796 caballero
        }
556
557
        if (xml.contains("labelFieldRotation")) {
558 2673 fjp
            setLabelRotationField(xml.getStringProperty("labelFieldRotation"));
559 2796 caballero
        }
560
561
        useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbol");
562 10506 caballero
        if (xml.contains("sorter"))
563
                sorter = xml.getBooleanProperty("sorter");
564 2796 caballero
        setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
565
566 3035 fjp
        // FJP: Esto no es necesario ya. Para comprobar si tenemos un campo de altura, miramos
567
        // si getLabelHeightField devuelve null o no.
568
        /* if (xml.contains("isBWithHeightText")) {
569 2673 fjp
            setBWithHeightText(xml.getBooleanProperty("isBWithHeightText"));
570 3035 fjp
        } */
571 1034 vcaballero
572 2796 caballero
        //addSymbol(new NullUniqueValue(),getDefaultSymbol());
573
        int numKeys = xml.getIntProperty("numKeys");
574 1034 vcaballero
575 2796 caballero
        if (numKeys > 0) {
576
            String className = xml.getStringProperty("tipoValueKeys");
577
            String[] sk = xml.getStringArrayProperty("keys");
578
            String[] sv = xml.getStringArrayProperty("values");
579
            Value auxValue = null;
580
            Value auxValue2 = null;
581 6631 caballero
            int[] stk=null;
582
            if (xml.contains("typeKeys")) {
583
                                stk = xml.getIntArrayProperty("typeKeys");
584
                                int[] stv = xml.getIntArrayProperty("typeValues");
585
                                for (int i = 0; i < numKeys; i++) {
586
                                        boolean isDefault = false;
587
                                        if (getValue(sk[i], stk[i]) == null) {
588
                                                isDefault = true;
589
                                        }
590 1034 vcaballero
591 6631 caballero
                                        if (className
592
                                                        .equals("com.hardcode.gdbms.engine.values.NullUniqueValue")
593
                                                        || isDefault) {
594
                                                auxValue = new NullUniqueValue();
595
                                                auxValue2 = ValueFactory.createNullValue();
596
                                        } else {
597
                                                auxValue = getValue(sk[i], stk[i]); // ValueFactory.createValue(sk[i],
598
                                                                                                                        // className);
599
                                                auxValue2 = getValue(sv[i], stv[i]); // ValueFactory.createValue(sv[i],
600
                                                                                                                                // className);
601
                                        }
602 9558 jaume
603
                                        // (substituir la de baix per esta) ISymbol sym = SymbolFactory.createFromXML(xml.getChild(i + 1), null);
604
                                        ISymbol sym = FSymbol.createFromXML(xml.getChild(i + 1));
605 6631 caballero
                                        symbols.put(auxValue2, sym);
606
                                        keys.add(auxValue);
607
                                }
608
                        } else {
609
                                for (int i = 0; i < numKeys; i++) {
610
                                        boolean isDefault = false;
611
                                        if (getValue(sk[i]) == null) {
612
                                                isDefault = true;
613
                                        }
614
                                        if (className
615
                                                        .equals("com.hardcode.gdbms.engine.values.NullUniqueValue")
616
                                                        || isDefault) {
617
                                                auxValue = new NullUniqueValue();
618
                                                auxValue2 = ValueFactory.createNullValue();
619
                                        } else {
620
                                                auxValue = getValue(sk[i]); // ValueFactory.createValue(sk[i],
621
                                                                                                        // className);
622
                                                auxValue2 = getValue(sv[i]); // ValueFactory.createValue(sv[i],
623
                                                                                                                // className);
624
                                        }
625 7828 caballero
                                        ISymbol sym = FSymbol.createFromXML(xml.getChild(i + 1));
626 6631 caballero
                                        symbols.put(auxValue2, sym);
627
                                        keys.add(auxValue);
628
                                }
629
                        }
630 2796 caballero
        }
631
    }
632 2415 caballero
633 2796 caballero
    /**
634 6631 caballero
         * Devuelve el valor a partir de su valor en un string.
635
         *
636
         * @param s
637
         *            String con el valor.
638
         * @deprecated M?todo utilizado hasta la 1.0 alpha 855 Debes utilizar a partir de ahora getValue(String s,int type);
639
         * @return Value.
640
         */
641 2796 caballero
    private Value getValue(String s) {
642
        Value val = new NullUniqueValue();
643
        if (s.equals("Resto de Valores"))return val;
644
        try {
645
            try {
646
                val = ValueFactory.createValueByType(s, Types.INTEGER);
647 2415 caballero
648 2796 caballero
                return val;
649
            } catch (NumberFormatException e) {
650
            }
651 2415 caballero
652 2796 caballero
            try {
653
                val = ValueFactory.createValueByType(s, Types.BIGINT);
654 2415 caballero
655 2796 caballero
                return val;
656
            } catch (NumberFormatException e) {
657
            }
658 2415 caballero
659 2796 caballero
            try {
660
                val = ValueFactory.createValueByType(s, Types.FLOAT);
661 1034 vcaballero
662 2796 caballero
                return val;
663
            } catch (NumberFormatException e) {
664
            }
665 470 fjp
666 2796 caballero
            try {
667
                val = ValueFactory.createValueByType(s, Types.DOUBLE);
668 1034 vcaballero
669 2796 caballero
                return val;
670
            } catch (NumberFormatException e) {
671
            }
672 6368 caballero
673 2796 caballero
            val = ValueFactory.createValueByType(s, Types.LONGVARCHAR);
674 6368 caballero
675 2796 caballero
        } catch (ParseException e) {
676
            e.printStackTrace();
677
        }
678 1034 vcaballero
679 2796 caballero
        return val;
680
    }
681 10679 jaume
682 6631 caballero
    /**
683
     * Devuelve el valor a partir de su valor en un string.
684
     *
685
     * @param s String con el valor.
686
     *
687
     * @return Value.
688
     */
689
    private Value getValue(String s,int type) {
690
        Value val = new NullUniqueValue();
691
        if (type==Types.OTHER)
692
                return val;
693
        try {
694
                val = ValueFactory.createValueByType(s, type);
695
        } catch (ParseException e) {
696
            e.printStackTrace();
697
        }
698
        return val;
699
    }
700 1034 vcaballero
701 2796 caballero
    public Legend cloneLegend() throws XMLException {
702 6631 caballero
        return LegendFactory.createFromXML(getXMLEntity());
703 2796 caballero
    }
704 1034 vcaballero
705 10679 jaume
706 2796 caballero
    /* (non-Javadoc)
707
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
708
     */
709 10627 caballero
    public void setDataSource(DataSource ds) throws FieldNotFoundException,
710
                        ReadDriverException {
711
                dataSource = ds;
712
                ds.start();
713
                fieldId = ds.getFieldIndexByName(fieldName);
714
                ds.stop();
715
        }
716 801 fjp
717 10627 caballero
    /*
718
         * (non-Javadoc)
719
         *
720
         * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getSymbolByValue(com.hardcode.gdbms.engine.values.Value)
721
         */
722 10506 caballero
       public ISymbol getSymbolByValue(Value key) {
723
            boolean auxSorted=sorter;
724
            sorter=true;
725
        ISymbol symbol=(ISymbol) symbols.get(key);
726
        sorter=auxSorted;
727
        if (symbol!=null) {
728
                return symbol;
729 2796 caballero
        } else if (useDefaultSymbol) {
730
            return getDefaultSymbol();
731
        }
732 10506 caballero
        return null;
733 2415 caballero
734 2796 caballero
    }
735 2600 caballero
736 10679 jaume
    /**
737
         * @return
738
         * @uml.property  name="shapeType"
739
         */
740 2796 caballero
    public int getShapeType() {
741
        return shapeType;
742
    }
743 2600 caballero
744 2796 caballero
    public String getLabelHeightField() {
745
        return labelFieldHeight;
746
    }
747
748
    public void setLabelHeightField(String str) {
749
        labelFieldHeight = str;
750
    }
751
752
    public String getLabelRotationField() {
753
        return labelFieldRotation;
754
    }
755
756
    public void setLabelRotationField(String str) {
757
        labelFieldRotation = str;
758
    }
759
760
    public void useDefaultSymbol(boolean b) {
761
        useDefaultSymbol = b;
762
    }
763 10679 jaume
764 2796 caballero
    /**
765 10679 jaume
         * Devuelve si se utiliza o no el resto de valores para representarse.
766
         * @return  True si se utiliza el resto de valores.
767
         * @uml.property  name="useDefaultSymbol"
768
         */
769 2796 caballero
    public boolean isUseDefaultSymbol() {
770
        return useDefaultSymbol;
771
    }
772
773
    public void delSymbol(Object key) {
774
        keys.remove(key);
775
        symbols.remove(key);
776
    }
777 3268 fjp
778
    public String[] getUsedFields() {
779
        ArrayList usedFields = new ArrayList();
780
        if (getFieldName() != null)
781 6368 caballero
            usedFields.add(getFieldName());
782 3268 fjp
        if (getLabelField() != null)
783
            usedFields.add(getLabelField());
784
        if (getLabelHeightField() != null)
785
            usedFields.add(getLabelHeightField());
786
        if (getLabelRotationField() != null)
787
            usedFields.add(getLabelRotationField());
788 6368 caballero
789 3268 fjp
        return (String[]) usedFields.toArray(new String[0]);
790
791
    }
792 10506 caballero
793
        public void setSorter(boolean b) {
794
                sorter=b;
795
        }
796
797
        public boolean isSorter() {
798
                return sorter;
799
        }
800 322 fernando
}