Statistics
| Revision:

root / branches / simbologia / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / AbstractIntervalLegend.java @ 10293

History | View | Annotate | Download (9.51 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 10293 2007-02-13 16:19:19Z jaume $
45
* $Log$
46
* Revision 1.1.2.1  2007-02-13 16:19:19  jaume
47
* graduated symbol legends (start commiting)
48
*
49
*
50
*/
51
package com.iver.cit.gvsig.fmap.rendering;
52

    
53
import java.util.ArrayList;
54
import java.util.Comparator;
55
import java.util.TreeMap;
56

    
57
import com.hardcode.gdbms.engine.data.DataSource;
58
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
59
import com.hardcode.gdbms.engine.values.Value;
60
import com.iver.cit.gvsig.fmap.DriverException;
61
import com.iver.cit.gvsig.fmap.core.FShape;
62
import com.iver.cit.gvsig.fmap.core.IFeature;
63
import com.iver.cit.gvsig.fmap.core.ISymbol;
64
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
65
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
66
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
67

    
68
public abstract class AbstractIntervalLegend implements IntervalLegend{
69

    
70
        public static final int EQUAL_INTERVALS = 0;
71
        public static final int NATURAL_INTERVALS = 1;
72
        public static final int QUANTILE_INTERVALS = 2;
73
        protected TreeMap symbols = new TreeMap(new Comparator() {
74
                public int compare(Object o1, Object o2) {
75
                        if ((o1 != null) && (o2 != null)) {
76
                                if (o1 instanceof NullIntervalValue &&
77
                                                o2 instanceof NullIntervalValue) {
78
                                        return 0;
79
                                }
80

    
81
                                if (o2 instanceof NullIntervalValue) {
82
                                        return 1;
83
                                }
84

    
85
                                if (o1 instanceof NullIntervalValue) {
86
                                        return -1;
87
                                }
88

    
89
                                FInterval i2 = (FInterval) o2;
90
                                FInterval i1 = (FInterval) o1;
91

    
92
                                if (i1.getMin() > i2.getMin()) {
93
                                        return 1;
94
                                }
95

    
96
                                if (i1.getMin() < i2.getMin()) {
97
                                        return -1;
98
                                }
99
                                if (i1.getMax() < i2.getMax()) {
100
                                        return -1;
101
                                }
102
                                if (i1.getMax() > i2.getMax()) {
103
                                        return 1;
104
                                }
105
                        }
106

    
107
                        return 0;
108
                }
109
        });
110
        protected ArrayList keys = new ArrayList();
111
        protected int index = 0;
112
        protected String fieldName;
113
        private int fieldId;
114
        private ISymbol defaultSymbol;
115
        private DataSource dataSource;
116
        private int shapeType;
117
        protected int intervalType = NATURAL_INTERVALS;
118
        protected boolean useDefaultSymbol = false;
119
        protected String labelFieldName;
120
        protected String labelFieldHeight;
121
        protected String labelFieldRotation;
122

    
123
        public void addSymbol(Object key, ISymbol symbol) {
124
                //TODO guardar los intervalos.
125
                Object resul;
126
                resul = symbols.put(key, symbol);
127

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

    
137
                //}
138
        }
139

    
140
        /**
141
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getSymbol(int)
142
         */
143
        public ISymbol getSymbol(int recordIndex) throws DriverException {
144
                try {
145
                        Value val = dataSource.getFieldValue(recordIndex, fieldId);
146
                        IInterval interval = getInterval(val);
147
                        ISymbol theSymbol = getSymbolByInterval(interval);
148

    
149
                        if (theSymbol != null) {
150
                                return theSymbol;
151
                        } else if (useDefaultSymbol) {
152
                                return getDefaultSymbol();
153
                        }
154

    
155
                        return null;
156
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
157
                        throw new DriverException(e);
158
                }
159
        }
160

    
161
        /**
162
         * Devuelve un s?mbolo a partir de una IFeature.
163
         * OJO!! Cuando usamos un feature iterator de base de datos
164
         * el ?nico campo que vendr? rellenado es el de fieldID.
165
         * Los dem?s vendr?n a nulos para ahorra tiempo de creaci?n.
166
         *
167
         * @param feat IFeature.
168
         *
169
         * @return S?mbolo.
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
         * Devuelve el s?mbolo a partir del intervalo.
185
         *
186
         * @param key intervalo.
187
         *
188
         * @return s?mbolo.
189
         */
190
        public ISymbol getSymbolByInterval(IInterval key) {
191
                if (key == null) {
192
                        return null;
193
                }
194

    
195
                if (symbols.containsKey(key)) {
196
                        return (ISymbol) symbols.get(key);
197
                }
198

    
199
                return null;
200
        }
201

    
202
        public String[] getDescriptions() {
203
                String[] descriptions = new String[symbols.size()];
204
                ISymbol[] auxSym = getSymbols();
205

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

    
209
                return descriptions;
210
        }
211

    
212

    
213
        public Object[] getValues() {
214
                return symbols.keySet().toArray();
215
        }
216

    
217
        public void setIntervalSymbol(IInterval interval, ISymbol symbol) {
218
                /*symbols.put(interval, symbol);
219
                 values.put(new Integer(index), interval);
220
                 index++;
221
                 */
222
        }
223

    
224

    
225
        public void changeInterval(int index, IInterval newInterval) {
226
                /*Object value = values.remove(new Integer(index));
227
                 Object symbol = symbols.remove(value);
228
                 values.put(new Integer(index), newInterval);
229
                 symbols.put(newInterval, symbol);
230
                 */
231
        }
232

    
233
        public void clear() {
234
                index = 0;
235
                keys.clear();
236
                symbols.clear();
237
        }
238

    
239
        public ISymbol[] getSymbols() {
240
                return (ISymbol[]) symbols.values().toArray(new ISymbol[0]);
241
        }
242

    
243
        public String getFieldName() {
244
                return fieldName;
245
        }
246

    
247
        public void setDefaultSymbol(ISymbol s) {
248
                defaultSymbol = s;
249
        }
250

    
251
        public void setFieldName(String str) {
252
                fieldName = str;
253
        }
254

    
255
        public void setLabelField(String fieldName) {
256
                labelFieldName = fieldName;
257
        }
258

    
259
        public String getLabelField() {
260
                return labelFieldName;
261
        }
262

    
263
        public ISymbol getDefaultSymbol() {
264
                NullIntervalValue niv=new NullIntervalValue();
265
                if (symbols.containsKey(niv))
266
                        return (ISymbol)symbols.get(niv);
267
                return defaultSymbol;
268
        }
269

    
270
        public void setDataSource(DataSource ds) throws FieldNotFoundException, DriverException {
271
                try {
272
                        dataSource = ds;
273
                        ds.start();
274
                        fieldId = ds.getFieldIndexByName(fieldName);
275
                        ds.stop();
276
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
277
                        throw new DriverException(e);
278
                }
279
        }
280

    
281
        /**
282
         * Devuelve el intervalo a partir del valor.
283
         *
284
         * @param v valor.
285
         *
286
         * @return intervalo.
287
         */
288
        public IInterval getInterval(Value v) {
289
                /*if (v instanceof NullValue){
290
                 System.out.println("Si");
291
                 }*/
292
                for (int i = 0; i < keys.size(); i++) {
293
                        if (((IInterval) keys.get(i)).isInInterval(v)) {
294
                                return (IInterval) keys.get(i);
295
                        }
296
                }
297

    
298
                return null;
299
        }
300

    
301
        public int getShapeType() {
302
                return shapeType;
303
        }
304

    
305
        public void setShapeType(int shapeType) {
306
                if (this.shapeType != shapeType) {
307
                        switch (shapeType) {
308
                        case FShape.POINT:
309
                                defaultSymbol = new SimpleMarkerSymbol();
310

    
311
                                break;
312

    
313
                        case FShape.LINE:
314
                                defaultSymbol = new SimpleLineSymbol();
315

    
316
                                break;
317

    
318
                        case FShape.POLYGON:
319
                                defaultSymbol = new SimpleFillSymbol();
320

    
321
                                break;
322

    
323
                        default:
324
                                throw new Error("Unknown symbol type");
325
                        //                  defaultSymbol = new FSymbol(shapeType);
326
                        }
327

    
328
                        this.shapeType = shapeType;
329
                }
330
        }
331

    
332
        /**
333
         * Inserta el tipo de clasificaci?n de los intervalos.
334
         *
335
         * @param tipoClasificacion Tipo de clasificaci?n.
336
         */
337
        public void setIntervalType(int tipoClasificacion) {
338
                intervalType = tipoClasificacion;
339
        }
340

    
341
        /**
342
         * Devuelve el tipo de clasificaci?n de los intervalos.
343
         *
344
         * @return Tipo de clasificaci?n.
345
         */
346
        public int getIntervalType() {
347
                return intervalType;
348
        }
349

    
350
        /**
351
         * Inserta si se representan el resto de valores o no.
352
         *
353
         * @param b True si se tienen que representar el resto de valores.
354
         */
355
        public void useDefaultSymbol(boolean b) {
356
                useDefaultSymbol = b;
357
        }
358

    
359
        /**
360
         * Devuelve si se utiliza o no el resto de valores para representarse.
361
         *
362
         * @return True si se utiliza el resto de valores.
363
         */
364
        public boolean isUseDefaultSymbol() {
365
                return useDefaultSymbol;
366
        }
367

    
368
        /**
369
         * Elimina un s?mbolo a partir de su clave.
370
         *
371
         * @param obj clave.
372
         */
373
        public void delSymbol(Object obj) {
374
                keys.remove(obj);
375
                symbols.remove(obj);
376
        }
377

    
378
        public String[] getUsedFields() {
379
                ArrayList usedFields = new ArrayList();
380
                if (getFieldName() != null)
381
                        usedFields.add(getFieldName());
382
                if (getLabelField() != null)
383
                        usedFields.add(getLabelField());
384
                if (getLabelHeightField() != null)
385
                        usedFields.add(getLabelHeightField());
386
                if (getLabelRotationField() != null)
387
                        usedFields.add(getLabelRotationField());
388

    
389
                return (String[]) usedFields.toArray(new String[0]);
390

    
391
        }
392

    
393
        public String getLabelHeightField() {
394
            return labelFieldHeight;
395
        }
396

    
397
        /**
398
         * Inserta el alto del campo.
399
         *
400
         * @param str alto.
401
         */
402
        public void setLabelHeightField(String str) {
403
            labelFieldHeight = str;
404
        }
405

    
406
        public String getLabelRotationField() {
407
            return labelFieldRotation;
408
        }
409

    
410
        /**
411
         * Inserta la rotaci?n del campo.
412
         *
413
         * @param str Rotaci?n.
414
         */
415
        public void setLabelRotationField(String str) {
416
            labelFieldRotation = str;
417
        }
418

    
419
}