Statistics
| Revision:

svn-gvsig-desktop / branches / Fmap_GisPlanet / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / VectorialIntervalLegend.java @ 1848

History | View | Annotate | Download (13.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 com.hardcode.gdbms.engine.data.DataSource;
44
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
45
import com.hardcode.gdbms.engine.values.DoubleValue;
46
import com.hardcode.gdbms.engine.values.FloatValue;
47
import com.hardcode.gdbms.engine.values.IntValue;
48
import com.hardcode.gdbms.engine.values.LongValue;
49
import com.hardcode.gdbms.engine.values.Value;
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.StringUtilities;
58
import com.iver.utiles.XMLEntity;
59

    
60
import java.awt.Color;
61

    
62
import java.util.ArrayList;
63
import java.util.Comparator;
64
import java.util.TreeMap;
65

    
66

    
67
/**
68
 * Leyenda Vectorial por intervalos.
69
 *
70
 * @author Vicente Caballero Navarro
71
 */
72
public class VectorialIntervalLegend implements IntervalLegend, VectorialLegend {
73
        private TreeMap symbols = new TreeMap(new Comparator() {
74
                                public int compare(Object o1, Object o2) {
75
                                        if ((o1 != null) && (o2 != null)) {
76
                                                FInterval i2 = (FInterval) o2;
77
                                                FInterval i1 = (FInterval) o1;
78

    
79
                                                if (i1.getMin() > i2.getMin()) {
80
                                                        return 1;
81
                                                }
82

    
83
                                                if (i1.getMin() < i2.getMin()) {
84
                                                        return -1;
85
                                                }
86
                                        }
87

    
88
                                        return 0;
89
                                }
90
                        }); // Para poder ordenar
91
        private ArrayList keys = new ArrayList(); // En lugar de un HashSet, para tener acceso por ?ndice
92
        private int index = 0;
93
        private String fieldName;
94
        private int fieldId;
95
        private String labelFieldName;
96
        private String labelFieldHeight;
97
        private String labelFieldRotation;
98
        private FSymbol defaultSymbol;
99
        private DataSource dataSource;
100
        private Color startColor = Color.red;
101
        private Color endColor = Color.blue;
102
        private int shapeType;
103

    
104
        /**
105
         * Crea un nuevo VectorialIntervalLegend.
106
         */
107
        public VectorialIntervalLegend() {
108
                defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
109
        }
110

    
111
        /**
112
         * Crea un nuevo VectorialIntervalLegend.
113
         *
114
         * @param type tipo de shape.
115
         */
116
        public VectorialIntervalLegend(int type) {
117
                defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
118
        }
119

    
120
        /**
121
         * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#addSymbol(java.lang.Object,
122
         *                 FSymbol)
123
         */
124
        public void addSymbol(Object key, FSymbol symbol) {
125
                //TODO guardar los intervalos.
126
                Object resul;
127
                resul = symbols.put(key, symbol);
128

    
129
                /*if (resul != null) {
130
                   System.err.println("Error: la clave " + key +
131
                           " ya exist?a. Resul = " + resul);
132
                   System.err.println("symbol nuevo:" + symbol.m_Descrip +
133
                           " Sviejo= " + ((FSymbol) resul).m_Descrip);
134
                   } else {
135
                 */
136
                keys.add(key);
137

    
138
                //}
139
        }
140

    
141
        /*
142
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getSymbol(java.lang.Object)
143
         *
144
                   public FStyle2D getSymbol(Object value) {
145
                       return (FStyle2D) symbols.get(value);
146
                   }
147
                   // TODO transformar la funci?n anterior en la siguiente
148
         *
149
         */
150

    
151
        /**
152
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getSymbol(int)
153
         */
154
        public FSymbol getSymbol(int recordIndex) throws DriverException {
155
                try {
156
                        Value val = dataSource.getFieldValue(recordIndex, fieldId);
157
                        FInterval interval = getInterval(val);
158
                        FSymbol theSymbol = getSymbolByInterval(interval);
159

    
160
                        if (theSymbol != null) {
161
                                return theSymbol;
162
                        } else {
163
                                return getDefaultSymbol();
164
                        }
165
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
166
                        throw new DriverException(e);
167
                }
168
        }
169

    
170
        /**
171
         * Devuelve el s?mbolo a partir del intervalo.
172
         *
173
         * @param key intervalo.
174
         *
175
         * @return s?mbolo.
176
         */
177
        public FSymbol getSymbolByInterval(FInterval key) {
178
                if (key == null) {
179
                        return null;
180
                }
181

    
182
                if (symbols.containsKey(key)) {
183
                        return (FSymbol) symbols.get(key);
184
                }
185

    
186
                return null;
187
        }
188

    
189
        /**
190
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo#getDescriptions()
191
         */
192
        public String[] getDescriptions() {
193
                String[] descriptions = new String[symbols.size()];
194
                FSymbol[] auxSym = getSymbols();
195

    
196
                for (int i = 0; i < descriptions.length; i++)
197
                        descriptions[i] = auxSym[i].getDescription();
198

    
199
                return descriptions;
200
        }
201

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

    
209
        /**
210
         * @see com.iver.cit.gvsig.fmap.rendering.IntervalLegend#setIntervalSymbol(com.iver.cit.gvsig.fmap.rendering.FInterval,
211
         *                 org.geotools.renderer.style.Style2D)
212
         */
213
        public void setIntervalSymbol(FInterval interval, FSymbol symbol) {
214
                /*symbols.put(interval, symbol);
215
                   values.put(new Integer(index), interval);
216
                   index++;
217
                 */
218
        }
219

    
220
        /**
221
         * @see com.iver.cit.gvsig.fmap.rendering.IntervalLegend#changeInterval(int,
222
         *                 com.iver.cit.gvsig.fmap.rendering.FInterval)
223
         */
224
        public void changeInterval(int index, FInterval newInterval) {
225
                /*Object value = values.remove(new Integer(index));
226
                   Object symbol = symbols.remove(value);
227
                   values.put(new Integer(index), newInterval);
228
                   symbols.put(newInterval, symbol);
229
                 */
230
        }
231

    
232
        /**
233
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#clear()
234
         */
235
        public void clear() {
236
                index = 0;
237
                keys.clear();
238
                symbols.clear();
239
        }
240

    
241
        /**
242
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getSymbols()
243
         */
244
        public FSymbol[] getSymbols() {
245
                return (FSymbol[]) symbols.values().toArray(new FSymbol[0]);
246
        }
247

    
248
        /**
249
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getFieldName()
250
         */
251
        public String getFieldName() {
252
                return fieldName;
253
        }
254

    
255
        /**
256
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDefaultSymbol(com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D)
257
         */
258
        public void setDefaultSymbol(FSymbol s) {
259
                defaultSymbol = s;
260
        }
261

    
262
        /**
263
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#setFieldName(String)
264
         */
265
        public void setFieldName(String str) {
266
                fieldName = str;
267
        }
268

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

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

    
283
        /**
284
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
285
         */
286
        public FSymbol getDefaultSymbol() {
287
                return defaultSymbol;
288
        }
289

    
290
        /**
291
         * DOCUMENT ME!
292
         *
293
         * @return DOCUMENT ME!
294
         *
295
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
296
         */
297
        public XMLEntity getXMLEntity() {
298
                XMLEntity xml = new XMLEntity();
299
                xml.putProperty("className",this.getClass().getName());
300

    
301
                if (getDefaultSymbol() == null) {
302
                        xml.putProperty("useDefaultSymbol", 0);
303
                } else {
304
                        xml.putProperty("useDefaultSymbol", 1);
305
                        xml.addChild(getDefaultSymbol().getXMLEntity());
306
                }
307

    
308
                xml.putProperty("fieldName", fieldName);
309
                xml.putProperty("index", index);
310
                xml.putProperty("labelfield", labelFieldName);
311

    
312
                xml.putProperty("numKeys", keys.size());
313

    
314
                if (keys.size() > 0) {
315
                        xml.putProperty("tipoValueKeys", keys.get(0).getClass().getName());
316

    
317
                        String[] sk = new String[keys.size()];
318

    
319
                        for (int i = 0; i < keys.size(); i++) {
320
                                sk[i] = ((FInterval) keys.get(i)).toString();
321
                        }
322

    
323
                        xml.putProperty("keys", sk);
324

    
325
                        for (int i = 0; i < keys.size(); i++) {
326
                                xml.addChild(getSymbols()[i].getXMLEntity());
327
                        }
328
                }
329

    
330
                xml.putProperty("startColor", StringUtilities.color2String(startColor));
331
                xml.putProperty("endColor", StringUtilities.color2String(endColor));
332

    
333
                ///xml.putProperty("numSymbols", symbols.size());
334
                ///xml.putProperty("indexs", getIndexs());
335
                ///xml.putProperty("values", getValues());
336
                return xml;
337
        }
338

    
339
        /**
340
         * Inserta los atributos del XMLEntity.
341
         *
342
         * @param xml XMLEntity.
343
         */
344
        public void setXMLEntity(XMLEntity xml) {
345
                fieldName = xml.getStringProperty("fieldName");
346
                index = xml.getIntProperty("index");
347
                labelFieldName = xml.getStringProperty("labelfield");
348

    
349
                int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
350

    
351
                if (useDefaultSymbol == 1) {
352
                        setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
353
                } else {
354
                        setDefaultSymbol(null);
355
                }
356

    
357
                int numKeys = xml.getIntProperty("numKeys");
358

    
359
                if (numKeys > 0) {
360
                        String className = xml.getStringProperty("tipoValueKeys");
361
                        String[] sk = xml.getStringArrayProperty("keys");
362
                        FInterval auxInterval;
363

    
364
                        for (int i = 0; i < numKeys; i++) {
365
                                auxInterval = FInterval.create(sk[i]);
366
                                symbols.put(auxInterval,
367
                                        FSymbol.createFromXML(xml.getChild(i + useDefaultSymbol)));
368
                                keys.add(auxInterval);
369
                                System.out.println("auxInterval =" + auxInterval + "Symbol =" +
370
                                        FSymbol.createFromXML(xml.getChild(i + useDefaultSymbol))
371
                                                   .getDescription());
372
                        }
373
                }
374

    
375
                startColor = StringUtilities.string2Color(xml.getStringProperty(
376
                                        "startColor"));
377
                endColor = StringUtilities.string2Color(xml.getStringProperty(
378
                                        "endColor"));
379
        }
380

    
381
        /**
382
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
383
         */
384
        public Legend cloneLegend() throws XMLException {
385
                return (Legend) LegendFactory.createFromXML(getXMLEntity());
386
        }
387

    
388
        /* (non-Javadoc)
389
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
390
         */
391
        public void setDataSource(DataSource ds)
392
                throws FieldNotFoundException, DriverException {
393
                try {
394
                        dataSource = ds;
395
                        ds.start();
396
                        fieldId = ds.getFieldIndexByName(fieldName);
397
                        ds.stop();
398
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
399
                        throw new DriverException(e);
400
                }
401
        }
402

    
403
        /**
404
         * Devuelve el intervalo a partir del valor.
405
         *
406
         * @param v valor.
407
         *
408
         * @return intervalo.
409
         */
410
        public FInterval getInterval(Value v) {
411
                double valor = 0;
412

    
413
                if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.IntValue") {
414
                        valor = ((IntValue) v).getValue();
415
                } else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.DoubleValue") {
416
                        valor = ((DoubleValue) v).getValue();
417
                } else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.FloatValue") {
418
                        valor = ((FloatValue) v).getValue();
419
                } else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.LongValue") {
420
                        valor = ((LongValue) v).getValue();
421
                } else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.DateValue") {
422
                        //TODO POR IMPLEMENTAR
423
                }
424

    
425
                for (int i = 0; i < keys.size(); i++) {
426
                        if (((FInterval) keys.get(i)).isInInterval(valor)) {
427
                                return (FInterval) keys.get(i);
428
                        }
429
                }
430

    
431
                return null;
432
        }
433

    
434
        /**
435
         * Devuelve el color final.
436
         *
437
         * @return color final.
438
         */
439
        public Color getEndColor() {
440
                return endColor;
441
        }
442

    
443
        /**
444
         * Inserta el color final.
445
         *
446
         * @param endColor Color final.
447
         */
448
        public void setEndColor(Color endColor) {
449
                this.endColor = endColor;
450
        }
451

    
452
        /**
453
         * Devuelve el color inicial.
454
         *
455
         * @return Color inicial.
456
         */
457
        public Color getStartColor() {
458
                return startColor;
459
        }
460

    
461
        /**
462
         * Inserta el color inicial.
463
         *
464
         * @param startColor Color inicial.
465
         */
466
        public void setStartColor(Color startColor) {
467
                this.startColor = startColor;
468
        }
469

    
470
        /* (non-Javadoc)
471
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getShapeType()
472
         */
473
        public int getShapeType() {
474
                return shapeType;
475
        }
476

    
477
        /* (non-Javadoc)
478
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setShapeType(int)
479
         */
480
        public void setShapeType(int shapeType) {
481
                if (this.shapeType != shapeType) {
482
                        switch (shapeType) {
483
                                case FShape.POINT:
484
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
485

    
486
                                        break;
487

    
488
                                case FShape.LINE:
489
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
490

    
491
                                        break;
492

    
493
                                case FShape.POLYGON:
494
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
495

    
496
                                        break;
497
                        }
498

    
499
                        this.shapeType = shapeType;
500
                }
501
        }
502

    
503
        /* (non-Javadoc)
504
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
505
         */
506
        public String getLabelHeightField() {
507
                return labelFieldHeight;
508
        }
509

    
510
        /**
511
         * Inserta el alto del campo.
512
         *
513
         * @param str alto.
514
         */
515
        public void setLabelHeightField(String str) {
516
                labelFieldHeight = str;
517
        }
518

    
519
        /* (non-Javadoc)
520
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
521
         */
522
        public String getLabelRotationField() {
523
                return labelFieldRotation;
524
        }
525

    
526
        /**
527
         * Inserta la rotaci?n del campo.
528
         *
529
         * @param str Rotaci?n.
530
         */
531
        public void setLabelRotationField(String str) {
532
                labelFieldRotation = str;
533
        }
534
}