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