Statistics
| Revision:

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

History | View | Annotate | Download (11.8 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.Value;
49
import com.hardcode.gdbms.engine.values.ValueFactory;
50

    
51
import com.iver.cit.gvsig.fmap.DriverException;
52
import com.iver.cit.gvsig.fmap.core.FShape;
53
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
54
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
55
import com.iver.cit.gvsig.fmap.layers.XMLException;
56

    
57
import com.iver.utiles.XMLEntity;
58

    
59
import java.util.ArrayList;
60
import java.util.Comparator;
61
import java.util.TreeMap;
62

    
63

    
64
/**
65
 * Leyenda vectorial por valores ?nicos.
66
 *
67
 * @author Vicente Caballero Navarro
68
 */
69
public class VectorialUniqueValueLegend implements UniqueValueLegend,
70
        VectorialLegend {
71
        private TreeMap symbols = new TreeMap(new Comparator() {
72
                                public int compare(Object o1, Object o2) {
73
                                        int resul = -1;
74

    
75
                                        if ((o1 != null) && (o2 != null)) {
76
                                                Value v2 = (Value) o2;
77
                                                Value v1 = (Value) o1;
78
                                                BooleanValue boolVal;
79

    
80
                                                try {
81
                                                        boolVal = (BooleanValue) (v1.greater(v2));
82

    
83
                                                        if (boolVal.getValue()) {
84
                                                                return 1;
85
                                                        }
86

    
87
                                                        boolVal = (BooleanValue) (v1.less(v2));
88

    
89
                                                        if (boolVal.getValue()) {
90
                                                                return -1;
91
                                                        }
92
                                                } catch (IncompatibleTypesException e) {
93
                                                        // TODO Auto-generated catch block
94
                                                        e.printStackTrace();
95
                                                }
96
                                        }
97

    
98
                                        return 0;
99
                                }
100
                        }); // Para poder ordenar
101
        private ArrayList keys = new ArrayList(); // En lugar de un HashSet, para tener acceso por ?ndice
102
        private String fieldName;
103
        private int fieldId = -1;
104
        private String labelFieldName;
105
        private String labelFieldHeight;
106
        private String labelFieldRotation;
107
        private FSymbol defaultSymbol;
108
        private DataSource dataSource;
109
        private int shapeType;
110

    
111
        /**
112
         * Crea un nuevo VectorialUniqueValueLegend.
113
         */
114
        public VectorialUniqueValueLegend() {
115
                defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
116
        }
117

    
118
        /**
119
         * Crea un nuevo VectorialUniqueValueLegend.
120
         *
121
         * @param shapeType Tipo de shape.
122
         */
123
        public VectorialUniqueValueLegend(int shapeType) {
124
                switch (shapeType) {
125
                        case FShape.POINT:
126
                                defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
127

    
128
                                break;
129

    
130
                        case FShape.LINE:
131
                                defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
132

    
133
                                break;
134

    
135
                        case FShape.POLYGON:
136
                                defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
137

    
138
                                break;
139
                }
140
        }
141

    
142
        /**
143
         * Inserta el tipo de shape.
144
         *
145
         * @param shapeType Tipo de shape.
146
         */
147
        public void setShapeType(int shapeType) {
148
                if (this.shapeType != shapeType) {
149
                        switch (shapeType) {
150
                                case FShape.POINT:
151
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
152

    
153
                                        break;
154

    
155
                                case FShape.LINE:
156
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
157

    
158
                                        break;
159

    
160
                                case FShape.POLYGON:
161
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
162

    
163
                                        break;
164
                        }
165

    
166
                        this.shapeType = shapeType;
167
                }
168
        }
169

    
170
        /**
171
         * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#setValueSymbolByID(int,
172
         *                 FSymbol)
173
         */
174
        public void setValueSymbolByID(int id, FSymbol symbol) {
175
                symbols.put(keys.get(id), symbol);
176
        }
177

    
178
        /**
179
         * Devuelve un s?mbolo a partir del ID. Mira en el m_ArrayKeys  el elemento
180
         * ID, y con esa clave recupera el FSymbol por valor
181
         *
182
         * @param id ID.
183
         * @param symbol DOCUMENT ME!
184
         */
185

    
186
        /*
187
           public FSymbol getSymbolByID(int ID) {
188
               return (FSymbol) symbols.get(keys.get(ID));
189
           }
190
         */
191

    
192
        /**
193
         * Se usa en la tabla que muestra una leyenda.
194
         *
195
         * @param id
196
         * @param symbol
197
         */
198
        public void setValueSymbol(int id, FSymbol symbol) {
199
                symbols.put(keys.get(id), symbol);
200
        }
201

    
202
        /**
203
         * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getValues()
204
         */
205
        public Object[] getValues() {
206
                return symbols.keySet().toArray(new Object[0]);
207
        }
208

    
209
        /**
210
         * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#addSymbol(java.lang.Object,
211
         *                 FSymbol)
212
         */
213
        public void addSymbol(Object key, FSymbol symbol) {
214
                Object resul;
215
                resul = symbols.put(key, symbol);
216

    
217
                if (resul != null) {
218
                        System.err.println("Error: la clave " + key +
219
                                " ya exist?a. Resul = " + resul);
220
                        System.err.println("symbol nuevo:" + symbol.getDescription() +
221
                                " Sviejo= " + ((FSymbol) resul).getDescription());
222
                } else {
223
                        keys.add(key);
224
                }
225
        }
226

    
227
        /**
228
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#clear()
229
         */
230
        public void clear() {
231
                keys.clear();
232
                symbols.clear();
233
        }
234

    
235
        /**
236
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getDescriptions()
237
         */
238
        public String[] getDescriptions() {
239
                String[] descriptions = new String[symbols.size()];
240
                FSymbol[] auxSym = getSymbols();
241

    
242
                for (int i = 0; i < descriptions.length; i++)
243
                        descriptions[i] = auxSym[i].getDescription();
244

    
245
                return descriptions;
246
        }
247

    
248
        /**
249
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getSymbols()
250
         */
251
        public FSymbol[] getSymbols() {
252
                return (FSymbol[]) symbols.values().toArray(new FSymbol[0]);
253
        }
254

    
255
        /**
256
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getFieldName()
257
         */
258
        public String getFieldName() {
259
                return fieldName;
260
        }
261

    
262
        /**
263
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDefaultSymbol(com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D)
264
         */
265
        public void setDefaultSymbol(FSymbol s) {
266
                defaultSymbol = s;
267
        }
268

    
269
        /**
270
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getLabelField()
271
         */
272
        public String getLabelField() {
273
                return labelFieldName;
274
        }
275

    
276
        /**
277
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#setLabelField(int)
278
         */
279
        public void setLabelField(String fieldName) {
280
                labelFieldName = fieldName;
281
        }
282

    
283
        /**
284
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#setField()
285
         */
286
        public void setFieldName(String str) {
287
                fieldName = str;
288
        }
289

    
290
        /**
291
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getSymbol(int)
292
         */
293
        public FSymbol getSymbol(int recordIndex) throws DriverException {
294
                try {
295
                        Value val = dataSource.getFieldValue(recordIndex, fieldId);
296
                        FSymbol theSymbol = getSymbolByValue(val);
297

    
298
                        if (theSymbol != null) {
299
                                return theSymbol;
300
                        } else {
301
                                return getDefaultSymbol();
302
                        }
303
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
304
                        throw new DriverException(e);
305
                }
306
        }
307

    
308
        /**
309
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
310
         */
311
        public FSymbol getDefaultSymbol() {
312
                return defaultSymbol;
313
        }
314

    
315
        /**
316
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
317
         */
318
        public XMLEntity getXMLEntity() {
319
                XMLEntity xml = new XMLEntity();
320
                xml.putProperty("className",this.getClass().getName());
321
                xml.putProperty("fieldName", fieldName);
322
                xml.putProperty("labelfield", labelFieldName);
323

    
324
                if (getDefaultSymbol() == null) {
325
                        xml.putProperty("useDefaultSymbol", 0);
326
                } else {
327
                        xml.putProperty("useDefaultSymbol", 1);
328
                        xml.addChild(getDefaultSymbol().getXMLEntity());
329
                }
330

    
331
                xml.putProperty("numKeys", keys.size());
332

    
333
                if (keys.size() > 0) {
334
                        xml.putProperty("tipoValueKeys", keys.get(0).getClass().getName());
335

    
336
                        String[] sk = new String[keys.size()];
337
                        String[] sv = new String[keys.size()];
338

    
339
                        FSymbol[] fsymbols = getSymbols();
340
                        Object[] values = (Object[]) getValues();
341

    
342
                        for (int i = 0; i < keys.size(); i++) {
343
                                sk[i] = ((Value) keys.get(i)).toString();
344
                                sv[i] = ((Value) values[i]).toString();
345
                                xml.addChild(fsymbols[i].getXMLEntity());
346

    
347
                                ///System.out.println("get-----------"+sk[i]+"--"+fsymbols[i].getDescription()+"---"+fsymbols[i].getColor());
348
                        }
349

    
350
                        xml.putProperty("keys", sk);
351
                        xml.putProperty("values", sv);
352
                }
353

    
354
                return xml;
355
        }
356

    
357
        /**
358
         * Inserta el XMLEntity.
359
         *
360
         * @param xml XMLEntity.
361
         */
362
        public void setXMLEntity(XMLEntity xml) {
363
                clear();
364
                setFieldName(xml.getStringProperty("fieldName"));
365
                setLabelField(xml.getStringProperty("labelfield"));
366

    
367
                int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
368

    
369
                if (useDefaultSymbol == 1) {
370
                        setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
371
                } else {
372
                        setDefaultSymbol(null);
373
                }
374

    
375
                int numKeys = xml.getIntProperty("numKeys");
376

    
377
                if (numKeys > 0) {
378
                        String className = xml.getStringProperty("tipoValueKeys");
379
                        String[] sk = xml.getStringArrayProperty("keys");
380
                        String[] sv = xml.getStringArrayProperty("values");
381
                        Value auxValue;
382
                        Value auxValue2;
383

    
384
                        for (int i = 0; i < numKeys; i++) {
385
                                try {
386
                                        auxValue = ValueFactory.createValue(sk[i], className);
387
                                        auxValue2 = ValueFactory.createValue(sv[i], className);
388

    
389
                                        FSymbol sym = FSymbol.createFromXML(xml.getChild(i +
390
                                                                useDefaultSymbol));
391

    
392
                                        ///addSymbol(auxValue, sym);
393
                                        symbols.put(auxValue2, sym);
394
                                        keys.add(auxValue);
395

    
396
                                        ///System.out.println("---set------"+auxValue.toString());
397
                                        /// System.out.println("set-----------"+sk[i]+"--"+sym.getDescription()+"---"+sym.getColor());
398
                                } catch (SemanticException e) {
399
                                        // TODO Auto-generated catch block
400
                                        e.printStackTrace();
401
                                }
402
                        }
403
                }
404
        }
405

    
406
        /**
407
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
408
         */
409
        public Legend cloneLegend() throws XMLException {
410
                return (Legend) LegendFactory.createFromXML(getXMLEntity());
411
        }
412

    
413
        /* (non-Javadoc)
414
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
415
         */
416
        public void setDataSource(DataSource ds)
417
                throws FieldNotFoundException, DriverException {
418
                try {
419
                        dataSource = ds;
420
                        ds.start();
421
                        fieldId = ds.getFieldIndexByName(fieldName);
422
                        ds.stop();
423
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
424
                        throw new DriverException(e);
425
                }
426
        }
427

    
428
        /* (non-Javadoc)
429
         * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getSymbolByValue(com.hardcode.gdbms.engine.values.Value)
430
         */
431
        public FSymbol getSymbolByValue(Value key) {
432
                if (symbols.containsKey(key)) {
433
                        return (FSymbol) symbols.get(key);
434
                }
435

    
436
                return null;
437
        }
438

    
439
        /* (non-Javadoc)
440
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getShapeType()
441
         */
442
        public int getShapeType() {
443
                return shapeType;
444
        }
445

    
446
        /* (non-Javadoc)
447
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
448
         */
449
        public String getLabelHeightField() {
450
                return labelFieldHeight;
451
        }
452

    
453
        /**
454
         * Inserta el alto de campo.
455
         *
456
         * @param str alto.
457
         */
458
        public void setLabelHeightField(String str) {
459
                labelFieldHeight = str;
460
        }
461

    
462
        /* (non-Javadoc)
463
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
464
         */
465
        public String getLabelRotationField() {
466
                return labelFieldRotation;
467
        }
468

    
469
        /**
470
         * Inserta rotaci?n.
471
         *
472
         * @param str Rotaci?n.
473
         */
474
        public void setLabelRotationField(String str) {
475
                labelFieldRotation = str;
476
        }
477
}