Statistics
| Revision:

root / org.gvsig.legend.dotdensity.app.mainplugin / trunk / org.gvsig.legend.dotdensity.app.mainplugin / src / main / java / org / gvsig / symbology / fmap / rendering / DotDensityLegend.java @ 1858

History | View | Annotate | Download (11 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: DotDensityLegend.java 14420 2007-10-04 11:23:36Z jvidal $
45
 * $Log$
46
 * Revision 1.6  2007-09-19 16:25:39  jaume
47
 * ReadExpansionFileException removed from this context and removed unnecessary imports
48
 *
49
 * Revision 1.5  2007/07/25 07:13:34  jaume
50
 * code style
51
 *
52
 * Revision 1.4  2007/05/17 09:32:06  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.3  2007/03/09 11:20:56  jaume
56
 * Advanced symbology (start committing)
57
 *
58
 * Revision 1.2.2.5  2007/02/21 07:34:09  jaume
59
 * labeling starts working
60
 *
61
 * Revision 1.2.2.4  2007/02/15 16:23:44  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.2.2.3  2007/02/12 15:15:20  jaume
65
 * refactored interval legend and added graduated symbol legend
66
 *
67
 * Revision 1.2.2.2  2007/02/09 07:47:04  jaume
68
 * Isymbol moved
69
 *
70
 * Revision 1.2.2.1  2007/01/26 13:48:17  jaume
71
 * *** empty log message ***
72
 *
73
 * Revision 1.2  2007/01/16 11:50:50  jaume
74
 * *** empty log message ***
75
 *
76
 * Revision 1.1  2007/01/10 16:39:41  jaume
77
 * ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
78
 *
79
 * Revision 1.2  2006/11/17 12:49:58  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.1  2006/11/14 12:30:26  jaume
83
 * *** empty log message ***
84
 *
85
 *
86
 */
87
package org.gvsig.symbology.fmap.rendering;
88

    
89
import java.awt.Color;
90

    
91
import org.slf4j.Logger;
92
import org.slf4j.LoggerFactory;
93

    
94
import org.gvsig.fmap.dal.feature.Feature;
95
import org.gvsig.fmap.geom.Geometry;
96
import org.gvsig.fmap.geom.GeometryLocator;
97
import org.gvsig.fmap.geom.GeometryManager;
98
import org.gvsig.fmap.mapcontext.MapContextLocator;
99
import org.gvsig.fmap.mapcontext.MapContextManager;
100
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
101
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.VectorialUniqueValueLegend;
102
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol;
103
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.MultiLayerFillSymbol;
104
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol;
105
import org.gvsig.symbology.fmap.symbols.DotDensityFillSymbol;
106
import org.gvsig.tools.ToolsLocator;
107
import org.gvsig.tools.dynobject.DynStruct;
108
import org.gvsig.tools.persistence.PersistenceManager;
109
import org.gvsig.tools.persistence.PersistentState;
110
import org.gvsig.tools.persistence.exception.PersistenceException;
111
import org.gvsig.tools.util.Callable;
112

    
113

    
114
/**
115
 *
116
 * Implements a legend where the magnitudes of a specific area of the
117
 * map are represented by the density of the points that are distributed
118
 * in the surface.
119
 *
120
 *
121
 * @author  jaume dominguez faus - jaume.dominguez@iver.es
122
 */
123
public class DotDensityLegend extends VectorialUniqueValueLegend {
124

    
125
    private static Logger logger = LoggerFactory.getLogger(
126
        DotDensityLegend.class);
127
        
128
    public static final String
129
    DOT_DENSITY_LEGEND_PERSISTENCE_DEFINITION_NAME = 
130
    "DOT_DENSITY_LEGEND_PERSISTENCE_DEFINITION_NAME";
131
    public static final String
132
    DOT_DENSITY_LEGEND_NAME = "DOT_DENSITY_LEGEND_NAME";
133
    
134
        private double dotValue;
135
        private Color dotColor = Color.BLACK;
136
        private Color backgroundColor = Color.WHITE;
137
        private static final int SIMPLE_FILL_LAYER_INDEX = 0;
138
        private static final int DOT_DENSITY_LAYER_INDEX = 1;
139

    
140

    
141
        public ISymbol getSymbolByValue(Object val) {
142
                MultiLayerFillSymbol sym = (MultiLayerFillSymbol) getDefaultSymbol();
143
                DotDensityFillSymbol densitySym = (DotDensityFillSymbol) sym.getLayer(DOT_DENSITY_LAYER_INDEX);
144
                
145
                if (val instanceof Number) {
146
                    Number dou = (Number) val;
147
                densitySym.setDotCount(
148
                    (int) (dou.doubleValue()/dotValue));
149
                return sym;
150
                } else {
151
                    logger.info("Error: Unexpected value in dot density legend: " + val);
152
                    return null;
153
                }
154
        }
155

    
156
        public ISymbol getSymbolByFeature(Feature feat) {
157
            
158
            String[] ff = this.getClassifyingFieldNames();
159
            Object val = feat.get(ff[0]);
160
            return this.getSymbolByValue(val);
161
        }
162

    
163
        /**
164
         * Establishes the value for the dot used in the dot density legend
165
         *
166
         * @param dotValue
167
         */
168
        public void setDotValue(double dotValue) {
169
                this.dotValue = dotValue;
170
        }
171

    
172
    public void saveToState(PersistentState state) throws PersistenceException {
173

    
174
        super.saveToState(state);
175
        state.set("dotValue", dotValue);
176
        state.set("dotColor", getDotColor());
177
        state.set("bgColor", getBGColor());
178
    }
179

    
180
    public void loadFromState(PersistentState state)
181
        throws PersistenceException {
182
        
183
        super.loadFromState(state);
184
        
185
        dotValue = state.getDouble("dotValue");
186
        Object obj_col = state.get("dotColor");
187
        this.setDotColor((Color) obj_col);
188
        obj_col = state.get("bgColor");
189
        this.setBGColor((Color) obj_col);
190
    }
191
        
192
        
193
        
194

    
195
        /**
196
         * Returns the outline
197
         *
198
         * @return
199
         */
200
        public ILineSymbol getOutline() {
201
                // defined by the SimpleFillSymbol layer
202
                ISymbol symbol = getDefaultSymbol();
203
                if (!(symbol instanceof IFillSymbol)){
204
                        return null;
205
                }
206
                IFillSymbol fillsym = (IFillSymbol) symbol;
207
                if (fillsym instanceof MultiLayerFillSymbol){
208
                        fillsym = (IFillSymbol) ((MultiLayerFillSymbol) fillsym).
209
                        getLayer(SIMPLE_FILL_LAYER_INDEX);
210
                }
211
                if (fillsym == null){
212
                        return null;
213
                }
214

    
215
                return fillsym.getOutline();
216
        }
217

    
218
        /**
219
         * Returns the color for the dot used in the dot density legend.
220
         * @return
221
         */
222
        public Color getDotColor() {
223
//                try {
224
//                        // defined by the DotDensitySymbol layer
225
//                        DotDensityFillSymbol sym = (DotDensityFillSymbol) ((MultiLayerFillSymbol) getDefaultSymbol()).
226
//                        getLayer(DOT_DENSITY_LAYER_INDEX);
227
//                        return sym.getDotColor();
228
//                } catch (NullPointerException npE) {
229
//                        return null;
230
//                }
231
                return dotColor;
232
        }
233

    
234

    
235
        /**
236
         * Sets the color for the dot used in the dot density legend.
237
         * @return
238
         */
239
        public void setDotColor(Color color){
240

    
241
//                DotDensityFillSymbol sym = (DotDensityFillSymbol) ((MultiLayerFillSymbol) getDefaultSymbol()).
242
//                getLayer(DOT_DENSITY_LAYER_INDEX);
243
//                sym.setDotColor(color);
244

    
245
                this.dotColor = color;
246

    
247
        }
248

    
249
        /**
250
         * Obtains the background color for the dot density legend
251
         * @return
252
         */
253
        public Color getBGColor() {
254
//                try {
255
//                        // defined by the SimpleFillSymbol layer
256
//                        IFillSymbol symbol = (IFillSymbol) ((MultiLayerFillSymbol) getDefaultSymbol()).
257
//                        getLayer(SIMPLE_FILL_LAYER_INDEX);
258
//                        return symbol.getFillColor();
259
//                } catch (NullPointerException npE) {
260
//                        return null;
261
//                }
262
                return backgroundColor;
263
        }
264

    
265
        /**
266
         * Sets the background color for the dot density legend
267
         * @return
268
         */
269
        public void setBGColor(Color color) {
270
                this.backgroundColor = color;
271
        }
272
        /**
273
         * Returns the value for the dot that is used in the dot density legend
274
         * @return
275
         */
276
        public double getDotValue() {
277
            return this.dotValue;
278
        }
279
        /**
280
         * Obtains the size of the dot that is used in the dot density legend
281
         * @return
282
         */
283
        public double getDotSize() {
284

    
285
                // defined by the SimpleFillSymbol layer
286
                ISymbol symbol = getDefaultSymbol();
287
                if (symbol == null){
288
                        return -1;
289
                }
290
                if (symbol instanceof DotDensityFillSymbol){
291
                        return ((DotDensityFillSymbol)symbol).getDotSize();
292
                }
293
                if (!(symbol instanceof IFillSymbol)){
294
                        return -1;
295
                }
296
                IFillSymbol fillsym = (IFillSymbol) symbol;
297
                if (fillsym instanceof MultiLayerFillSymbol){
298
                        fillsym = (DotDensityFillSymbol) ((MultiLayerFillSymbol) fillsym).
299
                        getLayer(DOT_DENSITY_LAYER_INDEX);
300
                }
301
                if (fillsym instanceof DotDensityFillSymbol){
302
                        return ((DotDensityFillSymbol)fillsym).getDotSize();
303
                }
304
                return -1;
305

    
306
        }
307

    
308
        /**
309
         * Sets the size of the dot that is used in the dot density legend
310
         */
311
        public void setDotSize(double value) {
312
                DotDensityFillSymbol sym = (DotDensityFillSymbol) ((MultiLayerFillSymbol) getDefaultSymbol()).
313
                getLayer(DOT_DENSITY_LAYER_INDEX);
314
                sym.setDotSize(value);
315
        }
316
        
317
        
318
    public static boolean isPolygonal(int ty) {
319
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
320
        return geomManager.isSubtype(Geometry.TYPES.MULTISURFACE, ty) || 
321
            geomManager.isSubtype(Geometry.TYPES.SURFACE, ty);
322
    }
323

    
324
    // =============================
325
    
326
    public static class RegisterPersistence implements Callable {
327

    
328
        public Object call() throws Exception {
329
            
330
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
331
            if (manager.getDefinition(
332
                DOT_DENSITY_LEGEND_PERSISTENCE_DEFINITION_NAME) == null) {
333
                DynStruct definition = manager
334
                    .addDefinition(DotDensityLegend.class,
335
                        DOT_DENSITY_LEGEND_PERSISTENCE_DEFINITION_NAME,
336
                        DOT_DENSITY_LEGEND_PERSISTENCE_DEFINITION_NAME
337
                        + " Persistence definition", null, null);
338
                
339
                definition.extend(manager.getDefinition(
340
                    VectorialUniqueValueLegend
341
                    .VECTORIAL_UNIQUE_VALUE_LEGEND_PERSISTENCE_DEFINITION_NAME));
342
                
343
                definition.addDynFieldDouble("dotValue").setMandatory(true);
344
                definition.addDynFieldObject("dotColor")
345
                .setClassOfValue(Color.class).setMandatory(true);
346
                definition.addDynFieldObject("bgColor")
347
                .setClassOfValue(Color.class).setMandatory(true);
348
            }
349
            return Boolean.TRUE;
350
        }
351

    
352
    }
353

    
354
    public static class RegisterLegend implements Callable {
355

    
356
        public Object call() throws Exception {
357
            MapContextManager manager =
358
                MapContextLocator.getMapContextManager();
359

    
360
            manager.registerLegend(
361
                DOT_DENSITY_LEGEND_NAME,
362
                DotDensityLegend.class);
363

    
364
            return Boolean.TRUE;
365
        }
366

    
367
    }
368
    
369
    public Object clone() throws CloneNotSupportedException {
370
        
371
        DotDensityLegend resp = (DotDensityLegend) super.clone();
372
        resp.setDotColor(this.getDotColor());
373
        resp.setDotSize(this.getDotSize());
374
        resp.setDotValue(this.getDotValue());
375
        return resp;
376
    }
377

    
378

    
379
}