28 |
28 |
import java.util.HashSet;
|
29 |
29 |
import java.util.Iterator;
|
30 |
30 |
import java.util.Set;
|
|
31 |
import java.util.logging.Level;
|
|
32 |
import java.util.logging.Logger;
|
31 |
33 |
|
32 |
34 |
import org.gvsig.fmap.dal.DataStore;
|
33 |
35 |
import org.gvsig.fmap.dal.DataStoreNotification;
|
... | ... | |
63 |
65 |
public static final String DYNCLASS_PERSISTENT_NAME =
|
64 |
66 |
"DefaultFeatureReferenceSelection";
|
65 |
67 |
|
66 |
|
protected SelectionData selectionData = new SelectionData();
|
|
68 |
protected SelectionData selectionData = null;
|
67 |
69 |
|
68 |
70 |
private FeatureStore featureStore;
|
69 |
71 |
|
... | ... | |
87 |
89 |
super();
|
88 |
90 |
this.featureStore = featureStore;
|
89 |
91 |
this.helper = new DefaultFeatureSelectionHelper(featureStore);
|
90 |
|
selectionData.setTotalSize(featureStore.getFeatureCount());
|
91 |
92 |
}
|
92 |
93 |
|
93 |
94 |
/**
|
... | ... | |
108 |
109 |
super();
|
109 |
110 |
this.featureStore = featureStore;
|
110 |
111 |
this.helper = helper;
|
111 |
|
selectionData.setTotalSize(featureStore.getFeatureCount());
|
112 |
112 |
}
|
113 |
113 |
|
114 |
114 |
/**
|
... | ... | |
144 |
144 |
getCommands().select(this, reference);
|
145 |
145 |
}
|
146 |
146 |
boolean change = false;
|
147 |
|
if (selectionData.isReversed()) {
|
148 |
|
change = selectionData.remove(reference);
|
|
147 |
if ( this.getData().isReversed()) {
|
|
148 |
change = this.getData().remove(reference);
|
149 |
149 |
} else {
|
150 |
|
change = selectionData.add(reference);
|
|
150 |
change = this.getData().add(reference);
|
151 |
151 |
}
|
152 |
152 |
|
153 |
153 |
if (change) {
|
... | ... | |
175 |
175 |
getCommands().deselect(this, reference);
|
176 |
176 |
}
|
177 |
177 |
boolean change = false;
|
178 |
|
if (selectionData.isReversed()) {
|
179 |
|
change = selectionData.add(reference);
|
|
178 |
if (this.getData().isReversed()) {
|
|
179 |
change = this.getData().add(reference);
|
180 |
180 |
} else {
|
181 |
|
change = selectionData.remove(reference);
|
|
181 |
change = this.getData().remove(reference);
|
182 |
182 |
}
|
183 |
183 |
|
184 |
184 |
if (change) {
|
... | ... | |
202 |
202 |
getCommands().startComplex("_selectionSelectAll");
|
203 |
203 |
getCommands().selectAll(this);
|
204 |
204 |
}
|
205 |
|
if (!selectionData.isReversed()) {
|
206 |
|
selectionData.setReversed(true);
|
|
205 |
if (!this.getData().isReversed()) {
|
|
206 |
this.getData().setReversed(true);
|
207 |
207 |
}
|
208 |
208 |
clearFeatureReferences();
|
209 |
209 |
if (undoable && getFeatureStore().isEditing()) {
|
... | ... | |
222 |
222 |
* if the action must be undoable
|
223 |
223 |
*/
|
224 |
224 |
public void deselectAll(boolean undoable) throws DataException {
|
|
225 |
if( this.selectionData==null ) {
|
|
226 |
return;
|
|
227 |
}
|
225 |
228 |
if (undoable && getFeatureStore().isEditing()) {
|
226 |
229 |
getCommands().startComplex("_selectionDeselectAll");
|
227 |
230 |
getCommands().deselectAll(this);
|
228 |
231 |
}
|
229 |
|
if (selectionData.isReversed()) {
|
230 |
|
selectionData.setReversed(false);
|
|
232 |
if (this.getData().isReversed()) {
|
|
233 |
this.getData().setReversed(false);
|
231 |
234 |
}
|
232 |
235 |
clearFeatureReferences();
|
233 |
236 |
if (undoable && getFeatureStore().isEditing()) {
|
... | ... | |
238 |
241 |
}
|
239 |
242 |
|
240 |
243 |
public boolean isSelected(FeatureReference reference) {
|
241 |
|
if (selectionData.isReversed()) {
|
242 |
|
return !selectionData.contains(reference);
|
|
244 |
if( this.selectionData==null ) {
|
|
245 |
return false;
|
|
246 |
}
|
|
247 |
if (this.getData().isReversed()) {
|
|
248 |
return !this.getData().contains(reference);
|
243 |
249 |
} else {
|
244 |
|
return selectionData.contains(reference);
|
|
250 |
return this.getData().contains(reference);
|
245 |
251 |
}
|
246 |
252 |
}
|
247 |
253 |
|
... | ... | |
258 |
264 |
if (undoable && getFeatureStore().isEditing()) {
|
259 |
265 |
getCommands().selectionReverse(this);
|
260 |
266 |
}
|
261 |
|
selectionData.setReversed(!selectionData.isReversed());
|
|
267 |
this.getData().setReversed(!this.getData().isReversed());
|
262 |
268 |
notifyObservers(DataStoreNotification.SELECTION_CHANGE);
|
263 |
269 |
}
|
|
270 |
|
|
271 |
public boolean isEmpty() {
|
|
272 |
if( this.selectionData == null ) {
|
|
273 |
return true;
|
|
274 |
}
|
|
275 |
return this.getSelectedCount()==0;
|
|
276 |
}
|
264 |
277 |
|
265 |
278 |
public long getSelectedCount() {
|
266 |
|
if (selectionData.isReversed()) {
|
267 |
|
return selectionData.getTotalSize() - selectionData.getSize()
|
|
279 |
if( this.selectionData == null ) {
|
|
280 |
return 0;
|
|
281 |
}
|
|
282 |
if (this.getData().isReversed()) {
|
|
283 |
return this.getData().getTotalSize() - this.getData().getSize()
|
268 |
284 |
+ helper.getFeatureStoreDeltaSize();
|
269 |
285 |
} else {
|
270 |
|
return selectionData.getSize();
|
|
286 |
return this.getData().getSize();
|
271 |
287 |
}
|
272 |
288 |
}
|
273 |
289 |
|
274 |
290 |
public Iterator referenceIterator() {
|
275 |
|
return Collections.unmodifiableSet(selectionData.getSelected())
|
|
291 |
return Collections.unmodifiableSet(this.getData().getSelected())
|
276 |
292 |
.iterator();
|
277 |
293 |
}
|
278 |
294 |
|
... | ... | |
286 |
302 |
}
|
287 |
303 |
|
288 |
304 |
public void accept(Visitor visitor) throws BaseException {
|
289 |
|
for (Iterator iter = selectionData.getSelected().iterator(); iter
|
|
305 |
if( this.selectionData==null ) {
|
|
306 |
return;
|
|
307 |
}
|
|
308 |
for (Iterator iter = this.getData().getSelected().iterator(); iter
|
290 |
309 |
.hasNext();) {
|
291 |
310 |
visitor.visit(iter.next());
|
292 |
311 |
}
|
... | ... | |
299 |
318 |
FeatureStoreNotification storeNotif = (FeatureStoreNotification) notification;
|
300 |
319 |
if (FeatureStoreNotification.AFTER_DELETE
|
301 |
320 |
.equalsIgnoreCase(storeNotif.getType())) {
|
302 |
|
selectionData.remove(storeNotif.getFeature().getReference());
|
|
321 |
this.getData().remove(storeNotif.getFeature().getReference());
|
303 |
322 |
}
|
304 |
323 |
}
|
305 |
324 |
}
|
306 |
325 |
|
307 |
326 |
public SelectionData getData() {
|
|
327 |
if( selectionData==null ) {
|
|
328 |
selectionData = new SelectionData();
|
|
329 |
try {
|
|
330 |
selectionData.setTotalSize(featureStore.getFeatureCount());
|
|
331 |
} catch (DataException ex) {
|
|
332 |
throw new RuntimeException("Can't initialize SelectionData, don't get the feature count.",ex);
|
|
333 |
}
|
|
334 |
}
|
308 |
335 |
return selectionData;
|
309 |
336 |
}
|
310 |
337 |
|
... | ... | |
316 |
343 |
public String toString() {
|
317 |
344 |
return getClass().getName() + ": " + getSelectedCount()
|
318 |
345 |
+ " features selected, reversed = "
|
319 |
|
+ selectionData.isReversed() + ", featureIds contained: "
|
320 |
|
+ selectionData.getSelected();
|
|
346 |
+ this.getData().isReversed() + ", featureIds contained: "
|
|
347 |
+ this.getData().getSelected();
|
321 |
348 |
}
|
322 |
349 |
|
323 |
350 |
protected boolean isReversed() {
|
324 |
|
return selectionData.isReversed();
|
|
351 |
if( this.selectionData==null ) {
|
|
352 |
return false;
|
|
353 |
}
|
|
354 |
return this.getData().isReversed();
|
325 |
355 |
}
|
326 |
356 |
|
327 |
357 |
/**
|
328 |
358 |
* Removes all the stored FeatureRefence objects.
|
329 |
359 |
*/
|
330 |
360 |
protected void clearFeatureReferences() {
|
331 |
|
selectionData.clear();
|
|
361 |
if( this.selectionData==null ) {
|
|
362 |
return;
|
|
363 |
}
|
|
364 |
this.getData().clear();
|
332 |
365 |
}
|
333 |
366 |
|
334 |
367 |
/**
|
... | ... | |
445 |
478 |
|
446 |
479 |
public void saveToState(PersistentState state) throws PersistenceException {
|
447 |
480 |
state.set("store", featureStore);
|
448 |
|
state.set("reversed", selectionData.isReversed());
|
449 |
|
state.set("totalSize", selectionData.getTotalSize());
|
450 |
|
state.set("selected", selectionData.getSelected().iterator());
|
|
481 |
state.set("reversed", this.getData().isReversed());
|
|
482 |
state.set("totalSize", this.getData().getTotalSize());
|
|
483 |
state.set("selected", this.getData().getSelected().iterator());
|
451 |
484 |
}
|
452 |
485 |
|
453 |
486 |
public void loadFromState(PersistentState state)
|
454 |
487 |
throws PersistenceException {
|
455 |
488 |
featureStore = (FeatureStore)state.get("store");
|
456 |
489 |
helper = new DefaultFeatureSelectionHelper((DefaultFeatureStore)featureStore);
|
457 |
|
selectionData.setReversed(state.getBoolean("reversed"));
|
458 |
|
selectionData.setTotalSize(state.getLong("totalSize"));
|
|
490 |
this.getData().setReversed(state.getBoolean("reversed"));
|
|
491 |
this.getData().setTotalSize(state.getLong("totalSize"));
|
459 |
492 |
Iterator it = state.getIterator("selected");
|
460 |
493 |
while (it.hasNext()) {
|
461 |
494 |
DefaultFeatureReference ref = (DefaultFeatureReference) it.next();
|
462 |
|
selectionData.add(ref);
|
|
495 |
this.getData().add(ref);
|
463 |
496 |
}
|
464 |
497 |
|
465 |
498 |
/*
|
... | ... | |
565 |
598 |
}
|
566 |
599 |
}
|
567 |
600 |
|
|
601 |
@Override
|
568 |
602 |
public Object clone() throws CloneNotSupportedException {
|
569 |
603 |
DefaultFeatureReferenceSelection clone = (DefaultFeatureReferenceSelection) super
|
570 |
604 |
.clone();
|
571 |
605 |
// Original observers aren't cloned
|
572 |
606 |
clone.delegateObservable = new DelegateWeakReferencingObservable(clone);
|
573 |
607 |
// Clone internal data
|
574 |
|
clone.selectionData = (SelectionData) selectionData.clone();
|
|
608 |
clone.selectionData = (SelectionData) this.getData().clone();
|
575 |
609 |
// featureStore and helper are already swallow cloned by our parent
|
576 |
610 |
return clone;
|
577 |
611 |
}
|