Statistics
| Revision:

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

History | View | Annotate | Download (6.19 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 void setOriginLyr(FLyrVect originLyr) {
115
                this.originLyr = originLyr;
116
        }
117
         
118
        public FLyrVect getOriginLyr() {
119
                return originLyr;
120
        }
121
         
122
        
123
        public void setTopologyErrorContainer(ITopologyErrorContainer errorContainer){
124
                this.errorContainer = errorContainer;
125
        }
126
        
127
        public ITopologyErrorContainer getTopologyErrorContainer(){
128
                return errorContainer;
129
        }
130

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

    
247
        public Topology getTopology() {
248
                return topology;
249
        }
250

    
251
        public void setTopology(Topology topology) {
252
                this.topology = topology;
253
        }
254
}
255