Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / AbstractIntervalLegend.java @ 11558

History | View | Annotate | Download (8.15 KB)

1
/* 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: AbstractIntervalLegend.java 11558 2007-05-10 09:46:45Z jaume $
45
* $Log$
46
* Revision 1.4  2007-05-10 09:44:08  jaume
47
* Refactored legend interface names
48
*
49
* Revision 1.3  2007/03/27 09:28:40  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.2  2007/03/09 11:20:56  jaume
53
* Advanced symbology (start committing)
54
*
55
* Revision 1.1.2.2  2007/02/15 16:23:44  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.1.2.1  2007/02/13 16:19:19  jaume
59
* graduated symbol legends (start commiting)
60
*
61
*
62
*/
63
package com.iver.cit.gvsig.fmap.rendering;
64

    
65
import java.util.ArrayList;
66
import java.util.Comparator;
67
import java.util.TreeMap;
68

    
69
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
70
import com.hardcode.gdbms.engine.data.DataSource;
71
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
72
import com.hardcode.gdbms.engine.values.Value;
73
import com.iver.cit.gvsig.fmap.core.IFeature;
74
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
75

    
76
public abstract class AbstractIntervalLegend implements IIntervalLegend{
77

    
78
        public static final int EQUAL_INTERVALS = 0;
79
        public static final int NATURAL_INTERVALS = 1;
80
        public static final int QUANTILE_INTERVALS = 2;
81
        protected TreeMap symbols = new TreeMap(new Comparator() {
82
                public int compare(Object o1, Object o2) {
83
                        if ((o1 != null) && (o2 != null)) {
84
                                if (o1 instanceof NullIntervalValue &&
85
                                                o2 instanceof NullIntervalValue) {
86
                                        return 0;
87
                                }
88

    
89
                                if (o2 instanceof NullIntervalValue) {
90
                                        return 1;
91
                                }
92

    
93
                                if (o1 instanceof NullIntervalValue) {
94
                                        return -1;
95
                                }
96

    
97
                                FInterval i2 = (FInterval) o2;
98
                                FInterval i1 = (FInterval) o1;
99

    
100
                                if (i1.getMin() > i2.getMin()) {
101
                                        return 1;
102
                                }
103

    
104
                                if (i1.getMin() < i2.getMin()) {
105
                                        return -1;
106
                                }
107
                                if (i1.getMax() < i2.getMax()) {
108
                                        return -1;
109
                                }
110
                                if (i1.getMax() > i2.getMax()) {
111
                                        return 1;
112
                                }
113
                        }
114

    
115
                        return 0;
116
                }
117
        });
118
        protected ArrayList keys = new ArrayList();
119
        protected int index = 0;
120
        protected String fieldName;
121
        private int fieldId;
122
        private ISymbol defaultSymbol;
123
        private DataSource dataSource;
124
        protected int intervalType = NATURAL_INTERVALS;
125
        protected boolean useDefaultSymbol = false;
126
        /**
127
         * @deprecated
128
         */
129
        protected String labelFieldName;
130
        /**
131
         * @deprecated
132
         */
133
        protected String labelFieldHeight;
134
        /**
135
         * @deprecated
136
         */
137
        protected String labelFieldRotation;
138

    
139
        public void addSymbol(Object key, ISymbol symbol) {
140
                //TODO guardar los intervalos.
141
                Object resul;
142
                resul = symbols.put(key, symbol);
143

    
144
                /*if (resul != null) {
145
                 System.err.println("Error: la clave " + key +
146
                 " ya exist?a. Resul = " + resul);
147
                 System.err.println("symbol nuevo:" + symbol.m_Descrip +
148
                 " Sviejo= " + ((FSymbol) resul).m_Descrip);
149
                 } else {
150
                 */
151
                keys.add(key);
152

    
153
                //}
154
        }
155

    
156
        /**
157
         * @see com.iver.cit.gvsig.fmap.rendering.IVectorialLegend#getSymbol(int)
158
         */
159
          public ISymbol getSymbol(int recordIndex) throws ReadDriverException {
160
               Value val = dataSource.getFieldValue(recordIndex, fieldId);
161
               IInterval interval = getInterval(val);
162
               ISymbol theSymbol = getSymbolByInterval(interval);
163

    
164
               if (theSymbol != null) {
165
                   return theSymbol;
166
               } else if (useDefaultSymbol) {
167
                   return getDefaultSymbol();
168
               }
169

    
170
               return null;
171
            }
172

    
173

    
174
        public ISymbol getSymbolByFeature(IFeature feat) {
175
                Value val = feat.getAttribute(fieldId);
176
                IInterval interval = getInterval(val);
177
                ISymbol theSymbol = getSymbolByInterval(interval);
178

    
179
                if (theSymbol != null) {
180
                        return theSymbol;
181
                } else {
182
                        return getDefaultSymbol();
183
                }
184
        }
185

    
186

    
187
        public ISymbol getSymbolByInterval(IInterval key) {
188
                if (key == null) {
189
                        return null;
190
                }
191

    
192
                if (symbols.containsKey(key)) {
193
                        return (ISymbol) symbols.get(key);
194
                }
195

    
196
                return null;
197
        }
198

    
199
        public String[] getDescriptions() {
200
                String[] descriptions = new String[symbols.size()];
201
                ISymbol[] auxSym = getSymbols();
202

    
203
                for (int i = 0; i < descriptions.length; i++)
204
                        descriptions[i] = auxSym[i].getDescription();
205

    
206
                return descriptions;
207
        }
208

    
209

    
210
        public Object[] getValues() {
211
                return symbols.keySet().toArray();
212
        }
213

    
214
        public void clear() {
215
                index = 0;
216
                keys.clear();
217
                symbols.clear();
218
        }
219

    
220
        public ISymbol[] getSymbols() {
221
                return (ISymbol[]) symbols.values().toArray(new ISymbol[0]);
222
        }
223

    
224
        public String getFieldName() {
225
                return fieldName;
226
        }
227

    
228
        public void setDefaultSymbol(ISymbol s) {
229
                defaultSymbol = s;
230
        }
231

    
232
        public void setFieldName(String str) {
233
                fieldName = str;
234
        }
235

    
236
        /**
237
         * @deprecated
238
         */
239
        public void setLabelField(String fieldName) {
240
                labelFieldName = fieldName;
241
        }
242

    
243
        /**
244
         * @deprecated
245
         */
246
        public String getLabelField() {
247
                return labelFieldName;
248
        }
249

    
250
        public ISymbol getDefaultSymbol() {
251
                NullIntervalValue niv=new NullIntervalValue();
252
                if (symbols.containsKey(niv))
253
                        return (ISymbol)symbols.get(niv);
254
                return defaultSymbol;
255
        }
256

    
257
    /* (non-Javadoc)
258
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
259
     */
260
    public void setDataSource(DataSource ds)
261
        throws FieldNotFoundException, ReadDriverException {
262
        dataSource = ds;
263
        ds.start();
264
        fieldId = ds.getFieldIndexByName(fieldName);
265
        ds.stop();
266
    }
267

    
268
        /**
269
         * Devuelve el intervalo a partir del valor.
270
         *
271
         * @param v valor.
272
         *
273
         * @return intervalo.
274
         */
275
        public IInterval getInterval(Value v) {
276
                /*if (v instanceof NullValue){
277
                 System.out.println("Si");
278
                 }*/
279
                for (int i = 0; i < keys.size(); i++) {
280
                        if (((IInterval) keys.get(i)).isInInterval(v)) {
281
                                return (IInterval) keys.get(i);
282
                        }
283
                }
284

    
285
                return null;
286
        }
287

    
288
        public void setIntervalType(int tipoClasificacion) {
289
                intervalType = tipoClasificacion;
290
        }
291

    
292
        /**
293
         * Devuelve el tipo de clasificaci?n de los intervalos.
294
         *
295
         * @return Tipo de clasificaci?n.
296
         */
297
        public int getIntervalType() {
298
                return intervalType;
299
        }
300

    
301
        public void useDefaultSymbol(boolean b) {
302
                useDefaultSymbol = b;
303
        }
304

    
305

    
306
        public boolean isUseDefaultSymbol() {
307
                return useDefaultSymbol;
308
        }
309

    
310
        /**
311
         * Elimina un s?mbolo a partir de su clave.
312
         *
313
         * @param obj clave.
314
         */
315
        public void delSymbol(Object obj) {
316
                keys.remove(obj);
317
                symbols.remove(obj);
318
        }
319

    
320
        /**
321
         * @deprecated
322
         * @return
323
         */
324
        public String[] getUsedFields() {
325
                ArrayList usedFields = new ArrayList();
326
                if (getFieldName() != null)
327
                        usedFields.add(getFieldName());
328
                if (getLabelField() != null)
329
                        usedFields.add(getLabelField());
330
                if (getLabelHeightField() != null)
331
                        usedFields.add(getLabelHeightField());
332
                if (getLabelRotationField() != null)
333
                        usedFields.add(getLabelRotationField());
334

    
335
                return (String[]) usedFields.toArray(new String[0]);
336

    
337
        }
338

    
339
        /**
340
         * @deprecated
341
         * @return
342
         */
343
        public String getLabelHeightField() {
344
            return labelFieldHeight;
345
        }
346

    
347
        /**
348
         * Inserta el alto del campo.
349
         *
350
         * @param str alto.
351
         * @deprecated
352
         */
353
        public void setLabelHeightField(String str) {
354
            labelFieldHeight = str;
355
        }
356

    
357
        /**
358
         * @deprecated
359
         * @return
360
         */
361
        public String getLabelRotationField() {
362
            return labelFieldRotation;
363
        }
364

    
365
        /**
366
         * @deprecated
367
         * Inserta la rotaci?n del campo.
368
         *
369
         * @param str Rotaci?n.
370
         */
371
        public void setLabelRotationField(String str) {
372
            labelFieldRotation = str;
373
        }
374

    
375
}