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 @ 44160
History | View | Annotate | Download (25.2 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | 43089 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
11 | 43089 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
16 | 43089 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
20 | 43089 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | package org.gvsig.fmap.dal.feature.impl; |
||
24 | |||
25 | import java.util.ArrayList; |
||
26 | import java.util.HashMap; |
||
27 | 42834 | dmartinezizquierdo | import java.util.Iterator; |
28 | 40435 | jjdelcerro | 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 | 42821 | dmartinezizquierdo | import org.gvsig.fmap.dal.exception.DataRuntimeException; |
35 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeature; |
36 | import org.gvsig.fmap.dal.feature.Feature; |
||
37 | 44113 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQuery; |
38 | 40435 | jjdelcerro | 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 | 44113 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
43 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureType; |
44 | import org.gvsig.fmap.dal.feature.exception.ReversedSelectionIteratorException; |
||
45 | 43089 | jjdelcerro | import org.gvsig.fmap.dal.feature.impl.DefaultFeatureReferenceSelection.SelectionData; |
46 | import org.gvsig.fmap.dal.feature.impl.featureset.AbstractFeatureSet; |
||
47 | 44113 | jjdelcerro | import org.gvsig.fmap.dal.feature.impl.featureset.DefaultFeatureSet; |
48 | 43089 | jjdelcerro | import org.gvsig.fmap.dal.feature.impl.undo.FeatureCommandsStack; |
49 | 40435 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
50 | import org.gvsig.tools.dispose.DisposableIterator; |
||
51 | 43088 | jjdelcerro | import org.gvsig.tools.dispose.DisposeUtils; |
52 | 40435 | jjdelcerro | import org.gvsig.tools.dynobject.DynStruct; |
53 | import org.gvsig.tools.exception.BaseException; |
||
54 | 43089 | jjdelcerro | import org.gvsig.tools.observer.Observable; |
55 | import org.gvsig.tools.observer.Observer; |
||
56 | 40435 | jjdelcerro | import org.gvsig.tools.persistence.PersistenceManager; |
57 | import org.gvsig.tools.persistence.PersistentState; |
||
58 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
59 | 43358 | jjdelcerro | import org.gvsig.tools.visitor.Visitor; |
60 | 42834 | dmartinezizquierdo | import org.slf4j.Logger; |
61 | import org.slf4j.LoggerFactory; |
||
62 | 40435 | jjdelcerro | |
63 | /**
|
||
64 | * Default implementation of the FeatureSelection interface. Internally, only
|
||
65 | * FeatureReference values are stored.
|
||
66 | 42821 | dmartinezizquierdo | *
|
67 | 40435 | jjdelcerro | * This implementation performs better if used with the selection related
|
68 | * methods: select, deselect and isSelected ones.
|
||
69 | 42821 | dmartinezizquierdo | *
|
70 | 40435 | jjdelcerro | * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
|
71 | */
|
||
72 | 43089 | jjdelcerro | public class DefaultFeatureSelection extends AbstractFeatureSet |
73 | implements FeatureSelection {
|
||
74 | 40435 | jjdelcerro | |
75 | 43089 | jjdelcerro | public class RemoveFromFeatureSelectionException extends DataRuntimeException { |
76 | 40435 | jjdelcerro | |
77 | 43089 | jjdelcerro | /**
|
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 | 40435 | jjdelcerro | |
84 | 43089 | jjdelcerro | public RemoveFromFeatureSelectionException(Throwable cause) { |
85 | super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
||
86 | //setValue("store", store);
|
||
87 | } |
||
88 | } |
||
89 | 40435 | jjdelcerro | |
90 | 43089 | jjdelcerro | /**
|
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 | 40435 | jjdelcerro | |
97 | 43089 | jjdelcerro | private final Logger LOGGER = LoggerFactory |
98 | .getLogger(FeatureIteratorFacade.class); |
||
99 | 40435 | jjdelcerro | |
100 | 43089 | jjdelcerro | private java.util.Iterator refIterator;
|
101 | 40435 | jjdelcerro | |
102 | 43089 | jjdelcerro | private FeatureStore featureStore;
|
103 | private Feature currentFeature = null; |
||
104 | 40435 | jjdelcerro | |
105 | 43089 | jjdelcerro | public FeatureIteratorFacade(java.util.Iterator iter,
|
106 | FeatureStore featureStore) { |
||
107 | this.refIterator = iter;
|
||
108 | this.featureStore = featureStore;
|
||
109 | } |
||
110 | 40435 | jjdelcerro | |
111 | 43089 | jjdelcerro | @Override
|
112 | public boolean hasNext() { |
||
113 | return refIterator.hasNext();
|
||
114 | } |
||
115 | 40435 | jjdelcerro | |
116 | 43089 | jjdelcerro | @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 | 42821 | dmartinezizquierdo | |
130 | 43089 | jjdelcerro | /**
|
131 | * Returns the next FeatureReference.
|
||
132 | *
|
||
133 | * @return the next FeatureReference
|
||
134 | */
|
||
135 | public FeatureReference nextFeatureReference() {
|
||
136 | return (FeatureReference) refIterator.next();
|
||
137 | } |
||
138 | 40435 | jjdelcerro | |
139 | 43089 | jjdelcerro | @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 | 40435 | jjdelcerro | |
149 | 43089 | jjdelcerro | public class RemoveFromFeatureSelectionException extends DataRuntimeException { |
150 | 40435 | jjdelcerro | |
151 | 43089 | jjdelcerro | /**
|
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 | 40435 | jjdelcerro | |
158 | 43089 | jjdelcerro | public RemoveFromFeatureSelectionException(Throwable cause) { |
159 | super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
||
160 | //setValue("store", store);
|
||
161 | } |
||
162 | } |
||
163 | 40435 | jjdelcerro | |
164 | 43089 | jjdelcerro | @Override
|
165 | public void dispose() { |
||
166 | if (refIterator instanceof DisposableIterator) { |
||
167 | ((DisposableIterator) refIterator).dispose(); |
||
168 | } |
||
169 | refIterator = null;
|
||
170 | featureStore = null;
|
||
171 | } |
||
172 | } |
||
173 | 40435 | jjdelcerro | |
174 | 43089 | jjdelcerro | /**
|
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 | 40435 | jjdelcerro | |
182 | 43089 | jjdelcerro | private SelectionData selectionData;
|
183 | 40435 | jjdelcerro | |
184 | 43089 | jjdelcerro | private DisposableIterator iterator;
|
185 | 40435 | jjdelcerro | |
186 | 43089 | jjdelcerro | private Feature nextFeature = null; |
187 | private Feature currentFeature = null; |
||
188 | 40435 | jjdelcerro | |
189 | 43089 | jjdelcerro | private FeatureSet featureSet;
|
190 | 40435 | jjdelcerro | |
191 | 43089 | jjdelcerro | public ReversedFeatureIteratorFacade(SelectionData selectionData,
|
192 | FeatureStore featureStore, boolean fastIterator) {
|
||
193 | this.selectionData = selectionData;
|
||
194 | 40435 | jjdelcerro | |
195 | 43089 | jjdelcerro | // Load a Set with all the store features
|
196 | try {
|
||
197 | 44113 | jjdelcerro | featureSet = new IgnoreInsertAndUpdateFeatureSet(
|
198 | (DefaultFeatureStore) featureStore, |
||
199 | new DefaultFeatureQuery(featureStore.getDefaultFeatureType())
|
||
200 | ); |
||
201 | 43089 | jjdelcerro | iterator = featureSet.fastIterator(); |
202 | } catch (DataException ex) {
|
||
203 | throw new ReversedSelectionIteratorException(ex); |
||
204 | } |
||
205 | 40435 | jjdelcerro | |
206 | 43089 | jjdelcerro | // Filter the features not selected and position in the next
|
207 | // selected feature
|
||
208 | positionInNextElement(); |
||
209 | } |
||
210 | 40435 | jjdelcerro | |
211 | 43089 | jjdelcerro | @Override
|
212 | public boolean hasNext() { |
||
213 | return nextFeature != null; |
||
214 | } |
||
215 | 40435 | jjdelcerro | |
216 | 43089 | jjdelcerro | @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 | 40435 | jjdelcerro | |
225 | 43089 | jjdelcerro | @Override
|
226 | public void remove() { |
||
227 | try {
|
||
228 | featureSet.delete(currentFeature); |
||
229 | } catch (DataException e) {
|
||
230 | throw new RemoveFromFeatureSelectionException(e); |
||
231 | 40435 | jjdelcerro | |
232 | 43089 | jjdelcerro | } |
233 | } |
||
234 | 40435 | jjdelcerro | |
235 | 43089 | jjdelcerro | 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 | 43088 | jjdelcerro | } |
244 | } |
||
245 | } |
||
246 | 40435 | jjdelcerro | |
247 | 43089 | jjdelcerro | @Override
|
248 | public void dispose() { |
||
249 | this.featureSet.dispose();
|
||
250 | this.iterator.dispose();
|
||
251 | this.selectionData = null; |
||
252 | this.nextFeature = null; |
||
253 | } |
||
254 | } |
||
255 | 40435 | jjdelcerro | |
256 | 43089 | jjdelcerro | private Map featureTypeCounts = new HashMap(1); |
257 | private final Map<Feature, Iterator> featureIterators = new HashMap<>(); |
||
258 | private final DefaultFeatureReferenceSelection featureReferenceSelection; |
||
259 | 40435 | jjdelcerro | |
260 | 43089 | jjdelcerro | /**
|
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 | 40435 | jjdelcerro | |
273 | 43089 | jjdelcerro | /**
|
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 | 40435 | jjdelcerro | |
287 | 43089 | jjdelcerro | /**
|
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 | 40435 | jjdelcerro | |
297 | 43089 | jjdelcerro | @Override
|
298 | public FeatureStore getFeatureStore() {
|
||
299 | return this.featureReferenceSelection.getFeatureStore(); |
||
300 | } |
||
301 | 40435 | jjdelcerro | |
302 | 43089 | jjdelcerro | private void notifyObservers(String notificationType) { |
303 | this.featureReferenceSelection.notifyObservers(notificationType);
|
||
304 | } |
||
305 | 40435 | jjdelcerro | |
306 | 43089 | jjdelcerro | public FeatureCommandsStack getCommands() {
|
307 | return this.featureReferenceSelection.getCommands(); |
||
308 | } |
||
309 | 40435 | jjdelcerro | |
310 | 43089 | jjdelcerro | @Override
|
311 | public void enableNotifications() { |
||
312 | this.featureReferenceSelection.enableNotifications();
|
||
313 | } |
||
314 | 40435 | jjdelcerro | |
315 | 43089 | jjdelcerro | @Override
|
316 | public void disableNotifications() { |
||
317 | this.featureReferenceSelection.disableNotifications();
|
||
318 | } |
||
319 | 40435 | jjdelcerro | |
320 | 43089 | jjdelcerro | public boolean isReversed() { |
321 | return this.featureReferenceSelection.isReversed(); |
||
322 | } |
||
323 | 40435 | jjdelcerro | |
324 | 43089 | jjdelcerro | @Override
|
325 | public long getSelectedCount() { |
||
326 | return this.featureReferenceSelection.getSelectedCount(); |
||
327 | } |
||
328 | 40435 | jjdelcerro | |
329 | 43089 | jjdelcerro | public DefaultFeatureReferenceSelection.SelectionData getData() {
|
330 | return this.featureReferenceSelection.getData(); |
||
331 | } |
||
332 | 40435 | jjdelcerro | |
333 | 43089 | jjdelcerro | @Override
|
334 | public boolean select(FeatureReference reference) { |
||
335 | return this.featureReferenceSelection.select(reference); |
||
336 | } |
||
337 | 40435 | jjdelcerro | |
338 | 43089 | jjdelcerro | public boolean select(FeatureReference reference, boolean undoable) { |
339 | return this.featureReferenceSelection.select(reference, undoable); |
||
340 | } |
||
341 | 40435 | jjdelcerro | |
342 | 43089 | jjdelcerro | @Override
|
343 | public boolean deselect(FeatureReference reference) { |
||
344 | return this.featureReferenceSelection.deselect(reference); |
||
345 | } |
||
346 | 40435 | jjdelcerro | |
347 | 43089 | jjdelcerro | public boolean deselect(FeatureReference reference, boolean undoable) { |
348 | return this.featureReferenceSelection.deselect(reference, undoable); |
||
349 | } |
||
350 | 42834 | dmartinezizquierdo | |
351 | 43089 | jjdelcerro | @Override
|
352 | public Iterator referenceIterator() { |
||
353 | return this.featureReferenceSelection.referenceIterator(); |
||
354 | } |
||
355 | 42834 | dmartinezizquierdo | |
356 | 43089 | jjdelcerro | @Override
|
357 | public void selectAll() throws DataException { |
||
358 | this.featureReferenceSelection.selectAll();
|
||
359 | } |
||
360 | 42834 | dmartinezizquierdo | |
361 | 43089 | jjdelcerro | @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 | 40435 | jjdelcerro | |
375 | 43089 | jjdelcerro | @Override
|
376 | public void reverse() { |
||
377 | this.featureReferenceSelection.reverse();
|
||
378 | } |
||
379 | 40435 | jjdelcerro | |
380 | 43089 | jjdelcerro | @Override
|
381 | public void dispose() { |
||
382 | this.featureReferenceSelection.dispose();
|
||
383 | } |
||
384 | 40435 | jjdelcerro | |
385 | 43089 | jjdelcerro | @Override
|
386 | public void update(Observable o, Object o1) { |
||
387 | this.featureReferenceSelection.update(o, o1);
|
||
388 | } |
||
389 | 40435 | jjdelcerro | |
390 | 43089 | jjdelcerro | @Override
|
391 | public void addObserver(Observer obsrvr) { |
||
392 | this.featureReferenceSelection.addObserver(obsrvr);
|
||
393 | } |
||
394 | 40435 | jjdelcerro | |
395 | 43089 | jjdelcerro | @Override
|
396 | public void deleteObserver(Observer obsrvr) { |
||
397 | this.featureReferenceSelection.deleteObserver(obsrvr);
|
||
398 | } |
||
399 | 40435 | jjdelcerro | |
400 | 43089 | jjdelcerro | @Override
|
401 | public void deleteObservers() { |
||
402 | this.featureReferenceSelection.deleteObservers();
|
||
403 | } |
||
404 | 40435 | jjdelcerro | |
405 | 43089 | jjdelcerro | @Override
|
406 | public void beginComplexNotification() { |
||
407 | this.featureReferenceSelection.beginComplexNotification();
|
||
408 | } |
||
409 | 40435 | jjdelcerro | |
410 | 43089 | jjdelcerro | @Override
|
411 | public void endComplexNotification() { |
||
412 | this.featureReferenceSelection.endComplexNotification();
|
||
413 | } |
||
414 | 40435 | jjdelcerro | |
415 | 43089 | jjdelcerro | @Override
|
416 | public void saveToState(PersistentState ps) throws PersistenceException { |
||
417 | this.featureReferenceSelection.saveToState(ps);
|
||
418 | } |
||
419 | 40435 | jjdelcerro | |
420 | 43089 | jjdelcerro | @Override
|
421 | public boolean select(Feature feature) { |
||
422 | return select(feature, true); |
||
423 | } |
||
424 | 40435 | jjdelcerro | |
425 | 43089 | jjdelcerro | /**
|
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 | 42821 | dmartinezizquierdo | |
437 | 43089 | jjdelcerro | // 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 | 42821 | dmartinezizquierdo | } |
475 | 43089 | jjdelcerro | } 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 | 40435 | jjdelcerro | |
488 | 43089 | jjdelcerro | @Override
|
489 | public boolean deselect(Feature feature) { |
||
490 | return deselect(feature, true); |
||
491 | } |
||
492 | 42821 | dmartinezizquierdo | |
493 | 43089 | jjdelcerro | /**
|
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 | 42821 | dmartinezizquierdo | |
504 | 43089 | jjdelcerro | LOG.debug("Deselected feature: {}", feature);
|
505 | 42821 | dmartinezizquierdo | |
506 | 43089 | jjdelcerro | if (isReversed()) {
|
507 | addFeatureTypeCount(feature.getType()); |
||
508 | } else {
|
||
509 | removeFeatureTypeCount(feature.getType()); |
||
510 | } |
||
511 | return deselect(feature.getReference(), undoable);
|
||
512 | } |
||
513 | 42821 | dmartinezizquierdo | |
514 | 43089 | jjdelcerro | @Override
|
515 | public boolean deselect(FeatureSet features) throws DataException { |
||
516 | return deselect(features, true); |
||
517 | } |
||
518 | 40435 | jjdelcerro | |
519 | 43089 | jjdelcerro | /**
|
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 | 40435 | jjdelcerro | |
551 | 43089 | jjdelcerro | @Override
|
552 | public boolean isSelected(Feature feature) { |
||
553 | if (feature == null) { |
||
554 | return false; |
||
555 | } |
||
556 | 43646 | jjdelcerro | if( this.featureReferenceSelection.isEmpty() ) { |
557 | return false; |
||
558 | } |
||
559 | 43089 | jjdelcerro | // 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 | 42834 | dmartinezizquierdo | } |
570 | 40435 | jjdelcerro | |
571 | 43089 | jjdelcerro | @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 | 42834 | dmartinezizquierdo | } |
581 | 40435 | jjdelcerro | |
582 | 43089 | jjdelcerro | @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 | 40435 | jjdelcerro | |
593 | 43089 | jjdelcerro | if (count > 0) { |
594 | types.add(type); |
||
595 | } |
||
596 | } |
||
597 | 40435 | jjdelcerro | |
598 | 43089 | jjdelcerro | return types;
|
599 | } |
||
600 | 40435 | jjdelcerro | |
601 | 43089 | jjdelcerro | @Override
|
602 | public long getSize() throws DataException { |
||
603 | return getSelectedCount();
|
||
604 | } |
||
605 | 40435 | jjdelcerro | |
606 | 43089 | jjdelcerro | /**
|
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 | 43358 | jjdelcerro | public DisposableIterator iterator(long index) { |
614 | return iterator(index, 0, false); |
||
615 | } |
||
616 | 40435 | jjdelcerro | |
617 | 43358 | jjdelcerro | @Override
|
618 | public DisposableIterator iterator(long index, long elements) { |
||
619 | return iterator(index, elements, false); |
||
620 | } |
||
621 | |||
622 | 43089 | jjdelcerro | /**
|
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 | 43358 | jjdelcerro | 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 | 43089 | jjdelcerro | } |
637 | 40435 | jjdelcerro | |
638 | 43358 | jjdelcerro | |
639 | 43089 | jjdelcerro | protected void clearFeatureReferences() { |
640 | this.featureReferenceSelection.clearFeatureReferences();
|
||
641 | featureTypeCounts.clear(); |
||
642 | } |
||
643 | 40435 | jjdelcerro | |
644 | 43089 | jjdelcerro | /**
|
645 | * Creates an iterator for the Selection.
|
||
646 | */
|
||
647 | 43358 | jjdelcerro | private DisposableIterator iterator(long index, long elements, boolean fastIterator) { |
648 | 43089 | jjdelcerro | 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 | 40435 | jjdelcerro | |
656 | 43089 | jjdelcerro | } else {
|
657 | // TODO: maybe we could add a new referenceIterator(int index)
|
||
658 | // method that could be implemented in a more performant way
|
||
659 | 40435 | jjdelcerro | |
660 | 43089 | jjdelcerro | 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 | 40435 | jjdelcerro | |
668 | 43089 | jjdelcerro | 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 | 40435 | jjdelcerro | |
679 | 43089 | jjdelcerro | 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 | 40435 | jjdelcerro | } |
689 | |||
690 | 43089 | jjdelcerro | @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 | 40435 | jjdelcerro | } |
699 | |||
700 | 43089 | jjdelcerro | @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 | 44097 | omartinez | |
710 | |||
711 | @Override
|
||
712 | public void commitChanges() throws DataException { |
||
713 | } |
||
714 | 43089 | jjdelcerro | |
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 | 43358 | jjdelcerro | @Override
|
752 | protected void doAccept(Visitor visitor, long firstValueIndex, long elements) throws BaseException { |
||
753 | 43646 | jjdelcerro | if( this.featureReferenceSelection.isEmpty() ) { |
754 | return;
|
||
755 | } |
||
756 | 43358 | jjdelcerro | 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 | 44113 | jjdelcerro | 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 | 40767 | jjdelcerro | } |