Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / rendering / SingleSymbolLegend.java @ 10832

History | View | Annotate | Download (7.98 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.SLDTags;
50
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
51
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
52
import com.iver.cit.gvsig.fmap.core.symbols.SimpleFillSymbol;
53
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
54
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
55
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
56
import com.iver.cit.gvsig.fmap.layers.XMLException;
57
import com.iver.utiles.XMLEntity;
58

    
59

    
60
/**
61
 * Leyenda se s?mbolo ?nico.
62
 * @author   Vicente Caballero Navarro
63
 */
64
public class SingleSymbolLegend implements VectorialLegend {
65
        private ISymbol defaultSymbol;
66
        private String labelFieldName = null;
67
        private String labelFieldHeight = null;
68
        private String labelFieldRotation = null;
69
        /**
70
         * @uml.property  name="shapeType"
71
         */
72
        private int shapeType = FShape.POLYGON; // Por defecto, tipo pol?gono
73
        // private boolean bWithHeightText;
74

    
75

    
76
        public SingleSymbolLegend() {        }
77

    
78

    
79
        /**
80
         * Crea un nuevo SingleSymbolLegend.
81
         *
82
         * @param style S?mbolo.
83
         */
84
        public SingleSymbolLegend(ISymbol style) {
85
                defaultSymbol = style;
86
        }
87

    
88
        /*
89
        /**
90
         * Crea un nuevo SingleSymbolLegend.
91
         * /
92
        public SingleSymbolLegend() {
93
                defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
94
        }*/
95

    
96
        public void setDefaultSymbol(ISymbol s) {
97
                setShapeType(s.getSymbolType());
98
                defaultSymbol = s;
99
        }
100

    
101
        /**
102
         * @deprecated
103
         */
104
        public String getLabelField() {
105
                return labelFieldName;
106
        }
107

    
108
        /**
109
         * @deprecated
110
         */
111
        public void setLabelField(String fieldName) {
112
                labelFieldName = fieldName;
113
        }
114

    
115
        public ISymbol getSymbol(int recordIndex) {
116
                return defaultSymbol;
117
        }
118

    
119
        public ISymbol getDefaultSymbol() {
120
                return defaultSymbol;
121
        }
122

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

    
166
                        xmlBuilder.closeTag();
167
                        xmlBuilder.closeTag();
168
                        xmlBuilder.closeTag();
169
                        xmlBuilder.closeTag();
170
                        xmlBuilder.closeTag();
171
                        return xmlBuilder.getXML();
172
                }
173
                catch(Exception e)
174
                {
175
                        e.printStackTrace();
176
                        return null;
177
                }
178
        }
179

    
180
        public XMLEntity getXMLEntity() {
181
                XMLEntity xml = new XMLEntity();
182
                xml.putProperty("className",this.getClass().getName());
183
                xml.putProperty("labelFieldName", labelFieldName);
184
        xml.putProperty("labelHeightFieldName", labelFieldHeight);
185
        xml.putProperty("labelRotationFieldName", labelFieldRotation);
186
                xml.addChild(defaultSymbol.getXMLEntity());
187

    
188
                return xml;
189
        }
190

    
191
        public void setXMLEntity03(XMLEntity xml) {
192
                setLabelField(xml.getStringProperty("labelFieldName"));
193

    
194
                FSymbol auxSym = FSymbol.createFromXML03(xml.getChild(0));
195
                setDefaultSymbol(auxSym);
196
        }
197

    
198
        public void setXMLEntity(XMLEntity xml) {
199
                setLabelField(xml.getStringProperty("labelFieldName"));
200
        if (xml.contains("labelHeightFieldName"))
201
            setLabelHeightField(xml.getStringProperty("labelHeightFieldName"));
202
        if (xml.contains("labelRotationFieldName"))
203
            setLabelRotationField(xml.getStringProperty("labelRotationFieldName"));
204

    
205
//                FSymbol auxSym = FSymbol.createFromXML(xml.getChild(0));
206
        ISymbol auxSym = SymbologyFactory.createSymbolFromXML(xml.getChild(0), null);
207
                setDefaultSymbol(auxSym);
208
        }
209

    
210

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

    
215

    
216
        public void setDataSource(DataSource ds) {
217
                // No hacemos nada, no lo vamos a usar
218
        }
219

    
220
        public int getShapeType() {
221
                return shapeType;
222
        }
223

    
224
        public void setShapeType(int shapeType) {
225
                if (this.shapeType != shapeType) {
226
                        switch (shapeType) {
227
                                case FShape.POINT:
228
                                        defaultSymbol = new SimpleMarkerSymbol();
229
                                        break;
230

    
231
                                case FShape.LINE:
232
                                        defaultSymbol = new SimpleLineSymbol();
233

    
234
                                        break;
235

    
236
                                case FShape.POLYGON:
237
                                        defaultSymbol = new SimpleFillSymbol();
238

    
239
                                        break;
240
                        }
241

    
242
                        this.shapeType = shapeType;
243
                }
244
        }
245

    
246
        /**
247
         * @deprecated
248
         */
249
        public String getLabelHeightField() {
250
                return labelFieldHeight;
251
        }
252

    
253
        /**
254
         * Inserta el texto que refleja el alto del campo.
255
         * @deprecated
256
         * @param str String.
257
         */
258
        public void setLabelHeightField(String str) {
259
                labelFieldHeight = str;
260
        }
261

    
262
        /**
263
         * @deprecated
264
         */
265
        public String getLabelRotationField() {
266
                return labelFieldRotation;
267
        }
268

    
269
        /**
270
         * Inserta el texto que refleja la rotaci?n del campo.
271
         * @deprecated
272
         *
273
         * @param str String.
274
         */
275
        public void setLabelRotationField(String str) {
276
                labelFieldRotation = str;
277
        }
278

    
279
    public ISymbol getSymbolByFeature(IFeature feat) {
280
        return defaultSymbol;
281
    }
282

    
283
        public void useDefaultSymbol(boolean b) {
284
                // TODO Auto-generated method stub
285
        }
286

    
287
    public String[] getUsedFields() {
288
        ArrayList usedFields = new ArrayList();
289
        if (getLabelField() != null)
290
            usedFields.add(getLabelField());
291
        if (getLabelHeightField() != null)
292
            usedFields.add(getLabelHeightField());
293
        if (getLabelRotationField() != null)
294
            usedFields.add(getLabelRotationField());
295

    
296
        return (String[]) usedFields.toArray(new String[0]);
297
    }
298

    
299
    public boolean isUseDefaultSymbol() {
300
            return true;
301

    
302
    }
303
}