Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_907 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / SingleSymbolLegend.java @ 11015

History | View | Annotate | Download (9.14 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.util.ArrayList;
44

    
45
import com.hardcode.gdbms.engine.data.DataSource;
46
import com.iver.cit.gvsig.fmap.core.FShape;
47
import com.iver.cit.gvsig.fmap.core.IFeature;
48
import com.iver.cit.gvsig.fmap.core.ISLDCompatible;
49
import com.iver.cit.gvsig.fmap.core.ISymbol;
50
import com.iver.cit.gvsig.fmap.core.SLDTags;
51
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
52
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
53
import com.iver.cit.gvsig.fmap.layers.XMLException;
54
import com.iver.utiles.XMLEntity;
55

    
56

    
57
/**
58
 * Leyenda se s?mbolo ?nico.
59
 *
60
 * @author Vicente Caballero Navarro
61
 */
62
public class SingleSymbolLegend implements VectorialLegend, Legend{
63
        private ISymbol defaultSymbol;
64
        private String labelFieldName = null;
65
        private String labelFieldHeight = null;
66
        private String labelFieldRotation = null;
67
        private int shapeType = FShape.POLYGON; // Por defecto, tipo pol?gono
68
        // private boolean bWithHeightText;
69
        /**
70
         * Crea un nuevo SingleSymbolLegend.
71
         *
72
         * @param style S?mbolo.
73
         */
74
        public SingleSymbolLegend(FSymbol style) {
75
                defaultSymbol = style;
76
        }
77

    
78
        /**
79
         * Crea un nuevo SingleSymbolLegend.
80
         */
81
        public SingleSymbolLegend() {
82
                defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
83
        }
84

    
85
        /**
86
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDefaultSymbol(ISymbol)
87
         */
88
        public void setDefaultSymbol(ISymbol s) {
89
                setShapeType(s.getSymbolType());
90
                defaultSymbol = s;
91
        }
92

    
93
        /**
94
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelField()
95
         */
96
        public String getLabelField() {
97
                return labelFieldName;
98
        }
99

    
100
        /**
101
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setLabelField(int)
102
         */
103
        public void setLabelField(String fieldName) {
104
                labelFieldName = fieldName;
105
        }
106

    
107
        /**
108
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getSymbol(int)
109
         */
110
        public ISymbol getSymbol(int recordIndex) {
111
                return defaultSymbol;
112
        }
113

    
114
        /**
115
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
116
         */
117
        public ISymbol getDefaultSymbol() {
118
                return defaultSymbol;
119
        }
120

    
121
        /**
122
         * creates the SLD String that defines this legend type.
123
         */
124
        public String getSLDString(String layerName)
125
        {                
126
//                StyledLayerDescriptor sld = new StyledLayerDescriptor();
127
//                StyleFactory styleFactory = StyleFactory.createStyleFactory();
128
//                Style style = styleFactory.createStyle();
129
//                style.setName("default");                
130
//            try{                    
131
//                        FeatureTypeStyle featStyle = styleFactory.createFeatureTypeStyle();                        
132
//                        Symbolizer[] theSymbolizers = new Symbolizer[1];                        
133
//                        theSymbolizers[0] = SDLUtils.toGeotoolsSymbol(this.defaultSymbol);                                                
134
//                        Rule myRule = styleFactory.createRule();
135
//                        myRule.setSymbolizers(theSymbolizers);
136
//                        featStyle.addRule(myRule);
137
//                        style.addFeatureTypeStyle(featStyle);                                        
138
//                        NamedLayer namedLayer = new NamedLayer();
139
//                        namedLayer.setName("comunidades");
140
//                        namedLayer.addStyle(style);                        
141
//                        sld.addStyledLayer(namedLayer);                        
142
//                        SLDTransformer st = new SLDTransformer();
143
//                        String result =  st.transform(style);
144
//                        return result;                                
145
//            }catch(Exception e) {
146
//                    e.printStackTrace(); 
147
//                    return null;
148
//            }
149
                try{
150
                        XmlBuilder xmlBuilder = new XmlBuilder();
151
                        xmlBuilder.writeHeader();
152
                        xmlBuilder.openTag(SLDTags.SLD_ROOT, SLDTags.VERSION_ATTR, SLDTags.VERSION_1_0_0);
153
                        xmlBuilder.openTag(SLDTags.NAMEDLAYER);
154
                        xmlBuilder.writeTag(SLDTags.NAME,layerName);
155
                        xmlBuilder.openTag(SLDTags.USERSTYLE);
156
                        xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE);
157
                        xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"FeatureTypeName");
158
                        xmlBuilder.openTag(SLDTags.RULE);
159
                        if (this.defaultSymbol instanceof ISLDCompatible)
160
                        {
161
                                ISLDCompatible symSLD = (ISLDCompatible) this.defaultSymbol;
162
                                xmlBuilder.writeRaw(symSLD.toSLD());
163
                        }
164
                        else
165
                                throw new RuntimeException("Cannot convert default Symbol " + this.defaultSymbol.getDescription() + " to SLD");
166

    
167
                        xmlBuilder.closeTag();
168
                        xmlBuilder.closeTag();
169
                        xmlBuilder.closeTag();
170
                        xmlBuilder.closeTag();
171
                        xmlBuilder.closeTag();
172
                        return xmlBuilder.getXML(); 
173
                }
174
                catch(Exception e)
175
                {
176
                        e.printStackTrace();
177
                        return null;
178
                }                
179
        }        
180
        
181
        /**
182
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
183
         */
184
        public XMLEntity getXMLEntity() {
185
                XMLEntity xml = new XMLEntity();
186
                xml.putProperty("className",this.getClass().getName());
187
                xml.putProperty("labelFieldName", labelFieldName);
188
        xml.putProperty("labelHeightFieldName", labelFieldHeight);
189
        xml.putProperty("labelRotationFieldName", labelFieldRotation);
190
                xml.addChild(defaultSymbol.getXMLEntity());
191

    
192
                return xml;
193
        }
194

    
195
        /**
196
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setXMLEntity()
197
         */
198
        public void setXMLEntity03(XMLEntity xml) {
199
                setLabelField(xml.getStringProperty("labelFieldName"));
200

    
201
                FSymbol auxSym = FSymbol.createFromXML03(xml.getChild(0));
202
                setDefaultSymbol(auxSym);
203
        }
204

    
205
        /**
206
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setXMLEntity()
207
         */
208
        public void setXMLEntity(XMLEntity xml) {
209
                setLabelField(xml.getStringProperty("labelFieldName"));
210
        if (xml.contains("labelHeightFieldName"))
211
            setLabelHeightField(xml.getStringProperty("labelHeightFieldName"));
212
        if (xml.contains("labelRotationFieldName"))
213
            setLabelRotationField(xml.getStringProperty("labelRotationFieldName"));
214

    
215
                FSymbol auxSym = FSymbol.createFromXML(xml.getChild(0));
216
                setDefaultSymbol(auxSym);
217
        }
218

    
219
        /**
220
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
221
         */
222
        public Legend cloneLegend() throws XMLException {
223
                return (Legend) LegendFactory.createFromXML(getXMLEntity());
224
        }
225

    
226
        /* (non-Javadoc)
227
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
228
         */
229
        public void setDataSource(DataSource ds) {
230
                // No hacemos nada, no lo vamos a usar
231
        }
232

    
233
        /* (non-Javadoc)
234
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getShapeType()
235
         */
236
        public int getShapeType() {
237
                return shapeType;
238
        }
239

    
240
        /* (non-Javadoc)
241
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setShapeType(int)
242
         */
243
        public void setShapeType(int shapeType) {
244
                if (this.shapeType != shapeType) {
245
                        switch (shapeType) {
246
                                case FShape.POINT:
247
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
248

    
249
                                        break;
250

    
251
                                case FShape.LINE:
252
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
253

    
254
                                        break;
255

    
256
                                case FShape.POLYGON:
257
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
258

    
259
                                        break;
260
                        }
261

    
262
                        this.shapeType = shapeType;
263
                }
264
        }
265

    
266
        /* (non-Javadoc)
267
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
268
         */
269
        public String getLabelHeightField() {
270
                return labelFieldHeight;
271
        }
272

    
273
        /**
274
         * Inserta el texto que refleja el alto del campo.
275
         *
276
         * @param str String.
277
         */
278
        public void setLabelHeightField(String str) {
279
                labelFieldHeight = str;
280
        }
281

    
282
        /* (non-Javadoc)
283
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
284
         */
285
        public String getLabelRotationField() {
286
                return labelFieldRotation;
287
        }
288

    
289
        /**
290
         * Inserta el texto que refleja la rotaci?n del campo.
291
         *
292
         * @param str String.
293
         */
294
        public void setLabelRotationField(String str) {
295
                labelFieldRotation = str;
296
        }
297

    
298
    public ISymbol getSymbolByFeature(IFeature feat) {
299
        return defaultSymbol;
300
    }
301

    
302
        public void useDefaultSymbol(boolean b) {
303
                // TODO Auto-generated method stub
304
        }
305

    
306
    /* (non-Javadoc)
307
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getUsedFields()
308
     */
309
    public String[] getUsedFields() {
310
        ArrayList usedFields = new ArrayList();
311
        if (getLabelField() != null)
312
            usedFields.add(getLabelField());
313
        if (getLabelHeightField() != null)
314
            usedFields.add(getLabelHeightField());
315
        if (getLabelRotationField() != null)
316
            usedFields.add(getLabelRotationField());
317
                
318
        return (String[]) usedFields.toArray(new String[0]);
319
    }
320

    
321
        /* public boolean isBWithHeightText() {
322
                return bWithHeightText;
323
        }
324

325
        public void setBWithHeightText(boolean withHeightText) {
326
                bWithHeightText = withHeightText;
327
        } */
328
}