Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / VectorialIntervalLegend.java @ 1034

History | View | Annotate | Download (12.1 KB)

1
package com.iver.cit.gvsig.fmap.rendering;
2

    
3
import com.hardcode.gdbms.engine.data.DataSource;
4
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
5
import com.hardcode.gdbms.engine.values.DoubleValue;
6
import com.hardcode.gdbms.engine.values.FloatValue;
7
import com.hardcode.gdbms.engine.values.IntValue;
8
import com.hardcode.gdbms.engine.values.LongValue;
9
import com.hardcode.gdbms.engine.values.Value;
10

    
11
import com.iver.cit.gvsig.fmap.DriverException;
12
import com.iver.cit.gvsig.fmap.core.FShape;
13
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
14
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
15
import com.iver.cit.gvsig.fmap.layers.XMLException;
16

    
17
import com.iver.utiles.StringUtilities;
18
import com.iver.utiles.XMLEntity;
19

    
20
import java.awt.Color;
21

    
22
import java.util.ArrayList;
23
import java.util.Comparator;
24
import java.util.TreeMap;
25

    
26

    
27
/**
28
 * Leyenda Vectorial por intervalos.
29
 *
30
 * @author Vicente Caballero Navarro
31
 */
32
public class VectorialIntervalLegend implements IntervalLegend, VectorialLegend {
33
        private TreeMap symbols = new TreeMap(new Comparator() {
34
                                public int compare(Object o1, Object o2) {
35
                                        if ((o1 != null) && (o2 != null)) {
36
                                                FInterval i2 = (FInterval) o2;
37
                                                FInterval i1 = (FInterval) o1;
38

    
39
                                                if (i1.getMin() > i2.getMin()) {
40
                                                        return 1;
41
                                                }
42

    
43
                                                if (i1.getMin() < i2.getMin()) {
44
                                                        return -1;
45
                                                }
46
                                        }
47

    
48
                                        return 0;
49
                                }
50
                        }); // Para poder ordenar
51
        private ArrayList keys = new ArrayList(); // En lugar de un HashSet, para tener acceso por ?ndice
52
        private int index = 0;
53
        private String fieldName;
54
        private int fieldId;
55
        private String labelFieldName;
56
        private String labelFieldHeight;
57
        private String labelFieldRotation;
58
        private FSymbol defaultSymbol;
59
        private DataSource dataSource;
60
        private Color startColor = Color.red;
61
        private Color endColor = Color.blue;
62
        private int shapeType;
63

    
64
        /**
65
         * Crea un nuevo VectorialIntervalLegend.
66
         */
67
        public VectorialIntervalLegend() {
68
                defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
69
        }
70

    
71
        /**
72
         * Crea un nuevo VectorialIntervalLegend.
73
         *
74
         * @param type tipo de shape.
75
         */
76
        public VectorialIntervalLegend(int type) {
77
                defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
78
        }
79

    
80
        /**
81
         * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#addSymbol(java.lang.Object,
82
         *                 FSymbol)
83
         */
84
        public void addSymbol(Object key, FSymbol symbol) {
85
                //TODO guardar los intervalos.
86
                Object resul;
87
                resul = symbols.put(key, symbol);
88

    
89
                /*if (resul != null) {
90
                   System.err.println("Error: la clave " + key +
91
                           " ya exist?a. Resul = " + resul);
92
                   System.err.println("symbol nuevo:" + symbol.m_Descrip +
93
                           " Sviejo= " + ((FSymbol) resul).m_Descrip);
94
                   } else {
95
                 */
96
                keys.add(key);
97

    
98
                //}
99
        }
100

    
101
        /*
102
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getSymbol(java.lang.Object)
103
         *
104
                   public FStyle2D getSymbol(Object value) {
105
                       return (FStyle2D) symbols.get(value);
106
                   }
107
                   // TODO transformar la funci?n anterior en la siguiente
108
         *
109
         */
110

    
111
        /**
112
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getSymbol(int)
113
         */
114
        public FSymbol getSymbol(int recordIndex) throws DriverException {
115
                try {
116
                        Value val = dataSource.getFieldValue(recordIndex, fieldId);
117
                        FInterval interval = getInterval(val);
118
                        FSymbol theSymbol = getSymbolByInterval(interval);
119

    
120
                        if (theSymbol != null) {
121
                                return theSymbol;
122
                        } else {
123
                                return getDefaultSymbol();
124
                        }
125
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
126
                        throw new DriverException(e);
127
                }
128
        }
129

    
130
        /**
131
         * Devuelve el s?mbolo a partir del intervalo.
132
         *
133
         * @param key intervalo.
134
         *
135
         * @return s?mbolo.
136
         */
137
        public FSymbol getSymbolByInterval(FInterval key) {
138
                if (key == null) {
139
                        return null;
140
                }
141

    
142
                if (symbols.containsKey(key)) {
143
                        return (FSymbol) symbols.get(key);
144
                }
145

    
146
                return null;
147
        }
148

    
149
        /**
150
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo#getDescriptions()
151
         */
152
        public String[] getDescriptions() {
153
                String[] descriptions = new String[symbols.size()];
154
                FSymbol[] auxSym = getSymbols();
155

    
156
                for (int i = 0; i < descriptions.length; i++)
157
                        descriptions[i] = auxSym[i].getDescription();
158

    
159
                return descriptions;
160
        }
161

    
162
        /**
163
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo#getValues()
164
         */
165
        public Object[] getValues() {
166
                return (Object[]) symbols.keySet().toArray(new Object[0]);
167
        }
168

    
169
        /**
170
         * @see com.iver.cit.gvsig.fmap.rendering.IntervalLegend#setIntervalSymbol(com.iver.cit.gvsig.fmap.rendering.FInterval,
171
         *                 org.geotools.renderer.style.Style2D)
172
         */
173
        public void setIntervalSymbol(FInterval interval, FSymbol symbol) {
174
                /*symbols.put(interval, symbol);
175
                   values.put(new Integer(index), interval);
176
                   index++;
177
                 */
178
        }
179

    
180
        /**
181
         * @see com.iver.cit.gvsig.fmap.rendering.IntervalLegend#changeInterval(int,
182
         *                 com.iver.cit.gvsig.fmap.rendering.FInterval)
183
         */
184
        public void changeInterval(int index, FInterval newInterval) {
185
                /*Object value = values.remove(new Integer(index));
186
                   Object symbol = symbols.remove(value);
187
                   values.put(new Integer(index), newInterval);
188
                   symbols.put(newInterval, symbol);
189
                 */
190
        }
191

    
192
        /**
193
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#clear()
194
         */
195
        public void clear() {
196
                index = 0;
197
                keys.clear();
198
                symbols.clear();
199
        }
200

    
201
        /**
202
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getSymbols()
203
         */
204
        public FSymbol[] getSymbols() {
205
                return (FSymbol[]) symbols.values().toArray(new FSymbol[0]);
206
        }
207

    
208
        /**
209
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getFieldName()
210
         */
211
        public String getFieldName() {
212
                return fieldName;
213
        }
214

    
215
        /**
216
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#setDefaultSymbol(com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D)
217
         */
218
        public void setDefaultSymbol(FSymbol s) {
219
                defaultSymbol = s;
220
        }
221

    
222
        /**
223
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#setFieldName(String)
224
         */
225
        public void setFieldName(String str) {
226
                fieldName = str;
227
        }
228

    
229
        /**
230
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#setLabelField(int)
231
         */
232
        public void setLabelField(String fieldName) {
233
                labelFieldName = fieldName;
234
        }
235

    
236
        /**
237
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getLabelField()
238
         */
239
        public String getLabelField() {
240
                return labelFieldName;
241
        }
242

    
243
        /**
244
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
245
         */
246
        public FSymbol getDefaultSymbol() {
247
                return defaultSymbol;
248
        }
249

    
250
        /**
251
         * DOCUMENT ME!
252
         *
253
         * @return DOCUMENT ME!
254
         *
255
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
256
         */
257
        public XMLEntity getXMLEntity() {
258
                XMLEntity xml = new XMLEntity();
259
                xml.putProperty("nameClass", this.getClass().getName());
260

    
261
                if (getDefaultSymbol() == null) {
262
                        xml.putProperty("useDefaultSymbol", 0);
263
                } else {
264
                        xml.putProperty("useDefaultSymbol", 1);
265
                        xml.addChild(getDefaultSymbol().getXMLEntity());
266
                }
267

    
268
                xml.putProperty("fieldName", fieldName);
269
                xml.putProperty("index", index);
270
                xml.putProperty("labelfield", labelFieldName);
271

    
272
                xml.putProperty("numKeys", keys.size());
273

    
274
                if (keys.size() > 0) {
275
                        xml.putProperty("tipoValueKeys", keys.get(0).getClass().getName());
276

    
277
                        String[] sk = new String[keys.size()];
278

    
279
                        for (int i = 0; i < keys.size(); i++) {
280
                                sk[i] = ((FInterval) keys.get(i)).toString();
281
                        }
282

    
283
                        xml.putProperty("keys", sk);
284

    
285
                        for (int i = 0; i < keys.size(); i++) {
286
                                xml.addChild(getSymbols()[i].getXMLEntity());
287
                        }
288
                }
289

    
290
                xml.putProperty("startColor", StringUtilities.color2String(startColor));
291
                xml.putProperty("endColor", StringUtilities.color2String(endColor));
292

    
293
                ///xml.putProperty("numSymbols", symbols.size());
294
                ///xml.putProperty("indexs", getIndexs());
295
                ///xml.putProperty("values", getValues());
296
                return xml;
297
        }
298

    
299
        /**
300
         * Inserta los atributos del XMLEntity.
301
         *
302
         * @param xml XMLEntity.
303
         */
304
        public void setXMLEntity(XMLEntity xml) {
305
                fieldName = xml.getStringProperty("fieldName");
306
                index = xml.getIntProperty("index");
307
                labelFieldName = xml.getStringProperty("labelfield");
308

    
309
                int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
310

    
311
                if (useDefaultSymbol == 1) {
312
                        setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
313
                } else {
314
                        setDefaultSymbol(null);
315
                }
316

    
317
                int numKeys = xml.getIntProperty("numKeys");
318

    
319
                if (numKeys > 0) {
320
                        String className = xml.getStringProperty("tipoValueKeys");
321
                        String[] sk = xml.getStringArrayProperty("keys");
322
                        FInterval auxInterval;
323

    
324
                        for (int i = 0; i < numKeys; i++) {
325
                                auxInterval = FInterval.create(sk[i]);
326
                                symbols.put(auxInterval,
327
                                        FSymbol.createFromXML(xml.getChild(i + useDefaultSymbol)));
328
                                keys.add(auxInterval);
329
                                System.out.println("auxInterval =" + auxInterval + "Symbol =" +
330
                                        FSymbol.createFromXML(xml.getChild(i + useDefaultSymbol))
331
                                                   .getDescription());
332
                        }
333
                }
334

    
335
                startColor = StringUtilities.string2Color(xml.getStringProperty(
336
                                        "startColor"));
337
                endColor = StringUtilities.string2Color(xml.getStringProperty(
338
                                        "endColor"));
339
        }
340

    
341
        /**
342
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
343
         */
344
        public Legend cloneLegend() throws XMLException, DriverException {
345
                return (Legend) LegendFactory.createFromXML(getXMLEntity());
346
        }
347

    
348
        /* (non-Javadoc)
349
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
350
         */
351
        public void setDataSource(DataSource ds)
352
                throws FieldNotFoundException, DriverException {
353
                try {
354
                        dataSource = ds;
355
                        ds.start();
356
                        fieldId = ds.getFieldIndexByName(fieldName);
357
                        ds.stop();
358
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
359
                        throw new DriverException(e);
360
                }
361
        }
362

    
363
        /**
364
         * Devuelve el intervalo a partir del valor.
365
         *
366
         * @param v valor.
367
         *
368
         * @return intervalo.
369
         */
370
        public FInterval getInterval(Value v) {
371
                double valor = 0;
372

    
373
                if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.IntValue") {
374
                        valor = ((IntValue) v).getValue();
375
                } else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.DoubleValue") {
376
                        valor = ((DoubleValue) v).getValue();
377
                } else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.FloatValue") {
378
                        valor = ((FloatValue) v).getValue();
379
                } else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.LongValue") {
380
                        valor = ((LongValue) v).getValue();
381
                } else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.DateValue") {
382
                        //TODO POR IMPLEMENTAR
383
                }
384

    
385
                for (int i = 0; i < keys.size(); i++) {
386
                        if (((FInterval) keys.get(i)).isInInterval(valor)) {
387
                                return (FInterval) keys.get(i);
388
                        }
389
                }
390

    
391
                return null;
392
        }
393

    
394
        /**
395
         * Devuelve el color final.
396
         *
397
         * @return color final.
398
         */
399
        public Color getEndColor() {
400
                return endColor;
401
        }
402

    
403
        /**
404
         * Inserta el color final.
405
         *
406
         * @param endColor Color final.
407
         */
408
        public void setEndColor(Color endColor) {
409
                this.endColor = endColor;
410
        }
411

    
412
        /**
413
         * Devuelve el color inicial.
414
         *
415
         * @return Color inicial.
416
         */
417
        public Color getStartColor() {
418
                return startColor;
419
        }
420

    
421
        /**
422
         * Inserta el color inicial.
423
         *
424
         * @param startColor Color inicial.
425
         */
426
        public void setStartColor(Color startColor) {
427
                this.startColor = startColor;
428
        }
429

    
430
        /* (non-Javadoc)
431
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getShapeType()
432
         */
433
        public int getShapeType() {
434
                return shapeType;
435
        }
436

    
437
        /* (non-Javadoc)
438
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setShapeType(int)
439
         */
440
        public void setShapeType(int shapeType) {
441
                if (this.shapeType != shapeType) {
442
                        switch (shapeType) {
443
                                case FShape.POINT:
444
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
445

    
446
                                        break;
447

    
448
                                case FShape.LINE:
449
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
450

    
451
                                        break;
452

    
453
                                case FShape.POLYGON:
454
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
455

    
456
                                        break;
457
                        }
458

    
459
                        this.shapeType = shapeType;
460
                }
461
        }
462

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

    
470
        /**
471
         * Inserta el alto del campo.
472
         *
473
         * @param str alto.
474
         */
475
        public void setLabelHeightField(String str) {
476
                labelFieldHeight = str;
477
        }
478

    
479
        /* (non-Javadoc)
480
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
481
         */
482
        public String getLabelRotationField() {
483
                return labelFieldRotation;
484
        }
485

    
486
        /**
487
         * Inserta la rotaci?n del campo.
488
         *
489
         * @param str Rotaci?n.
490
         */
491
        public void setLabelRotationField(String str) {
492
                labelFieldRotation = str;
493
        }
494
}