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 net.refractions.udig.project.ILayer;
018: import net.refractions.udig.project.command.AbstractCommand;
019: import net.refractions.udig.project.command.UndoableMapCommand;
020: import net.refractions.udig.project.command.factory.EditCommandFactory;
021: import net.refractions.udig.tool.edit.internal.Messages;
022:
023: import org.eclipse.core.runtime.IProgressMonitor;
024: import org.eclipse.core.runtime.SubProgressMonitor;
025: import org.geotools.data.DefaultQuery;
026: import org.geotools.data.FeatureStore;
027: import org.geotools.feature.Feature;
028: import org.geotools.feature.FeatureIterator;
029: import org.geotools.filter.FilterFactoryFinder;
030:
031: import com.vividsolutions.jts.geom.Geometry;
032:
033: /**
034: * If there is no feature with the Feature ID in the layer then a new feature will be
035: * created. Otherwise the feature's geometry will be set.
036: *
037: * @author jones
038: * @since 1.1.0
039: */
040: public class CreateOrSetFeatureCommand extends AbstractCommand
041: implements UndoableMapCommand {
042:
043: private Geometry geom;
044: private ILayer layer;
045: private String fid;
046: private boolean createFeature;
047: private UndoableMapCommand modifyCommand;
048: private Feature feature;
049: private UndoableMapCommand createCommand;
050:
051: public CreateOrSetFeatureCommand(String fid2, ILayer layer2,
052: Geometry geom2) {
053: this .fid = fid2;
054: this .layer = layer2;
055: this .geom = geom2;
056: }
057:
058: public void run(IProgressMonitor monitor) throws Exception {
059: monitor.beginTask(Messages.CreateOrSetFeature_name, 10);
060: monitor.worked(1);
061: FeatureStore store = layer.getResource(FeatureStore.class,
062: new SubProgressMonitor(monitor, 2));
063: FeatureIterator iter = store.getFeatures(
064: new DefaultQuery(store.getSchema().getTypeName(),
065: FilterFactoryFinder.createFilterFactory()
066: .createFidFilter(fid))).features();
067: try {
068: createFeature = !iter.hasNext();
069: } finally {
070: iter.close();
071: }
072: if (createFeature) {
073: createFeature(new SubProgressMonitor(monitor, 8));
074: } else {
075: modifyFeature(new SubProgressMonitor(monitor, 8));
076: }
077: }
078:
079: /**
080: * Modifies the existing feature.
081: *
082: * @param monitor
083: * @throws Exception
084: */
085: private void modifyFeature(IProgressMonitor monitor)
086: throws Exception {
087:
088: modifyCommand = EditCommandFactory.getInstance()
089: .createSetGeomteryCommand(fid, layer, geom);
090: modifyCommand.setMap(getMap());
091: modifyCommand.run(monitor);
092: }
093:
094: /**
095: * Creates a new feature
096: * @param monitor
097: */
098: private void createFeature(IProgressMonitor monitor)
099: throws Exception {
100: feature = layer.getSchema().create(
101: new Object[layer.getSchema().getAttributeCount()]);
102: feature.setDefaultGeometry(geom);
103: createCommand = EditCommandFactory.getInstance()
104: .createAddFeatureCommand(feature, layer);
105: createCommand.setMap(getMap());
106: createCommand.run(monitor);
107: }
108:
109: public String getName() {
110: return Messages.CreateOrSetFeature_name;
111: }
112:
113: public void rollback(IProgressMonitor monitor) throws Exception {
114: if (createFeature) {
115: createCommand.rollback(monitor);
116: } else {
117: modifyCommand.rollback(monitor);
118: }
119: }
120:
121: }
|