svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / DefaultFeatureSelection.java @ 46309
History | View | Annotate | Download (25.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.dal.feature.impl; |
24 |
|
25 |
import java.util.ArrayList; |
26 |
import java.util.HashMap; |
27 |
import java.util.Iterator; |
28 |
import java.util.List; |
29 |
import java.util.Map; |
30 |
import java.util.Map.Entry; |
31 |
|
32 |
import org.gvsig.fmap.dal.DataStoreNotification; |
33 |
import org.gvsig.fmap.dal.exception.DataException; |
34 |
import org.gvsig.fmap.dal.exception.DataRuntimeException; |
35 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
36 |
import org.gvsig.fmap.dal.feature.Feature; |
37 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
38 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
39 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
40 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
41 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
42 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
43 |
import org.gvsig.fmap.dal.feature.FeatureType; |
44 |
import org.gvsig.fmap.dal.feature.exception.ReversedSelectionIteratorException; |
45 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReferenceSelection.SelectionData; |
46 |
import org.gvsig.fmap.dal.feature.impl.featureset.AbstractFeatureSet; |
47 |
import org.gvsig.fmap.dal.feature.impl.featureset.DefaultFeatureSet; |
48 |
import org.gvsig.fmap.dal.feature.impl.undo.FeatureCommandsStack; |
49 |
import org.gvsig.tools.ToolsLocator; |
50 |
import org.gvsig.tools.dispose.DisposableIterator; |
51 |
import org.gvsig.tools.dispose.DisposeUtils; |
52 |
import org.gvsig.tools.dynobject.DynStruct; |
53 |
import org.gvsig.tools.exception.BaseException; |
54 |
import org.gvsig.tools.observer.Observable; |
55 |
import org.gvsig.tools.observer.Observer; |
56 |
import org.gvsig.tools.persistence.PersistenceManager; |
57 |
import org.gvsig.tools.persistence.PersistentState; |
58 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
59 |
import org.gvsig.tools.visitor.Visitor; |
60 |
import org.slf4j.Logger; |
61 |
import org.slf4j.LoggerFactory; |
62 |
|
63 |
/**
|
64 |
* Default implementation of the FeatureSelection interface. Internally, only
|
65 |
* FeatureReference values are stored.
|
66 |
*
|
67 |
* This implementation performs better if used with the selection related
|
68 |
* methods: select, deselect and isSelected ones.
|
69 |
*
|
70 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
71 |
*/
|
72 |
public class DefaultFeatureSelection extends AbstractFeatureSet |
73 |
implements FeatureSelection {
|
74 |
|
75 |
public class RemoveFromFeatureSelectionException extends DataRuntimeException { |
76 |
|
77 |
/**
|
78 |
*
|
79 |
*/
|
80 |
private static final long serialVersionUID = 2636692469445838928L; |
81 |
private final static String MESSAGE_FORMAT = "Can't remove feature from reversed selection."; |
82 |
private final static String MESSAGE_KEY = "_RemoveFromFeatureSelectionException"; |
83 |
|
84 |
public RemoveFromFeatureSelectionException(Throwable cause) { |
85 |
super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
86 |
//setValue("store", store);
|
87 |
} |
88 |
} |
89 |
|
90 |
/**
|
91 |
* Facade over a Iterator of FeatureReferences, to return Features instead.
|
92 |
*
|
93 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
94 |
*/
|
95 |
private class FeatureIteratorFacade implements DisposableIterator { |
96 |
|
97 |
private final Logger LOGGER = LoggerFactory |
98 |
.getLogger(FeatureIteratorFacade.class); |
99 |
|
100 |
private java.util.Iterator refIterator;
|
101 |
|
102 |
private FeatureStore featureStore;
|
103 |
private Feature currentFeature = null; |
104 |
|
105 |
public FeatureIteratorFacade(java.util.Iterator iter,
|
106 |
FeatureStore featureStore) { |
107 |
this.refIterator = iter;
|
108 |
this.featureStore = featureStore;
|
109 |
} |
110 |
|
111 |
@Override
|
112 |
public boolean hasNext() { |
113 |
return refIterator.hasNext();
|
114 |
} |
115 |
|
116 |
@Override
|
117 |
public Object next() { |
118 |
FeatureReference ref = nextFeatureReference(); |
119 |
try {
|
120 |
currentFeature = featureStore.getFeatureByReference(ref); |
121 |
return currentFeature;
|
122 |
} catch (DataException ex) {
|
123 |
LOGGER.error( |
124 |
"Error loading the Feature with FeatureReference: "
|
125 |
+ ref, ex); |
126 |
return null; |
127 |
} |
128 |
} |
129 |
|
130 |
/**
|
131 |
* Returns the next FeatureReference.
|
132 |
*
|
133 |
* @return the next FeatureReference
|
134 |
*/
|
135 |
public FeatureReference nextFeatureReference() {
|
136 |
return (FeatureReference) refIterator.next();
|
137 |
} |
138 |
|
139 |
@Override
|
140 |
public void remove() { |
141 |
try {
|
142 |
featureStore.delete(currentFeature); |
143 |
refIterator.remove(); |
144 |
} catch (DataException e) {
|
145 |
throw new RemoveFromFeatureSelectionException(e); |
146 |
} |
147 |
} |
148 |
|
149 |
public class RemoveFromFeatureSelectionException extends DataRuntimeException { |
150 |
|
151 |
/**
|
152 |
*
|
153 |
*/
|
154 |
private static final long serialVersionUID = 2636692469445838928L; |
155 |
private final static String MESSAGE_FORMAT = "Can't remove feature from selection."; |
156 |
private final static String MESSAGE_KEY = "_RemoveFromFeatureSelectionException"; |
157 |
|
158 |
public RemoveFromFeatureSelectionException(Throwable cause) { |
159 |
super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
160 |
//setValue("store", store);
|
161 |
} |
162 |
} |
163 |
|
164 |
@Override
|
165 |
public void dispose() { |
166 |
if (refIterator instanceof DisposableIterator) { |
167 |
((DisposableIterator) refIterator).dispose(); |
168 |
} |
169 |
refIterator = null;
|
170 |
featureStore = null;
|
171 |
} |
172 |
} |
173 |
|
174 |
/**
|
175 |
* Facade over a Iterator of FeatureReferences, to return Features instead,
|
176 |
* when the Selection is reversed
|
177 |
*
|
178 |
* @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
179 |
*/
|
180 |
private class ReversedFeatureIteratorFacade implements DisposableIterator { |
181 |
|
182 |
private SelectionData selectionData;
|
183 |
|
184 |
private DisposableIterator iterator;
|
185 |
|
186 |
private Feature nextFeature = null; |
187 |
private Feature currentFeature = null; |
188 |
|
189 |
private FeatureSet featureSet;
|
190 |
|
191 |
public ReversedFeatureIteratorFacade(SelectionData selectionData,
|
192 |
FeatureStore featureStore, boolean fastIterator) {
|
193 |
this.selectionData = selectionData;
|
194 |
|
195 |
// Load a Set with all the store features
|
196 |
try {
|
197 |
featureSet = new IgnoreInsertAndUpdateFeatureSet(
|
198 |
(DefaultFeatureStore) featureStore, |
199 |
new DefaultFeatureQuery(featureStore.getDefaultFeatureType())
|
200 |
); |
201 |
iterator = featureSet.fastIterator(); |
202 |
} catch (DataException ex) {
|
203 |
throw new ReversedSelectionIteratorException(ex); |
204 |
} |
205 |
|
206 |
// Filter the features not selected and position in the next
|
207 |
// selected feature
|
208 |
positionInNextElement(); |
209 |
} |
210 |
|
211 |
@Override
|
212 |
public boolean hasNext() { |
213 |
return nextFeature != null; |
214 |
} |
215 |
|
216 |
@Override
|
217 |
public Object next() { |
218 |
featureIterators.remove(currentFeature); |
219 |
currentFeature = nextFeature.getCopy(); |
220 |
featureIterators.put(currentFeature, this);
|
221 |
positionInNextElement(); |
222 |
return currentFeature;
|
223 |
} |
224 |
|
225 |
@Override
|
226 |
public void remove() { |
227 |
try {
|
228 |
featureSet.delete(currentFeature); |
229 |
} catch (DataException e) {
|
230 |
throw new RemoveFromFeatureSelectionException(e); |
231 |
|
232 |
} |
233 |
} |
234 |
|
235 |
private void positionInNextElement() { |
236 |
nextFeature = null;
|
237 |
while (iterator.hasNext()) {
|
238 |
nextFeature = (Feature) iterator.next(); |
239 |
if (selectionData.contains(nextFeature.getReference())) {
|
240 |
nextFeature = null;
|
241 |
} else {
|
242 |
break;
|
243 |
} |
244 |
} |
245 |
} |
246 |
|
247 |
@Override
|
248 |
public void dispose() { |
249 |
this.featureSet.dispose();
|
250 |
this.iterator.dispose();
|
251 |
this.selectionData = null; |
252 |
this.nextFeature = null; |
253 |
} |
254 |
} |
255 |
|
256 |
private Map featureTypeCounts = new HashMap(1); |
257 |
private final Map<Feature, Iterator> featureIterators = new HashMap<>(); |
258 |
private final DefaultFeatureReferenceSelection featureReferenceSelection; |
259 |
|
260 |
/**
|
261 |
* Creates a DefaultFeatureSelection, with a FeatureStore.
|
262 |
*
|
263 |
* @param featureStore the FeatureStore to load Features from
|
264 |
* @throws DataException if there is an error while getting the total number
|
265 |
* of Features of the Store.
|
266 |
* @see AbstractSetBasedDataSelection#DefaultSelection(int)
|
267 |
*/
|
268 |
public DefaultFeatureSelection(DefaultFeatureStore featureStore)
|
269 |
throws DataException {
|
270 |
this.featureReferenceSelection = new DefaultFeatureReferenceSelection(featureStore); |
271 |
} |
272 |
|
273 |
/**
|
274 |
* Creates a new Selection with the total size of Features from which the
|
275 |
* selection will be performed.
|
276 |
*
|
277 |
* @param featureStore the FeatureStore of the selected FeatureReferences
|
278 |
* @param helper to get some information of the Store
|
279 |
* @throws DataException if there is an error while getting the total number
|
280 |
* of Features of the Store.
|
281 |
*/
|
282 |
public DefaultFeatureSelection(FeatureStore featureStore,
|
283 |
FeatureSelectionHelper helper) throws DataException {
|
284 |
this.featureReferenceSelection = new DefaultFeatureReferenceSelection(featureStore, helper); |
285 |
} |
286 |
|
287 |
/**
|
288 |
* Constructor used by the persistence manager. Don't use directly. After to
|
289 |
* invoke this method, the persistence manager calls the the method
|
290 |
* {@link #loadFromState(PersistentState)} to set the values of the internal
|
291 |
* attributes that this class needs to work.
|
292 |
*/
|
293 |
public DefaultFeatureSelection() {
|
294 |
this.featureReferenceSelection = new DefaultFeatureReferenceSelection(); |
295 |
} |
296 |
|
297 |
@Override
|
298 |
public FeatureStore getFeatureStore() {
|
299 |
return this.featureReferenceSelection.getFeatureStore(); |
300 |
} |
301 |
|
302 |
private void notifyObservers(String notificationType) { |
303 |
this.featureReferenceSelection.notifyObservers(notificationType);
|
304 |
} |
305 |
|
306 |
public FeatureCommandsStack getCommands() {
|
307 |
return this.featureReferenceSelection.getCommands(); |
308 |
} |
309 |
|
310 |
@Override
|
311 |
public void enableNotifications() { |
312 |
this.featureReferenceSelection.enableNotifications();
|
313 |
} |
314 |
|
315 |
@Override
|
316 |
public void disableNotifications() { |
317 |
this.featureReferenceSelection.disableNotifications();
|
318 |
} |
319 |
|
320 |
public boolean isReversed() { |
321 |
return this.featureReferenceSelection.isReversed(); |
322 |
} |
323 |
|
324 |
@Override
|
325 |
public long getSelectedCount() { |
326 |
return this.featureReferenceSelection.getSelectedCount(); |
327 |
} |
328 |
|
329 |
public DefaultFeatureReferenceSelection.SelectionData getData() {
|
330 |
return this.featureReferenceSelection.getData(); |
331 |
} |
332 |
|
333 |
@Override
|
334 |
public boolean select(FeatureReference reference) { |
335 |
return this.featureReferenceSelection.select(reference); |
336 |
} |
337 |
|
338 |
public boolean select(FeatureReference reference, boolean undoable) { |
339 |
return this.featureReferenceSelection.select(reference, undoable); |
340 |
} |
341 |
|
342 |
@Override
|
343 |
public boolean deselect(FeatureReference reference) { |
344 |
return this.featureReferenceSelection.deselect(reference); |
345 |
} |
346 |
|
347 |
public boolean deselect(FeatureReference reference, boolean undoable) { |
348 |
return this.featureReferenceSelection.deselect(reference, undoable); |
349 |
} |
350 |
|
351 |
@Override
|
352 |
public Iterator<FeatureReference> referenceIterator() { |
353 |
return this.featureReferenceSelection.referenceIterator(); |
354 |
} |
355 |
|
356 |
@Override
|
357 |
public Iterable<FeatureReference> referenceIterable() { |
358 |
return this.featureReferenceSelection.referenceIterable(); |
359 |
} |
360 |
|
361 |
@Override
|
362 |
public void selectAll() throws DataException { |
363 |
this.featureReferenceSelection.selectAll();
|
364 |
} |
365 |
|
366 |
@Override
|
367 |
public void deselectAll() throws DataException { |
368 |
this.featureReferenceSelection.deselectAll();
|
369 |
} |
370 |
|
371 |
public void deselectAll(boolean undoable) throws DataException { |
372 |
this.featureReferenceSelection.deselectAll(undoable);
|
373 |
} |
374 |
|
375 |
@Override
|
376 |
public boolean isSelected(FeatureReference reference) { |
377 |
return this.featureReferenceSelection.isSelected(reference); |
378 |
} |
379 |
|
380 |
@Override
|
381 |
public void reverse() { |
382 |
this.featureReferenceSelection.reverse();
|
383 |
} |
384 |
|
385 |
@Override
|
386 |
public void dispose() { |
387 |
this.featureReferenceSelection.dispose();
|
388 |
} |
389 |
|
390 |
@Override
|
391 |
public void update(Observable o, Object o1) { |
392 |
this.featureReferenceSelection.update(o, o1);
|
393 |
} |
394 |
|
395 |
@Override
|
396 |
public void addObserver(Observer obsrvr) { |
397 |
this.featureReferenceSelection.addObserver(obsrvr);
|
398 |
} |
399 |
|
400 |
@Override
|
401 |
public void deleteObserver(Observer obsrvr) { |
402 |
this.featureReferenceSelection.deleteObserver(obsrvr);
|
403 |
} |
404 |
|
405 |
@Override
|
406 |
public void deleteObservers() { |
407 |
this.featureReferenceSelection.deleteObservers();
|
408 |
} |
409 |
|
410 |
@Override
|
411 |
public void beginComplexNotification() { |
412 |
this.featureReferenceSelection.beginComplexNotification();
|
413 |
} |
414 |
|
415 |
@Override
|
416 |
public void endComplexNotification() { |
417 |
this.featureReferenceSelection.endComplexNotification();
|
418 |
} |
419 |
|
420 |
@Override
|
421 |
public void saveToState(PersistentState ps) throws PersistenceException { |
422 |
this.featureReferenceSelection.saveToState(ps);
|
423 |
} |
424 |
|
425 |
@Override
|
426 |
public boolean select(Feature feature) { |
427 |
return select(feature, true); |
428 |
} |
429 |
|
430 |
/**
|
431 |
* @param feature
|
432 |
* @return
|
433 |
* @see #select(Feature)
|
434 |
* @param undoable if the action must be undoable
|
435 |
*/
|
436 |
public boolean select(Feature feature, boolean undoable) { |
437 |
// TODO: should we check if the feature is from the same FeatureStore??
|
438 |
if (feature == null) { |
439 |
return false; |
440 |
} |
441 |
|
442 |
// LOGGER.debug("Selected feature: {}", feature);
|
443 |
if (isReversed()) {
|
444 |
removeFeatureTypeCount(feature.getType()); |
445 |
} else {
|
446 |
addFeatureTypeCount(feature.getType()); |
447 |
} |
448 |
return select(feature.getReference(), undoable);
|
449 |
} |
450 |
|
451 |
@Override
|
452 |
public boolean select(FeatureSet features) throws DataException { |
453 |
return select(features, true); |
454 |
} |
455 |
|
456 |
/**
|
457 |
* @param features
|
458 |
* @return
|
459 |
* @throws org.gvsig.fmap.dal.exception.DataException
|
460 |
* @see #select(FeatureSet)
|
461 |
* @param undoable if the action must be undoable
|
462 |
*/
|
463 |
public boolean select(FeatureSet features, boolean undoable) |
464 |
throws DataException {
|
465 |
boolean change = false; |
466 |
boolean inComplex = false; |
467 |
if (undoable && this.featureReferenceSelection.getFeatureStore().isEditing() |
468 |
&& !this.featureReferenceSelection.getCommands().inComplex()) {
|
469 |
|
470 |
this.featureReferenceSelection.getCommands().startComplex("_selectionSelectFeatureSet"); |
471 |
inComplex = this.featureReferenceSelection.getCommands().inComplex();
|
472 |
} |
473 |
|
474 |
disableNotifications(); |
475 |
DisposableIterator iter = null;
|
476 |
try {
|
477 |
for (iter = features.fastIterator(); iter.hasNext();) {
|
478 |
change |= select((Feature) iter.next(), undoable); |
479 |
} |
480 |
} finally {
|
481 |
DisposeUtils.disposeQuietly(iter); |
482 |
} |
483 |
enableNotifications(); |
484 |
if (undoable && getFeatureStore().isEditing() && inComplex) {
|
485 |
getCommands().endComplex(); |
486 |
} |
487 |
if (change) {
|
488 |
notifyObservers(DataStoreNotification.SELECTION_CHANGE); |
489 |
} |
490 |
return change;
|
491 |
} |
492 |
|
493 |
@Override
|
494 |
public boolean deselect(Feature feature) { |
495 |
return deselect(feature, true); |
496 |
} |
497 |
|
498 |
/**
|
499 |
* @param feature
|
500 |
* @return
|
501 |
* @see #deselect(Feature)
|
502 |
* @param undoable if the action must be undoable
|
503 |
*/
|
504 |
public boolean deselect(Feature feature, boolean undoable) { |
505 |
if (feature == null) { |
506 |
return false; |
507 |
} |
508 |
|
509 |
LOG.debug("Deselected feature: {}", feature);
|
510 |
|
511 |
if (isReversed()) {
|
512 |
addFeatureTypeCount(feature.getType()); |
513 |
} else {
|
514 |
removeFeatureTypeCount(feature.getType()); |
515 |
} |
516 |
return deselect(feature.getReference(), undoable);
|
517 |
} |
518 |
|
519 |
@Override
|
520 |
public boolean deselect(FeatureSet features) throws DataException { |
521 |
return deselect(features, true); |
522 |
} |
523 |
|
524 |
/**
|
525 |
* @param features
|
526 |
* @return
|
527 |
* @throws org.gvsig.fmap.dal.exception.DataException
|
528 |
* @see #deselect(FeatureSet)
|
529 |
* @param undoable if the action must be undoable
|
530 |
*/
|
531 |
public boolean deselect(FeatureSet features, boolean undoable) |
532 |
throws DataException {
|
533 |
boolean change = false; |
534 |
if (undoable && getFeatureStore().isEditing()) {
|
535 |
getCommands().startComplex("_selectionDeselectFeatureSet");
|
536 |
} |
537 |
disableNotifications(); |
538 |
DisposableIterator iter = null;
|
539 |
try {
|
540 |
for (iter = features.fastIterator(); iter.hasNext();) {
|
541 |
change |= deselect((Feature) iter.next(), undoable); |
542 |
} |
543 |
} finally {
|
544 |
DisposeUtils.disposeQuietly(iter); |
545 |
} |
546 |
enableNotifications(); |
547 |
if (undoable && getFeatureStore().isEditing()) {
|
548 |
getCommands().endComplex(); |
549 |
} |
550 |
if (change) {
|
551 |
notifyObservers(DataStoreNotification.SELECTION_CHANGE); |
552 |
} |
553 |
return change;
|
554 |
} |
555 |
|
556 |
@Override
|
557 |
public boolean isSelected(Feature feature) { |
558 |
if (feature == null) { |
559 |
return false; |
560 |
} |
561 |
if( this.featureReferenceSelection.isEmpty() ) { |
562 |
return false; |
563 |
} |
564 |
// Use the selection data size as a small optimization for the most
|
565 |
// common case, when nothing is selected and every feature is checked
|
566 |
// while drawing or painting the table document.
|
567 |
if (getData().isReversed()) {
|
568 |
return getData().getSize() == 0 |
569 |
|| !getData().contains(feature.getReference()); |
570 |
} else {
|
571 |
return getData().getSize() > 0 |
572 |
&& getData().contains(feature.getReference()); |
573 |
} |
574 |
} |
575 |
|
576 |
@Override
|
577 |
public FeatureType getDefaultFeatureType() {
|
578 |
try {
|
579 |
return getFeatureStore().getDefaultFeatureType();
|
580 |
} catch (DataException ex) {
|
581 |
LOG.error("Error getting the default feature type "
|
582 |
+ "of the FeatureStore: " + getFeatureStore(), ex);
|
583 |
} |
584 |
return null; |
585 |
} |
586 |
|
587 |
@Override
|
588 |
public List getFeatureTypes() { |
589 |
// Go through the map of FeatureTypes, and return only the ones that
|
590 |
// have at least a Feature.
|
591 |
List types = new ArrayList(); |
592 |
for (java.util.Iterator iterator = featureTypeCounts.entrySet()
|
593 |
.iterator(); iterator.hasNext();) { |
594 |
Map.Entry entry = (Entry) iterator.next();
|
595 |
FeatureType type = (FeatureType) entry.getKey(); |
596 |
Long count = (Long) entry.getValue(); |
597 |
|
598 |
if (count > 0) { |
599 |
types.add(type); |
600 |
} |
601 |
} |
602 |
|
603 |
return types;
|
604 |
} |
605 |
|
606 |
@Override
|
607 |
public long getSize() throws DataException { |
608 |
return getSelectedCount();
|
609 |
} |
610 |
|
611 |
/**
|
612 |
* Returns the list of selected values, or the deselected ones if the
|
613 |
* selection has been reversed.
|
614 |
*
|
615 |
* WARN: not very good performance implementation.
|
616 |
*/
|
617 |
@Override
|
618 |
public DisposableIterator iterator(long index) { |
619 |
return iterator(index, 0, false); |
620 |
} |
621 |
|
622 |
@Override
|
623 |
public DisposableIterator iterator(long index, long elements) { |
624 |
return iterator(index, elements, false); |
625 |
} |
626 |
|
627 |
/**
|
628 |
* Returns the list of selected values, or the deselected ones if the
|
629 |
* selection has been reversed.
|
630 |
*
|
631 |
* WARN: not really a fast implementation.
|
632 |
*/
|
633 |
@Override
|
634 |
public DisposableIterator fastIterator(long index) { |
635 |
return fastIterator(index, 0); |
636 |
} |
637 |
|
638 |
@Override
|
639 |
public DisposableIterator fastIterator(long index, long elements) { |
640 |
return iterator(index, elements, true); |
641 |
} |
642 |
|
643 |
|
644 |
protected void clearFeatureReferences() { |
645 |
this.featureReferenceSelection.clearFeatureReferences();
|
646 |
featureTypeCounts.clear(); |
647 |
} |
648 |
|
649 |
/**
|
650 |
* Creates an iterator for the Selection.
|
651 |
*/
|
652 |
private DisposableIterator iterator(long index, long elements, boolean fastIterator) { |
653 |
if (isReversed()) {
|
654 |
DisposableIterator iter = new ReversedFeatureIteratorFacade(
|
655 |
getData(), getFeatureStore(), fastIterator); |
656 |
for (long l = 0; l < index && iter.hasNext(); l++) { |
657 |
iter.next(); |
658 |
} |
659 |
return iter;
|
660 |
|
661 |
} else {
|
662 |
// TODO: maybe we could add a new referenceIterator(int index)
|
663 |
// method that could be implemented in a more performant way
|
664 |
|
665 |
java.util.Iterator iter = getData().getSelected().iterator(); |
666 |
for (long l = 0; l < index && iter.hasNext(); l++) { |
667 |
iter.next(); |
668 |
} |
669 |
return new FeatureIteratorFacade(iter, getFeatureStore()); |
670 |
} |
671 |
} |
672 |
|
673 |
private Long removeFeatureTypeCount(FeatureType featureType) { |
674 |
Long count = (Long) featureTypeCounts.get(featureType); |
675 |
if (count == null) { |
676 |
count = new Long(-1); |
677 |
} else {
|
678 |
count = count - 1;
|
679 |
} |
680 |
featureTypeCounts.put(featureType, count); |
681 |
return count;
|
682 |
} |
683 |
|
684 |
private Long addFeatureTypeCount(FeatureType featureType) { |
685 |
Long count = (Long) featureTypeCounts.get(featureType); |
686 |
if (count == null) { |
687 |
count = new Long(1); |
688 |
} else {
|
689 |
count = count + 1;
|
690 |
} |
691 |
featureTypeCounts.put(featureType, count); |
692 |
return count;
|
693 |
} |
694 |
|
695 |
@Override
|
696 |
public void delete(Feature feature) throws DataException { |
697 |
Iterator it = this.featureIterators.get(feature); |
698 |
if (it != null) { |
699 |
it.remove(); |
700 |
return;
|
701 |
} |
702 |
feature.getStore().delete(feature); |
703 |
} |
704 |
|
705 |
@Override
|
706 |
public void insert(EditableFeature feature) throws DataException { |
707 |
feature.getStore().insert(feature); |
708 |
} |
709 |
|
710 |
@Override
|
711 |
public void update(EditableFeature feature) throws DataException { |
712 |
feature.getStore().update(feature); |
713 |
} |
714 |
|
715 |
|
716 |
@Override
|
717 |
public void commitChanges() throws DataException { |
718 |
} |
719 |
|
720 |
/*
|
721 |
* (non-Javadoc)
|
722 |
*
|
723 |
* @seeorg.gvsig.fmap.dal.feature.impl.DefaultFeatureReferenceSelection#
|
724 |
* loadFromState(org.gvsig.tools.persistence.PersistentState)
|
725 |
*/
|
726 |
@Override
|
727 |
public void loadFromState(PersistentState state) |
728 |
throws PersistenceException {
|
729 |
this.featureReferenceSelection.loadFromState(state);
|
730 |
} |
731 |
|
732 |
protected void doDispose() throws BaseException { |
733 |
this.featureReferenceSelection.doDispose();
|
734 |
featureTypeCounts.clear(); |
735 |
} |
736 |
|
737 |
public static void registerPersistent() { |
738 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
739 |
DynStruct definition = manager.addDefinition( |
740 |
DefaultFeatureSelection.class, "DefaultFeatureSelection",
|
741 |
"DefaultFeatureSelection Persistent definition", null, null); |
742 |
|
743 |
definition.extend(manager.getDefinition(DefaultFeatureReferenceSelection.DYNCLASS_PERSISTENT_NAME)); |
744 |
definition.addDynFieldMap("featureTypeCounts")
|
745 |
.setClassOfItems(Long.class).setMandatory(false); |
746 |
|
747 |
} |
748 |
|
749 |
@Override
|
750 |
public Object clone() throws CloneNotSupportedException { |
751 |
DefaultFeatureSelection clone = (DefaultFeatureSelection) super.clone();
|
752 |
clone.featureTypeCounts = new HashMap(featureTypeCounts); |
753 |
return clone;
|
754 |
} |
755 |
|
756 |
@Override
|
757 |
protected void doAccept(Visitor visitor, long firstValueIndex, long elements) throws BaseException { |
758 |
if( this.featureReferenceSelection.isEmpty() ) { |
759 |
return;
|
760 |
} |
761 |
DisposableIterator iterator = fastIterator(firstValueIndex, elements); |
762 |
if (iterator != null) { |
763 |
try {
|
764 |
while (iterator.hasNext()) {
|
765 |
Feature feature = (Feature) iterator.next(); |
766 |
visitor.visit(feature); |
767 |
} |
768 |
} finally {
|
769 |
iterator.dispose(); |
770 |
} |
771 |
} |
772 |
} |
773 |
|
774 |
private static class IgnoreInsertAndUpdateFeatureSet extends DefaultFeatureSet { |
775 |
|
776 |
public IgnoreInsertAndUpdateFeatureSet(DefaultFeatureStore store, FeatureQuery query) throws DataException { |
777 |
super(store, query);
|
778 |
} |
779 |
|
780 |
public void update(Observable obsevable, Object notification) { |
781 |
String type = ((FeatureStoreNotification) notification).getType();
|
782 |
if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_INSERT)
|
783 |
|| type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE)) { |
784 |
return;
|
785 |
} |
786 |
super.update(obsevable, notification);
|
787 |
} |
788 |
|
789 |
} |
790 |
|
791 |
@Override
|
792 |
public boolean isAvailable() { |
793 |
return this.featureReferenceSelection.isAvailable(); |
794 |
} |
795 |
|
796 |
} |