Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.labeling.app / org.gvsig.labeling.app.mainplugin / src / main / java / org / gvsig / labeling / label / FeatureDependentLabeled.java @ 43020

History | View | Annotate | Download (7.95 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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

    
42
/* CVS MESSAGES:
43
*
44
* $Id: FeatureDependentLabeled.java 10671 2007-03-09 08:33:43Z jaume $
45
* $Log$
46
* Revision 1.2  2007-03-09 08:33:43  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.1.2.2  2007/02/01 11:42:47  jaume
50
* *** empty log message ***
51
*
52
* Revision 1.1.2.1  2007/01/30 18:10:45  jaume
53
* start commiting labeling stuff
54
*
55
*
56
*/
57
package org.gvsig.labeling.label;
58

    
59

    
60
import java.util.ArrayList;
61
import java.util.Iterator;
62
import java.util.List;
63

    
64
import org.cresques.cts.IProjection;
65
import org.gvsig.fmap.dal.exception.DataException;
66
import org.gvsig.fmap.dal.feature.FeatureQuery;
67
import org.gvsig.fmap.dal.feature.FeatureSet;
68
import org.gvsig.fmap.dal.feature.FeatureStore;
69
import org.gvsig.fmap.geom.primitive.Envelope;
70
import org.gvsig.fmap.mapcontext.ViewPort;
71
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
72
import org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory;
73
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelClass;
74
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelingMethod;
75
import org.gvsig.labeling.lang.EvaluatorCreator;
76
import org.gvsig.labeling.lang.LabelClassUtils;
77
import org.gvsig.tools.ToolsLocator;
78
import org.gvsig.tools.dynobject.DynStruct;
79
import org.gvsig.tools.evaluator.AndEvaluator;
80
import org.gvsig.tools.evaluator.Evaluator;
81
import org.gvsig.tools.persistence.PersistenceManager;
82
import org.gvsig.tools.persistence.PersistentState;
83
import org.gvsig.tools.persistence.exception.PersistenceException;
84
import org.slf4j.Logger;
85
import org.slf4j.LoggerFactory;
86

    
87

    
88
public class FeatureDependentLabeled implements ILabelingMethod {
89
        
90
        public static final String FEATURE_DEP_LABELED_PERSISTENCE_NAME =
91
                        "FEATURE_DEP_LABELED_PERSISTENCE_NAME";
92

    
93
        private static Logger logger =
94
                        LoggerFactory.getLogger(FeatureDependentLabeled.class);
95
        
96
        private boolean flagDefinesPriorities;
97
        private List<ILabelClass> classes = new ArrayList<ILabelClass>();
98

    
99
        public void addLabelClass(ILabelClass lbl) throws IllegalArgumentException{
100
                if (getLabelClassByName(lbl.getName()) !=null) {
101
                        throw new IllegalArgumentException("A class with the same name already exists!");
102
                }
103
                classes.add(lbl);
104
        }
105

    
106
        public void deleteLabelClass(ILabelClass lbl) {
107
                classes.remove(lbl);
108
        }
109

    
110
        public String getClassName() {
111
                return getClass().getName();
112
        }
113

    
114
        /*
115
        public XMLEntity getXMLEntity() {
116
                XMLEntity xml = new XMLEntity();
117
                xml.putProperty("className", getClassName());
118
                xml.putProperty("definesPriorities", definesPriorities());
119

120
                LabelClass[] labels = getLabelClasses();
121
                if (labels!=null) {
122
                        XMLEntity xmlLabels = new XMLEntity();
123
                        xmlLabels.putProperty("id", "LabelClasses");
124
                        for (int i = 0; i < labels.length; i++) {
125
                                xmlLabels.addChild(labels[i].getXMLEntity());
126
                        }
127
                        xml.addChild(xmlLabels);
128
                }
129
                return xml;
130
        }
131
        */
132

    
133
        public ILabelClass[] getLabelClasses() {
134
                return classes.toArray(new ILabelClass[0]);
135
        }
136

    
137
        /*
138
        public void setXMLEntity(XMLEntity xml) {
139
                if (xml.contains("definesPriorities"))
140
                        setDefinesPriorities(xml.getBooleanProperty("definesPriorities"));
141

142
                XMLEntity aux = xml.firstChild("id", "defaultLabelClass");
143

144
//                if (aux!=null)
145
//                        defaultLabel = LabelingFactory.createLabelClassFromXML(aux);
146

147
                aux = xml.firstChild("id", "LabelClasses");
148
                if (aux!=null) {
149
                        for (int i = 0; i < aux.getChildrenCount(); i++) {
150
                                addLabelClass(LabelingFactory.
151
                                                createLabelClassFromXML(aux.getChild(i)));
152
                        }
153
                }
154
        }
155
        */
156

    
157
        public boolean allowsMultipleClass() {
158
                return true;
159
        }
160

    
161
        public void renameLabelClass(ILabelClass lbl, String newName) {
162
                ILabelClass label = (ILabelClass) classes.get(classes.indexOf(lbl));
163
                label.setName(newName);
164
        }
165

    
166
        public FeatureSet getFeatureIteratorByLabelClass(
167
                        FLyrVect layer,
168
                        ILabelClass lc,
169
                        ViewPort viewPort,
170
                        String[] usedFields) throws DataException {
171
                
172
                Envelope vp_env = viewPort.getAdjustedEnvelope();
173
                IProjection ipro = layer.getProjection();
174
                Evaluator iee = SpatialEvaluatorsFactory.getInstance().intersects(
175
                        vp_env, 
176
                        ipro, 
177
                        layer.getFeatureStore()
178
                );
179
                FeatureStore fsto = layer.getFeatureStore();
180
                FeatureQuery fq = fsto.createFeatureQuery();
181
                
182
                
183
                if (usedFields != null) {
184
            fq.setAttributeNames(usedFields);
185
        }
186
                
187
                if (!LabelClassUtils.isUseSqlQuery(lc)) {
188
                        /*
189
                         * No SQL, use only view port filter
190
                         */
191
                        fq.addFilter(iee);
192
                        return fsto.getFeatureSet(fq);
193
                }
194
                
195
                /*
196
                 * There is SQL
197
                 */
198
                String sql_str = lc.getSQLQuery();
199
                if (! LabelClassUtils.validExpression(sql_str, fsto, true)) {
200
                        logger.error("SQL for labeling exists but is not valid: " + sql_str);
201
                }
202
                
203
                Evaluator eval = EvaluatorCreator.getEvaluator(sql_str);
204
                /*
205
                 * This will make the view port evaluator be applied first
206
                 */
207
                AndEvaluator and_ev = new AndEvaluator(iee);
208
                and_ev.addEvaluator(eval);
209
                fq.addFilter(and_ev);
210
                return fsto.getFeatureSet(fq);
211
        }
212

    
213
        public boolean definesPriorities() {
214
                return flagDefinesPriorities;
215
        }
216

    
217
        public void setDefinesPriorities(boolean flag) {
218
                if (flag == false) {
219
                        ILabelClass[] lcs = getLabelClasses();
220
                        for (int i = 0; i < lcs.length; i++) {
221
                                lcs[i].setPriority(0);
222
                        }
223
                }
224
                flagDefinesPriorities = flag;
225
        }
226

    
227
        public void clearAllClasses() {
228
                classes.clear();
229
        }
230

    
231
        public ILabelClass getLabelClassByName(String labelName) {
232
                ILabelClass[] classes = getLabelClasses();
233
                for (int i = 0; i < classes.length; i++) {
234
                        if (classes[i].getName().equals(labelName)) {
235
                                return classes[i];
236
                        }
237
                }
238
                return null;
239
        }
240

    
241
        public ILabelingMethod cloneMethod() {
242
                
243
                ILabelingMethod resp = (ILabelingMethod) LabelClassUtils.clone(this);
244
                return resp;
245
        }
246
        
247
        public static void registerPersistent() {
248
                
249
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
250
                if( manager.getDefinition(FEATURE_DEP_LABELED_PERSISTENCE_NAME)==null ) {
251
                        DynStruct definition = manager.addDefinition(
252
                                        FeatureDependentLabeled.class,
253
                                        FEATURE_DEP_LABELED_PERSISTENCE_NAME,
254
                                        FEATURE_DEP_LABELED_PERSISTENCE_NAME+" Persistence definition",
255
                                        null, 
256
                                        null
257
                        );
258
                        
259
                        definition.addDynFieldBoolean("definesPriorities").setMandatory(true);
260
                        definition.addDynFieldList("classes").setClassOfItems(ILabelClass.class)
261
                        .setMandatory(true);
262
                }                
263
        }        
264

    
265
        public void loadFromState(PersistentState state) throws PersistenceException {
266
                
267
                this.setDefinesPriorities(state.getBoolean("definesPriorities"));
268
                List aux = state.getList("classes");
269
                ILabelClass item = null;
270
                Iterator iter = aux.iterator();
271
                
272
                try {
273
                        while (iter.hasNext()) {
274
                                item = (ILabelClass) iter.next();
275
                                this.addLabelClass(item);
276
                        }
277
                } catch (IllegalArgumentException exc) {
278
                        throw new PersistenceException(exc);
279
                }
280
        }
281

    
282
        public void saveToState(PersistentState state) throws PersistenceException {
283
                
284
                state.set("definesPriorities", flagDefinesPriorities);
285
                state.set("classes", classes);
286
                
287
        }
288
}