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 java.io.IOException;
012: import java.util.Iterator;
013:
014: import net.refractions.udig.core.IBlockingProvider;
015: import net.refractions.udig.core.StaticBlockingProvider;
016: import net.refractions.udig.project.ILayer;
017: import net.refractions.udig.project.command.MapCommand;
018: import net.refractions.udig.project.command.UndoableMapCommand;
019: import net.refractions.udig.project.internal.Layer;
020: import net.refractions.udig.project.internal.Messages;
021:
022: import org.eclipse.core.runtime.IProgressMonitor;
023: import org.geotools.data.FeatureSource;
024: import org.geotools.data.FeatureStore;
025: import org.geotools.feature.Feature;
026: import org.geotools.feature.FeatureCollection;
027: import org.geotools.filter.FidFilter;
028: import org.geotools.filter.Filter;
029: import org.geotools.filter.FilterFactoryFinder;
030:
031: /**
032: * Sets the current editable Feature
033: * <p>
034: * </p>
035: *
036: * @author Jesse
037: * @since 1.0.0
038: */
039: public class SetEditFeatureCommand extends
040: AbstractLayerManagerControlCommand implements
041: UndoableMapCommand {
042: private IBlockingProvider<Feature> newVictim;
043:
044: private IBlockingProvider<ILayer> newLayer;
045:
046: private Feature oldEditVictim;
047:
048: private Layer oldEditLayer;
049:
050: private Filter oldSelection;
051:
052: private Layer layer;
053:
054: /**
055: * Creates a new instance of SetEditFeatureCommand.
056: *
057: * @param feature the new editable Feature.
058: * @param featureStore A featurestore that contains the editable Feature.
059: */
060: public SetEditFeatureCommand(Feature feature, ILayer layer) {
061: init(new StaticBlockingProvider<Feature>(feature),
062: new StaticBlockingProvider<ILayer>(layer));
063: }
064:
065: /**
066: * Creates a new instance of SetEditFeatureCommand
067: *
068: * @param feature the new editable Feature
069: */
070: public SetEditFeatureCommand(final Feature feature) {
071: this .init(new StaticBlockingProvider<Feature>(feature),
072: this .newLayer = new IBlockingProvider<ILayer>() {
073:
074: public ILayer get(IProgressMonitor monitor)
075: throws IOException {
076: for (Iterator<Layer> iter = editManager
077: .getMapInternal().getLayersInternal()
078: .iterator(); iter.hasNext();) {
079: Layer this Layer = iter.next();
080: if (this Layer
081: .hasResource(FeatureSource.class)) {
082: FeatureStore fs = this Layer
083: .getResource(
084: FeatureStore.class,
085: null);
086: FeatureCollection results = fs
087: .getFeatures(FilterFactoryFinder
088: .createFilterFactory()
089: .createFidFilter(
090: feature.getID()));
091: if (results.size() == 1) {
092: return this Layer;
093: }
094: }
095: }
096: return null;
097: }
098:
099: });
100: }
101:
102: public SetEditFeatureCommand(IBlockingProvider<Feature> feature,
103: IBlockingProvider<ILayer> layer) {
104: init(feature, layer);
105: }
106:
107: private void init(IBlockingProvider<Feature> feature,
108: IBlockingProvider<ILayer> layer2) {
109: this .newVictim = feature;
110: this .newLayer = layer2;
111: }
112:
113: /**
114: * @see net.refractions.udig.project.internal.command.MapCommand#run()
115: */
116: public void run(IProgressMonitor monitor) throws Exception {
117: oldEditVictim = editManager.getEditFeature();
118: oldEditLayer = editManager.getEditLayerInternal();
119: if (newVictim == null) {
120: editManager.setEditFeature(null, null);
121: layer = (Layer) newLayer.get(monitor);
122: if (layer != null) {
123: oldSelection = layer.getFilter();
124: layer.setFilter(Filter.ALL);
125: }
126: } else {
127: Feature feature = newVictim.get(monitor);
128: layer = (Layer) newLayer.get(monitor);
129: if (layer != null) {
130:
131: editManager.setEditFeature(feature, layer);
132: oldSelection = layer.getFilter();
133: Filter filter;
134: if (feature == null)
135: filter = Filter.ALL;
136: else
137: filter = FilterFactoryFinder.createFilterFactory()
138: .createFidFilter(feature.getID());
139: layer.setFilter(filter);
140: }
141: }
142: }
143:
144: /**
145: * @see net.refractions.udig.project.internal.command.MapCommand#copy()
146: */
147: public MapCommand copy() {
148: return new SetEditFeatureCommand(newVictim, newLayer);
149: }
150:
151: /**
152: * @see net.refractions.udig.project.command.MapCommand#getName()
153: */
154: public String getName() {
155: return Messages.SetEditFeatureCommand_setCurrentEditFeature;
156: }
157:
158: public void rollback(IProgressMonitor monitor) throws Exception {
159: if (layer != null)
160: layer.setFilter(oldSelection);
161: editManager.setEditFeature(oldEditVictim, oldEditLayer);
162: }
163:
164: }
|