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.core.IBlockingProvider;
012: import net.refractions.udig.project.ILayer;
013: import net.refractions.udig.project.command.MapCommand;
014: import net.refractions.udig.project.command.PostDeterminedEffectCommand;
015: import net.refractions.udig.project.command.UndoableMapCommand;
016: import net.refractions.udig.project.internal.Layer;
017: import net.refractions.udig.project.internal.Messages;
018:
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.core.runtime.SubProgressMonitor;
021: import org.geotools.data.FeatureStore;
022: import org.geotools.feature.Feature;
023: import org.geotools.filter.FilterFactoryFinder;
024:
025: /**
026: * Deletes a feature from the map.
027: *
028: * @author Jesse
029: * @since 1.0.0
030: */
031: public class DeleteFeatureCommand extends AbstractEditCommand implements
032: UndoableMapCommand, PostDeterminedEffectCommand {
033:
034: IBlockingProvider<Feature> featureProvider;
035:
036: private IBlockingProvider<ILayer> layerProvider;
037:
038: protected boolean done;
039:
040: private Feature feature;
041:
042: private ILayer oldLayer;
043:
044: /**
045: * Construct <code>DeleteFeatureCommand</code>.
046: */
047: public DeleteFeatureCommand(
048: IBlockingProvider<Feature> featureProvider,
049: IBlockingProvider<ILayer> layerProvider) {
050: this .featureProvider = featureProvider;
051: this .layerProvider = layerProvider;
052: }
053:
054: /**
055: * @see net.refractions.udig.project.command.MapCommand#run()
056: */
057: public void run(IProgressMonitor monitor) throws Exception {
058: execute(monitor);
059: }
060:
061: public boolean execute(IProgressMonitor monitor) throws Exception {
062: monitor.beginTask(Messages.DeleteFeatureCommand_deleteFeature,
063: 4);
064: monitor.worked(1);
065: feature = featureProvider
066: .get(new SubProgressMonitor(monitor, 1));
067: if (feature == null)
068: return false;
069: oldLayer = layerProvider
070: .get(new SubProgressMonitor(monitor, 1));
071: oldLayer.getResource(FeatureStore.class, null).removeFeatures(
072: FilterFactoryFinder.createFilterFactory()
073: .createFidFilter(feature.getID()));
074: map.getEditManagerInternal().setEditFeature(null, null);
075: return true;
076: }
077:
078: public MapCommand copy() {
079: return new DeleteFeatureCommand(featureProvider, layerProvider);
080: }
081:
082: /**
083: * @see net.refractions.udig.project.command.MapCommand#getName()
084: */
085: public String getName() {
086: return Messages.DeleteFeatureCommand_deleteFeature;
087: }
088:
089: /**
090: * @see net.refractions.udig.project.command.UndoableCommand#rollback()
091: */
092: public void rollback(IProgressMonitor monitor) throws Exception {
093: if (feature == null)
094: return;
095: map.getEditManagerInternal().setEditFeature(feature,
096: (Layer) oldLayer);
097: map.getEditManagerInternal().addFeature(feature,
098: (Layer) oldLayer);
099: }
100:
101: }
|