Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / rendering / DotDensityLegend.java @ 25709

History | View | Annotate | Download (8.71 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.gvsig.symbology.fmap.symbols.DotDensityFillSymbol;
92

    
93
import com.hardcode.gdbms.engine.values.NumericValue;
94
import com.hardcode.gdbms.engine.values.Value;
95
import com.iver.cit.gvsig.fmap.core.IFeature;
96
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
97
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
98
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
99
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
100
import com.iver.cit.gvsig.fmap.core.symbols.MultiLayerFillSymbol;
101
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
102
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
103
import com.iver.utiles.StringUtilities;
104
import com.iver.utiles.XMLEntity;
105

    
106

    
107
/**
108
 *
109
 * Implements a legend where the magnitudes of a specific area of the map are
110
 * represented by the density of the points that are distributed in the surface.
111
 *
112
 *
113
 * @author  jaume dominguez faus - jaume.dominguez@iver.es
114
 */
115
public class DotDensityLegend extends VectorialUniqueValueLegend {
116

    
117
        private double dotValue;
118
        private Color dotColor;
119
        private Color backgroundColor;
120
        private static final int SIMPLE_FILL_LAYER_INDEX = 0;
121
        private static final int DOT_DENSITY_LAYER_INDEX = 1;
122

    
123
        @Override
124
        public ISymbol getSymbolByValue(Value key) {
125
                MultiLayerFillSymbol sym = (MultiLayerFillSymbol) getDefaultSymbol();
126
                DotDensityFillSymbol densitySym = (DotDensityFillSymbol) sym.getLayer(DOT_DENSITY_LAYER_INDEX);
127
                densitySym.setDotCount((int) (((NumericValue) key).doubleValue()/dotValue));
128
                return sym;
129
        }
130

    
131
        @Override
132
        public ISymbol getSymbolByFeature(IFeature feat) {
133
//                return getSymbolByValue(feat.getAttribute(
134
//                                FLyrVect.forTestOnlyVariableUseIterators_REMOVE_THIS_FIELD
135
//                                ? 0 :fieldId));
136

    
137
                return getSymbolByValue(feat.getAttribute(0));
138
        }
139

    
140
        /**
141
         * Establishes the value for the dot used in the dot density legend
142
         *
143
         * @param dotValue
144
         */
145
        public void setDotValue(double dotValue) {
146
                this.dotValue = dotValue;
147
        }
148

    
149
        /**
150
         * <p>Returns an XML entity with all needed information about the class to de-serialize
151
         *  and create instances of it.</p>
152
         *
153
         * @return the XML entity of the class that persists
154
         */
155
        @Override
156
        public XMLEntity getXMLEntity() {
157
                XMLEntity xml = new XMLEntity();
158
                xml.putProperty("className", getClass().getName());
159
                xml.putProperty("dotValue", dotValue);
160
                xml.putProperty("fieldName", getClassifyingFieldNames());
161
                xml.putProperty("dotColor",StringUtilities.color2String(getDotColor()));
162
                xml.putProperty("bgColor",StringUtilities.color2String(getBGColor()));
163
                xml.addChild(getDefaultSymbol().getXMLEntity());
164
                return xml;
165
        }
166

    
167
        /**
168
         * <p>Sets an XML entity with all needed information about the class to de-serialize
169
         *  and create instances of it.</p>
170
         *
171
         * @param xml the XML entity of the class that persists
172
         */
173
        @Override
174
        public void setXMLEntity(XMLEntity xml) {
175
                dotValue = xml.getDoubleProperty("dotValue");
176
                setClassifyingFieldNames(xml.getStringArrayProperty("fieldName"));
177
                setDefaultSymbol(SymbologyFactory.createSymbolFromXML(xml.getChild(0), null));
178
                if(xml.contains("dotColor"))
179
                        setDotColor(StringUtilities.string2Color(xml.getStringProperty("dotColor")));
180
                if(xml.contains("bgColor"))
181
                        setBGColor(StringUtilities.string2Color(xml.getStringProperty("bgColor")));
182
        }
183

    
184
        /**
185
         * Returns the outline
186
         *
187
         * @return
188
         */
189
        public ILineSymbol getOutline() {
190
                // defined by the SimpleFillSymbol layer
191
                ISymbol symbol = getDefaultSymbol();
192
                if (!(symbol instanceof IFillSymbol)){
193
                        return null;
194
                }
195
                IFillSymbol fillsym = (IFillSymbol) symbol;
196
                if (fillsym instanceof MultiLayerFillSymbol){
197
                        fillsym = (IFillSymbol) ((MultiLayerFillSymbol) fillsym).
198
                        getLayer(SIMPLE_FILL_LAYER_INDEX);
199
                }
200
                if (fillsym == null){
201
                        return null;
202
                }
203

    
204
                return fillsym.getOutline();
205
        }
206

    
207
        /**
208
         * Returns the color for the dot used in the dot density legend.
209
         * @return
210
         */
211
        public Color getDotColor() {
212
//                try {
213
//                        // defined by the DotDensitySymbol layer
214
//                        DotDensityFillSymbol sym = (DotDensityFillSymbol) ((MultiLayerFillSymbol) getDefaultSymbol()).
215
//                        getLayer(DOT_DENSITY_LAYER_INDEX);
216
//                        return sym.getDotColor();
217
//                } catch (NullPointerException npE) {
218
//                        return null;
219
//                }
220
                return dotColor;
221
        }
222

    
223

    
224
        /**
225
         * Sets the color for the dot used in the dot density legend.
226
         * @return
227
         */
228
        public void setDotColor(Color color){
229

    
230
//                DotDensityFillSymbol sym = (DotDensityFillSymbol) ((MultiLayerFillSymbol) getDefaultSymbol()).
231
//                getLayer(DOT_DENSITY_LAYER_INDEX);
232
//                sym.setDotColor(color);
233

    
234
                this.dotColor = color;
235

    
236
        }
237

    
238
        /**
239
         * Obtains the background color for the dot density legend
240
         * @return
241
         */
242
        public Color getBGColor() {
243
//                try {
244
//                        // defined by the SimpleFillSymbol layer
245
//                        IFillSymbol symbol = (IFillSymbol) ((MultiLayerFillSymbol) getDefaultSymbol()).
246
//                        getLayer(SIMPLE_FILL_LAYER_INDEX);
247
//                        return symbol.getFillColor();
248
//                } catch (NullPointerException npE) {
249
//                        return null;
250
//                }
251
                return backgroundColor;
252
        }
253

    
254
        /**
255
         * Sets the background color for the dot density legend
256
         * @return
257
         */
258
        public void setBGColor(Color color) {
259
                this.backgroundColor = color;
260
        }
261
        /**
262
         * Returns the value for the dot that is used in the dot density legend
263
         * @return
264
         */
265
        public double getDotValue() {
266
                try {
267
                        XMLEntity xml = getXMLEntity();
268
                        return xml.getDoubleProperty("dotValue");
269
                } catch (NullPointerException npE) {
270
                        return 0;
271
                }
272
        }
273
        /**
274
         * Obtains the size of the dot that is used in the dot density legend
275
         * @return
276
         */
277
        public double getDotSize() {
278

    
279
                // defined by the SimpleFillSymbol layer
280
                ISymbol symbol = getDefaultSymbol();
281
                if (symbol == null){
282
                        return -1;
283
                }
284
                if (symbol instanceof DotDensityFillSymbol){
285
                        return ((DotDensityFillSymbol)symbol).getDotSize();
286
                }
287
                if (!(symbol instanceof IFillSymbol)){
288
                        return -1;
289
                }
290
                IFillSymbol fillsym = (IFillSymbol) symbol;
291
                if (fillsym instanceof MultiLayerFillSymbol){
292
                        fillsym = (DotDensityFillSymbol) ((MultiLayerFillSymbol) fillsym).
293
                        getLayer(DOT_DENSITY_LAYER_INDEX);
294
                }
295
                if (fillsym instanceof DotDensityFillSymbol){
296
                        return ((DotDensityFillSymbol)fillsym).getDotSize();
297
                }
298
                return -1;
299

    
300
        }
301

    
302
        /**
303
         * Sets the size of the dot that is used in the dot density legend
304
         */
305
        public void setDotSize(double value) {
306

    
307
                DotDensityFillSymbol sym = (DotDensityFillSymbol) ((MultiLayerFillSymbol) getDefaultSymbol()).
308
                getLayer(DOT_DENSITY_LAYER_INDEX);
309
                sym.setDotSize(value);
310

    
311
        }
312

    
313
}