Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / VectorialIntervalLegend.java @ 21535

History | View | Annotate | Download (9.53 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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
package com.iver.cit.gvsig.fmap.rendering;
42

    
43
import java.awt.Color;
44

    
45
import com.iver.cit.gvsig.fmap.core.ISLDCompatible;
46
import com.iver.cit.gvsig.fmap.core.SLDTags;
47
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
48
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
49
import com.iver.cit.gvsig.fmap.layers.XMLException;
50
import com.iver.utiles.StringUtilities;
51
import com.iver.utiles.XMLEntity;
52

    
53

    
54
/**
55
 * <p>VectorialIntervalLegend (which should be better called GraduatedColorLegend) is
56
 * a legend that allows to classify ranges of values using a color gradient based
57
 * on a value.<b>
58
 * </p>
59
 * <p>
60
 * The color gradient will be calculated according the starting color, the
61
 * ending color and the amount of intervals.
62
 * </p>
63
 * @author  Vicente Caballero Navarro
64
 */
65
public class VectorialIntervalLegend extends AbstractIntervalLegend {
66
    private Color startColor = Color.red;
67
    private Color endColor = Color.blue;
68
    /**
69
     * Constructor method
70
     */
71
    public VectorialIntervalLegend() {
72
        //defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
73
    }
74

    
75
    /**
76
     * Constructor method
77
     *
78
     * @param type type of the shape.
79
     */
80
    public VectorialIntervalLegend(int type) {
81
        setShapeType(type);
82
    }
83
    
84
    public XMLEntity getXMLEntity() {
85
        XMLEntity xml = new XMLEntity();
86
        xml.putProperty("className", this.getClass().getName());
87
        xml.putProperty("useDefaultSymbolB", useDefaultSymbol);
88
        if (getDefaultSymbol() == null) {
89
            xml.putProperty("useDefaultSymbol", 0);
90
        } else {
91
            xml.putProperty("useDefaultSymbol", 1);
92
            xml.addChild(getDefaultSymbol().getXMLEntity());
93
        }
94

    
95
        xml.putProperty("fieldName", fieldNames[0]);
96
        xml.putProperty("index", index);
97

    
98
        xml.putProperty("intervalType", intervalType);
99
        xml.putProperty("numKeys", keys.size());
100

    
101
        if (keys.size() > 0) {
102
            xml.putProperty("tipoValueKeys", keys.get(0).getClass().getName());
103

    
104
            String[] sk = new String[keys.size()];
105

    
106
            for (int i = 0; i < keys.size(); i++) {
107
                sk[i] = ((IInterval) keys.get(i)).toString();
108
            }
109

    
110
            xml.putProperty("keys", getValues());
111

    
112
            for (int i = 0; i < keys.size(); i++) {
113
                xml.addChild(getSymbols()[i].getXMLEntity());
114
            }
115
        }
116

    
117
        xml.putProperty("startColor", StringUtilities.color2String(startColor));
118
        xml.putProperty("endColor", StringUtilities.color2String(endColor));
119

    
120
        ///xml.putProperty("numSymbols", symbols.size());
121
        ///xml.putProperty("indexs", getIndexs());
122
        ///xml.putProperty("values", getValues());
123
        return xml;
124
    }
125

    
126
    
127
    public void setXMLEntity03(XMLEntity xml) {
128
        fieldNames = new String[] {xml.getStringProperty("fieldName")};
129
        index = xml.getIntProperty("index");
130

    
131
        if (xml.contains("intervalType")) { /* TODO Esta condici?n es para 
132
                                                                                   poder cargar la versi?n 0.3,
133
                                                                                   se puede eliminar cuando ya
134
                                                                                   no queramos soportar esta 
135
                                                                                   versi?n. */
136
            intervalType = xml.getIntProperty("intervalType");
137
        }
138

    
139
        int useDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
140

    
141
        if (useDefaultSymbol == 1) {
142
            setDefaultSymbol( SymbologyFactory.createSymbolFromXML(xml.getChild(0), null));
143
        } else {
144
            setDefaultSymbol(null);
145
        }
146

    
147
        int numKeys = xml.getIntProperty("numKeys");
148

    
149
        if (numKeys > 0) {
150
            String className = xml.getStringProperty("tipoValueKeys");
151
            String[] sk = xml.getStringArrayProperty("keys");
152
            IInterval auxInterval;
153

    
154
            for (int i = 0; i < numKeys; i++) {
155
                auxInterval = FInterval.create(sk[i]);
156
                symbols.put(auxInterval,
157
                    SymbologyFactory.createSymbolFromXML(xml.getChild(i + useDefaultSymbol), null));
158
                keys.add(auxInterval);
159
                System.out.println("auxInterval =" + auxInterval + "Symbol =" +
160
                                 SymbologyFactory.createSymbolFromXML(xml.getChild(i + useDefaultSymbol), null)
161
                           .getDescription());
162
            }
163
        }
164

    
165
        startColor = StringUtilities.string2Color(xml.getStringProperty(
166
                    "startColor"));
167
        endColor = StringUtilities.string2Color(xml.getStringProperty(
168
                    "endColor"));
169
    }
170

    
171
   
172
    public void setXMLEntity(XMLEntity xml) {
173
        fieldNames = new String[] {xml.getStringProperty("fieldName")};
174
        index = xml.getIntProperty("index");
175

    
176
        if (xml.contains("intervalType")) { //TODO Esta condici?n es para poder cargar la versi?n 0.3, se puede eliminar cuando ya no queramos soportar esta versi?n.
177
            intervalType = xml.getIntProperty("intervalType");
178
        }
179
        useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbolB");
180
        int hasDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
181

    
182
        if (hasDefaultSymbol == 1) {
183
            setDefaultSymbol(SymbologyFactory.createSymbolFromXML(xml.getChild(0), null));
184
        } else {
185
            setDefaultSymbol(null);
186
        }
187

    
188
        int numKeys = xml.getIntProperty("numKeys");
189

    
190
        if (numKeys > 0) {
191
            String[] sk = xml.getStringArrayProperty("keys");
192
            IInterval auxInterval;
193

    
194
            for (int i = 0; i < numKeys; i++) {
195
                auxInterval = FInterval.create(sk[i]);
196
                symbols.put(auxInterval,
197
                                SymbologyFactory.createSymbolFromXML(xml.getChild(i + hasDefaultSymbol), null));
198
                keys.add(auxInterval);
199
                System.out.println("auxInterval =" + auxInterval + "Symbol =" +
200
                                SymbologyFactory.createSymbolFromXML(xml.getChild(i + hasDefaultSymbol), null)
201
                           .getDescription());
202
            }
203
        }
204

    
205
        startColor = StringUtilities.string2Color(xml.getStringProperty(
206
                    "startColor"));
207
        endColor = StringUtilities.string2Color(xml.getStringProperty(
208
                    "endColor"));
209
    }
210

    
211
    
212
    public ILegend cloneLegend() throws XMLException {
213
        return LegendFactory.createFromXML(getXMLEntity());
214
    }
215

    
216
    /**
217
         * Returns the final color
218
         * @return Color  final color.
219
         * @uml.property  name="endColor"
220
         */
221
    public Color getEndColor() {
222
        return endColor;
223
    }
224

    
225
    /**
226
         * Inserts the final color.
227
         * @param endColor final color.
228
         * @uml.property  name="endColor"
229
         */
230
    public void setEndColor(Color endColor) {
231
        this.endColor = endColor;
232
    }
233

    
234
    /**
235
         * Returns the initial color.
236
         * @return  Color initial color.
237
         * @uml.property  name="startColor"
238
         */
239
    public Color getStartColor() {
240
        return startColor;
241
    }
242

    
243
    /**
244
         * Inserts the initial color
245
         * @param startColor initial color.
246
         * @uml.property  name="startColor"
247
         */
248
    public void setStartColor(Color startColor) {
249
        this.startColor = startColor;
250
    }
251

    
252
        public int getShapeType() {
253
                return shapeType;
254
        }
255

    
256
        public void setShapeType(int shapeType) {
257
                if (this.shapeType != shapeType) {
258
                        setDefaultSymbol(SymbologyFactory.
259
                                        createDefaultSymbolByShapeType(shapeType));
260
                        this.shapeType = shapeType;
261
                }
262
        }
263

    
264

    
265
        public String getClassName() {
266
                return getClass().getName();
267
        }
268
        
269
        // 
270
        /**
271
         * <p>This method does not exist officially. But what it does is just apply the
272
         * values of a VectorialIntervalLegend to other so, It is not needed to 
273
         * replicate the code of populating the legend in every panel that produces
274
         * a legend by intervals.<br>
275
         * </p>
276
         * <p>
277
         * ok this method is messy, but it's useful ;-)
278
         * </p>
279
         */
280
        public static void initializeVectorialIntervalLegend(
281
                        AbstractIntervalLegend srcLegend,
282
                        AbstractIntervalLegend targetLegend) {
283
                targetLegend.shapeType        = srcLegend.shapeType;
284
                targetLegend.symbols          = srcLegend.symbols;
285
                targetLegend.keys             = srcLegend.keys;
286
                targetLegend.index            = srcLegend.index;
287
                targetLegend.setClassifyingFieldNames(srcLegend.getClassifyingFieldNames());
288
                targetLegend.setClassifyingFieldTypes(srcLegend.getClassifyingFieldTypes());
289
                targetLegend.fieldId          = srcLegend.fieldId;
290
                targetLegend.defaultSymbol    = srcLegend.defaultSymbol;
291
                targetLegend.dataSource       = srcLegend.dataSource;
292
                targetLegend.intervalType     = srcLegend.intervalType;
293
                targetLegend.useDefaultSymbol = srcLegend.useDefaultSymbol;
294
        }
295

    
296
}