Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libTopology / src / org / gvsig / topology / AbstractTopologyRule.java @ 16999

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

    
54
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
55
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
56
import com.iver.cit.gvsig.fmap.core.IFeature;
57
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
58
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
59
import com.iver.utiles.XMLEntity;
60
import com.iver.utiles.swing.threads.CancellableProgressTask;
61

    
62
/**
63
 * Default implementation of ITopologyRule
64
 * 
65
 * @author azabala
66
 * 
67
 */
68
public abstract class AbstractTopologyRule implements IOneLyrRule  {
69
        
70
        /**
71
         * We need a reference to topology to get a layer from its name
72
         * in setXML method.
73
         */
74
        protected Topology topology;
75
        
76
 
77
        protected FLyrVect originLyr;
78
        
79
        /**
80
         * Container for the topology errors detected.
81
         */
82
        protected ITopologyErrorContainer errorContainer;
83
        
84
        /**
85
         * Unique identifier of the rule to distinct it of the rest of rules
86
         * of a topology
87
         */
88
        private int ruleId;
89
        
90
        
91
        
92
        /**
93
         * Constructor.
94
         * 
95
         *  @param originLyr
96
         *            layer which features we are checking
97
         * 
98
         * @param topology reference to the topology that owns this rule.
99
         */
100
        public AbstractTopologyRule(Topology topology, FLyrVect originLyr){
101
                this.topology = topology;
102
                this.originLyr = originLyr;
103
        }
104
        
105
        /**
106
         * Constructor without topology param
107
         * 
108
         * @param originLyr
109
         */
110
        public AbstractTopologyRule(FLyrVect originLyr){
111
                this.originLyr = originLyr;
112
        }
113
        
114
        public AbstractTopologyRule(){
115
                //default constructor. Needed for persistence
116
        }
117
         
118
        public void setOriginLyr(FLyrVect originLyr) {
119
                this.originLyr = originLyr;
120
        }
121
         
122
        public FLyrVect getOriginLyr() {
123
                return originLyr;
124
        }
125
         
126
        
127
        public void setTopologyErrorContainer(ITopologyErrorContainer errorContainer){
128
                this.errorContainer = errorContainer;
129
        }
130
        
131
        public ITopologyErrorContainer getTopologyErrorContainer(){
132
                return errorContainer;
133
        }
134

    
135
        public String getName(){
136
                return getClass().getName();
137
        }
138
        
139
        
140
        public abstract String getDescription();
141
                
142
        
143
         
144
        /**
145
         * Checks if the rule's parameters (sourceLyr, destinationLyr) verify rule
146
         * preconditions (geometry type, etc.)
147
         */
148
        public abstract void checkPreconditions() throws TopologyRuleDefinitionException ;
149
         
150
        
151
        public void checkRule(){
152
                this.checkRule((CancellableProgressTask)null);
153
        }
154
        
155
        public void checkRule(Rectangle2D rect){
156
                checkRule(null, rect);
157
        }
158
        
159
        
160
        public void checkRule(CancellableProgressTask progressMonitor){
161
                try {
162
                        // when we dont pass field names iterator only iterates over
163
                        // geometries
164
                        IFeatureIterator featureIterator = originLyr.getSource().getFeatureIterator();
165
                        while(featureIterator.hasNext()){
166
                                IFeature feature = featureIterator.next();
167
                                if(progressMonitor != null){
168
                                        if(progressMonitor.isCanceled()/*
169
                                                                                                         * ||
170
                                                                                                         * progressMonitor.isFinished()
171
                                                                                                         */){
172
                                                // TODO Maybe we could show progress info of rule
173
                                                // checking.
174
                                                // example: feature 1 of N...etc
175
                                                return;
176
                                        }
177
                                   }
178
                                validateFeature(feature);
179
                        }
180
                } catch (ExpansionFileReadException e) {
181
                        // TODO Auto-generated catch block
182
                        e.printStackTrace();
183
                } catch (ReadDriverException e) {
184
                        // TODO Auto-generated catch block
185
                        e.printStackTrace();
186
                }        
187
        }
188
         
189
        public  void checkRule(CancellableProgressTask progressMonitor, Rectangle2D rect){
190
                try {
191
                        IFeatureIterator iterator = originLyr.getSource().getFeatureIterator(rect, null, null, true);
192
                        while(iterator.hasNext()){
193
                                IFeature feature = iterator.next();
194
                                if(progressMonitor != null){
195
                                        if(progressMonitor.isCanceled()/*
196
                                                                                                         * ||
197
                                                                                                         * progressMonitor.isFinished()
198
                                                                                                         */){
199
                                                // TODO Maybe we could show progress info of rule
200
                                                // checking.
201
                                                // example: feature 1 of N...etc
202
                                                return;
203
                                        }
204
                                   }
205
                                validateFeature(feature);
206
                        }
207
                } catch (ExpansionFileReadException e) {
208
                        // TODO Auto-generated catch block
209
                        e.printStackTrace();
210
                } catch (ReadDriverException e) {
211
                        // TODO Auto-generated catch block
212
                        e.printStackTrace();
213
                }        
214
        }
215
        
216
        public abstract void validateFeature(IFeature feature);
217
        
218
        
219
        public void addTopologyError(TopologyError topologyError){
220
                this.errorContainer.addTopologyError(topologyError);
221
        }
222
        
223
        /*
224
         * Implementation of IPersistence
225
         */
226
        public String getClassName(){
227
                return this.getClass().getName();
228
        }
229
           
230
        public XMLEntity getXMLEntity(){
231
                XMLEntity xml = new XMLEntity();
232
                xml.putProperty("className", getClassName());
233
                xml.putProperty("originLayerName", this.originLyr.getName());
234
                xml.putProperty("ruleId", this.ruleId);
235
                return xml;
236
        }
237
            
238
        public void setXMLEntity(XMLEntity xml){
239
                String originLayerName = "";
240
                if (xml.contains("originLayer")) {
241
                        originLayerName = xml.getStringProperty("originLayer");
242
                        this.originLyr = (FLyrVect) topology.getLayer(originLayerName);
243
                }//if
244
                
245
                if(xml.contains("ruleId")){
246
                        ruleId = xml.getIntProperty("ruleId");
247
                }
248
        }
249
        
250
    public void setId(int ruleId){
251
            this.ruleId = ruleId;
252
    }
253
        
254
        public int getId(){
255
                return ruleId;
256
        }
257

    
258
        public Topology getTopology() {
259
                return topology;
260
        }
261

    
262
        public void setTopology(Topology topology) {
263
                this.topology = topology;
264
        }
265
        
266
        public boolean equals(Object o){
267
                if(o.getClass().equals(this.getClass()))
268
                        return false;
269
                AbstractTopologyRule oRule = (AbstractTopologyRule)o;
270
                if(!oRule.originLyr.equals(this.originLyr))
271
                        return false;
272
                if(this instanceof ITwoLyrRule){
273
                        if(! (o instanceof ITwoLyrRule))
274
                                return false;
275
                        
276
                        ITwoLyrRule thisRule = (ITwoLyrRule)this;
277
                        ITwoLyrRule oTwoRule = (ITwoLyrRule)oRule;
278
                        if(! thisRule.getDescription().equals(oTwoRule.getDestinationLyr()))
279
                                return false;
280
                }
281
                return true;
282
        }
283
        
284
        public int hashCode(){
285
                return 1;
286
        }
287
}
288