Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / QuantityByCategoryLegend.java @ 13606

History | View | Annotate | Download (8.78 KB)

1
package com.iver.cit.gvsig.fmap.rendering;
2

    
3
import java.util.ArrayList;
4
import java.util.logging.Logger;
5

    
6
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
7
import com.hardcode.gdbms.engine.data.DataSource;
8
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
9
import com.iver.cit.gvsig.fmap.core.FShape;
10
import com.iver.cit.gvsig.fmap.core.IFeature;
11
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
12
import com.iver.cit.gvsig.fmap.core.styles.SimpleMarkerFillPropertiesStyle;
13
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
14
import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol;
15
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
16
import com.iver.cit.gvsig.fmap.core.symbols.MarkerFillSymbol;
17
import com.iver.cit.gvsig.fmap.layers.XMLException;
18
import com.iver.utiles.XMLEntity;
19
/**
20
 * @author jaume dominguez faus - jaume.dominguez@iver.es
21
 */
22
public class QuantityByCategoryLegend implements IClassifiedVectorialLegend  {
23
        private GraduatedSymbolLegend graduatedSymbol = new GraduatedSymbolLegend();
24
        private VectorialIntervalLegend colorRamp = new VectorialIntervalLegend();
25
        private ISymbol defaultSymbol;
26
        private int shapeType;
27
        private boolean isUseDefaultSymbol;
28

    
29
    public void clear() {
30
        colorRamp.clear();
31
        graduatedSymbol.clear();
32
    }
33

    
34
    public String[] getFieldNames() {
35
            ArrayList l = new ArrayList();
36
            for (int i = 0; i < graduatedSymbol.getFieldNames().length; i++) {
37
                        l.add(graduatedSymbol.getFieldNames()[i]);
38
                }
39

    
40
            for (int i = 0; i < colorRamp.getFieldNames().length; i++) {
41
                        l.add(colorRamp.getFieldNames()[i]);
42
                }
43
            return (String[]) l.toArray(new String[l.size()]);
44
    }
45

    
46
    /**
47
     * Sets the field names required to build this legend. In this case
48
     * fieldNames is an array of length 2 where the first element is
49
     * the field name for the embedded GraduatedSymbolLegend, and the
50
     * second is the field name for the embedded colorRamp (VectorialIntervalLegend)
51
     * legend.
52
     */
53
    public void setFieldNames(String[] fieldNames) {
54

    
55
            if (fieldNames.length!=2) {
56

    
57
            }
58
        colorRamp.setFieldNames(new String[] {fieldNames[1]});
59
        graduatedSymbol.setFieldNames(new String[] {fieldNames[0]});
60
    }
61

    
62
    public void addSymbol(Object key, ISymbol symbol) {
63
            System.out.println("adding "+key+"["+symbol+"]");
64
//        // TODO Implement it
65
//        throw new Error("Not yet implemented!");
66
    }
67

    
68
    public void delSymbol(Object key) {
69
            colorRamp.delSymbol(key);
70
            graduatedSymbol.delSymbol(key);
71
    }
72

    
73
    public String[] getDescriptions() {
74
            String[] desc1 = colorRamp.getDescriptions();
75
            String[] desc2 = graduatedSymbol.getDescriptions();
76

    
77
            String[] descriptions = new String[desc1.length + desc2.length];
78
            for (int i = 0; i < descriptions.length; i++) {
79
                    descriptions[i] = (i <desc1.length) ? desc1[i] : desc2[i % desc1.length];
80
                }
81
            return descriptions;
82
    }
83

    
84
    public ISymbol[] getSymbols() {
85
            ISymbol[] symbols1 = colorRamp.getSymbols();
86
            ISymbol[] symbols2 = graduatedSymbol.getSymbols();
87

    
88
            ISymbol[] symbols = new ISymbol[symbols1.length + symbols2.length];
89
            for (int i = 0; i < symbols.length; i++) {
90
                    symbols[i] = (i < symbols1.length) ? symbols1[i] : symbols2[i % symbols1.length];
91
                }
92
            return symbols;
93
    }
94

    
95
    public Object[] getValues() {
96
            Object[] objects1 = colorRamp.getValues();
97
            Object[] objects2 = graduatedSymbol.getValues();
98

    
99
            Object[] objects = new FInterval[objects1.length + objects2.length];
100
            for (int i = 0; i < objects.length; i++) {
101
                    objects[i] = (i < objects1.length) ? objects1[i] : objects2[i % objects1.length];
102
                }
103
            return objects;
104
    }
105

    
106
    public ISymbol getDefaultSymbol() {
107
            return defaultSymbol;
108
    }
109

    
110
    public XMLEntity getXMLEntity() {
111
        XMLEntity xml = new XMLEntity();
112
        xml.putProperty("className", getClass().getName());
113
        xml.putProperty("shapeType", shapeType);
114
        xml.putProperty("isUseDefaultSymbol", isUseDefaultSymbol);
115
        xml.addChild(graduatedSymbol.getXMLEntity());
116

    
117
        xml.addChild(colorRamp.getXMLEntity());
118
        if (defaultSymbol != null)
119
                xml.addChild(defaultSymbol.getXMLEntity());
120
        return xml;
121
    }
122

    
123
    public String getSLDString(String layerName) {
124
        // TODO Implement it
125
        throw new Error("Not yet implemented!");
126

    
127
    }
128
    public ILegend cloneLegend() throws XMLException {
129
            return (ILegend)LegendFactory.createFromXML(getXMLEntity());
130

    
131
    }
132

    
133
        public GraduatedSymbolLegend getGraduatedSymbolLegend() {
134
                return graduatedSymbol;
135
        }
136

    
137
        public VectorialIntervalLegend getColorRampLegend() {
138
                return colorRamp;
139
        }
140

    
141
        public void setDataSource(DataSource ds) throws FieldNotFoundException, ReadDriverException {
142
                graduatedSymbol.setDataSource(ds);
143
                colorRamp.setDataSource(ds);
144
        }
145

    
146
        public ISymbol getSymbol(int i) throws ReadDriverException {
147
                IMarkerSymbol sym1 = (IMarkerSymbol) graduatedSymbol.getSymbol(i);
148
                ISymbol sym2 =  colorRamp.getSymbol(i);
149
                IMultiLayerSymbol multiSym = null;
150
                switch (shapeType) {
151
                case FShape.POLYGON:
152
                        /*
153
                         * symbol from the GraduatedSymbolLegend is a marker, but
154
                         * what we need is a fill symbol. Will use a MarkerFillSymbol
155
                         * to enable support for Polygons
156
                         */
157
                        MarkerFillSymbol aux = new MarkerFillSymbol();
158
                        // tell the fill style to draw the IMarkerSymbol
159
                        // as a IFillSymbol centering it in the shape polygon
160
                        // centroid and applying offset (if any).
161
                        aux.setMarker(sym1);
162
                        SimpleMarkerFillPropertiesStyle p = new SimpleMarkerFillPropertiesStyle();
163
                        p.setFillStyle(SimpleMarkerFillPropertiesStyle.SINGLE_CENTERED_SYMBOL);
164
                        aux.setMarkerFillProperties(p);
165

    
166
                        multiSym = SymbologyFactory.
167
                                createEmptyMultiLayerSymbol(FShape.POLYGON);
168
                        multiSym.addLayer(sym2);
169
                        multiSym.addLayer(aux);
170
                break;
171
                case FShape.LINE:
172
                        throw new Error("Shape type not yet supported");
173
                default:
174
                        throw new Error("Unsupported shape type");
175

    
176
                }
177

    
178
                return multiSym;
179
        }
180

    
181
        public ISymbol getSymbolByFeature(IFeature feat) {
182
                ISymbol sym1 = (ISymbol) graduatedSymbol.getSymbolByFeature(feat);
183
                ISymbol sym2 = colorRamp.getSymbolByFeature(feat);
184
                IMultiLayerSymbol multiSym = null;
185
                switch (shapeType) {
186
                case FShape.POLYGON:
187

    
188

    
189
                        multiSym = SymbologyFactory.
190
                        createEmptyMultiLayerSymbol(FShape.POLYGON);
191
                        multiSym.addLayer(sym2);
192
                        multiSym.addLayer(sym1);
193
                break;
194
                case FShape.LINE:
195
                        throw new Error("Shape type not yet supported");
196
                default:
197
                        throw new Error("Unsupported shape type");
198

    
199
                }
200

    
201
                return multiSym;
202
        }
203

    
204
        public int getShapeType() {
205
                return shapeType;
206
        }
207

    
208
        public void setShapeType(int shapeType) {
209
                this.shapeType = shapeType;
210
                graduatedSymbol.setShapeType(FShape.POINT);
211
                colorRamp.setShapeType(shapeType);
212
        }
213

    
214
        public void setDefaultSymbol(ISymbol s) throws IllegalArgumentException {
215
                if (s == null) throw new NullPointerException("Default symbol cannot be null");
216
                this.defaultSymbol = s;
217
        }
218

    
219
        public void setXMLEntity(XMLEntity xml) {
220
                shapeType = xml.getIntProperty("shapeType");
221
        isUseDefaultSymbol = xml.getBooleanProperty("isUseDefaultSymbol");
222
        try {
223
                        graduatedSymbol = (GraduatedSymbolLegend) LegendFactory.createFromXML(xml.getChild(0));
224
                        colorRamp = (VectorialIntervalLegend) LegendFactory.createFromXML(xml.getChild(1));
225
        } catch (XMLException e) {
226
                        // TODO Auto-generated catch block
227
                Logger.getAnonymousLogger().severe(e.getFormatString());
228
                }
229

    
230
        if (defaultSymbol != null)
231
                xml.addChild(defaultSymbol.getXMLEntity());
232
        }
233

    
234
        public void setXMLEntity03(XMLEntity xml) {
235
                // nothing to do here
236
        }
237

    
238
        public boolean isUseDefaultSymbol() {
239
                return isUseDefaultSymbol;
240
        }
241

    
242
        public void useDefaultSymbol(boolean b) {
243
                this.isUseDefaultSymbol = b;
244
        }
245

    
246
        public String[] getUsedFields() {
247
                // TODO Implement it
248
                throw new Error("Not yet implemented!");
249

    
250
        }
251

    
252
        public void setGraduateSymbolLegend(ILegend legend) {
253
                this.graduatedSymbol = (GraduatedSymbolLegend) legend;
254
        }
255

    
256
        public void setColorRampLegend(ILegend legend) {
257
                this.colorRamp = (VectorialIntervalLegend) legend;
258
        }
259

    
260
        public ZSort getZSort() {
261
                // TODO Implement it
262
                throw new Error("Not yet implemented!");
263

    
264
        }
265
        public void setZSort(ZSort zSort) {
266
                // TODO Implement it
267
                throw new Error("Not yet implemented!");
268

    
269
        }
270

    
271
        public String getClassName() {
272
                return getClass().getName();
273
        }
274

    
275
        public void replace(ISymbol oldSymbol, ISymbol newSymbol) {
276
                ISymbol[] symbols;
277
                // look first in the graduated symbol legend
278
                symbols = graduatedSymbol.getSymbols();
279
                
280
                for (int i = 0; i < symbols.length; i++) {
281
                        if (symbols[i].equals(oldSymbol)) {
282
                                graduatedSymbol.replace(oldSymbol, newSymbol);
283
                                return;
284
                        }
285
                }
286
                
287
                // if the symbol wasn't found yet, proceed with color ramp
288
                symbols = colorRamp.getSymbols();
289
                
290
                for (int i = 0; i < symbols.length; i++) {
291
                        if (symbols[i].equals(oldSymbol)) {
292
                                colorRamp.replace(oldSymbol, newSymbol);
293
                                return;
294
                        }
295
                }
296
                
297
        }
298

    
299
}