Statistics
| Revision:

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

History | View | Annotate | Download (14.9 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.IFeature;
54
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
55
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
56
import com.iver.cit.gvsig.fmap.layers.XMLException;
57

    
58
import com.iver.utiles.StringUtilities;
59
import com.iver.utiles.XMLEntity;
60

    
61
import java.awt.Color;
62

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

    
67

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

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

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

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

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

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

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

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

    
139
                //}
140
        }
141

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

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

    
161
                        if (theSymbol != null) {
162
                                return theSymbol;
163
                        } else {
164
                                return getDefaultSymbol();
165
                        }
166
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
167
                        throw new DriverException(e);
168
                }
169
        }
170
    public FSymbol getSymbolByFeature(IFeature feat) {
171
        Value val = feat.getAttribute(fieldId);
172
        FInterval interval = getInterval(val);
173
        FSymbol theSymbol = getSymbolByInterval(interval);
174

    
175
        if (theSymbol != null) {
176
            return theSymbol;
177
        } else {
178
            return getDefaultSymbol();
179
        }
180
    }
181

    
182
        /**
183
         * Devuelve el s?mbolo a partir del intervalo.
184
         *
185
         * @param key intervalo.
186
         *
187
         * @return s?mbolo.
188
         */
189
        public FSymbol getSymbolByInterval(FInterval key) {
190
                if (key == null) {
191
                        return null;
192
                }
193

    
194
                if (symbols.containsKey(key)) {
195
                        return (FSymbol) symbols.get(key);
196
                }
197

    
198
                return null;
199
        }
200

    
201
        /**
202
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo#getDescriptions()
203
         */
204
        public String[] getDescriptions() {
205
                String[] descriptions = new String[symbols.size()];
206
                FSymbol[] auxSym = getSymbols();
207

    
208
                for (int i = 0; i < descriptions.length; i++)
209
                        descriptions[i] = auxSym[i].getDescription();
210

    
211
                return descriptions;
212
        }
213

    
214
        /**
215
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo#getValues()
216
         */
217
        public Object[] getValues() {
218
                return (Object[]) symbols.keySet().toArray(new Object[0]);
219
        }
220

    
221
        /**
222
         * @see com.iver.cit.gvsig.fmap.rendering.IntervalLegend#setIntervalSymbol(com.iver.cit.gvsig.fmap.rendering.FInterval,
223
         *                 org.geotools.renderer.style.Style2D)
224
         */
225
        public void setIntervalSymbol(FInterval interval, FSymbol symbol) {
226
                /*symbols.put(interval, symbol);
227
                   values.put(new Integer(index), interval);
228
                   index++;
229
                 */
230
        }
231

    
232
        /**
233
         * @see com.iver.cit.gvsig.fmap.rendering.IntervalLegend#changeInterval(int,
234
         *                 com.iver.cit.gvsig.fmap.rendering.FInterval)
235
         */
236
        public void changeInterval(int index, FInterval newInterval) {
237
                /*Object value = values.remove(new Integer(index));
238
                   Object symbol = symbols.remove(value);
239
                   values.put(new Integer(index), newInterval);
240
                   symbols.put(newInterval, symbol);
241
                 */
242
        }
243

    
244
        /**
245
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#clear()
246
         */
247
        public void clear() {
248
                index = 0;
249
                keys.clear();
250
                symbols.clear();
251
        }
252

    
253
        /**
254
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getSymbols()
255
         */
256
        public FSymbol[] getSymbols() {
257
                return (FSymbol[]) symbols.values().toArray(new FSymbol[0]);
258
        }
259

    
260
        /**
261
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getFieldName()
262
         */
263
        public String getFieldName() {
264
                return fieldName;
265
        }
266

    
267
        /**
268
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDefaultSymbol(com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D)
269
         */
270
        public void setDefaultSymbol(FSymbol s) {
271
                defaultSymbol = s;
272
        }
273

    
274
        /**
275
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#setFieldName(String)
276
         */
277
        public void setFieldName(String str) {
278
                fieldName = str;
279
        }
280

    
281
        /**
282
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#setLabelField(int)
283
         */
284
        public void setLabelField(String fieldName) {
285
                labelFieldName = fieldName;
286
        }
287

    
288
        /**
289
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getLabelField()
290
         */
291
        public String getLabelField() {
292
                return labelFieldName;
293
        }
294

    
295
        /**
296
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
297
         */
298
        public FSymbol getDefaultSymbol() {
299
                return defaultSymbol;
300
        }
301

    
302
        /**
303
         * DOCUMENT ME!
304
         *
305
         * @return DOCUMENT ME!
306
         *
307
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
308
         */
309
        public XMLEntity getXMLEntity() {
310
                XMLEntity xml = new XMLEntity();
311
                xml.putProperty("className",this.getClass().getName());
312

    
313
                if (getDefaultSymbol() == null) {
314
                        xml.putProperty("useDefaultSymbol", 0);
315
                } else {
316
                        xml.putProperty("useDefaultSymbol", 1);
317
                        xml.addChild(getDefaultSymbol().getXMLEntity());
318
                }
319

    
320
                xml.putProperty("fieldName", fieldName);
321
                xml.putProperty("index", index);
322
                xml.putProperty("labelfield", labelFieldName);
323

    
324
                xml.putProperty("numKeys", keys.size());
325

    
326
                if (keys.size() > 0) {
327
                        xml.putProperty("tipoValueKeys", keys.get(0).getClass().getName());
328

    
329
                        String[] sk = new String[keys.size()];
330

    
331
                        for (int i = 0; i < keys.size(); i++) {
332
                                sk[i] = ((FInterval) keys.get(i)).toString();
333
                        }
334

    
335
                        xml.putProperty("keys", sk);
336

    
337
                        for (int i = 0; i < keys.size(); i++) {
338
                                xml.addChild(getSymbols()[i].getXMLEntity());
339
                        }
340
                }
341

    
342
                xml.putProperty("startColor", StringUtilities.color2String(startColor));
343
                xml.putProperty("endColor", StringUtilities.color2String(endColor));
344

    
345
                ///xml.putProperty("numSymbols", symbols.size());
346
                ///xml.putProperty("indexs", getIndexs());
347
                ///xml.putProperty("values", getValues());
348
                return xml;
349
        }
350

    
351
        /**
352
         * Inserta los atributos del XMLEntity.
353
         *
354
         * @param xml XMLEntity.
355
         */
356
        public void setXMLEntity03(XMLEntity xml) {
357
                fieldName = xml.getStringProperty("fieldName");
358
                index = xml.getIntProperty("index");
359
                labelFieldName = xml.getStringProperty("labelfield");
360

    
361
                int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
362

    
363
                if (useDefaultSymbol == 1) {
364
                        setDefaultSymbol(FSymbol.createFromXML03(xml.getChild(0)));
365
                } else {
366
                        setDefaultSymbol(null);
367
                }
368

    
369
                int numKeys = xml.getIntProperty("numKeys");
370

    
371
                if (numKeys > 0) {
372
                        String className = xml.getStringProperty("tipoValueKeys");
373
                        String[] sk = xml.getStringArrayProperty("keys");
374
                        FInterval auxInterval;
375

    
376
                        for (int i = 0; i < numKeys; i++) {
377
                                auxInterval = FInterval.create(sk[i]);
378
                                symbols.put(auxInterval,
379
                                        FSymbol.createFromXML03(xml.getChild(i + useDefaultSymbol)));
380
                                keys.add(auxInterval);
381
                                System.out.println("auxInterval =" + auxInterval + "Symbol =" +
382
                                        FSymbol.createFromXML03(xml.getChild(i + useDefaultSymbol))
383
                                                   .getDescription());
384
                        }
385
                }
386

    
387
                startColor = StringUtilities.string2Color(xml.getStringProperty(
388
                                        "startColor"));
389
                endColor = StringUtilities.string2Color(xml.getStringProperty(
390
                                        "endColor"));
391
        }
392

    
393
        /**
394
         * Inserta los atributos del XMLEntity.
395
         *
396
         * @param xml XMLEntity.
397
         */
398
        public void setXMLEntity(XMLEntity xml) {
399
                fieldName = xml.getStringProperty("fieldName");
400
                index = xml.getIntProperty("index");
401
                labelFieldName = xml.getStringProperty("labelfield");
402

    
403
                int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
404

    
405
                if (useDefaultSymbol == 1) {
406
                        setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
407
                } else {
408
                        setDefaultSymbol(null);
409
                }
410

    
411
                int numKeys = xml.getIntProperty("numKeys");
412

    
413
                if (numKeys > 0) {
414
                        String className = xml.getStringProperty("tipoValueKeys");
415
                        String[] sk = xml.getStringArrayProperty("keys");
416
                        FInterval auxInterval;
417

    
418
                        for (int i = 0; i < numKeys; i++) {
419
                                auxInterval = FInterval.create(sk[i]);
420
                                symbols.put(auxInterval,
421
                                        FSymbol.createFromXML(xml.getChild(i + useDefaultSymbol)));
422
                                keys.add(auxInterval);
423
                                System.out.println("auxInterval =" + auxInterval + "Symbol =" +
424
                                        FSymbol.createFromXML(xml.getChild(i + useDefaultSymbol))
425
                                                   .getDescription());
426
                        }
427
                }
428

    
429
                startColor = StringUtilities.string2Color(xml.getStringProperty(
430
                                        "startColor"));
431
                endColor = StringUtilities.string2Color(xml.getStringProperty(
432
                                        "endColor"));
433
        }
434

    
435
        /**
436
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
437
         */
438
        public Legend cloneLegend() throws XMLException {
439
                return (Legend) LegendFactory.createFromXML(getXMLEntity());
440
        }
441

    
442
        /* (non-Javadoc)
443
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
444
         */
445
        public void setDataSource(DataSource ds)
446
                throws FieldNotFoundException, DriverException {
447
                try {
448
                        dataSource = ds;
449
                        ds.start();
450
                        fieldId = ds.getFieldIndexByName(fieldName);
451
                        ds.stop();
452
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
453
                        throw new DriverException(e);
454
                }
455
        }
456

    
457
        /**
458
         * Devuelve el intervalo a partir del valor.
459
         *
460
         * @param v valor.
461
         *
462
         * @return intervalo.
463
         */
464
        public FInterval getInterval(Value v) {
465
                double valor = 0;
466

    
467
                if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.IntValue") {
468
                        valor = ((IntValue) v).getValue();
469
                } else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.DoubleValue") {
470
                        valor = ((DoubleValue) v).getValue();
471
                } else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.FloatValue") {
472
                        valor = ((FloatValue) v).getValue();
473
                } else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.LongValue") {
474
                        valor = ((LongValue) v).getValue();
475
                } else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.DateValue") {
476
                        //TODO POR IMPLEMENTAR
477
                }
478

    
479
                for (int i = 0; i < keys.size(); i++) {
480
                        if (((FInterval) keys.get(i)).isInInterval(valor)) {
481
                                return (FInterval) keys.get(i);
482
                        }
483
                }
484

    
485
                return null;
486
        }
487

    
488
        /**
489
         * Devuelve el color final.
490
         *
491
         * @return color final.
492
         */
493
        public Color getEndColor() {
494
                return endColor;
495
        }
496

    
497
        /**
498
         * Inserta el color final.
499
         *
500
         * @param endColor Color final.
501
         */
502
        public void setEndColor(Color endColor) {
503
                this.endColor = endColor;
504
        }
505

    
506
        /**
507
         * Devuelve el color inicial.
508
         *
509
         * @return Color inicial.
510
         */
511
        public Color getStartColor() {
512
                return startColor;
513
        }
514

    
515
        /**
516
         * Inserta el color inicial.
517
         *
518
         * @param startColor Color inicial.
519
         */
520
        public void setStartColor(Color startColor) {
521
                this.startColor = startColor;
522
        }
523

    
524
        /* (non-Javadoc)
525
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getShapeType()
526
         */
527
        public int getShapeType() {
528
                return shapeType;
529
        }
530

    
531
        /* (non-Javadoc)
532
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setShapeType(int)
533
         */
534
        public void setShapeType(int shapeType) {
535
                if (this.shapeType != shapeType) {
536
                        switch (shapeType) {
537
                                case FShape.POINT:
538
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
539

    
540
                                        break;
541

    
542
                                case FShape.LINE:
543
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
544

    
545
                                        break;
546

    
547
                                case FShape.POLYGON:
548
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
549

    
550
                                        break;
551
                        }
552

    
553
                        this.shapeType = shapeType;
554
                }
555
        }
556

    
557
        /* (non-Javadoc)
558
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
559
         */
560
        public String getLabelHeightField() {
561
                return labelFieldHeight;
562
        }
563

    
564
        /**
565
         * Inserta el alto del campo.
566
         *
567
         * @param str alto.
568
         */
569
        public void setLabelHeightField(String str) {
570
                labelFieldHeight = str;
571
        }
572

    
573
        /* (non-Javadoc)
574
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
575
         */
576
        public String getLabelRotationField() {
577
                return labelFieldRotation;
578
        }
579

    
580
        /**
581
         * Inserta la rotaci?n del campo.
582
         *
583
         * @param str Rotaci?n.
584
         */
585
        public void setLabelRotationField(String str) {
586
                labelFieldRotation = str;
587
        }
588

    
589
}