Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libTopology / src / org / gvsig / topology / errorfixes / AbstractTopologyErrorFix.java @ 23040

History | View | Annotate | Download (4.78 KB)

1
/*
2
 * Created on 10-abr-2006
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
package org.gvsig.topology.errorfixes;
50

    
51
import java.util.ArrayList;
52
import java.util.List;
53
import java.util.Map;
54

    
55
import org.gvsig.exceptions.BaseException;
56
import org.gvsig.topology.ITopologyErrorFix;
57
import org.gvsig.topology.TopologyError;
58

    
59
import com.iver.cit.gvsig.fmap.core.IFeature;
60
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
61
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
62
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
63
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
64

    
65
/**
66
 * Abstract base class for all those fixes that modifies a topology error's causing feature.
67
 * 
68
 * This fix is not worthy for deletion or adition fixes.
69
 * 
70
 * @author Alvaro Zabala
71
 *
72
 */
73
public abstract class AbstractTopologyErrorFix implements ITopologyErrorFix {
74

    
75
        protected EditableAdapter prepareEditableAdapterFromLayer(FLyrVect lyr) throws BaseException{
76
                
77
                if(! lyr.isEditing()){
78
                        lyr.setEditing(true);
79
                }
80
                
81
                ReadableVectorial source = lyr.getSource();
82
                if(!(source instanceof EditableAdapter))
83
                        throw new BaseException(){
84
                                protected Map values() {
85
                                        return null;
86
                                }};
87
                
88
                EditableAdapter editAdapter = (EditableAdapter) source;
89
                editAdapter.startComplexRow();
90
                return editAdapter;
91
                
92
        }
93
        
94
        
95
        protected EditableAdapter[] prepareEdition(TopologyError topologyError) throws BaseException{
96
                EditableAdapter[] solution = null;
97
                
98
                List<EditableAdapter> solutionList = new ArrayList<EditableAdapter>();
99
                
100
                FLyrVect originLyr = topologyError.getOriginLayer();
101
                EditableAdapter editAdapter = prepareEditableAdapterFromLayer(originLyr);
102
                solutionList.add(editAdapter);
103
                
104
                FLyrVect destinationLyr = topologyError.getDestinationLayer();
105
                if(destinationLyr != null){
106
                        EditableAdapter editAdapter2 = prepareEditableAdapterFromLayer(destinationLyr);
107
                        solutionList.add(editAdapter2);
108
                }
109
                
110
                solution = new EditableAdapter[solutionList.size()];
111
                solutionList.toArray(solution);
112
                return solution;
113
        }
114
        
115
        
116
        public void fix(TopologyError topologyError) throws BaseException {
117
                EditableAdapter[] adapters = prepareEdition(topologyError);
118
                List<IFeature>[] correctedFeatures = fixAlgorithm(topologyError);
119
                if(correctedFeatures != null){
120
                        finalizeEdition(adapters, correctedFeatures, getEditionDescription());
121
                        topologyError.getTopology().removeError(topologyError);
122
                }
123

    
124
        }
125
        
126
        /**
127
         * It returns a List<IFeature> for each layer related with the topology error.
128
         * Index 0 for first layer, Index 1 for second layer, etc
129
         * @param error
130
         * @return
131
         * @throws BaseException
132
         */
133
        public abstract List<IFeature>[] fixAlgorithm(TopologyError error) throws BaseException ;
134
        
135
        public abstract String getEditionDescription();
136
        
137
        protected void finalizeEdition(EditableAdapter[] adapters, List<IFeature>[] newFeatures, String editionDescription) throws BaseException{
138
                for(int i = 0; i < adapters.length; i++){
139
                        List<IFeature> features = newFeatures[i];
140
                        EditableAdapter adapter = adapters[i];
141
                        for(int j = 0; j < features.size(); j++ ){
142
                                IFeature feature = features.get(j);
143
                                //FIXME TopologyError must have the feature index of origin and destination features
144
                                int featureIndex = Integer.parseInt(feature.getID());
145
                                //TODO Probar si la llamada a modifyRow actualiza los features dibujados
146
                                adapter.modifyRow(featureIndex, 
147
                                                                                feature, 
148
                                                                                editionDescription, 
149
                                                                                EditionEvent.GRAPHIC);
150
                                adapter.endComplexRow(editionDescription);
151
                        }//for j
152
                }//for
153
        }
154

    
155
}