svn-gvsig-desktop / trunk / libraries / libTopology / src / org / gvsig / topology / AbstractTopologyRule.java @ 21253
History | View | Annotate | Download (7.92 KB)
1 |
/*
|
---|---|
2 |
* Created on 07-sep-2007
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
/* CVS MESSAGES:
|
45 |
*
|
46 |
* $Id:
|
47 |
* $Log:
|
48 |
*
|
49 |
*/
|
50 |
package org.gvsig.topology; |
51 |
|
52 |
import java.awt.geom.Rectangle2D; |
53 |
import java.net.URL; |
54 |
import java.util.Locale; |
55 |
|
56 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
57 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
58 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
59 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
60 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
61 |
import com.iver.utiles.XMLEntity; |
62 |
import com.iver.utiles.swing.threads.CancellableProgressTask; |
63 |
|
64 |
/**
|
65 |
* Default implementation of ITopologyRule
|
66 |
*
|
67 |
* @author azabala
|
68 |
*
|
69 |
*/
|
70 |
public abstract class AbstractTopologyRule implements IOneLyrRule { |
71 |
|
72 |
/**
|
73 |
* We need a reference to topology to get a layer from its name
|
74 |
* in setXML method.
|
75 |
*/
|
76 |
protected Topology topology;
|
77 |
|
78 |
|
79 |
protected FLyrVect originLyr;
|
80 |
|
81 |
/**
|
82 |
* Container for the topology errors detected.
|
83 |
*/
|
84 |
protected ITopologyErrorContainer errorContainer;
|
85 |
|
86 |
/**
|
87 |
* Unique identifier of the rule to distinct it of the rest of rules
|
88 |
* of a topology
|
89 |
*/
|
90 |
private int ruleId; |
91 |
|
92 |
|
93 |
|
94 |
/**
|
95 |
* Constructor.
|
96 |
*
|
97 |
* @param originLyr
|
98 |
* layer which features we are checking
|
99 |
*
|
100 |
* @param topology reference to the topology that owns this rule.
|
101 |
*/
|
102 |
public AbstractTopologyRule(Topology topology, FLyrVect originLyr){
|
103 |
this.topology = topology;
|
104 |
this.originLyr = originLyr;
|
105 |
} |
106 |
|
107 |
/**
|
108 |
* Constructor without topology param
|
109 |
*
|
110 |
* @param originLyr
|
111 |
*/
|
112 |
public AbstractTopologyRule(FLyrVect originLyr){
|
113 |
this.originLyr = originLyr;
|
114 |
} |
115 |
|
116 |
public AbstractTopologyRule(){
|
117 |
//default constructor. Needed for persistence
|
118 |
} |
119 |
|
120 |
public void setOriginLyr(FLyrVect originLyr) { |
121 |
this.originLyr = originLyr;
|
122 |
} |
123 |
|
124 |
public FLyrVect getOriginLyr() {
|
125 |
return originLyr;
|
126 |
} |
127 |
|
128 |
|
129 |
public void setTopologyErrorContainer(ITopologyErrorContainer errorContainer){ |
130 |
this.errorContainer = errorContainer;
|
131 |
} |
132 |
|
133 |
public ITopologyErrorContainer getTopologyErrorContainer(){
|
134 |
return errorContainer;
|
135 |
} |
136 |
|
137 |
public abstract String getName(); |
138 |
|
139 |
|
140 |
public URL getDescription(){ |
141 |
Locale locale = Locale.getDefault(); |
142 |
String localeStr = locale.getLanguage();
|
143 |
String urlStr = "docs/"+ |
144 |
getClass().getName() + |
145 |
"/description_%lang%.html";
|
146 |
String localizedUrl = urlStr.replaceAll("%lang%", localeStr); |
147 |
|
148 |
URL url = AbstractTopologyRule.class.getResource(localizedUrl);
|
149 |
if(url == null){ |
150 |
// for languages used in Spain, fallback to Spanish if their translation is not available
|
151 |
if (localeStr.equals("ca")||localeStr.equals("gl")||localeStr.equals("eu")||localeStr.equals("va")) { |
152 |
localeStr = "es";
|
153 |
localizedUrl = urlStr.replaceAll("%lang", localeStr);
|
154 |
url = AbstractTopologyRule.class.getResource(localizedUrl); |
155 |
if(url != null) |
156 |
return url;
|
157 |
} |
158 |
// as a last resort, fallback to English
|
159 |
localeStr = "en";
|
160 |
localizedUrl = urlStr.replaceAll("%lang", localeStr);
|
161 |
url = AbstractTopologyRule.class.getResource(localizedUrl); |
162 |
} |
163 |
return url;
|
164 |
} |
165 |
|
166 |
/**
|
167 |
* Checks if the rule's parameters (sourceLyr, destinationLyr) verify rule
|
168 |
* preconditions (geometry type, etc.)
|
169 |
*/
|
170 |
public abstract void checkPreconditions() throws TopologyRuleDefinitionException ; |
171 |
|
172 |
|
173 |
public void checkRule(){ |
174 |
this.checkRule((CancellableProgressTask)null); |
175 |
} |
176 |
|
177 |
public void checkRule(Rectangle2D rect){ |
178 |
checkRule(null, rect);
|
179 |
} |
180 |
|
181 |
|
182 |
public void checkRule(CancellableProgressTask progressMonitor){ |
183 |
try {
|
184 |
// when we dont pass field names iterator only iterates over
|
185 |
// geometries
|
186 |
IFeatureIterator featureIterator = originLyr.getSource().getFeatureIterator(); |
187 |
while(featureIterator.hasNext()){
|
188 |
IFeature feature = featureIterator.next(); |
189 |
if(progressMonitor != null){ |
190 |
if(progressMonitor.isCanceled()/* |
191 |
* ||
|
192 |
* progressMonitor.isFinished()
|
193 |
*/){
|
194 |
// TODO Maybe we could show progress info of rule
|
195 |
// checking.
|
196 |
// example: feature 1 of N...etc
|
197 |
return;
|
198 |
} |
199 |
} |
200 |
validateFeature(feature); |
201 |
} |
202 |
this.ruleChecked();
|
203 |
} catch (ExpansionFileReadException e) {
|
204 |
e.printStackTrace(); |
205 |
} catch (ReadDriverException e) {
|
206 |
e.printStackTrace(); |
207 |
} |
208 |
} |
209 |
|
210 |
public void checkRule(CancellableProgressTask progressMonitor, Rectangle2D rect){ |
211 |
try {
|
212 |
IFeatureIterator iterator = originLyr.getSource().getFeatureIterator(rect, null, null, true); |
213 |
while(iterator.hasNext()){
|
214 |
IFeature feature = iterator.next(); |
215 |
if(progressMonitor != null){ |
216 |
if(progressMonitor.isCanceled()/* |
217 |
* ||
|
218 |
* progressMonitor.isFinished()
|
219 |
*/){
|
220 |
// TODO Maybe we could show progress info of rule
|
221 |
// checking.
|
222 |
// example: feature 1 of N...etc
|
223 |
return;
|
224 |
} |
225 |
} |
226 |
validateFeature(feature); |
227 |
}//while
|
228 |
ruleChecked(); |
229 |
} catch (ExpansionFileReadException e) {
|
230 |
// TODO Auto-generated catch block
|
231 |
e.printStackTrace(); |
232 |
} catch (ReadDriverException e) {
|
233 |
// TODO Auto-generated catch block
|
234 |
e.printStackTrace(); |
235 |
} |
236 |
} |
237 |
|
238 |
/**
|
239 |
* This method must be overwrited by all of those subclases which uses temporal results,
|
240 |
* cach?s, etc.
|
241 |
*/
|
242 |
public void ruleChecked(){}; |
243 |
|
244 |
public abstract void validateFeature(IFeature feature); |
245 |
|
246 |
|
247 |
public void addTopologyError(TopologyError topologyError){ |
248 |
this.errorContainer.addTopologyError(topologyError);
|
249 |
} |
250 |
|
251 |
/*
|
252 |
* Implementation of IPersistence
|
253 |
*/
|
254 |
public String getClassName(){ |
255 |
return this.getClass().getName(); |
256 |
} |
257 |
|
258 |
public XMLEntity getXMLEntity(){
|
259 |
XMLEntity xml = new XMLEntity();
|
260 |
xml.putProperty("className", getClassName());
|
261 |
xml.putProperty("originLayerName", this.originLyr.getName()); |
262 |
xml.putProperty("ruleId", this.ruleId); |
263 |
return xml;
|
264 |
} |
265 |
|
266 |
public void setXMLEntity(XMLEntity xml){ |
267 |
String originLayerName = ""; |
268 |
if (xml.contains("originLayer")) { |
269 |
originLayerName = xml.getStringProperty("originLayer");
|
270 |
this.originLyr = (FLyrVect) topology.getLayer(originLayerName);
|
271 |
}//if
|
272 |
|
273 |
if(xml.contains("ruleId")){ |
274 |
ruleId = xml.getIntProperty("ruleId");
|
275 |
} |
276 |
} |
277 |
|
278 |
public void setId(int ruleId){ |
279 |
this.ruleId = ruleId;
|
280 |
} |
281 |
|
282 |
public int getId(){ |
283 |
return ruleId;
|
284 |
} |
285 |
|
286 |
public Topology getTopology() {
|
287 |
return topology;
|
288 |
} |
289 |
|
290 |
public void setTopology(Topology topology) { |
291 |
this.topology = topology;
|
292 |
} |
293 |
|
294 |
public boolean equals(Object o){ |
295 |
if(!o.getClass().equals(this.getClass())) |
296 |
return false; |
297 |
AbstractTopologyRule oRule = (AbstractTopologyRule)o; |
298 |
if(!oRule.originLyr.equals(this.originLyr)) |
299 |
return false; |
300 |
if(this instanceof ITwoLyrRule){ |
301 |
if(! (o instanceof ITwoLyrRule)) |
302 |
return false; |
303 |
|
304 |
ITwoLyrRule thisRule = (ITwoLyrRule)this;
|
305 |
ITwoLyrRule oTwoRule = (ITwoLyrRule)oRule; |
306 |
if(! thisRule.getDestinationLyr().equals(oTwoRule.getDestinationLyr()))
|
307 |
return false; |
308 |
} |
309 |
return true; |
310 |
} |
311 |
|
312 |
public int hashCode(){ |
313 |
return 1; |
314 |
} |
315 |
} |
316 |
|