Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / VectorialUniqueValueLegend.java @ 560

History | View | Annotate | Download (9.46 KB)

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

    
3
import java.util.ArrayList;
4
import java.util.Comparator;
5
import java.util.TreeMap;
6

    
7
import com.hardcode.gdbms.engine.data.DataSource;
8
import com.hardcode.gdbms.engine.data.DriverException;
9
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
10
import com.hardcode.gdbms.engine.instruction.IncompatibleTypesException;
11
import com.hardcode.gdbms.engine.instruction.SemanticException;
12
import com.hardcode.gdbms.engine.values.BooleanValue;
13
import com.hardcode.gdbms.engine.values.Value;
14
import com.hardcode.gdbms.engine.values.ValueFactory;
15
import com.iver.cit.gvsig.fmap.core.FShape;
16
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
17
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
18
import com.iver.cit.gvsig.fmap.layers.XMLException;
19
import com.iver.utiles.XMLEntity;
20

    
21

    
22
/**
23
 * DOCUMENT ME!
24
 *
25
 * @author Vicente Caballero Navarro
26
 */
27
public class VectorialUniqueValueLegend implements UniqueValueLegend,
28
    VectorialLegend {
29
    private TreeMap symbols = new TreeMap(
30
            new Comparator() {
31
                    public int compare(Object o1, Object o2)
32
                    {
33
                            int resul = -1;
34
                                if ((o1 != null) && (o2 != null))
35
                                {
36
                                        Value v2 = (Value) o2;
37
                                        Value v1 = (Value)o1;
38
                                        BooleanValue boolVal;
39
                                        try {
40
                                                boolVal = (BooleanValue) (v1.greater(v2));
41
                                                if (boolVal.getValue())
42
                                                        return 1;
43
                                                boolVal = (BooleanValue) (v1.less(v2));
44
                                                if (boolVal.getValue())
45
                                                        return -1;
46
                                        } catch (IncompatibleTypesException e) {
47
                                                // TODO Auto-generated catch block
48
                                                e.printStackTrace();
49
                                        }
50
                                }
51
                                return 0;
52
                        }
53
                }                
54
    ); // Para poder ordenar
55
    private ArrayList keys = new ArrayList(); // En lugar de un HashSet, para tener acceso por ?ndice
56
    private String fieldName;
57
    private int fieldId = -1;
58
    private String labelFieldName;
59
    private FSymbol defaultSymbol;
60
    private DataSource dataSource;
61
    
62
    public VectorialUniqueValueLegend(){
63
            defaultSymbol=LegendFactory.DEFAULT_POLYGON_SYMBOL;
64
    }
65
    public void setShapeType(int shapeType){
66
            switch (shapeType)
67
                {
68
                        case FShape.POINT:
69
                                defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
70
                                break;
71
                        case FShape.LINE:
72
                                defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
73
                                break;
74
                        case FShape.POLYGON:
75
                                defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
76
                                break;
77
                        
78
                }
79
    }
80
    /**
81
         * 
82
         */
83
        public VectorialUniqueValueLegend(int shapeType) {
84

    
85
                switch (shapeType)
86
                {
87
                        case FShape.POINT:
88
                                defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
89
                                break;
90
                        case FShape.LINE:
91
                                defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
92
                                break;
93
                        case FShape.POLYGON:
94
                                defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
95
                                break;
96
                        
97
                }
98

    
99
        }
100
        
101
    /**
102
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#setValueSymbolByID(int,
103
     *      FSymbol)
104
     */
105
    public void setValueSymbolByID(int id, FSymbol symbol) {
106
            symbols.put(keys.get(id), symbol);
107
    }
108
    /**
109
     * Devuelve un s?mbolo a partir del ID. Mira en el m_ArrayKeys  el elemento
110
     * ID, y con esa clave recupera el FSymbol por valor
111
     *
112
     * @param ID ID.
113
     *
114
     * @return s?mbolo que corresponde a ese ID.
115
     */
116
    /*
117
    public FSymbol getSymbolByID(int ID) {
118
        return (FSymbol) symbols.get(keys.get(ID));
119
    }
120
*/
121
    /**
122
     * Se usa en la tabla que muestra una leyenda.
123
     *
124
     * @param id
125
     * @param symbol
126
     */
127
    public void setValueSymbol(int id, FSymbol symbol) {
128
        symbols.put(keys.get(id), symbol);
129
    }
130
    
131
    /**
132
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getValues()
133
     */
134
    public Object[] getValues() {
135
        return symbols.keySet().toArray(new Object[0]);
136
    }
137

    
138

    
139
    /**
140
     * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#addSymbol(java.lang.Object,
141
     *      FSymbol)
142
     */
143
    public void addSymbol(Object key, FSymbol symbol) {
144
        Object resul;
145
        resul = symbols.put(key, symbol);
146

    
147
        if (resul != null) {
148
            System.err.println("Error: la clave " + key +
149
                " ya exist?a. Resul = " + resul);
150
            System.err.println("symbol nuevo:" + symbol.m_Descrip +
151
                " Sviejo= " + ((FSymbol) resul).m_Descrip);
152
        } else {
153
            keys.add(key);
154
        }
155
    }
156

    
157
    /**
158
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#clear()
159
     */
160
    public void clear() {
161
        keys.clear();
162
        symbols.clear();
163
    }
164

    
165
    /**
166
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getDescriptions()
167
     */
168
    public String[] getDescriptions() {
169
            String[] descriptions = new String[symbols.size()];
170
            FSymbol[] auxSym = getSymbols();
171
            for (int i=0; i< descriptions.length; i++)
172
                    descriptions[i] = auxSym[i].m_Descrip;
173
        return descriptions;
174
    }
175

    
176
    /**
177
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getSymbols()
178
     */
179
    public FSymbol[] getSymbols() {
180
        return (FSymbol[]) symbols.values().toArray(new FSymbol[0]);
181
    }
182

    
183
    /**
184
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#getFieldName()
185
     */
186
    public String getFieldName() {
187
        return fieldName;
188
    }
189

    
190
    /**
191
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#setDefaultSymbol(com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D)
192
     */
193
    public void setDefaultSymbol(FSymbol s) {
194
        defaultSymbol = s;
195
    }
196

    
197
    /**
198
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getLabelField()
199
     */
200
    public String getLabelField() {
201
        return labelFieldName;
202
    }
203

    
204
    /**
205
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#setLabelField(int)
206
     */
207
    public void setLabelField(String fieldName) {
208
        labelFieldName = fieldName;
209
    }
210

    
211
    /**
212
     * @see com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend#setField()
213
     */
214
    public void setFieldName(String str) {
215
        fieldName = str;
216
    }
217

    
218
    /**
219
     * @throws DriverException
220
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getSymbol(int)
221
     */
222
    public FSymbol getSymbol(int recordIndex) throws DriverException {
223
            Value val = dataSource.getFieldValue(recordIndex,fieldId);
224
            FSymbol theSymbol = getSymbolByValue(val);
225
            if (theSymbol != null)
226
                    return theSymbol;
227
            else return getDefaultSymbol();
228
    }
229

    
230
    /**
231
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
232
     */
233
    public FSymbol getDefaultSymbol() {
234
        return defaultSymbol;
235
    }
236

    
237
    /**
238
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
239
     */
240
    public XMLEntity getXMLEntity() {
241
        XMLEntity xml = new XMLEntity();
242
        xml.putProperty("nameClass",this.getClass().getName());
243
        xml.putProperty("fieldName", fieldName);
244
        xml.putProperty("labelfield", labelFieldName);
245
        if (getDefaultSymbol() == null)
246
        {
247
                xml.putProperty("useDefaultSymbol", false);
248
        }
249
        else
250
        {
251
                xml.putProperty("useDefaultSymbol", true);
252
                xml.addChild(getDefaultSymbol().getXMLEntity());
253
        }
254
        xml.putProperty("numKeys", keys.size());
255
        if (keys.size() > 0)
256
        {                        
257
                        xml.putProperty("tipoValueKeys", keys.get(0).getClass().getName());
258
                String[] sk = new String[keys.size()];
259
        
260
                for (int i = 0; i < keys.size(); i++) {
261
                    sk[i] = ((Value) keys.get(i)).toString();
262
                }
263
                xml.putProperty("keys", sk);
264
                        
265
                FSymbol[] fsymbols=getSymbols();
266
                for (int i = 0; i < keys.size(); i++) {
267
                    xml.addChild(fsymbols[i].getXMLEntity());
268
                }
269
        }
270
        return xml;
271
    }
272

    
273
    /**
274
     * DOCUMENT ME!
275
     *
276
     * @param xml DOCUMENT ME!
277
     *
278
     * @return DOCUMENT ME!
279
     */
280
    public void setXMLEntity(XMLEntity xml) {
281
            clear();
282
            setFieldName(xml.getStringProperty("fieldName"));
283
            setLabelField(xml.getStringProperty("labelfield"));
284
            boolean useDefaultSymbol = xml.getBooleanProperty("useDefaultSymbol");
285
            if (useDefaultSymbol)
286
                    setDefaultSymbol(FSymbol.createFromXML(xml.getChild(0)));
287
            else
288
                    setDefaultSymbol(null);
289
            int numKeys = xml.getIntProperty("numKeys");
290
            if (numKeys > 0)
291
            {
292
                    String className = xml.getStringProperty("tipoValueKeys");
293
                    String[] sk = xml.getStringArrayProperty("keys");
294
                    Value auxValue;
295
            for (int i = 0; i < numKeys; i++) {
296
                    try {
297
                                        auxValue = ValueFactory.createValue(sk[i], className);
298
                                        symbols.put(auxValue, FSymbol.createFromXML(xml.getChild(i+1)));
299
                                } catch (SemanticException e) {
300
                                        // TODO Auto-generated catch block
301
                                        e.printStackTrace();
302
                                }                    
303
            }
304
                    
305
            }
306
                    
307
            
308
            
309
    }
310

    
311
    /**
312
     * @throws IllegalAccessException
313
     * @throws InstantiationException
314
     * @throws ClassNotFoundException
315
     * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
316
     */
317
    public Legend cloneLegend() throws XMLException {
318
        return (Legend)LegendFactory.createFromXML(getXMLEntity());
319
    }
320
        /* (non-Javadoc)
321
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
322
         */
323
        public void setDataSource(DataSource ds) throws FieldNotFoundException, DriverException {
324
                dataSource = ds;
325
                ds.start();
326
                fieldId = ds.getFieldIndexByName(fieldName);
327
                ds.stop();
328
                
329
        }
330

    
331
        /* (non-Javadoc)
332
         * @see com.iver.cit.gvsig.fmap.rendering.UniqueValueLegend#getSymbolByValue(com.hardcode.gdbms.engine.values.Value)
333
         */
334
        public FSymbol getSymbolByValue(Value key) {
335
                if (symbols.containsKey(key))
336
                        return (FSymbol) symbols.get(key);
337
                return null;
338
        }
339
}