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

History | View | Annotate | Download (6.79 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

    
62
import org.cresques.cts.IProjection;
63
import org.gvsig.fmap.dal.exception.DataException;
64
import org.gvsig.fmap.dal.exception.ReadException;
65
import org.gvsig.fmap.dal.feature.FeatureQuery;
66
import org.gvsig.fmap.dal.feature.FeatureSet;
67
import org.gvsig.fmap.dal.feature.FeatureStore;
68
import org.gvsig.fmap.dal.feature.FeatureType;
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.IntersectsEnvelopeEvaluator;
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.evaluator.AndEvaluator;
78
import org.gvsig.tools.evaluator.Evaluator;
79
import org.gvsig.tools.persistence.PersistentState;
80
import org.gvsig.tools.persistence.exception.PersistenceException;
81
import org.slf4j.Logger;
82
import org.slf4j.LoggerFactory;
83

    
84

    
85
public class FeatureDependentLabeled implements ILabelingMethod {
86
        
87
        private static Logger logger =
88
                        LoggerFactory.getLogger(FeatureDependentLabeled.class);
89
        
90
        private boolean flagDefinesPriorities;
91
        private ArrayList<ILabelClass> classes = new ArrayList<ILabelClass>();
92

    
93
        public void addLabelClass(ILabelClass lbl) throws IllegalArgumentException{
94
                if (getLabelClassByName(lbl.getName()) !=null) {
95
                        throw new IllegalArgumentException("A class with the same name already exists!");
96
                }
97
                classes.add(lbl);
98
        }
99

    
100
        public void deleteLabelClass(ILabelClass lbl) {
101
                classes.remove(lbl);
102
        }
103

    
104
        public String getClassName() {
105
                return getClass().getName();
106
        }
107

    
108
        /*
109
        public XMLEntity getXMLEntity() {
110
                XMLEntity xml = new XMLEntity();
111
                xml.putProperty("className", getClassName());
112
                xml.putProperty("definesPriorities", definesPriorities());
113

114
                LabelClass[] labels = getLabelClasses();
115
                if (labels!=null) {
116
                        XMLEntity xmlLabels = new XMLEntity();
117
                        xmlLabels.putProperty("id", "LabelClasses");
118
                        for (int i = 0; i < labels.length; i++) {
119
                                xmlLabels.addChild(labels[i].getXMLEntity());
120
                        }
121
                        xml.addChild(xmlLabels);
122
                }
123
                return xml;
124
        }
125
        */
126

    
127
        public ILabelClass[] getLabelClasses() {
128
                return classes.toArray(new ILabelClass[0]);
129
        }
130

    
131
        /*
132
        public void setXMLEntity(XMLEntity xml) {
133
                if (xml.contains("definesPriorities"))
134
                        setDefinesPriorities(xml.getBooleanProperty("definesPriorities"));
135

136
                XMLEntity aux = xml.firstChild("id", "defaultLabelClass");
137

138
//                if (aux!=null)
139
//                        defaultLabel = LabelingFactory.createLabelClassFromXML(aux);
140

141
                aux = xml.firstChild("id", "LabelClasses");
142
                if (aux!=null) {
143
                        for (int i = 0; i < aux.getChildrenCount(); i++) {
144
                                addLabelClass(LabelingFactory.
145
                                                createLabelClassFromXML(aux.getChild(i)));
146
                        }
147
                }
148
        }
149
        */
150

    
151
        public boolean allowsMultipleClass() {
152
                return true;
153
        }
154

    
155
        public void renameLabelClass(ILabelClass lbl, String newName) {
156
                ILabelClass label = (ILabelClass) classes.get(classes.indexOf(lbl));
157
                label.setName(newName);
158
        }
159

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

    
205
        public boolean definesPriorities() {
206
                return flagDefinesPriorities;
207
        }
208

    
209
        public void setDefinesPriorities(boolean flag) {
210
                if (flag == false) {
211
                        ILabelClass[] lcs = getLabelClasses();
212
                        for (int i = 0; i < lcs.length; i++) {
213
                                lcs[i].setPriority(0);
214
                        }
215
                }
216
                flagDefinesPriorities = flag;
217
        }
218

    
219
        public void clearAllClasses() {
220
                classes.clear();
221
        }
222

    
223
        public ILabelClass getLabelClassByName(String labelName) {
224
                ILabelClass[] classes = getLabelClasses();
225
                for (int i = 0; i < classes.length; i++) {
226
                        if (classes[i].getName().equals(labelName)) {
227
                                return classes[i];
228
                        }
229
                }
230
                return null;
231
        }
232

    
233
        public ILabelingMethod cloneMethod() {
234
                // TODO Auto-generated method stub
235
                throw new Error("Not yet implemented!");
236
        }
237
        
238
        
239

    
240
        public void loadFromState(PersistentState arg0) throws PersistenceException {
241
                // TODO Auto-generated method stub
242
                
243
        }
244

    
245
        public void saveToState(PersistentState arg0) throws PersistenceException {
246
                // TODO Auto-generated method stub
247
                
248
        }
249
}