Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / feature / impl / DefaultFeatureReferenceSelection.java @ 24248

History | View | Annotate | Download (8.58 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.Collections;
30
import java.util.HashSet;
31
import java.util.Iterator;
32
import java.util.Set;
33

    
34
import org.gvsig.fmap.data.DataStore;
35
import org.gvsig.fmap.data.DataStoreNotification;
36
import org.gvsig.fmap.data.exceptions.DataException;
37
import org.gvsig.fmap.data.feature.FeatureReference;
38
import org.gvsig.fmap.data.feature.FeatureReferenceSelection;
39
import org.gvsig.fmap.data.feature.FeatureStore;
40
import org.gvsig.fmap.data.feature.FeatureStoreNotification;
41
import org.gvsig.fmap.data.feature.impl.commands.AbstractCommandsRecord;
42
import org.gvsig.tools.exception.BaseException;
43
import org.gvsig.tools.observer.Observable;
44
import org.gvsig.tools.observer.impl.BaseObservable;
45
import org.gvsig.tools.persistence.AbstractPersistenceManager;
46
import org.gvsig.tools.persistence.PersistenceException;
47
import org.gvsig.tools.persistence.PersistentState;
48
import org.gvsig.tools.visitor.Visitor;
49

    
50
/**
51
 * Default implementation of a FeatureReferenceSelection, based on the usage of
52
 * a java.util.Set to store individual selected or not selected
53
 * FeatureReferences, depending on the usage of the {@link #reverse()} method.
54
 *
55
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
56
 */
57
public class DefaultFeatureReferenceSelection extends BaseObservable
58
        implements FeatureReferenceSelection {
59

    
60
    private Set selected = new HashSet();
61

    
62
    /**
63
     * Sets how the Set of selected values has to be dealt.
64
     * <p>
65
     * If selected is FALSE, then values into the Set are the selected ones,
66
     * anything else is not selected.
67
     * </p>
68
     * <p>
69
     * If selected is TRUE, then values into the Set are values not selected,
70
     * anything else is selected.
71
     * </p>
72
     */
73
    private boolean reversed = false;
74

    
75
    private long totalSize;
76

    
77
    private DefaultFeatureStore featureStore;
78

    
79
    /**
80
     * Creates a new Selection with the total size of Features from which the
81
     * selection will be performed.
82
     *
83
     * Takes by default Integer.MAX_VALUE as the total size of elements that may
84
     * be selected.
85
     *
86
     * @param featureStore
87
     *            the FeatureStore of the selected FeatureReferences
88
     */
89
    public DefaultFeatureReferenceSelection(FeatureStore featureStore) {
90
        this(featureStore, Long.MAX_VALUE);
91
    }
92

    
93
    /**
94
     * Creates a new Selection with the total size of Features from which the
95
     * selection will be performed.
96
     *
97
     * Takes by default Integer.MAX_VALUE as the total size of elements that may
98
     * be selected.
99
     *
100
     * @param featureStore
101
     *            the FeatureStore of the selected FeatureReferences
102
     * @param totalSize
103
     *            the total size of elements (selected and not selected)
104
     */
105
    public DefaultFeatureReferenceSelection(FeatureStore featureStore,
106
            long totalSize) {
107
        super();
108
        this.totalSize = totalSize;
109
        this.featureStore = (DefaultFeatureStore) featureStore;
110
    }
111

    
112
    public boolean select(FeatureReference reference) {
113
            if (getFeatureStore().isEditing()) {
114
                        getCommands().select(this, reference);
115
                }
116
        boolean change = false;
117
        if (reversed) {
118
            change = selected.remove(reference);
119
        } else {
120
            change = selected.add(reference);
121
        }
122

    
123
        if (change) {
124
            notifyObservers(DataStoreNotification.SELECTION_CHANGE);
125
        }
126

    
127
        return change;
128
    }
129

    
130
    public boolean deselect(FeatureReference reference) {
131
            if (getFeatureStore().isEditing()) {
132
                        getCommands().deselect(this, reference);
133
                }
134
        boolean change = false;
135
        if (reversed) {
136
            change = selected.add(reference);
137
        } else {
138
            change = selected.remove(reference);
139
        }
140
        if (change) {
141
            notifyObservers(DataStoreNotification.SELECTION_CHANGE);
142
        }
143

    
144
        return change;
145
    }
146

    
147
    public void selectAll() {
148
            if (getFeatureStore().isEditing()) {
149
                        getCommands().startComplex("_selectionSelectAll");
150
                        getCommands().selectAll(this);
151
                }
152
        if (!reversed) {
153
            reverse();
154
        }
155
        selected.clear();
156
            if (getFeatureStore().isEditing()) {
157
                        getCommands().endComplex();
158
                }
159
        notifyObservers(DataStoreNotification.SELECTION_CHANGE);
160
    }
161

    
162
    public void deselectAll() {
163
            if (getFeatureStore().isEditing()) {
164
                        getCommands().startComplex("_selectionDeselectAll");
165
                        getCommands().deselectAll(this);
166
                }
167
        if (reversed) {
168
            reverse();
169
        }
170
        selected.clear();
171
            if (getFeatureStore().isEditing()) {
172
                        getCommands().endComplex();
173
                }
174

    
175

    
176
        notifyObservers(DataStoreNotification.SELECTION_CHANGE);
177
    }
178

    
179
    public boolean isSelected(FeatureReference reference) {
180
        if (reversed) {
181
            return !selected.contains(reference);
182
        } else {
183
            return selected.contains(reference);
184
        }
185
    }
186

    
187
    public void reverse() {
188
            if (getFeatureStore().isEditing()) {
189
                        getCommands().selectionReverse(this);
190
                }
191
                reversed = !reversed;
192
        notifyObservers(DataStoreNotification.SELECTION_CHANGE);
193
    }
194

    
195
    /**
196
     * Usage of this method only has sense if you don't use the
197
     * {@link #reverse()} method or if you use the Constructor with the total
198
     * size parameter. Otherwise, it will return Long.MAX_VALUE minus the number
199
     * of selected values if the selection is reversed.
200
     */
201
    public long getSelectedCount() {
202
        if (reversed) {
203
            return totalSize - selected.size();
204
        } else {
205
            return selected.size();
206
        }
207
    }
208

    
209
    public Iterator referenceIterator() {
210
        return Collections.unmodifiableSet(selected).iterator();
211
    }
212

    
213
    public void dispose() {
214
        deselectAll();
215
    }
216

    
217
    public boolean isFromStore(DataStore store) {
218
        return featureStore.equals(store);
219
    }
220

    
221
    public void accept(Visitor visitor) throws BaseException {
222
        for (Iterator iter = selected.iterator(); iter.hasNext();) {
223
            visitor.visit(iter.next());
224
        }
225
    }
226

    
227
    public void update(Observable observable, Object notification) {
228
        // If a Feature is deleted, remove it from the selection Set.
229
        if (notification instanceof FeatureStoreNotification) {
230
            FeatureStoreNotification storeNotif = (FeatureStoreNotification) notification;
231
            if (FeatureStoreNotification.AFTER_DELETE
232
                    .equalsIgnoreCase(storeNotif.getType())) {
233
                selected.remove(storeNotif.getFeature().getReference());
234
            }
235
        }
236
    }
237

    
238

    
239
    /**
240
         * Returns the FeatureStore of the selected FeatureReferences.
241
         *
242
         * @return the featureStore
243
         */
244
        protected FeatureStore getFeatureStore() {
245
                return featureStore;
246
    }
247

    
248
        public void loadState(PersistentState state) throws PersistenceException {
249
                state.set("reversed", reversed);
250
                state.set("totalSize", totalSize);
251
                state.set("selected", selected.iterator());
252
        }
253

    
254
        public void setState(PersistentState state) throws PersistenceException {
255
                this.reversed = state.getBoolean("reversed");
256
                this.totalSize = state.getLong("totalSize");
257
                Iterator it = state.getIterator("selected");
258
                // FIXME: Esto no funcionara bien. Hay que darle una pensada mas.
259
                while( it.hasNext() ) {
260
                        DefaultFeatureReference ref = new DefaultFeatureReference(
261
                                        this.featureStore);
262
                        ref.setState((PersistentState) it.next());
263
                        this.selected.add(ref);
264
                }
265
        }
266

    
267
        public PersistentState getState() throws PersistenceException {
268
                return AbstractPersistenceManager.getState(this);
269
        }
270

    
271
    protected AbstractCommandsRecord getCommands() {
272
                try {
273
                        return (AbstractCommandsRecord) getFeatureStore()
274
                                        .getCommandsRecord();
275
                } catch (DataException e) {
276
                        // Ignore
277
                        return null;
278
                }
279
        }
280

    
281

    
282
}