Statistics
| Revision:

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

History | View | Annotate | Download (8.82 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 4111 ldiaz
import com.iver.cit.gvsig.fmap.core.SLDTags;
49 801 fjp
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
50 458 fjp
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
51 490 fernando
import com.iver.cit.gvsig.fmap.layers.XMLException;
52 435 vcaballero
import com.iver.utiles.XMLEntity;
53 335 fjp
54
55 1034 vcaballero
/**
56
 * Leyenda se s?mbolo ?nico.
57
 *
58
 * @author Vicente Caballero Navarro
59
 */
60 3771 ldiaz
public class SingleSymbolLegend implements VectorialLegend, Legend{
61 801 fjp
        private FSymbol defaultSymbol;
62 3268 fjp
        private String labelFieldName = null;
63
        private String labelFieldHeight = null;
64
        private String labelFieldRotation = null;
65 801 fjp
        private int shapeType = FShape.POLYGON; // Por defecto, tipo pol?gono
66 3035 fjp
        // private boolean bWithHeightText;
67 1034 vcaballero
        /**
68
         * Crea un nuevo SingleSymbolLegend.
69
         *
70
         * @param style S?mbolo.
71
         */
72
        public SingleSymbolLegend(FSymbol style) {
73 801 fjp
                defaultSymbol = style;
74 335 fjp
        }
75 1034 vcaballero
76
        /**
77
         * Crea un nuevo SingleSymbolLegend.
78
         */
79
        public SingleSymbolLegend() {
80 801 fjp
                defaultSymbol = LegendFactory.DEFAULT_POLYGON_SYMBOL;
81 442 vcaballero
        }
82 1034 vcaballero
83 335 fjp
        /**
84 1055 vcaballero
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDefaultSymbol(FSymbol)
85 335 fjp
         */
86 458 fjp
        public void setDefaultSymbol(FSymbol s) {
87 801 fjp
                setShapeType(s.getSymbolType());
88 941 vcaballero
                defaultSymbol = s;
89 335 fjp
        }
90
91
        /**
92 1055 vcaballero
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelField()
93 335 fjp
         */
94 470 fjp
        public String getLabelField() {
95
                return labelFieldName;
96 335 fjp
        }
97
98
        /**
99 1055 vcaballero
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setLabelField(int)
100 335 fjp
         */
101 470 fjp
        public void setLabelField(String fieldName) {
102
                labelFieldName = fieldName;
103 335 fjp
        }
104
105
        /**
106 1055 vcaballero
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getSymbol(int)
107 335 fjp
         */
108 458 fjp
        public FSymbol getSymbol(int recordIndex) {
109 801 fjp
                return defaultSymbol;
110 335 fjp
        }
111
112
        /**
113 458 fjp
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getDefaultSymbol()
114 335 fjp
         */
115 458 fjp
        public FSymbol getDefaultSymbol() {
116 801 fjp
                return defaultSymbol;
117 335 fjp
        }
118 1034 vcaballero
119 435 vcaballero
        /**
120 3632 ldiaz
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getSLDString()
121
         */
122 4567 ldiaz
        public String getSLDString(String layerName)
123 3771 ldiaz
        {
124 4111 ldiaz
//                StyledLayerDescriptor sld = new StyledLayerDescriptor();
125 4070 ldiaz
//                StyleFactory styleFactory = StyleFactory.createStyleFactory();
126
//                Style style = styleFactory.createStyle();
127 4111 ldiaz
//                style.setName("default");
128
//            try{
129
//                        FeatureTypeStyle featStyle = styleFactory.createFeatureTypeStyle();
130
//                        Symbolizer[] theSymbolizers = new Symbolizer[1];
131
//                        theSymbolizers[0] = SDLUtils.toGeotoolsSymbol(this.defaultSymbol);
132
//                        Rule myRule = styleFactory.createRule();
133
//                        myRule.setSymbolizers(theSymbolizers);
134
//                        featStyle.addRule(myRule);
135
//                        style.addFeatureTypeStyle(featStyle);
136
//                        NamedLayer namedLayer = new NamedLayer();
137
//                        namedLayer.setName("comunidades");
138
//                        namedLayer.addStyle(style);
139
//                        sld.addStyledLayer(namedLayer);
140
//                        SLDTransformer st = new SLDTransformer();
141
//                        String result =  st.transform(style);
142
//                        return result;
143
//            }catch(Exception e) {
144
//                    e.printStackTrace();
145
//                    return null;
146
//            }
147
                try{
148
                        XmlBuilder xmlBuilder = new XmlBuilder();
149
                        xmlBuilder.writeHeader();
150
                        xmlBuilder.openTag(SLDTags.SLD_ROOT, SLDTags.VERSION_ATTR, SLDTags.VERSION_1_0_0);
151
                        xmlBuilder.openTag(SLDTags.NAMEDLAYER);
152 4575 ldiaz
                        xmlBuilder.writeTag(SLDTags.NAME,layerName);
153 4111 ldiaz
                        xmlBuilder.openTag(SLDTags.USERSTYLE);
154
                        xmlBuilder.openTag(SLDTags.FEATURETYPESTYLE);
155
                        xmlBuilder.writeTag(SLDTags.FEATURETYPENAME,"FeatureTypeName");
156
                        xmlBuilder.openTag(SLDTags.RULE);
157 4575 ldiaz
158 4111 ldiaz
                        xmlBuilder.writeRaw(this.defaultSymbol.toSLD());
159
                        xmlBuilder.closeTag();
160
                        xmlBuilder.closeTag();
161
                        xmlBuilder.closeTag();
162
                        xmlBuilder.closeTag();
163
                        xmlBuilder.closeTag();
164
                        return xmlBuilder.getXML();
165
                }
166
                catch(Exception e)
167
                {
168
                        e.printStackTrace();
169
                        return null;
170
                }
171 3632 ldiaz
        }
172
173
        /**
174 458 fjp
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#getXMLEntity()
175 435 vcaballero
         */
176
        public XMLEntity getXMLEntity() {
177 1034 vcaballero
                XMLEntity xml = new XMLEntity();
178 1094 vcaballero
                xml.putProperty("className",this.getClass().getName());
179 1034 vcaballero
                xml.putProperty("labelFieldName", labelFieldName);
180 3035 fjp
        xml.putProperty("labelHeightFieldName", labelFieldHeight);
181
        xml.putProperty("labelRotationFieldName", labelFieldRotation);
182 801 fjp
                xml.addChild(defaultSymbol.getXMLEntity());
183 1034 vcaballero
184 435 vcaballero
                return xml;
185
        }
186 335 fjp
187 1034 vcaballero
        /**
188 1055 vcaballero
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setXMLEntity()
189 1034 vcaballero
         */
190 2183 fernando
        public void setXMLEntity03(XMLEntity xml) {
191
                setLabelField(xml.getStringProperty("labelFieldName"));
192
193
                FSymbol auxSym = FSymbol.createFromXML03(xml.getChild(0));
194
                setDefaultSymbol(auxSym);
195
        }
196
197
        /**
198
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setXMLEntity()
199
         */
200 1034 vcaballero
        public void setXMLEntity(XMLEntity xml) {
201
                setLabelField(xml.getStringProperty("labelFieldName"));
202 3035 fjp
        if (xml.contains("labelHeightFieldName"))
203
            setLabelHeightField(xml.getStringProperty("labelHeightFieldName"));
204
        if (xml.contains("labelRotationFieldName"))
205
            setLabelRotationField(xml.getStringProperty("labelRotationFieldName"));
206 1034 vcaballero
207
                FSymbol auxSym = FSymbol.createFromXML(xml.getChild(0));
208
                setDefaultSymbol(auxSym);
209
        }
210
211
        /**
212
         * @see com.iver.cit.gvsig.fmap.rendering.Legend#cloneLegend()
213
         */
214
        public Legend cloneLegend() throws XMLException {
215
                return (Legend) LegendFactory.createFromXML(getXMLEntity());
216
        }
217
218
        /* (non-Javadoc)
219
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setDataSource(com.hardcode.gdbms.engine.data.DataSource)
220
         */
221
        public void setDataSource(DataSource ds) {
222
                // No hacemos nada, no lo vamos a usar
223
        }
224
225
        /* (non-Javadoc)
226
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getShapeType()
227
         */
228
        public int getShapeType() {
229
                return shapeType;
230
        }
231
232
        /* (non-Javadoc)
233
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#setShapeType(int)
234
         */
235
        public void setShapeType(int shapeType) {
236
                if (this.shapeType != shapeType) {
237
                        switch (shapeType) {
238
                                case FShape.POINT:
239
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT);
240
241
                                        break;
242
243
                                case FShape.LINE:
244
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
245
246
                                        break;
247
248
                                case FShape.POLYGON:
249
                                        defaultSymbol = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
250
251
                                        break;
252
                        }
253
254
                        this.shapeType = shapeType;
255 801 fjp
                }
256
        }
257 435 vcaballero
258 1034 vcaballero
        /* (non-Javadoc)
259
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelHeightField()
260
         */
261
        public String getLabelHeightField() {
262
                return labelFieldHeight;
263
        }
264 801 fjp
265 1034 vcaballero
        /**
266
         * Inserta el texto que refleja el alto del campo.
267
         *
268
         * @param str String.
269
         */
270
        public void setLabelHeightField(String str) {
271
                labelFieldHeight = str;
272
        }
273 801 fjp
274 1034 vcaballero
        /* (non-Javadoc)
275
         * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getLabelRotationField()
276
         */
277
        public String getLabelRotationField() {
278
                return labelFieldRotation;
279
        }
280
281
        /**
282
         * Inserta el texto que refleja la rotaci?n del campo.
283
         *
284
         * @param str String.
285
         */
286
        public void setLabelRotationField(String str) {
287
                labelFieldRotation = str;
288
        }
289 2183 fernando
290
    public FSymbol getSymbolByFeature(IFeature feat) {
291
        return defaultSymbol;
292
    }
293 2414 caballero
294
        public void useDefaultSymbol(boolean b) {
295
                // TODO Auto-generated method stub
296
        }
297 2600 caballero
298 3268 fjp
    /* (non-Javadoc)
299
     * @see com.iver.cit.gvsig.fmap.rendering.VectorialLegend#getUsedFields()
300
     */
301
    public String[] getUsedFields() {
302
        ArrayList usedFields = new ArrayList();
303
        if (getLabelField() != null)
304
            usedFields.add(getLabelField());
305
        if (getLabelHeightField() != null)
306
            usedFields.add(getLabelHeightField());
307
        if (getLabelRotationField() != null)
308
            usedFields.add(getLabelRotationField());
309
310
        return (String[]) usedFields.toArray(new String[0]);
311
    }
312
313 3035 fjp
        /* public boolean isBWithHeightText() {
314 2600 caballero
                return bWithHeightText;
315
        }
316

317
        public void setBWithHeightText(boolean withHeightText) {
318
                bWithHeightText = withHeightText;
319 3035 fjp
        } */
320 801 fjp
}