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.project.internal.commands.edit;
016:
017: import java.io.IOException;
018: import java.util.Iterator;
019:
020: import net.refractions.udig.project.ILayer;
021: import net.refractions.udig.project.command.AbstractCommand;
022: import net.refractions.udig.project.command.UndoableMapCommand;
023: import net.refractions.udig.project.internal.Messages;
024:
025: import org.eclipse.core.runtime.IProgressMonitor;
026: import org.geotools.data.FeatureStore;
027: import org.geotools.feature.Feature;
028: import org.geotools.feature.FeatureCollection;
029: import org.geotools.feature.FeatureIterator;
030: import org.geotools.feature.collection.AbstractFeatureCollection;
031: import org.geotools.filter.FilterFactoryFinder;
032:
033: /**
034: * Adds the new feature to the
035: * @author jones
036: * @since 1.1.0
037: */
038: public class AddFeatureCommand extends AbstractCommand implements
039: UndoableMapCommand {
040:
041: private Feature feature;
042: private ILayer layer;
043: private FeatureStore resource;
044: private String fid;
045:
046: public AddFeatureCommand(Feature feature, ILayer layer) {
047: this .feature = feature;
048: this .layer = layer;
049: }
050:
051: public void run(IProgressMonitor monitor) throws Exception {
052: resource = layer.getResource(FeatureStore.class, monitor);
053: if (resource == null)
054: return;
055: FeatureCollection c = new AbstractFeatureCollection(resource
056: .getSchema()) {
057:
058: @Override
059: public int size() {
060: return 1;
061: }
062:
063: @Override
064: protected Iterator openIterator() {
065: return new Iterator() {
066: Feature next = feature;
067:
068: public Object next() {
069: Feature tmp = next;
070: next = null;
071: return tmp;
072: }
073:
074: public boolean hasNext() {
075: return next != null;
076: }
077:
078: public void remove() {
079: throw new UnsupportedOperationException();
080: }
081:
082: };
083: }
084:
085: @Override
086: protected void closeIterator(Iterator close) {
087: }
088:
089: };
090: fid = (String) resource.addFeatures(c).iterator().next();
091: }
092:
093: public Feature getNewFeature() throws IOException {
094: if (resource != null) {
095: FeatureCollection features = resource
096: .getFeatures(FilterFactoryFinder
097: .createFilterFactory().createFidFilter(fid));
098: FeatureIterator iter = features.features();
099: try {
100: return iter.next();
101: } finally {
102: features.close(iter);
103: }
104: }
105: return null;
106: }
107:
108: public String getName() {
109: return Messages.AddFeatureCommand_name;
110: }
111:
112: public void rollback(IProgressMonitor monitor) throws Exception {
113: resource.removeFeatures(FilterFactoryFinder
114: .createFilterFactory().createFidFilter(fid));
115: }
116:
117: /**
118: * @return Returns the fid.
119: */
120: public String getFid() {
121: return fid;
122: }
123:
124: }
|