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.tools.edit.commands;
016:
017: import java.util.Collections;
018:
019: import net.refractions.udig.project.ILayer;
020: import net.refractions.udig.project.command.AbstractCommand;
021: import net.refractions.udig.project.command.UndoableMapCommand;
022: import net.refractions.udig.project.internal.Layer;
023: import net.refractions.udig.project.internal.commands.edit.AddFeatureCommand;
024: import net.refractions.udig.tool.edit.internal.Messages;
025: import net.refractions.udig.tools.edit.support.EditGeom;
026: import net.refractions.udig.tools.edit.support.EditUtils;
027:
028: import org.eclipse.core.runtime.IProgressMonitor;
029: import org.eclipse.core.runtime.SubProgressMonitor;
030: import org.geotools.feature.Feature;
031: import org.geotools.filter.Filter;
032: import org.geotools.filter.FilterFactoryFinder;
033:
034: /**
035: * Creates a new feature and sets it as the EditFeature
036: * @author jones
037: * @since 1.1.0
038: */
039: public class CreateAndSetNewFeature extends AbstractCommand implements
040: UndoableMapCommand {
041:
042: private Layer layer;
043: private Feature feature;
044: private AddFeatureCommand addFeatureCommand;
045: private Feature oldFeature;
046: private ILayer oldLayer;
047: private EditGeom geom;
048: private String oldID;
049: private Filter oldSelection;
050:
051: public CreateAndSetNewFeature(EditGeom geom, Feature feature,
052: ILayer layer) {
053: this .layer = (Layer) layer;
054: this .feature = feature;
055: this .geom = geom;
056: }
057:
058: public void run(IProgressMonitor monitor) throws Exception {
059: monitor.beginTask(getName(), 14);
060: monitor.worked(2);
061: boolean prev = layer.eDeliver();
062: try {
063: layer.eSetDeliver(false);
064: addFeatureCommand = new AddFeatureCommand(feature, layer);
065: addFeatureCommand.setMap(getMap());
066: SubProgressMonitor subProgressMonitor = new SubProgressMonitor(
067: monitor, 10);
068: addFeatureCommand.run(subProgressMonitor);
069: subProgressMonitor.done();
070: this .oldFeature = getMap().getEditManager()
071: .getEditFeature();
072: this .oldLayer = getMap().getEditManager().getEditLayer();
073: this .oldID = geom.getFeatureIDRef().get();
074: geom.getFeatureIDRef().set(addFeatureCommand.getFid());
075:
076: getMap().getEditManagerInternal().setEditFeature(
077: addFeatureCommand.getNewFeature(), layer);
078: oldSelection = layer.getFilter();
079: Filter filter = FilterFactoryFinder.createFilterFactory()
080: .createFidFilter(addFeatureCommand.getFid());
081: layer.setFilter(filter);
082:
083: EditUtils.instance.refreshLayer(layer, Collections
084: .singleton(addFeatureCommand.getFid()), null,
085: false, true);
086: } finally {
087: layer.eSetDeliver(prev);
088: }
089:
090: monitor.done();
091: }
092:
093: public String getName() {
094: return Messages.CreateAndSetNewFeature_name;
095: }
096:
097: public void rollback(IProgressMonitor monitor) throws Exception {
098: monitor.beginTask(getName(), 14);
099: monitor.worked(2);
100: boolean prev = layer.eDeliver();
101: try {
102: layer.eSetDeliver(false);
103: layer.setFilter(oldSelection);
104:
105: SubProgressMonitor subProgressMonitor = new SubProgressMonitor(
106: monitor, 10);
107: addFeatureCommand.rollback(subProgressMonitor);
108: subProgressMonitor.done();
109: geom.getFeatureIDRef().set(oldID);
110:
111: getMap().getEditManagerInternal().setEditFeature(
112: oldFeature, (Layer) oldLayer);
113: EditUtils.instance.refreshLayer(layer, Collections
114: .singleton(addFeatureCommand.getFid()), null,
115: false, false);
116: } finally {
117: layer.eSetDeliver(prev);
118: }
119: monitor.done();
120: }
121:
122: }
|