Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / feature / impl / DefaultFeatureSelection.java @ 24178

History | View | Annotate | Download (7.33 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.data.feature.impl;
28

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

    
32
import org.gvsig.fmap.data.DataStoreNotification;
33
import org.gvsig.fmap.data.exceptions.DataException;
34
import org.gvsig.fmap.data.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
    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
     * 
59
     * @see AbstractSetBasedDataSelection#DefaultSelection(int)
60
     */
61
    public DefaultFeatureSelection(FeatureStore featureStore) {
62
        super(featureStore);
63
    }
64

    
65
    /**
66
     * Creates a DefaultFeatureSelection, with a FeatureStore.
67
     * 
68
     * @param featureStore
69
     *            the FeatureStore to load Features from
70
     * @param totalSize
71
     *            the maximum number of Features that may be selected
72
     */
73
    public DefaultFeatureSelection(FeatureStore featureStore, long totalSize) {
74
        super(featureStore, totalSize);
75
    }
76

    
77
    public boolean select(Feature feature) {
78
        if (feature == null) {
79
            return false;
80
        }
81
        return select(feature.getReference());
82
    }
83

    
84
    public boolean select(FeatureSet features) throws DataException {
85
        // TODO: verificar si las features son del mismo FeatureStore
86
        boolean change = false;
87
        disableNotifications();
88
        for (Iterator iter = features.fastIterator(); iter.hasNext();) {
89
            change |= select((Feature) iter.next());
90
        }
91
        enableNotifications();
92
        if (change) {
93
            notifyObservers(DataStoreNotification.SELECTION_CHANGE);
94
        }
95
        return change;
96
    }
97

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

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

    
119
    public boolean isSelected(Feature feature) {
120
        if (feature == null) {
121
            return false;
122
        }
123
        return isSelected(feature.getReference());
124
    }
125

    
126
    public FeatureType getDefaultFeatureType() {
127
        try {
128
            return getFeatureStore().getDefaultFeatureType();
129
        } catch (DataException ex) {
130
            logger.error("Error getting the default feature type "
131
                    + "of the FeatureStore: " + getFeatureStore(), ex);
132
        }
133
        return null;
134
    }
135

    
136
    public List getFeatureTypes() {
137
        // TODO: implement
138
        throw new UnsupportedOperationException("Not implemented");
139
    }
140

    
141
    public long getSize() throws DataException {
142
        return getSelectedCount();
143
    }
144

    
145
    public boolean isEmpty() throws DataException {
146
        return getSelectedCount() == 0;
147
    }
148

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

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

    
167
        Iterator iter = referenceIterator();
168
        for (long l = 0; l < index; l++) {
169
            iter.next();
170
        }
171
        return new FeatureIteratorFacade(iter);
172
    }
173

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

    
184
    /**
185
     * Returns the list of selected values, or the deselected ones if the
186
     * selection has been reversed.
187
     * 
188
     * WARN: not really a fast implementation.
189
     */
190
    public Iterator fastIterator(long index) {
191
        return iterator(index);
192
    }
193
    
194
   /**
195
     * Facade over a Iterator of FeatureReferences, to return Features instead.
196
     * 
197
     * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
198
     */
199
    public class FeatureIteratorFacade implements Iterator {
200

    
201
        final Logger logger = LoggerFactory
202
                .getLogger(FeatureIteratorFacade.class);
203

    
204
        private Iterator refIterator;
205

    
206
        public FeatureIteratorFacade(Iterator refIterator) {
207
            this.refIterator = refIterator;
208
        }
209

    
210
        public boolean hasNext() {
211
            return refIterator.hasNext();
212
        }
213

    
214
        public Object next() {
215
            FeatureReference ref = nextFeatureReference();
216
            try {
217
                return getFeature(ref);
218
            } catch (DataException ex) {
219
                logger.error(
220
                        "Error loading the Feature with FeatureReference: "
221
                                + ref, ex);
222

    
223
                return null;
224
            }
225
        }
226

    
227
        /**
228
         * Returns the next FeatureReference.
229
         * 
230
         * @return the next FeatureReference
231
         */
232
        public FeatureReference nextFeatureReference() {
233
            return (FeatureReference) refIterator.next();
234
        }
235

    
236
        public void remove() {
237
            refIterator.remove();
238
        }
239

    
240
        /**
241
         * Returns a Feature for the FeatureReference.
242
         */
243
        protected Feature getFeature(FeatureReference ref) throws DataException {
244
            return ref.getFeature();
245
        }
246
    }
247
}