001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.project.internal.commands.edit;
010:
011: import net.refractions.udig.project.ILayer;
012: import net.refractions.udig.project.command.MapCommand;
013: import net.refractions.udig.project.command.UndoableMapCommand;
014: import net.refractions.udig.project.internal.Layer;
015: import net.refractions.udig.project.internal.Messages;
016: import net.refractions.udig.project.internal.ProjectPlugin;
017:
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.geotools.data.FeatureStore;
020: import org.geotools.feature.Feature;
021: import org.geotools.feature.FeatureCollection;
022: import org.geotools.feature.FeatureIterator;
023: import org.geotools.feature.FeatureType;
024: import org.geotools.filter.Filter;
025: import org.geotools.filter.FilterFactory;
026: import org.geotools.filter.FilterFactoryFinder;
027:
028: /**
029: * Compares the editFeature with the corresponding feature in the data store (The feature with the
030: * same fid) and replaces the old feature with the edit feature. If the feature is a new feature
031: * then the feature is added to the datastore.
032: *
033: * @author jeichar
034: * @since 0.3
035: */
036: public class WriteEditFeatureCommand extends AbstractEditCommand
037: implements UndoableMapCommand {
038: private Feature old;
039: private boolean added = false;
040: private boolean noChange = false;
041:
042: public WriteEditFeatureCommand() {
043: }
044:
045: /**
046: * @see net.refractions.udig.project.internal.command.MapCommand#run()
047: */
048: @SuppressWarnings("deprecation")
049: public void run(IProgressMonitor monitor) throws Exception {
050: monitor.beginTask(Messages.WriteEditFeatureCommand_runTask,
051: IProgressMonitor.UNKNOWN);
052:
053: Feature editFeature = getMap().getEditManager()
054: .getEditFeature();
055: ILayer editLayer = getMap().getEditManager().getEditLayer();
056: editFeature = getMap().getEditManagerInternal()
057: .getEditFeature();
058: editLayer = getMap().getEditManagerInternal()
059: .getEditLayerInternal();
060: if (editFeature == null || editLayer == null) {
061: noChange = true;
062: return;
063: }
064: old = editFeature.getFeatureType().duplicate(editFeature);
065: FeatureType featureType = editFeature.getFeatureType();
066: FilterFactory factory = FilterFactoryFinder
067: .createFilterFactory();
068: FeatureStore store = editLayer.getResource(FeatureStore.class,
069: null);
070: Filter filter = factory.createFidFilter(editFeature.getID());
071: FeatureCollection results = store.getFeatures(filter);
072:
073: FeatureIterator reader = results.features();
074: try {
075: if (reader.hasNext()) {
076: try {
077: store.modifyFeatures(featureType
078: .getAttributeTypes(), editFeature
079: .getAttributes(new Object[featureType
080: .getAttributeCount()]), filter);
081: } catch (Exception e) {
082: ProjectPlugin.log("", e); //$NON-NLS-1$
083: noChange = true;
084: }
085:
086: } else {
087: added = true;
088: getMap().getEditManagerInternal().addFeature(
089: editFeature, (Layer) editLayer);
090: }
091: } finally {
092: if (reader != null)
093: reader.close();
094: }
095: monitor.done();
096: }
097:
098: /**
099: * @see net.refractions.udig.project.internal.command.MapCommand#copy()
100: */
101: public MapCommand copy() {
102: return new WriteEditFeatureCommand();
103: }
104:
105: /**
106: * @see net.refractions.udig.project.command.MapCommand#getName()
107: */
108: public String getName() {
109: return Messages.SetEditFeatureCommand_setCurrentEditFeature;
110: }
111:
112: public void rollback(IProgressMonitor monitor) throws Exception {
113: if (noChange)
114: return;
115: monitor.beginTask(
116: Messages.WriteEditFeatureCommand_rollbackTask,
117: IProgressMonitor.UNKNOWN);
118: Feature editFeature = getMap().getEditManager()
119: .getEditFeature();
120: ILayer editLayer = getMap().getEditManager().getEditLayer();
121: if (editFeature == null || editLayer == null)
122: return;
123:
124: FilterFactory factory = FilterFactoryFinder
125: .createFilterFactory();
126: Filter filter = factory.createFidFilter(old.getID());
127: FeatureStore store = editLayer.getResource(FeatureStore.class,
128: null);
129: if (added) {
130: store.removeFeatures(filter);
131: getMap().getEditManagerInternal()
132: .setEditFeature(null, null);
133: } else {
134: FeatureType featureType = old.getFeatureType();
135: store.modifyFeatures(featureType.getAttributeTypes(), old
136: .getAttributes(new Object[featureType
137: .getAttributeCount()]), filter);
138: }
139: monitor.done();
140: }
141:
142: }
|