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 @ 44376
History | View | Annotate | Download (25.2 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 referenceIterator() { |
353 |
return this.featureReferenceSelection.referenceIterator(); |
354 |
} |
355 |
|
356 |
@Override
|
357 |
public void selectAll() throws DataException { |
358 |
this.featureReferenceSelection.selectAll();
|
359 |
} |
360 |
|
361 |
@Override
|
362 |
public void deselectAll() throws DataException { |
363 |
this.featureReferenceSelection.deselectAll();
|
364 |
} |
365 |
|
366 |
public void deselectAll(boolean undoable) throws DataException { |
367 |
this.featureReferenceSelection.deselectAll(undoable);
|
368 |
} |
369 |
|
370 |
@Override
|
371 |
public boolean isSelected(FeatureReference reference) { |
372 |
return this.featureReferenceSelection.isSelected(reference); |
373 |
} |
374 |
|
375 |
@Override
|
376 |
public void reverse() { |
377 |
this.featureReferenceSelection.reverse();
|
378 |
} |
379 |
|
380 |
@Override
|
381 |
public void dispose() { |
382 |
this.featureReferenceSelection.dispose();
|
383 |
} |
384 |
|
385 |
@Override
|
386 |
public void update(Observable o, Object o1) { |
387 |
this.featureReferenceSelection.update(o, o1);
|
388 |
} |
389 |
|
390 |
@Override
|
391 |
public void addObserver(Observer obsrvr) { |
392 |
this.featureReferenceSelection.addObserver(obsrvr);
|
393 |
} |
394 |
|
395 |
@Override
|
396 |
public void deleteObserver(Observer obsrvr) { |
397 |
this.featureReferenceSelection.deleteObserver(obsrvr);
|
398 |
} |
399 |
|
400 |
@Override
|
401 |
public void deleteObservers() { |
402 |
this.featureReferenceSelection.deleteObservers();
|
403 |
} |
404 |
|
405 |
@Override
|
406 |
public void beginComplexNotification() { |
407 |
this.featureReferenceSelection.beginComplexNotification();
|
408 |
} |
409 |
|
410 |
@Override
|
411 |
public void endComplexNotification() { |
412 |
this.featureReferenceSelection.endComplexNotification();
|
413 |
} |
414 |
|
415 |
@Override
|
416 |
public void saveToState(PersistentState ps) throws PersistenceException { |
417 |
this.featureReferenceSelection.saveToState(ps);
|
418 |
} |
419 |
|
420 |
@Override
|
421 |
public boolean select(Feature feature) { |
422 |
return select(feature, true); |
423 |
} |
424 |
|
425 |
/**
|
426 |
* @param feature
|
427 |
* @return
|
428 |
* @see #select(Feature)
|
429 |
* @param undoable if the action must be undoable
|
430 |
*/
|
431 |
public boolean select(Feature feature, boolean undoable) { |
432 |
// TODO: should we check if the feature is from the same FeatureStore??
|
433 |
if (feature == null) { |
434 |
return false; |
435 |
} |
436 |
|
437 |
// LOGGER.debug("Selected feature: {}", feature);
|
438 |
if (isReversed()) {
|
439 |
removeFeatureTypeCount(feature.getType()); |
440 |
} else {
|
441 |
addFeatureTypeCount(feature.getType()); |
442 |
} |
443 |
return select(feature.getReference(), undoable);
|
444 |
} |
445 |
|
446 |
@Override
|
447 |
public boolean select(FeatureSet features) throws DataException { |
448 |
return select(features, true); |
449 |
} |
450 |
|
451 |
/**
|
452 |
* @param features
|
453 |
* @return
|
454 |
* @throws org.gvsig.fmap.dal.exception.DataException
|
455 |
* @see #select(FeatureSet)
|
456 |
* @param undoable if the action must be undoable
|
457 |
*/
|
458 |
public boolean select(FeatureSet features, boolean undoable) |
459 |
throws DataException {
|
460 |
boolean change = false; |
461 |
boolean inComplex = false; |
462 |
if (undoable && this.featureReferenceSelection.getFeatureStore().isEditing() |
463 |
&& !this.featureReferenceSelection.getCommands().inComplex()) {
|
464 |
|
465 |
this.featureReferenceSelection.getCommands().startComplex("_selectionSelectFeatureSet"); |
466 |
inComplex = this.featureReferenceSelection.getCommands().inComplex();
|
467 |
} |
468 |
|
469 |
disableNotifications(); |
470 |
DisposableIterator iter = null;
|
471 |
try {
|
472 |
for (iter = features.fastIterator(); iter.hasNext();) {
|
473 |
change |= select((Feature) iter.next(), undoable); |
474 |
} |
475 |
} finally {
|
476 |
DisposeUtils.disposeQuietly(iter); |
477 |
} |
478 |
enableNotifications(); |
479 |
if (undoable && getFeatureStore().isEditing() && inComplex) {
|
480 |
getCommands().endComplex(); |
481 |
} |
482 |
if (change) {
|
483 |
notifyObservers(DataStoreNotification.SELECTION_CHANGE); |
484 |
} |
485 |
return change;
|
486 |
} |
487 |
|
488 |
@Override
|
489 |
public boolean deselect(Feature feature) { |
490 |
return deselect(feature, true); |
491 |
} |
492 |
|
493 |
/**
|
494 |
* @param feature
|
495 |
* @return
|
496 |
* @see #deselect(Feature)
|
497 |
* @param undoable if the action must be undoable
|
498 |
*/
|
499 |
public boolean deselect(Feature feature, boolean undoable) { |
500 |
if (feature == null) { |
501 |
return false; |
502 |
} |
503 |
|
504 |
LOG.debug("Deselected feature: {}", feature);
|
505 |
|
506 |
if (isReversed()) {
|
507 |
addFeatureTypeCount(feature.getType()); |
508 |
} else {
|
509 |
removeFeatureTypeCount(feature.getType()); |
510 |
} |
511 |
return deselect(feature.getReference(), undoable);
|
512 |
} |
513 |
|
514 |
@Override
|
515 |
public boolean deselect(FeatureSet features) throws DataException { |
516 |
return deselect(features, true); |
517 |
} |
518 |
|
519 |
/**
|
520 |
* @param features
|
521 |
* @return
|
522 |
* @throws org.gvsig.fmap.dal.exception.DataException
|
523 |
* @see #deselect(FeatureSet)
|
524 |
* @param undoable if the action must be undoable
|
525 |
*/
|
526 |
public boolean deselect(FeatureSet features, boolean undoable) |
527 |
throws DataException {
|
528 |
boolean change = false; |
529 |
if (undoable && getFeatureStore().isEditing()) {
|
530 |
getCommands().startComplex("_selectionDeselectFeatureSet");
|
531 |
} |
532 |
disableNotifications(); |
533 |
DisposableIterator iter = null;
|
534 |
try {
|
535 |
for (iter = features.fastIterator(); iter.hasNext();) {
|
536 |
change |= deselect((Feature) iter.next(), undoable); |
537 |
} |
538 |
} finally {
|
539 |
DisposeUtils.disposeQuietly(iter); |
540 |
} |
541 |
enableNotifications(); |
542 |
if (undoable && getFeatureStore().isEditing()) {
|
543 |
getCommands().endComplex(); |
544 |
} |
545 |
if (change) {
|
546 |
notifyObservers(DataStoreNotification.SELECTION_CHANGE); |
547 |
} |
548 |
return change;
|
549 |
} |
550 |
|
551 |
@Override
|
552 |
public boolean isSelected(Feature feature) { |
553 |
if (feature == null) { |
554 |
return false; |
555 |
} |
556 |
if( this.featureReferenceSelection.isEmpty() ) { |
557 |
return false; |
558 |
} |
559 |
// Use the selection data size as a small optimization for the most
|
560 |
// common case, when nothing is selected and every feature is checked
|
561 |
// while drawing or painting the table document.
|
562 |
if (getData().isReversed()) {
|
563 |
return getData().getSize() == 0 |
564 |
|| !getData().contains(feature.getReference()); |
565 |
} else {
|
566 |
return getData().getSize() > 0 |
567 |
&& getData().contains(feature.getReference()); |
568 |
} |
569 |
} |
570 |
|
571 |
@Override
|
572 |
public FeatureType getDefaultFeatureType() {
|
573 |
try {
|
574 |
return getFeatureStore().getDefaultFeatureType();
|
575 |
} catch (DataException ex) {
|
576 |
LOG.error("Error getting the default feature type "
|
577 |
+ "of the FeatureStore: " + getFeatureStore(), ex);
|
578 |
} |
579 |
return null; |
580 |
} |
581 |
|
582 |
@Override
|
583 |
public List getFeatureTypes() { |
584 |
// Go through the map of FeatureTypes, and return only the ones that
|
585 |
// have at least a Feature.
|
586 |
List types = new ArrayList(); |
587 |
for (java.util.Iterator iterator = featureTypeCounts.entrySet()
|
588 |
.iterator(); iterator.hasNext();) { |
589 |
Map.Entry entry = (Entry) iterator.next();
|
590 |
FeatureType type = (FeatureType) entry.getKey(); |
591 |
Long count = (Long) entry.getValue(); |
592 |
|
593 |
if (count > 0) { |
594 |
types.add(type); |
595 |
} |
596 |
} |
597 |
|
598 |
return types;
|
599 |
} |
600 |
|
601 |
@Override
|
602 |
public long getSize() throws DataException { |
603 |
return getSelectedCount();
|
604 |
} |
605 |
|
606 |
/**
|
607 |
* Returns the list of selected values, or the deselected ones if the
|
608 |
* selection has been reversed.
|
609 |
*
|
610 |
* WARN: not very good performance implementation.
|
611 |
*/
|
612 |
@Override
|
613 |
public DisposableIterator iterator(long index) { |
614 |
return iterator(index, 0, false); |
615 |
} |
616 |
|
617 |
@Override
|
618 |
public DisposableIterator iterator(long index, long elements) { |
619 |
return iterator(index, elements, false); |
620 |
} |
621 |
|
622 |
/**
|
623 |
* Returns the list of selected values, or the deselected ones if the
|
624 |
* selection has been reversed.
|
625 |
*
|
626 |
* WARN: not really a fast implementation.
|
627 |
*/
|
628 |
@Override
|
629 |
public DisposableIterator fastIterator(long index) { |
630 |
return fastIterator(index, 0); |
631 |
} |
632 |
|
633 |
@Override
|
634 |
public DisposableIterator fastIterator(long index, long elements) { |
635 |
return iterator(index, elements, true); |
636 |
} |
637 |
|
638 |
|
639 |
protected void clearFeatureReferences() { |
640 |
this.featureReferenceSelection.clearFeatureReferences();
|
641 |
featureTypeCounts.clear(); |
642 |
} |
643 |
|
644 |
/**
|
645 |
* Creates an iterator for the Selection.
|
646 |
*/
|
647 |
private DisposableIterator iterator(long index, long elements, boolean fastIterator) { |
648 |
if (isReversed()) {
|
649 |
DisposableIterator iter = new ReversedFeatureIteratorFacade(
|
650 |
getData(), getFeatureStore(), fastIterator); |
651 |
for (long l = 0; l < index && iter.hasNext(); l++) { |
652 |
iter.next(); |
653 |
} |
654 |
return iter;
|
655 |
|
656 |
} else {
|
657 |
// TODO: maybe we could add a new referenceIterator(int index)
|
658 |
// method that could be implemented in a more performant way
|
659 |
|
660 |
java.util.Iterator iter = getData().getSelected().iterator(); |
661 |
for (long l = 0; l < index && iter.hasNext(); l++) { |
662 |
iter.next(); |
663 |
} |
664 |
return new FeatureIteratorFacade(iter, getFeatureStore()); |
665 |
} |
666 |
} |
667 |
|
668 |
private Long removeFeatureTypeCount(FeatureType featureType) { |
669 |
Long count = (Long) featureTypeCounts.get(featureType); |
670 |
if (count == null) { |
671 |
count = new Long(-1); |
672 |
} else {
|
673 |
count = count - 1;
|
674 |
} |
675 |
featureTypeCounts.put(featureType, count); |
676 |
return count;
|
677 |
} |
678 |
|
679 |
private Long addFeatureTypeCount(FeatureType featureType) { |
680 |
Long count = (Long) featureTypeCounts.get(featureType); |
681 |
if (count == null) { |
682 |
count = new Long(1); |
683 |
} else {
|
684 |
count = count + 1;
|
685 |
} |
686 |
featureTypeCounts.put(featureType, count); |
687 |
return count;
|
688 |
} |
689 |
|
690 |
@Override
|
691 |
public void delete(Feature feature) throws DataException { |
692 |
Iterator it = this.featureIterators.get(feature); |
693 |
if (it != null) { |
694 |
it.remove(); |
695 |
return;
|
696 |
} |
697 |
feature.getStore().delete(feature); |
698 |
} |
699 |
|
700 |
@Override
|
701 |
public void insert(EditableFeature feature) throws DataException { |
702 |
feature.getStore().insert(feature); |
703 |
} |
704 |
|
705 |
@Override
|
706 |
public void update(EditableFeature feature) throws DataException { |
707 |
feature.getStore().update(feature); |
708 |
} |
709 |
|
710 |
|
711 |
@Override
|
712 |
public void commitChanges() throws DataException { |
713 |
} |
714 |
|
715 |
/*
|
716 |
* (non-Javadoc)
|
717 |
*
|
718 |
* @seeorg.gvsig.fmap.dal.feature.impl.DefaultFeatureReferenceSelection#
|
719 |
* loadFromState(org.gvsig.tools.persistence.PersistentState)
|
720 |
*/
|
721 |
@Override
|
722 |
public void loadFromState(PersistentState state) |
723 |
throws PersistenceException {
|
724 |
this.featureReferenceSelection.loadFromState(state);
|
725 |
} |
726 |
|
727 |
protected void doDispose() throws BaseException { |
728 |
this.featureReferenceSelection.doDispose();
|
729 |
featureTypeCounts.clear(); |
730 |
} |
731 |
|
732 |
public static void registerPersistent() { |
733 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
734 |
DynStruct definition = manager.addDefinition( |
735 |
DefaultFeatureSelection.class, "DefaultFeatureSelection",
|
736 |
"DefaultFeatureSelection Persistent definition", null, null); |
737 |
|
738 |
definition.extend(manager.getDefinition(DefaultFeatureReferenceSelection.DYNCLASS_PERSISTENT_NAME)); |
739 |
definition.addDynFieldMap("featureTypeCounts")
|
740 |
.setClassOfItems(Long.class).setMandatory(false); |
741 |
|
742 |
} |
743 |
|
744 |
@Override
|
745 |
public Object clone() throws CloneNotSupportedException { |
746 |
DefaultFeatureSelection clone = (DefaultFeatureSelection) super.clone();
|
747 |
clone.featureTypeCounts = new HashMap(featureTypeCounts); |
748 |
return clone;
|
749 |
} |
750 |
|
751 |
@Override
|
752 |
protected void doAccept(Visitor visitor, long firstValueIndex, long elements) throws BaseException { |
753 |
if( this.featureReferenceSelection.isEmpty() ) { |
754 |
return;
|
755 |
} |
756 |
DisposableIterator iterator = fastIterator(firstValueIndex, elements); |
757 |
if (iterator != null) { |
758 |
try {
|
759 |
while (iterator.hasNext()) {
|
760 |
Feature feature = (Feature) iterator.next(); |
761 |
visitor.visit(feature); |
762 |
} |
763 |
} finally {
|
764 |
iterator.dispose(); |
765 |
} |
766 |
} |
767 |
} |
768 |
|
769 |
private static class IgnoreInsertAndUpdateFeatureSet extends DefaultFeatureSet { |
770 |
|
771 |
public IgnoreInsertAndUpdateFeatureSet(DefaultFeatureStore store, FeatureQuery query) throws DataException { |
772 |
super(store, query);
|
773 |
} |
774 |
|
775 |
public void update(Observable obsevable, Object notification) { |
776 |
String type = ((FeatureStoreNotification) notification).getType();
|
777 |
if (type.equalsIgnoreCase(FeatureStoreNotification.AFTER_INSERT)
|
778 |
|| type.equalsIgnoreCase(FeatureStoreNotification.AFTER_UPDATE)) { |
779 |
return;
|
780 |
} |
781 |
super.update(obsevable, notification);
|
782 |
} |
783 |
|
784 |
} |
785 |
|
786 |
|
787 |
} |