Statistics
| Revision:

root / trunk / libraries / libDataSource / src / org / gvsig / data / vectorial / FeatureManager.java @ 20469

History | View | Annotate | Download (6.8 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42

    
43
package org.gvsig.data.vectorial;
44

    
45
import java.util.ArrayList;
46
import java.util.HashMap;
47
import java.util.Iterator;
48

    
49
import org.gvsig.data.vectorial.expansionadapter.IExpansionAdapter;
50

    
51

    
52
/**
53
 * DOCUMENT ME!
54
 *
55
 * @author Vicente Caballero Navarro
56
 */
57
public class FeatureManager {
58
    protected HashMap relations = new HashMap();//<IFeatureID,Integer>
59
    protected IExpansionAdapter expansionAdapter;
60
    protected ArrayList deletedFeatures = new ArrayList();//<IFeatureID>
61
    protected int num=0;
62
    public FeatureManager(IExpansionAdapter expansionAdapter){
63
            this.expansionAdapter=expansionAdapter;
64
    }
65

    
66
    public void deleteFeature(IFeatureID id) {
67
        deletedFeatures.add(id);
68
        num--;
69
    }
70

    
71
    /**
72
     * DOCUMENT ME!
73
     *
74
     * @param feature DOCUMENT ME!
75
     */
76
    public void addFeature(IFeature feature) {
77
        int pos = expansionAdapter.addObject(feature);
78
        relations.put(feature.getID(), new Integer(pos));
79
        num++;
80
    }
81

    
82
    /**
83
     * DOCUMENT ME!
84
     *
85
     * @param id DOCUMENT ME!
86
     */
87
    public void deleteLastFeature(IFeatureID id) {
88
        expansionAdapter.deleteLastObject();
89
        relations.remove(id);
90
        num--;
91
    }
92

    
93
    /**
94
     * DOCUMENT ME!
95
     *
96
     * @param id DOCUMENT ME!
97
     *
98
     * @return DOCUMENT ME!
99
     */
100
    public boolean contains(IFeatureID id) {
101
        return relations.containsKey(id);
102
    }
103

    
104
    /**
105
     * DOCUMENT ME!
106
     *
107
     * @param id DOCUMENT ME!
108
     *
109
     * @return DOCUMENT ME!
110
     * @throws IsNotFeatureSettingException
111
     */
112
    public IFeature getFeature(IFeatureID id,IFeatureStore store) throws IsNotFeatureSettingException {
113
        int num = ((Integer) relations.get(id)).intValue();
114

    
115
        IFeature feature=(IFeature)expansionAdapter.getObject(num);
116
               return getCorrectFeature(feature, store);
117
    }
118

    
119
    private IFeature getCorrectFeature(IFeature feature,IFeatureStore store) throws IsNotFeatureSettingException {
120
             Iterator iterator=store.getDefaultFeatureType().iterator();
121
         IFeature newFeature=store.createFeature(store.getDefaultFeatureType());
122
         ((Feature)newFeature).loading();
123
         while (iterator.hasNext()) {
124
                         IFeatureAttributeDescriptor fad = (IFeatureAttributeDescriptor) iterator.next();
125
                         int i=fad.ordinal();
126
                         if (i<feature.size()){
127
                                 newFeature.set(i,feature.get(i));
128
                         }else{
129
                                 newFeature.set(i,fad.getDefaultValue());
130
                         }
131
                 }
132
         ((Feature)newFeature).stopLoading();
133
         return newFeature;
134
        }
135

    
136
        /**
137
     * DOCUMENT ME!
138
     *
139
     * @param feature DOCUMENT ME!
140
     * @param oldFeature DOCUMENT ME!
141
     */
142
    public void updateFeature(IFeature feature, IFeature oldFeature) {
143
        deletedFeatures.add(oldFeature.getID());
144

    
145
        int num = expansionAdapter.updateObject(feature);
146

    
147
        /*
148
         * Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
149
         * fichero de expansi?n.
150
         */
151
        relations.put(feature.getID(), new Integer(num));
152
    }
153

    
154
    /**
155
     * DOCUMENT ME!
156
     *
157
     * @param id DOCUMENT ME!
158
     */
159
    public void restoreFeature(IFeatureID id) {
160
        deletedFeatures.remove(id);
161
        num++;
162
    }
163
    public int getNum(){
164
            return num;
165
    }
166

    
167
    public IFeature getFeature(int i,IFeatureStore store) throws IsNotFeatureSettingException{
168
            IFeature feature=(IFeature)expansionAdapter.getObject(i);
169
            return getCorrectFeature(feature,store);
170
    }
171
    public boolean isDeleted(IFeature feature){
172
            return deletedFeatures.contains(feature.getID());
173
    }
174

    
175
        public void clear() {
176
                relations.clear();//<IFeatureID,Integer>
177
            expansionAdapter.close();
178
            deletedFeatures.clear();//<IFeatureID>
179
            num=0;
180
        }
181

    
182

    
183
//        public IFeatureCollection getDataCollection(IDataCollection originalCollection, IFeatureType type, String filter, String order) {
184
//                MemoryFeatureCollection memoryCollection=new MemoryFeatureCollection();
185
////                TODO Filtrar y ordenar, quedar? tambi?n pendiente el IFeatureType que se pasa como
186
//                //par?metro ya que aqu? lo ?nico que introducimos al DataCollection son IFeatureID,
187
//                //todav?a no creamos IFeatures
188
//                for (int i=0;i<relations.size();i++){
189
//                        IFeature feature=(IFeature)expansionAdapter.getObject(i);
190
//                        if (!deletedFeatures.contains(feature.getID())){
191
//                                memoryCollection.add(feature);
192
//                        }
193
//                }
194
//                Iterator iter=originalCollection.iterator();
195
//                int i=0;
196
//                featureStore.open();
197
//                while (iter.hasNext()) {
198
//                        IFeature feature = (IFeature) iter.next();
199
//                        if (!deletedFeatures.contains(feature.getID())){
200
//                                memoryCollection.add(feature);
201
//                        }
202
//                        i++;
203
//                }
204
//
205
//                //TODO en el filtro de las features originales hay que a?adir, que no muestre las que est?n el el HashMap deletedFeatures
206
////                IFeatureCollection featureCollection=(IFeatureCollection)featureStore.getDataCollection(type,filter,order);
207
//
208
//
209
//                EditingFeatureCollection collection=new EditingFeatureCollection(originalCollection, memoryCollection,type);
210
////                collection.
211
////                MemoryFeatureCollection collection=new MemoryFeatureCollection();
212
//                //TODO Filtrar y ordenar, quedar? tambi?n pendiente el IFeatureType que se pasa como
213
//                //par?metro ya que aqu? lo ?nico que introducimos al DataCollection son IFeatureID,
214
//                //todav?a no creamos IFeatures
215
////                featureStore.createDefaultFeature()
216
////                for (int i=0;i<driver.getFeatureCount();i++){
217
////                        MemoryFeatureID id=new MemoryFeatureID(driver.getFeatureByPosition(type,i));
218
////                        collection.add(id);
219
////                }
220
//                return collection;
221
//
222
//        }
223

    
224
}