Statistics
| Revision:

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

History | View | Annotate | Download (8.07 KB)

1 10679 jaume
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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
42
/* CVS MESSAGES:
43
*
44
* $Id$
45
* $Log$
46 10926 jaume
* Revision 1.3  2007-03-27 09:28:40  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.2  2007/03/09 11:20:56  jaume
50 10679 jaume
* Advanced symbology (start committing)
51
*
52
* Revision 1.1.2.2  2007/02/15 16:23:44  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.1.2.1  2007/02/13 16:19:19  jaume
56
* graduated symbol legends (start commiting)
57
*
58
*
59
*/
60
package com.iver.cit.gvsig.fmap.rendering;
61
62
import java.util.ArrayList;
63
import java.util.Comparator;
64
import java.util.TreeMap;
65
66
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
67
import com.hardcode.gdbms.engine.data.DataSource;
68
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
69
import com.hardcode.gdbms.engine.values.Value;
70
import com.iver.cit.gvsig.fmap.core.IFeature;
71
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
72
73
public abstract class AbstractIntervalLegend implements IntervalLegend{
74
75
        public static final int EQUAL_INTERVALS = 0;
76
        public static final int NATURAL_INTERVALS = 1;
77
        public static final int QUANTILE_INTERVALS = 2;
78
        protected TreeMap symbols = new TreeMap(new Comparator() {
79
                public int compare(Object o1, Object o2) {
80
                        if ((o1 != null) && (o2 != null)) {
81
                                if (o1 instanceof NullIntervalValue &&
82
                                                o2 instanceof NullIntervalValue) {
83
                                        return 0;
84
                                }
85
86
                                if (o2 instanceof NullIntervalValue) {
87
                                        return 1;
88
                                }
89
90
                                if (o1 instanceof NullIntervalValue) {
91
                                        return -1;
92
                                }
93
94
                                FInterval i2 = (FInterval) o2;
95
                                FInterval i1 = (FInterval) o1;
96
97
                                if (i1.getMin() > i2.getMin()) {
98
                                        return 1;
99
                                }
100
101
                                if (i1.getMin() < i2.getMin()) {
102
                                        return -1;
103
                                }
104
                                if (i1.getMax() < i2.getMax()) {
105
                                        return -1;
106
                                }
107
                                if (i1.getMax() > i2.getMax()) {
108
                                        return 1;
109
                                }
110
                        }
111
112
                        return 0;
113
                }
114
        });
115
        protected ArrayList keys = new ArrayList();
116
        protected int index = 0;
117
        protected String fieldName;
118
        private int fieldId;
119
        private ISymbol defaultSymbol;
120
        private DataSource dataSource;
121
        protected int intervalType = NATURAL_INTERVALS;
122
        protected boolean useDefaultSymbol = false;
123 10926 jaume
        /**
124
         * @deprecated
125
         */
126 10679 jaume
        protected String labelFieldName;
127 10926 jaume
        /**
128
         * @deprecated
129
         */
130 10679 jaume
        protected String labelFieldHeight;
131 10926 jaume
        /**
132
         * @deprecated
133
         */
134 10679 jaume
        protected String labelFieldRotation;
135
136
        public void addSymbol(Object key, ISymbol symbol) {
137
                //TODO guardar los intervalos.
138
                Object resul;
139
                resul = symbols.put(key, symbol);
140
141
                /*if (resul != null) {
142
                 System.err.println("Error: la clave " + key +
143
                 " ya exist?a. Resul = " + resul);
144
                 System.err.println("symbol nuevo:" + symbol.m_Descrip +
145
                 " Sviejo= " + ((FSymbol) resul).m_Descrip);
146
                 } else {
147
                 */
148
                keys.add(key);
149
150
                //}
151
        }
152
153
        /**
154
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getSymbol(int)
155
         */
156
          public ISymbol getSymbol(int recordIndex) throws ReadDriverException {
157
               Value val = dataSource.getFieldValue(recordIndex, fieldId);
158
               IInterval interval = getInterval(val);
159
               ISymbol theSymbol = getSymbolByInterval(interval);
160
161
               if (theSymbol != null) {
162
                   return theSymbol;
163
               } else if (useDefaultSymbol) {
164
                   return getDefaultSymbol();
165
               }
166
167
               return null;
168
            }
169
170
171
        public ISymbol getSymbolByFeature(IFeature feat) {
172
                Value val = feat.getAttribute(fieldId);
173
                IInterval interval = getInterval(val);
174
                ISymbol theSymbol = getSymbolByInterval(interval);
175
176
                if (theSymbol != null) {
177
                        return theSymbol;
178
                } else {
179
                        return getDefaultSymbol();
180
                }
181
        }
182
183
184
        public ISymbol getSymbolByInterval(IInterval key) {
185
                if (key == null) {
186
                        return null;
187
                }
188
189
                if (symbols.containsKey(key)) {
190
                        return (ISymbol) symbols.get(key);
191
                }
192
193
                return null;
194
        }
195
196
        public String[] getDescriptions() {
197
                String[] descriptions = new String[symbols.size()];
198
                ISymbol[] auxSym = getSymbols();
199
200
                for (int i = 0; i < descriptions.length; i++)
201
                        descriptions[i] = auxSym[i].getDescription();
202
203
                return descriptions;
204
        }
205
206
207
        public Object[] getValues() {
208
                return symbols.keySet().toArray();
209
        }
210
211
        public void clear() {
212
                index = 0;
213
                keys.clear();
214
                symbols.clear();
215
        }
216
217
        public ISymbol[] getSymbols() {
218
                return (ISymbol[]) symbols.values().toArray(new ISymbol[0]);
219
        }
220
221
        public String getFieldName() {
222
                return fieldName;
223
        }
224
225
        public void setDefaultSymbol(ISymbol s) {
226
                defaultSymbol = s;
227
        }
228
229
        public void setFieldName(String str) {
230
                fieldName = str;
231
        }
232
233
        /**
234
         * @deprecated
235
         */
236
        public void setLabelField(String fieldName) {
237
                labelFieldName = fieldName;
238
        }
239
240
        /**
241
         * @deprecated
242
         */
243
        public String getLabelField() {
244
                return labelFieldName;
245
        }
246
247
        public ISymbol getDefaultSymbol() {
248
                NullIntervalValue niv=new NullIntervalValue();
249
                if (symbols.containsKey(niv))
250
                        return (ISymbol)symbols.get(niv);
251
                return defaultSymbol;
252
        }
253
254
    /* (non-Javadoc)
255
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
256
     */
257
    public void setDataSource(DataSource ds)
258
        throws FieldNotFoundException, ReadDriverException {
259
        dataSource = ds;
260
        ds.start();
261
        fieldId = ds.getFieldIndexByName(fieldName);
262
        ds.stop();
263
    }
264
265
        /**
266
         * Devuelve el intervalo a partir del valor.
267
         *
268
         * @param v valor.
269
         *
270
         * @return intervalo.
271
         */
272
        public IInterval getInterval(Value v) {
273
                /*if (v instanceof NullValue){
274
                 System.out.println("Si");
275
                 }*/
276
                for (int i = 0; i < keys.size(); i++) {
277
                        if (((IInterval) keys.get(i)).isInInterval(v)) {
278
                                return (IInterval) keys.get(i);
279
                        }
280
                }
281
282
                return null;
283
        }
284
285
        public void setIntervalType(int tipoClasificacion) {
286
                intervalType = tipoClasificacion;
287
        }
288
289
        /**
290
         * Devuelve el tipo de clasificaci?n de los intervalos.
291
         *
292
         * @return Tipo de clasificaci?n.
293
         */
294
        public int getIntervalType() {
295
                return intervalType;
296
        }
297
298
        public void useDefaultSymbol(boolean b) {
299
                useDefaultSymbol = b;
300
        }
301
302
303
        public boolean isUseDefaultSymbol() {
304
                return useDefaultSymbol;
305
        }
306
307
        /**
308
         * Elimina un s?mbolo a partir de su clave.
309
         *
310
         * @param obj clave.
311
         */
312
        public void delSymbol(Object obj) {
313
                keys.remove(obj);
314
                symbols.remove(obj);
315
        }
316
317
        /**
318
         * @deprecated
319
         * @return
320
         */
321
        public String[] getUsedFields() {
322
                ArrayList usedFields = new ArrayList();
323
                if (getFieldName() != null)
324
                        usedFields.add(getFieldName());
325
                if (getLabelField() != null)
326
                        usedFields.add(getLabelField());
327
                if (getLabelHeightField() != null)
328
                        usedFields.add(getLabelHeightField());
329
                if (getLabelRotationField() != null)
330
                        usedFields.add(getLabelRotationField());
331
332
                return (String[]) usedFields.toArray(new String[0]);
333
334
        }
335
336
        /**
337
         * @deprecated
338
         * @return
339
         */
340
        public String getLabelHeightField() {
341
            return labelFieldHeight;
342
        }
343
344
        /**
345
         * Inserta el alto del campo.
346
         *
347
         * @param str alto.
348
         * @deprecated
349
         */
350
        public void setLabelHeightField(String str) {
351
            labelFieldHeight = str;
352
        }
353
354
        /**
355
         * @deprecated
356
         * @return
357
         */
358
        public String getLabelRotationField() {
359
            return labelFieldRotation;
360
        }
361
362
        /**
363
         * @deprecated
364
         * Inserta la rotaci?n del campo.
365
         *
366
         * @param str Rotaci?n.
367
         */
368
        public void setLabelRotationField(String str) {
369
            labelFieldRotation = str;
370
        }
371
372
}