Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / VectorialIntervalLegend.java @ 558

History | View | Annotate | Download (10.9 KB)

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

    
3
import com.hardcode.gdbms.engine.data.DataSource;
4
import com.hardcode.gdbms.engine.data.DriverException;
5
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
6
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
7
import com.hardcode.gdbms.engine.instruction.SemanticException;
8
import com.hardcode.gdbms.engine.values.BooleanValue;
9
import com.hardcode.gdbms.engine.values.DoubleValue;
10
import com.hardcode.gdbms.engine.values.FloatValue;
11
import com.hardcode.gdbms.engine.values.IntValue;
12
import com.hardcode.gdbms.engine.values.LongValue;
13
import com.hardcode.gdbms.engine.values.Value;
14
import com.hardcode.gdbms.engine.values.ValueFactory;
15

    
16
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
17
import com.iver.cit.gvsig.fmap.layers.XMLException;
18

    
19
import com.iver.utiles.StringUtilities;
20
import com.iver.utiles.XMLEntity;
21

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

    
27

    
28
/**
29
 * DOCUMENT ME!
30
 *
31
 * @author Vicente Caballero Navarro
32
 */
33
public class VectorialIntervalLegend implements IntervalLegend, VectorialLegend {
34
        private TreeMap symbols = new TreeMap(new Comparator() {
35
                                public int compare(Object o1, Object o2) {
36

    
37
                                        if ((o1 != null) && (o2 != null)) {
38
                                                FInterval i2 = (FInterval) o2;
39
                                                FInterval i1 = (FInterval) o1;
40
                                                if (i1.getMin()>i2.getMin()){
41
                                                        return 1;
42
                                                }
43
                                                if (i1.getMin()<i2.getMin()){
44
                                                        return -1;
45
                                                }
46
                                                
47
                                        }
48

    
49
                                        return 0;
50
                                }
51
                        }); // Para poder ordenar
52
        private ArrayList keys = new ArrayList(); // En lugar de un HashSet, para tener acceso por ?ndice
53
        private int index = 0;
54
        private String fieldName;
55
        private int fieldId;
56
        private String labelFieldName;
57
        private FSymbol defaultSymbol;
58
        private DataSource dataSource;
59
        private Color startColor=Color.red;
60
        private Color endColor=Color.blue;
61
        /**
62
         * Crea un nuevo VectorialIntervalLegend.
63
         */
64
        public VectorialIntervalLegend() {
65
                defaultSymbol=LegendFactory.DEFAULT_POLYGON_SYMBOL;
66
        }
67

    
68
        /**
69
         * Crea un nuevo VectorialIntervalLegend.
70
         *
71
         * @param type DOCUMENT ME!
72
         */
73
        public VectorialIntervalLegend(int type) {
74
                defaultSymbol=LegendFactory.DEFAULT_POLYGON_SYMBOL;
75
        }
76

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

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

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

    
107
        /**
108
         * DOCUMENT ME!
109
         *
110
         * @param recordIndex DOCUMENT ME!
111
         *
112
         * @return DOCUMENT ME!
113
         *
114
         * @throws DriverException
115
         *
116
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getSymbol(int)
117
         */
118
        public FSymbol getSymbol(int recordIndex) throws DriverException {
119
                Value val = dataSource.getFieldValue(recordIndex, fieldId);
120
                FInterval interval=getInterval(val);
121
                FSymbol theSymbol = getSymbolByInterval(interval);
122

    
123
                if (theSymbol != null) {
124
                        return theSymbol;
125
                } else {
126
                        return getDefaultSymbol();
127
                }
128
        }
129

    
130
        /**
131
         * DOCUMENT ME!
132
         *
133
         * @param key DOCUMENT ME!
134
         *
135
         * @return DOCUMENT ME!
136
         */
137
        public FSymbol getSymbolByInterval(FInterval key) {
138
                if (key == null)return null;
139
                if (symbols.containsKey(key)) {
140
                        return (FSymbol) symbols.get(key);
141
                }
142

    
143
                return null;
144
        }
145

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

    
153
                for (int i = 0; i < descriptions.length; i++)
154
                        descriptions[i] = auxSym[i].m_Descrip;
155

    
156
                return descriptions;
157
        }
158

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

    
166
        /**
167
         * DOCUMENT ME!
168
         *
169
         * @param interval DOCUMENT ME!
170
         * @param symbol DOCUMENT ME!
171
         */
172

    
173
        /*        private int[] getIndexs() {
174
           Integer[] integers = (Integer[]) values.keySet().toArray(new Integer[0]);
175
           int[] ent = new int[integers.length];
176
           for (int i = 0; i < integers.length; i++) {
177
                   ent[i] = integers[i].intValue();
178
           }
179
           return ent;
180
           }
181
         */
182

    
183
        /**
184
         * @see com.iver.cit.gvsig.fmap.rendering.IntervalLegend#setIntervalSymbol(com.iver.cit.gvsig.fmap.rendering.FInterval,
185
         *                 org.geotools.renderer.style.Style2D)
186
         */
187
        public void setIntervalSymbol(FInterval interval, FSymbol symbol) {
188
                /*symbols.put(interval, symbol);
189
                   values.put(new Integer(index), interval);
190
                   index++;
191
                 */
192
        }
193

    
194
        /**
195
         * @see com.iver.cit.gvsig.fmap.rendering.IntervalLegend#changeInterval(int,
196
         *                 com.iver.cit.gvsig.fmap.rendering.FInterval)
197
         */
198
        public void changeInterval(int index, FInterval newInterval) {
199
                /*Object value = values.remove(new Integer(index));
200
                   Object symbol = symbols.remove(value);
201
                   values.put(new Integer(index), newInterval);
202
                   symbols.put(newInterval, symbol);
203
                 */
204
        }
205

    
206
        /**
207
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#clear()
208
         */
209
        public void clear() {
210
                index = 0;
211
                keys.clear();
212
                symbols.clear();
213
        }
214

    
215
        /**
216
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getSymbols()
217
         */
218
        public FSymbol[] getSymbols() {
219
                return (FSymbol[]) symbols.values().toArray(new FSymbol[0]);
220
        }
221

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

    
229
        /**
230
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#setDefaultSymbol(com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D)
231
         */
232
        public void setDefaultSymbol(FSymbol s) {
233
                defaultSymbol = s;
234
        }
235

    
236
        /**
237
         * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#setFieldName(String)
238
         */
239
        public void setFieldName(String str) {
240
                fieldName = str;
241
        }
242

    
243
        /**
244
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#setLabelField(int)
245
         */
246
        public void setLabelField(String fieldName) {
247
                labelFieldName = fieldName;
248
        }
249

    
250
        /**
251
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getLabelField()
252
         */
253
        public String getLabelField() {
254
                return labelFieldName;
255
        }
256

    
257
        /**
258
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
259
         */
260
        public FSymbol getDefaultSymbol() {
261
                return defaultSymbol;
262
        }
263

    
264
        /**
265
         * @throws DriverException
266
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
267
         */
268
        public XMLEntity getXMLEntity() throws DriverException {
269
                XMLEntity xml = new XMLEntity();
270
                xml.putProperty("nameClass", this.getClass().getName());
271

    
272
                 if (getDefaultSymbol() == null){
273
                xml.putProperty("useDefaultSymbol", 0);
274
             }else{
275
                xml.putProperty("useDefaultSymbol", 1);
276
                xml.addChild(getDefaultSymbol().getXMLEntity());
277
             }
278
                xml.putProperty("fieldName", fieldName);
279
                xml.putProperty("index", index);
280
                xml.putProperty("labelfield", labelFieldName);
281

    
282
                xml.putProperty("numKeys", keys.size());
283
        if (keys.size() > 0)
284
        {                        
285
                        xml.putProperty("tipoValueKeys", keys.get(0).getClass().getName());
286
                String[] sk = new String[keys.size()];
287
        
288
                for (int i = 0; i < keys.size(); i++) {
289
                    sk[i] = ((FInterval) keys.get(i)).toString();
290
                }
291
                xml.putProperty("keys", sk);
292
                        
293
        
294
                for (int i = 0; i < keys.size(); i++) {
295
                    xml.addChild(getSymbols()[i].getXMLEntity());
296
                }
297
        }
298
        xml.putProperty("startColor",StringUtilities.color2String(startColor));
299
        xml.putProperty("endColor",StringUtilities.color2String(endColor));
300
                ///xml.putProperty("numSymbols", symbols.size());
301

    
302
                ///xml.putProperty("indexs", getIndexs());
303
                ///xml.putProperty("values", getValues());
304

    
305
                return xml;
306
        }
307

    
308
        /**
309
         * DOCUMENT ME!
310
         *
311
         * @param xml DOCUMENT ME!
312
         */
313
        public void setXMLEntity(XMLEntity xml) {
314
                fieldName = xml.getStringProperty("fieldName");
315
                index = xml.getIntProperty("index");
316
                labelFieldName = xml.getStringProperty("labelfield");
317
                int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
318
            if (useDefaultSymbol==1)
319
                    setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
320
            else
321
                    setDefaultSymbol(null);
322
            int numKeys = xml.getIntProperty("numKeys");
323
            if (numKeys > 0)
324
            {
325
                    String className = xml.getStringProperty("tipoValueKeys");
326
                    String[] sk = xml.getStringArrayProperty("keys");
327
                    FInterval auxInterval;
328
            for (int i = 0; i < numKeys; i++) {
329
                            auxInterval = FInterval.create(sk[i]);
330
                                        symbols.put(auxInterval, FSymbol.createFromXML(xml.getChild(i+useDefaultSymbol)));
331
                                        keys.add(auxInterval);
332
                                        System.out.println("auxInterval =" + auxInterval+ "Symbol ="+ FSymbol.createFromXML(xml.getChild(i+useDefaultSymbol)).m_Descrip);
333
            }
334
                    
335
            }
336
            startColor=StringUtilities.string2Color(xml.getStringProperty("startColor"));
337
            endColor=StringUtilities.string2Color(xml.getStringProperty("endColor"));
338
        }
339

    
340
        /**
341
         * DOCUMENT ME!
342
         *
343
         * @return DOCUMENT ME!
344
         * @throws XMLException
345
         * @throws DriverException
346
         *
347
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
348
         */
349
        public Legend cloneLegend() throws XMLException, DriverException {
350
                return (Legend) LegendFactory.createFromXML(getXMLEntity());
351
        }
352

    
353
        /* (non-Javadoc)
354
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
355
         */
356
        public void setDataSource(DataSource ds) throws FieldNotFoundException, DriverException {
357
                dataSource = ds;
358
                /*dataSource = ds;
359
                ds.start();
360
                fieldId = ds.getFieldIndexByName(fieldName);
361
                ds.stop();
362
                */
363
        }
364
        public FInterval getInterval(Value v){
365
                double valor=0;
366
                        if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.IntValue"){
367
                                valor=((IntValue)v).getValue();
368
                        }else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.DoubleValue"){
369
                                valor=((DoubleValue)v).getValue();
370
                        }else if(v.getClass().getName() == "com.hardcode.gdbms.engine.values.FloatValue"){
371
                                valor=((FloatValue)v).getValue();
372
                        }else if(v.getClass().getName() == "com.hardcode.gdbms.engine.values.LongValue"){
373
                                valor=((LongValue)v).getValue();
374
                        }else if (v.getClass().getName() == "com.hardcode.gdbms.engine.values.DateValue"){
375
                        //TODO POR IMPLEMENTAR
376
                        }
377
                        for (int i=0;i<keys.size();i++){
378
                                if (((FInterval)keys.get(i)).isInInterval(valor)){
379
                                        return (FInterval)keys.get(i);
380
                                }
381
                        }
382
                        return null;
383
                }
384
        public Color getEndColor() {
385
                return endColor;
386
        }
387
        public void setEndColor(Color endColor) {
388
                this.endColor = endColor;
389
        }
390
        public Color getStartColor() {
391
                return startColor;
392
        }
393
        public void setStartColor(Color startColor) {
394
                this.startColor = startColor;
395
        }
396
}