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 23824 cordinyana
/* 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 23894 jjdelcerro
 *
6 23824 cordinyana
 * 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 23894 jjdelcerro
 *
11 23824 cordinyana
 * 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 23894 jjdelcerro
 *
16 23824 cordinyana
 * 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 23894 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 23824 cordinyana
 * MA  02110-1301, USA.
20 23894 jjdelcerro
 *
21 23824 cordinyana
 */
22
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {Implement data selection}
26
 */
27 23754 jjdelcerro
package org.gvsig.fmap.data.feature.impl;
28
29 23824 cordinyana
import java.util.Iterator;
30
import java.util.List;
31
32 23928 cordinyana
import org.gvsig.fmap.data.DataStoreNotification;
33
import org.gvsig.fmap.data.exceptions.DataException;
34
import org.gvsig.fmap.data.feature.*;
35 24023 cordinyana
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37 23754 jjdelcerro
38 23824 cordinyana
/**
39 23928 cordinyana
 * 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 24023 cordinyana
 *
45 23824 cordinyana
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
46
 */
47 23928 cordinyana
public class DefaultFeatureSelection extends DefaultFeatureReferenceSelection
48 23824 cordinyana
        implements FeatureSelection {
49 23754 jjdelcerro
50 24023 cordinyana
    final Logger logger = LoggerFactory
51
            .getLogger(DefaultFeatureSelection.class);
52 23754 jjdelcerro
53 23824 cordinyana
    /**
54
     * Creates a DefaultFeatureSelection, with a FeatureStore.
55 24023 cordinyana
     *
56 23824 cordinyana
     * @param featureStore
57
     *            the FeatureStore to load Features from
58 24023 cordinyana
     *
59 23928 cordinyana
     * @see AbstractSetBasedDataSelection#DefaultSelection(int)
60 23824 cordinyana
     */
61
    public DefaultFeatureSelection(FeatureStore featureStore) {
62 23928 cordinyana
        super(featureStore);
63 23824 cordinyana
    }
64 23754 jjdelcerro
65 23824 cordinyana
    /**
66
     * Creates a DefaultFeatureSelection, with a FeatureStore.
67 24023 cordinyana
     *
68 23824 cordinyana
     * @param featureStore
69
     *            the FeatureStore to load Features from
70
     * @param totalSize
71
     *            the maximum number of Features that may be selected
72
     */
73 23928 cordinyana
    public DefaultFeatureSelection(FeatureStore featureStore, long totalSize) {
74
        super(featureStore, totalSize);
75 23824 cordinyana
    }
76 23754 jjdelcerro
77 23928 cordinyana
    public boolean select(Feature feature) {
78
        if (feature == null) {
79
            return false;
80
        }
81
        return select(feature.getReference());
82 23824 cordinyana
    }
83 23754 jjdelcerro
84 23928 cordinyana
    public boolean select(FeatureSet features) throws DataException {
85
        // TODO: verificar si las features son del mismo FeatureStore
86
        boolean change = false;
87 24178 cordinyana
        disableNotifications();
88 23928 cordinyana
        for (Iterator iter = features.fastIterator(); iter.hasNext();) {
89
            change |= select((Feature) iter.next());
90
        }
91
        enableNotifications();
92
        if (change) {
93 24178 cordinyana
            notifyObservers(DataStoreNotification.SELECTION_CHANGE);
94 23928 cordinyana
        }
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 24178 cordinyana
        disableNotifications();
109 23928 cordinyana
        for (Iterator iter = features.fastIterator(); iter.hasNext();) {
110
            change |= deselect((Feature) iter.next());
111
        }
112
        enableNotifications();
113
        if (change) {
114 24178 cordinyana
            notifyObservers(DataStoreNotification.SELECTION_CHANGE);
115 23928 cordinyana
        }
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 23824 cordinyana
    public FeatureType getDefaultFeatureType() {
127 23928 cordinyana
        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 23824 cordinyana
    }
135 23754 jjdelcerro
136 23824 cordinyana
    public List getFeatureTypes() {
137
        // TODO: implement
138
        throw new UnsupportedOperationException("Not implemented");
139
    }
140 23754 jjdelcerro
141 23928 cordinyana
    public long getSize() throws DataException {
142 23824 cordinyana
        return getSelectedCount();
143
    }
144
145 23928 cordinyana
    public boolean isEmpty() throws DataException {
146
        return getSelectedCount() == 0;
147
    }
148
149 23824 cordinyana
    /**
150 23928 cordinyana
     * 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 23824 cordinyana
     * WARN: not very good performance implementation.
162
     */
163 23928 cordinyana
    public Iterator iterator(long index) {
164
        // TODO: evaluar la opci?n de obtener las features
165
        // mediante una query al FeatureStore.
166 24023 cordinyana
167 23928 cordinyana
        Iterator iter = referenceIterator();
168
        for (long l = 0; l < index; l++) {
169 23824 cordinyana
            iter.next();
170
        }
171
        return new FeatureIteratorFacade(iter);
172
    }
173
174 23928 cordinyana
    /**
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 23824 cordinyana
    }
183
184 23928 cordinyana
    /**
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 24151 cordinyana
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 23824 cordinyana
}