Statistics
| Revision:

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

History | View | Annotate | Download (9.14 KB)

1 1100 fjp
/* 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 335 fjp
package com.iver.cit.gvsig.fmap.rendering;
42
43 3268 fjp
import java.util.ArrayList;
44
45 1034 vcaballero
import com.hardcode.gdbms.engine.data.DataSource;
46 801 fjp
import com.iver.cit.gvsig.fmap.core.FShape;
47 2183 fernando
import com.iver.cit.gvsig.fmap.core.IFeature;
48 8765 jjdelcerro
import com.iver.cit.gvsig.fmap.core.ISLDCompatible;
49
import com.iver.cit.gvsig.fmap.core.ISymbol;
50 4111 ldiaz
import com.iver.cit.gvsig.fmap.core.SLDTags;
51 801 fjp
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
52 458 fjp
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
53 490 fernando
import com.iver.cit.gvsig.fmap.layers.XMLException;
54 435 vcaballero
import com.iver.utiles.XMLEntity;
55 335 fjp
56
57 1034 vcaballero
/**
58
 * Leyenda se s?mbolo ?nico.
59
 *
60
 * @author Vicente Caballero Navarro
61
 */
62 3771 ldiaz
public class SingleSymbolLegend implements VectorialLegend, Legend{
63 8765 jjdelcerro
        private ISymbol defaultSymbol;
64 3268 fjp
        private String labelFieldName = null;
65
        private String labelFieldHeight = null;
66
        private String labelFieldRotation = null;
67 801 fjp
        private int shapeType = FShape.POLYGON; // Por defecto, tipo pol?gono
68 3035 fjp
        // private boolean bWithHeightText;
69 1034 vcaballero
        /**
70
         * Crea un nuevo SingleSymbolLegend.
71
         *
72
         * @param style S?mbolo.
73
         */
74
        public SingleSymbolLegend(FSymbol style) {
75 801 fjp
                defaultSymbol = style;
76 335 fjp
        }
77 1034 vcaballero
78
        /**
79
         * Crea un nuevo SingleSymbolLegend.
80
         */
81
        public SingleSymbolLegend() {
82 801 fjp
                defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
83 442 vcaballero
        }
84 1034 vcaballero
85 335 fjp
        /**
86 8765 jjdelcerro
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDefaultSymbol(ISymbol)
87 335 fjp
         */
88 8765 jjdelcerro
        public void setDefaultSymbol(ISymbol s) {
89 801 fjp
                setShapeType(s.getSymbolType());
90 941 vcaballero
                defaultSymbol = s;
91 335 fjp
        }
92
93
        /**
94 1055 vcaballero
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelField()
95 335 fjp
         */
96 470 fjp
        public String getLabelField() {
97
                return labelFieldName;
98 335 fjp
        }
99
100
        /**
101 1055 vcaballero
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setLabelField(int)
102 335 fjp
         */
103 470 fjp
        public void setLabelField(String fieldName) {
104
                labelFieldName = fieldName;
105 335 fjp
        }
106
107
        /**
108 1055 vcaballero
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getSymbol(int)
109 335 fjp
         */
110 8765 jjdelcerro
        public ISymbol getSymbol(int recordIndex) {
111 801 fjp
                return defaultSymbol;
112 335 fjp
        }
113
114
        /**
115 458 fjp
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
116 335 fjp
         */
117 8765 jjdelcerro
        public ISymbol getDefaultSymbol() {
118 801 fjp
                return defaultSymbol;
119 335 fjp
        }
120 1034 vcaballero
121 435 vcaballero
        /**
122 8765 jjdelcerro
         * creates the SLD String that defines this legend type.
123 3632 ldiaz
         */
124 4567 ldiaz
        public String getSLDString(String layerName)
125 3771 ldiaz
        {
126 4111 ldiaz
//                StyledLayerDescriptor sld = new StyledLayerDescriptor();
127 4070 ldiaz
//                StyleFactory styleFactory = StyleFactory.createStyleFactory();
128
//                Style style = styleFactory.createStyle();
129 4111 ldiaz
//                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 4575 ldiaz
                        xmlBuilder.writeTag(SLDTags.NAME,layerName);
155 4111 ldiaz
                        xmlBuilder.openTag(SLDTags.USERSTYLE);
156
                        xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE);
157
                        xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"FeatureTypeName");
158
                        xmlBuilder.openTag(SLDTags.RULE);
159 8765 jjdelcerro
                        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 4111 ldiaz
                        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 3632 ldiaz
        }
180
181
        /**
182 458 fjp
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
183 435 vcaballero
         */
184
        public XMLEntity getXMLEntity() {
185 1034 vcaballero
                XMLEntity xml = new XMLEntity();
186 1094 vcaballero
                xml.putProperty("className",this.getClass().getName());
187 1034 vcaballero
                xml.putProperty("labelFieldName", labelFieldName);
188 3035 fjp
        xml.putProperty("labelHeightFieldName", labelFieldHeight);
189
        xml.putProperty("labelRotationFieldName", labelFieldRotation);
190 801 fjp
                xml.addChild(defaultSymbol.getXMLEntity());
191 1034 vcaballero
192 435 vcaballero
                return xml;
193
        }
194 335 fjp
195 1034 vcaballero
        /**
196 1055 vcaballero
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setXMLEntity()
197 1034 vcaballero
         */
198 2183 fernando
        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 1034 vcaballero
        public void setXMLEntity(XMLEntity xml) {
209
                setLabelField(xml.getStringProperty("labelFieldName"));
210 3035 fjp
        if (xml.contains("labelHeightFieldName"))
211
            setLabelHeightField(xml.getStringProperty("labelHeightFieldName"));
212
        if (xml.contains("labelRotationFieldName"))
213
            setLabelRotationField(xml.getStringProperty("labelRotationFieldName"));
214 1034 vcaballero
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 801 fjp
                }
264
        }
265 435 vcaballero
266 1034 vcaballero
        /* (non-Javadoc)
267
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
268
         */
269
        public String getLabelHeightField() {
270
                return labelFieldHeight;
271
        }
272 801 fjp
273 1034 vcaballero
        /**
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 801 fjp
282 1034 vcaballero
        /* (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 2183 fernando
298 8765 jjdelcerro
    public ISymbol getSymbolByFeature(IFeature feat) {
299 2183 fernando
        return defaultSymbol;
300
    }
301 2414 caballero
302
        public void useDefaultSymbol(boolean b) {
303
                // TODO Auto-generated method stub
304
        }
305 2600 caballero
306 3268 fjp
    /* (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 3035 fjp
        /* public boolean isBWithHeightText() {
322 2600 caballero
                return bWithHeightText;
323
        }
324

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