root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeatureReferenceSelection.java @ 24496
History | View | Annotate | Download (9.04 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.*; |
30 |
|
31 |
import org.gvsig.fmap.dal.DataStore; |
32 |
import org.gvsig.fmap.dal.DataStoreNotification; |
33 |
import org.gvsig.fmap.dal.exceptions.DataException; |
34 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
35 |
import org.gvsig.fmap.dal.feature.FeatureReferenceSelection; |
36 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
37 |
import org.gvsig.fmap.dal.feature.impl.commands.AbstractCommandsRecord; |
38 |
import org.gvsig.tools.exception.BaseException; |
39 |
import org.gvsig.tools.observer.WeakReferencingObservable; |
40 |
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable; |
41 |
import org.gvsig.tools.persistence.AbstractPersistenceManager; |
42 |
import org.gvsig.tools.persistence.PersistenceException; |
43 |
import org.gvsig.tools.persistence.PersistentState; |
44 |
import org.gvsig.tools.visitor.Visitor; |
45 |
import org.slf4j.Logger; |
46 |
import org.slf4j.LoggerFactory; |
47 |
|
48 |
/**
|
49 |
* Default implementation of a FeatureReferenceSelection, based on the usage of
|
50 |
* a java.util.Set to store individual selected or not selected
|
51 |
* FeatureReferences, depending on the usage of the {@link #reverse()} method.
|
52 |
*
|
53 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
54 |
*/
|
55 |
public class DefaultFeatureReferenceSelection extends |
56 |
BaseWeakReferencingObservable implements FeatureReferenceSelection {
|
57 |
|
58 |
private static final Logger LOGGER = LoggerFactory |
59 |
.getLogger(DefaultFeatureReferenceSelection.class); |
60 |
|
61 |
private Set selected = new HashSet(); |
62 |
|
63 |
/**
|
64 |
* Sets how the Set of selected values has to be dealt.
|
65 |
* <p>
|
66 |
* If selected is FALSE, then values into the Set are the selected ones,
|
67 |
* anything else is not selected.
|
68 |
* </p>
|
69 |
* <p>
|
70 |
* If selected is TRUE, then values into the Set are values not selected,
|
71 |
* anything else is selected.
|
72 |
* </p>
|
73 |
*/
|
74 |
private boolean reversed = false; |
75 |
|
76 |
private long totalSize; |
77 |
|
78 |
private DefaultFeatureStore featureStore;
|
79 |
|
80 |
/**
|
81 |
* Creates a new Selection with the total size of Features from which the
|
82 |
* selection will be performed.
|
83 |
*
|
84 |
* @param featureStore
|
85 |
* the FeatureStore of the selected FeatureReferences
|
86 |
* @throws DataException
|
87 |
* if there is an error while getting the total number of
|
88 |
* Features of the Store.
|
89 |
*/
|
90 |
public DefaultFeatureReferenceSelection(DefaultFeatureStore featureStore)
|
91 |
throws DataException {
|
92 |
super();
|
93 |
this.featureStore = featureStore;
|
94 |
this.totalSize = getFeatureStoreTotalSize();
|
95 |
} |
96 |
|
97 |
public boolean select(FeatureReference reference) { |
98 |
if (getFeatureStore().isEditing()) {
|
99 |
getCommands().select(this, reference);
|
100 |
} |
101 |
boolean change = false; |
102 |
if (reversed) {
|
103 |
change = selected.remove(reference); |
104 |
} else {
|
105 |
change = selected.add(reference); |
106 |
} |
107 |
|
108 |
if (change) {
|
109 |
notifyObservers(DataStoreNotification.SELECTION_CHANGE); |
110 |
} |
111 |
|
112 |
return change;
|
113 |
} |
114 |
|
115 |
public boolean deselect(FeatureReference reference) { |
116 |
if (getFeatureStore().isEditing()) {
|
117 |
getCommands().deselect(this, reference);
|
118 |
} |
119 |
boolean change = false; |
120 |
if (reversed) {
|
121 |
change = selected.add(reference); |
122 |
} else {
|
123 |
change = selected.remove(reference); |
124 |
} |
125 |
|
126 |
if (change) {
|
127 |
notifyObservers(DataStoreNotification.SELECTION_CHANGE); |
128 |
} |
129 |
|
130 |
return change;
|
131 |
} |
132 |
|
133 |
public void selectAll() throws DataException { |
134 |
if (getFeatureStore().isEditing()) {
|
135 |
getCommands().startComplex("_selectionSelectAll");
|
136 |
getCommands().selectAll(this);
|
137 |
} |
138 |
if (!reversed) {
|
139 |
reverse(); |
140 |
} |
141 |
selected.clear(); |
142 |
if (getFeatureStore().isEditing()) {
|
143 |
getCommands().endComplex(); |
144 |
} |
145 |
notifyObservers(DataStoreNotification.SELECTION_CHANGE); |
146 |
} |
147 |
|
148 |
public void deselectAll() throws DataException { |
149 |
if (getFeatureStore().isEditing()) {
|
150 |
getCommands().startComplex("_selectionDeselectAll");
|
151 |
getCommands().deselectAll(this);
|
152 |
} |
153 |
if (reversed) {
|
154 |
reverse(); |
155 |
} |
156 |
selected.clear(); |
157 |
if (getFeatureStore().isEditing()) {
|
158 |
getCommands().endComplex(); |
159 |
} |
160 |
|
161 |
notifyObservers(DataStoreNotification.SELECTION_CHANGE); |
162 |
} |
163 |
|
164 |
public boolean isSelected(FeatureReference reference) { |
165 |
if (reversed) {
|
166 |
return !selected.contains(reference);
|
167 |
} else {
|
168 |
return selected.contains(reference);
|
169 |
} |
170 |
} |
171 |
|
172 |
public void reverse() { |
173 |
if (getFeatureStore().isEditing()) {
|
174 |
getCommands().selectionReverse(this);
|
175 |
} |
176 |
reversed = !reversed; |
177 |
notifyObservers(DataStoreNotification.SELECTION_CHANGE); |
178 |
} |
179 |
|
180 |
public long getSelectedCount() { |
181 |
if (reversed) {
|
182 |
// If the FeatureManager is present, and the store is in editing
|
183 |
// mode, the number of current Features may have changed.
|
184 |
FeatureManager featureManager = getFeatureStore() |
185 |
.getFeatureManager(); |
186 |
if (featureStore.isEditing() && featureManager != null) { |
187 |
return totalSize - selected.size()
|
188 |
+ featureManager.getDeltaSize(); |
189 |
} else {
|
190 |
return totalSize - selected.size();
|
191 |
} |
192 |
} else {
|
193 |
return selected.size();
|
194 |
} |
195 |
} |
196 |
|
197 |
public Iterator referenceIterator() { |
198 |
return Collections.unmodifiableSet(selected).iterator(); |
199 |
} |
200 |
|
201 |
public void dispose() { |
202 |
try {
|
203 |
deselectAll(); |
204 |
} catch (DataException ex) {
|
205 |
LOGGER.error("Error on dispose(), deselecting all selected values",
|
206 |
ex); |
207 |
} |
208 |
} |
209 |
|
210 |
public boolean isFromStore(DataStore store) { |
211 |
return featureStore.equals(store);
|
212 |
} |
213 |
|
214 |
public void accept(Visitor visitor) throws BaseException { |
215 |
for (Iterator iter = selected.iterator(); iter.hasNext();) { |
216 |
visitor.visit(iter.next()); |
217 |
} |
218 |
} |
219 |
|
220 |
public void update(WeakReferencingObservable observable, Object notification) { |
221 |
// If a Feature is deleted, remove it from the selection Set.
|
222 |
if (notification instanceof FeatureStoreNotification) { |
223 |
FeatureStoreNotification storeNotif = (FeatureStoreNotification) notification; |
224 |
if (FeatureStoreNotification.AFTER_DELETE
|
225 |
.equalsIgnoreCase(storeNotif.getType())) { |
226 |
selected.remove(storeNotif.getFeature().getReference()); |
227 |
} |
228 |
} |
229 |
} |
230 |
|
231 |
/**
|
232 |
* Returns the FeatureStore of the selected FeatureReferences.
|
233 |
*
|
234 |
* @return the featureStore
|
235 |
*/
|
236 |
protected DefaultFeatureStore getFeatureStore() {
|
237 |
return featureStore;
|
238 |
} |
239 |
|
240 |
public void loadState(PersistentState state) throws PersistenceException { |
241 |
state.set("reversed", reversed);
|
242 |
state.set("totalSize", totalSize);
|
243 |
state.set("selected", selected.iterator());
|
244 |
} |
245 |
|
246 |
public void setState(PersistentState state) throws PersistenceException { |
247 |
this.reversed = state.getBoolean("reversed"); |
248 |
this.totalSize = state.getLong("totalSize"); |
249 |
Iterator it = state.getIterator("selected"); |
250 |
// FIXME: Esto no funcionara bien. Hay que darle una pensada mas.
|
251 |
while (it.hasNext()) {
|
252 |
DefaultFeatureReference ref = new DefaultFeatureReference(
|
253 |
this.featureStore);
|
254 |
ref.setState((PersistentState) it.next()); |
255 |
this.selected.add(ref);
|
256 |
} |
257 |
} |
258 |
|
259 |
public PersistentState getState() throws PersistenceException { |
260 |
return AbstractPersistenceManager.getState(this); |
261 |
} |
262 |
|
263 |
private long getFeatureStoreTotalSize() throws DataException { |
264 |
return getFeatureStore().getFeatureSet().getSize();
|
265 |
} |
266 |
|
267 |
protected AbstractCommandsRecord getCommands() {
|
268 |
try {
|
269 |
return (AbstractCommandsRecord) getFeatureStore()
|
270 |
.getCommandsRecord(); |
271 |
} catch (DataException e) {
|
272 |
// Ignore
|
273 |
return null; |
274 |
} |
275 |
} |
276 |
|
277 |
} |