001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.ui;
016:
017: import java.util.ArrayList;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.List;
022: import java.util.NoSuchElementException;
023:
024: import org.eclipse.jface.viewers.IStructuredSelection;
025: import org.geotools.feature.Feature;
026: import org.geotools.feature.FeatureCollection;
027: import org.geotools.feature.FeatureIterator;
028:
029: /**
030: * Adapt a feature collection as a Selection so it can be used with StructedViewers.
031: *
032: * @author Jesse
033: * @since 1.1.0
034: */
035: public class FeatureCollectionSelection implements
036: IStructuredSelection, IBlockingSelection {
037: Collection<Iterator> openIterators = new ArrayList<Iterator>();
038: FeatureCollection wrapped;
039: private volatile Feature firstElement;
040:
041: public FeatureCollectionSelection(FeatureCollection selectedFeatures) {
042: this .wrapped = selectedFeatures;
043: }
044:
045: @Override
046: protected void finalize() throws Throwable {
047: for (Iterator iterator : openIterators) {
048: wrapped.close(iterator);
049: }
050: super .finalize();
051: }
052:
053: public Object getFirstElement() {
054: if (isEmpty())
055: throw new NoSuchElementException(
056: "Feature Collection is empty, there is no first element"); //$NON-NLS-1$
057: if (firstElement == null) {
058: synchronized (this ) {
059: if (firstElement == null) {
060: FeatureIterator iter = wrapped.features();
061: try {
062: firstElement = iter.next();
063: } finally {
064: iter.close();
065: }
066: }
067: }
068: }
069: return firstElement;
070: }
071:
072: public Iterator iterator() {
073: Iterator iter = wrapped.iterator();
074: openIterators.add(iter);
075: return iter;
076: }
077:
078: public int size() {
079: return wrapped.size();
080: }
081:
082: public Object[] toArray() {
083: return toList().toArray();
084: }
085:
086: @SuppressWarnings("unchecked")
087: public List toList() {
088: if (wrapped instanceof List)
089: return (List) wrapped;
090: LinkedList arrayList = new LinkedList();
091: Iterator iter = wrapped.iterator();
092: try {
093: while (iter.hasNext()) {
094: arrayList.add(iter.next());
095: }
096: } finally {
097: wrapped.close(iter);
098: }
099: return arrayList;
100: }
101:
102: public boolean isEmpty() {
103: return wrapped.isEmpty();
104: }
105:
106: }
|