Statistics
| Revision:

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

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