Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap / src / org / gvsig / fmap / mapcontext / rendering / legend / VectorialIntervalLegend.java @ 21144

History | View | Annotate | Download (7.73 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 org.gvsig.fmap.mapcontext.rendering.legend;
42

    
43
import java.awt.Color;
44

    
45
import org.gvsig.fmap.mapcontext.layers.XMLException;
46
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
47
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
48

    
49
import com.iver.utiles.StringUtilities;
50
import com.iver.utiles.XMLEntity;
51

    
52

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

    
74
    /**
75
     * Constructor method
76
     *
77
     * @param type type of the shape.
78
     */
79
    public VectorialIntervalLegend(int type) {
80
        setShapeType(type);
81
    }
82

    
83
    public XMLEntity getXMLEntity() {
84
        XMLEntity xml = new XMLEntity();
85
        xml.putProperty("className", this.getClass().getName());
86
        xml.putProperty("useDefaultSymbolB", useDefaultSymbol);
87
        if (getDefaultSymbol() == null) {
88
            xml.putProperty("useDefaultSymbol", 0);
89
        } else {
90
            xml.putProperty("useDefaultSymbol", 1);
91
            xml.addChild(getDefaultSymbol().getXMLEntity());
92
        }
93

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

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

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

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

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

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

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

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

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

    
125

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

    
130
        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.
131
            intervalType = xml.getIntProperty("intervalType");
132
        }
133
        useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbolB");
134
        int hasDefaultSymbol = xml.getIntProperty("useDefaultSymbol");
135

    
136
        if (hasDefaultSymbol == 1) {
137
            setDefaultSymbol(SymbologyFactory.createSymbolFromXML(xml.getChild(0), null));
138
        } else {
139
            setDefaultSymbol(null);
140
        }
141

    
142
        int numKeys = xml.getIntProperty("numKeys");
143

    
144
        if (numKeys > 0) {
145
            String[] sk = xml.getStringArrayProperty("keys");
146
            IInterval auxInterval;
147

    
148
            for (int i = 0; i < numKeys; i++) {
149
                auxInterval = FInterval.create(sk[i]);
150
                symbols.put(auxInterval,
151
                                SymbologyFactory.createSymbolFromXML(xml.getChild(i + hasDefaultSymbol), null));
152
                keys.add(auxInterval);
153
                System.out.println("auxInterval =" + auxInterval + "Symbol =" +
154
                                SymbologyFactory.createSymbolFromXML(xml.getChild(i + hasDefaultSymbol), null)
155
                           .getDescription());
156
            }
157
        }
158

    
159
        startColor = StringUtilities.string2Color(xml.getStringProperty(
160
                    "startColor"));
161
        endColor = StringUtilities.string2Color(xml.getStringProperty(
162
                    "endColor"));
163
    }
164

    
165

    
166
    public ILegend cloneLegend() throws XMLException {
167
        return LegendFactory.createFromXML(getXMLEntity());
168
    }
169

    
170
    /**
171
         * Returns the final color
172
         * @return Color  final color.
173
         * @uml.property  name="endColor"
174
         */
175
    public Color getEndColor() {
176
        return endColor;
177
    }
178

    
179
    /**
180
         * Inserts the final color.
181
         * @param endColor final color.
182
         * @uml.property  name="endColor"
183
         */
184
    public void setEndColor(Color endColor) {
185
        this.endColor = endColor;
186
    }
187

    
188
    /**
189
         * Returns the initial color.
190
         * @return  Color initial color.
191
         * @uml.property  name="startColor"
192
         */
193
    public Color getStartColor() {
194
        return startColor;
195
    }
196

    
197
    /**
198
         * Inserts the initial color
199
         * @param startColor initial color.
200
         * @uml.property  name="startColor"
201
         */
202
    public void setStartColor(Color startColor) {
203
        this.startColor = startColor;
204
    }
205

    
206
        public int getShapeType() {
207
                return shapeType;
208
        }
209

    
210
        public void setShapeType(int shapeType) {
211
                if (this.shapeType != shapeType) {
212
                        setDefaultSymbol(SymbologyFactory.
213
                                        createDefaultSymbolByShapeType(shapeType));
214
                        this.shapeType = shapeType;
215
                }
216
        }
217

    
218

    
219
        public String getClassName() {
220
                return getClass().getName();
221
        }
222

    
223
        //
224
        /**
225
         * <p>This method does not exist officially. But what it does is just apply the
226
         * values of a VectorialIntervalLegend to other so, It is not needed to
227
         * replicate the code of populating the legend in every panel that produces
228
         * a legend by intervals.<br>
229
         * </p>
230
         * <p>
231
         * ok this method is messy, but it's useful ;-)
232
         * </p>
233
         */
234
        public static void initializeVectorialIntervalLegend(
235
                        AbstractIntervalLegend srcLegend,
236
                        AbstractIntervalLegend targetLegend) {
237
                targetLegend.shapeType        = srcLegend.shapeType;
238
                targetLegend.symbols          = srcLegend.symbols;
239
                targetLegend.keys             = srcLegend.keys;
240
                targetLegend.index            = srcLegend.index;
241
                targetLegend.setClassifyingFieldNames(srcLegend.getClassifyingFieldNames());
242
                targetLegend.setClassifyingFieldTypes(srcLegend.getClassifyingFieldTypes());
243
                targetLegend.fieldId          = srcLegend.fieldId;
244
                targetLegend.defaultSymbol    = srcLegend.defaultSymbol;
245
                targetLegend.featureStore       = srcLegend.featureStore;
246
                targetLegend.intervalType     = srcLegend.intervalType;
247
                targetLegend.useDefaultSymbol = srcLegend.useDefaultSymbol;
248
        }
249

    
250
}