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 @ 40911

History | View | Annotate | Download (8.18 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.awt.Color;
61
import java.awt.Font;
62
import java.util.ArrayList;
63
import java.util.Iterator;
64
import java.util.List;
65

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

    
92

    
93
public class FeatureDependentLabeled implements ILabelingMethod {
94
        
95
        public static final String FEATURE_DEP_LABELED_PERSISTENCE_NAME =
96
                        "FEATURE_DEP_LABELED_PERSISTENCE_NAME";
97

    
98
        private static Logger logger =
99
                        LoggerFactory.getLogger(FeatureDependentLabeled.class);
100
        
101
        private boolean flagDefinesPriorities;
102
        private List<ILabelClass> classes = new ArrayList<ILabelClass>();
103

    
104
        public void addLabelClass(ILabelClass lbl) throws IllegalArgumentException{
105
                if (getLabelClassByName(lbl.getName()) !=null) {
106
                        throw new IllegalArgumentException("A class with the same name already exists!");
107
                }
108
                classes.add(lbl);
109
        }
110

    
111
        public void deleteLabelClass(ILabelClass lbl) {
112
                classes.remove(lbl);
113
        }
114

    
115
        public String getClassName() {
116
                return getClass().getName();
117
        }
118

    
119
        /*
120
        public XMLEntity getXMLEntity() {
121
                XMLEntity xml = new XMLEntity();
122
                xml.putProperty("className", getClassName());
123
                xml.putProperty("definesPriorities", definesPriorities());
124

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

    
138
        public ILabelClass[] getLabelClasses() {
139
                return classes.toArray(new ILabelClass[0]);
140
        }
141

    
142
        /*
143
        public void setXMLEntity(XMLEntity xml) {
144
                if (xml.contains("definesPriorities"))
145
                        setDefinesPriorities(xml.getBooleanProperty("definesPriorities"));
146

147
                XMLEntity aux = xml.firstChild("id", "defaultLabelClass");
148

149
//                if (aux!=null)
150
//                        defaultLabel = LabelingFactory.createLabelClassFromXML(aux);
151

152
                aux = xml.firstChild("id", "LabelClasses");
153
                if (aux!=null) {
154
                        for (int i = 0; i < aux.getChildrenCount(); i++) {
155
                                addLabelClass(LabelingFactory.
156
                                                createLabelClassFromXML(aux.getChild(i)));
157
                        }
158
                }
159
        }
160
        */
161

    
162
        public boolean allowsMultipleClass() {
163
                return true;
164
        }
165

    
166
        public void renameLabelClass(ILabelClass lbl, String newName) {
167
                ILabelClass label = (ILabelClass) classes.get(classes.indexOf(lbl));
168
                label.setName(newName);
169
        }
170

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

    
216
        public boolean definesPriorities() {
217
                return flagDefinesPriorities;
218
        }
219

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

    
230
        public void clearAllClasses() {
231
                classes.clear();
232
        }
233

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

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

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

    
285
        public void saveToState(PersistentState state) throws PersistenceException {
286
                
287
                state.set("definesPriorities", flagDefinesPriorities);
288
                state.set("classes", classes);
289
                
290
        }
291
}