Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureSelection.java @ 24496

History | View | Annotate | Download (7.68 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Gobernment (CIT)
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {Implement data selection}
26
 */
27
package org.gvsig.fmap.dal.feature.impl;
28

    
29
import java.util.Iterator;
30
import java.util.List;
31

    
32
import org.gvsig.fmap.dal.DataStoreNotification;
33
import org.gvsig.fmap.dal.exceptions.DataException;
34
import org.gvsig.fmap.dal.feature.*;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
/**
39
 * Default implementation of the FeatureSelection interface. Internally, only
40
 * FeatureReference values are stored.
41
 * 
42
 * This implementation performs better if used with the selection related
43
 * methods: select, deselect and isSelected ones.
44
 * 
45
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
46
 */
47
public class DefaultFeatureSelection extends DefaultFeatureReferenceSelection
48
        implements FeatureSelection {
49

    
50
    private static final Logger LOGGER = LoggerFactory
51
            .getLogger(DefaultFeatureSelection.class);
52

    
53
    /**
54
     * Creates a DefaultFeatureSelection, with a FeatureStore.
55
     * 
56
     * @param featureStore
57
     *            the FeatureStore to load Features from
58
     * @throws DataException
59
     *             if there is an error while getting the total number of
60
     *             Features of the Store.
61
     * @see AbstractSetBasedDataSelection#DefaultSelection(int)
62
     */
63
    public DefaultFeatureSelection(DefaultFeatureStore featureStore)
64
            throws DataException {
65
        super(featureStore);
66
    }
67

    
68
    public boolean select(Feature feature) {
69
        if (feature == null) {
70
            return false;
71
        }
72
        return select(feature.getReference());
73
    }
74

    
75
    public boolean select(FeatureSet features) throws DataException {
76
        // TODO: verificar si las features son del mismo FeatureStore
77
        boolean change = false;
78
            if (getFeatureStore().isEditing()) {
79
                        getCommands().startComplex("_selectionSelectFeatureSet");
80
                }
81

    
82
        disableNotifications();
83
        for (Iterator iter = features.fastIterator(); iter.hasNext();) {
84
            change |= select((Feature) iter.next());
85
        }
86
        enableNotifications();
87
            if (getFeatureStore().isEditing()) {
88
                        getCommands().endComplex();
89
                }
90
        if (change) {
91
            notifyObservers(DataStoreNotification.SELECTION_CHANGE);
92
        }
93
        return change;
94
    }
95

    
96
    public boolean deselect(Feature feature) {
97
        if (feature == null) {
98
            return false;
99
        }
100
        return deselect(feature.getReference());
101
    }
102

    
103
    public boolean deselect(FeatureSet features) throws DataException {
104
        // TODO: verificar si las features son del mismo FeatureStore
105
        boolean change = false;
106
            if (getFeatureStore().isEditing()) {
107
                        getCommands().startComplex("_selectionDeselectFeatureSet");
108
                }
109
        disableNotifications();
110
        for (Iterator iter = features.fastIterator(); iter.hasNext();) {
111
            change |= deselect((Feature) iter.next());
112
        }
113
        enableNotifications();
114
            if (getFeatureStore().isEditing()) {
115
                        getCommands().endComplex();
116
                }
117
        if (change) {
118
            notifyObservers(DataStoreNotification.SELECTION_CHANGE);
119
        }
120
        return change;
121
    }
122

    
123
    public boolean isSelected(Feature feature) {
124
        if (feature == null) {
125
            return false;
126
        }
127
        return isSelected(feature.getReference());
128
    }
129

    
130
    public FeatureType getDefaultFeatureType() {
131
        try {
132
            return getFeatureStore().getDefaultFeatureType();
133
        } catch (DataException ex) {
134
            LOGGER.error("Error getting the default feature type "
135
                    + "of the FeatureStore: " + getFeatureStore(), ex);
136
        }
137
        return null;
138
    }
139

    
140
    public List getFeatureTypes() {
141
        // TODO: implement
142
        throw new UnsupportedOperationException("Not implemented");
143
    }
144

    
145
    public long getSize() throws DataException {
146
        return getSelectedCount();
147
    }
148

    
149
    public boolean isEmpty() throws DataException {
150
        return getSelectedCount() == 0;
151
    }
152

    
153
    /**
154
     * Returns the list of selected values, or the deselected ones if the
155
     * selection has been reversed.
156
     */
157
    public Iterator iterator() {
158
        return iterator(0);
159
    }
160

    
161
    /**
162
     * Returns the list of selected values, or the deselected ones if the
163
     * selection has been reversed.
164
     * 
165
     * WARN: not very good performance implementation.
166
     */
167
    public Iterator iterator(long index) {
168
        // TODO: evaluar la opci?n de obtener las features
169
        // mediante una query al FeatureStore.
170

    
171
        Iterator iter = referenceIterator();
172
        for (long l = 0; l < index; l++) {
173
            iter.next();
174
        }
175
        return new FeatureIteratorFacade(iter, getFeatureStore());
176
    }
177

    
178
    /**
179
     * Returns the list of selected values, or the deselected ones if the
180
     * selection has been reversed.
181
     * 
182
     * WARN: not really a fast implementation.
183
     */
184
    public Iterator fastIterator() {
185
        return fastIterator(0);
186
    }
187

    
188
    /**
189
     * Returns the list of selected values, or the deselected ones if the
190
     * selection has been reversed.
191
     * 
192
     * WARN: not really a fast implementation.
193
     */
194
    public Iterator fastIterator(long index) {
195
        return iterator(index);
196
    }
197

    
198
    /**
199
     * Facade over a Iterator of FeatureReferences, to return Features instead.
200
     * 
201
     * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
202
     */
203
    public class FeatureIteratorFacade implements Iterator {
204

    
205
        final Logger logger = LoggerFactory
206
                .getLogger(FeatureIteratorFacade.class);
207

    
208
        private Iterator refIterator;
209

    
210
        private DefaultFeatureStore featureStore;
211

    
212
        public FeatureIteratorFacade(Iterator refIterator,
213
                DefaultFeatureStore featureStore) {
214
            this.refIterator = refIterator;
215
            this.featureStore = featureStore;
216
        }
217

    
218
        public boolean hasNext() {
219
            return refIterator.hasNext();
220
        }
221

    
222
        public Object next() {
223
            FeatureReference ref = nextFeatureReference();
224
            try {
225
                return featureStore.getFeatureByReference(ref);
226
            } catch (DataException ex) {
227
                logger.error(
228
                        "Error loading the Feature with FeatureReference: "
229
                                + ref, ex);
230
                return null;
231
            }
232
        }
233

    
234
        /**
235
         * Returns the next FeatureReference.
236
         * 
237
         * @return the next FeatureReference
238
         */
239
        public FeatureReference nextFeatureReference() {
240
            return (FeatureReference) refIterator.next();
241
        }
242

    
243
        public void remove() {
244
            refIterator.remove();
245
        }
246

    
247
        /**
248
         * Returns a Feature for the FeatureReference.
249
         */
250
        protected Feature getFeature(FeatureReference ref) throws DataException {
251
            return ref.getFeature();
252
        }
253
    }
254
}