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.text.MessageFormat;
012:
013: import net.refractions.udig.core.IBlockingProvider;
014: import net.refractions.udig.project.ILayer;
015: import net.refractions.udig.project.IMap;
016: import net.refractions.udig.project.command.UndoableMapCommand;
017: import net.refractions.udig.project.command.provider.EditFeatureProvider;
018: import net.refractions.udig.project.command.provider.EditLayerProvider;
019: import net.refractions.udig.project.command.provider.FIDFeatureProvider;
020: import net.refractions.udig.project.internal.Messages;
021:
022: import org.eclipse.core.runtime.IProgressMonitor;
023: import org.geotools.data.FeatureStore;
024: import org.geotools.feature.AttributeType;
025: import org.geotools.feature.Feature;
026: import org.geotools.filter.FidFilter;
027: import org.geotools.filter.FilterFactoryFinder;
028:
029: import com.vividsolutions.jts.geom.Geometry;
030:
031: /**
032: * This command modifies an attribute of the current editFeature(the victim that is currently
033: * edittable).
034: *
035: * @author jeichar
036: * @since 0.3
037: */
038: public class SetAttributeCommand extends AbstractEditCommand implements
039: UndoableMapCommand {
040: protected String xpath;
041:
042: protected final Object value;
043:
044: private Object oldValue;
045:
046: private final IBlockingProvider<Feature> editFeature;
047:
048: protected final IBlockingProvider<ILayer> editLayer;
049:
050: /**
051: * Creates a new instance of SetAttributeCommand.
052: *
053: * @param feature the feature to modify
054: * @param xpath the xpath that identifies an attribute in the current edit feature.
055: * @param value the value that will replace the old attribute value.
056: */
057: public SetAttributeCommand(IBlockingProvider<Feature> feature,
058: IBlockingProvider<ILayer> layer, String xpath, Object value) {
059: this .xpath = xpath;
060: this .value = value;
061: editFeature = feature;
062: editLayer = layer;
063: }
064:
065: /**
066: * @param featureID
067: * @param layer
068: * @param xpath2
069: * @param geom
070: */
071: public SetAttributeCommand(String featureID,
072: IBlockingProvider<ILayer> layer, String xpath2,
073: Geometry geom) {
074: this (new FIDFeatureProvider(featureID, layer), layer, xpath2,
075: geom);
076: }
077:
078: /**
079: * Creates a new instance of SetAttributeCommand.
080: *
081: * @param feature the feature to modify
082: * @param xpath the xpath that identifies an attribute in the current edit feature.
083: * @param value the value that will replace the old attribute value.
084: */
085: public SetAttributeCommand(String xpath, Object value) {
086: editFeature = new EditFeatureProvider(this );
087: editLayer = new EditLayerProvider(this , this );
088: this .xpath = xpath;
089: this .value = value;
090: }
091:
092: /**
093: * @see net.refractions.udig.project.command.MapCommand#run()
094: */
095: public void run(IProgressMonitor monitor) throws Exception {
096: ILayer layer = editLayer.get(monitor);
097: if (layer == null) {
098: System.err
099: .println("class " + editLayer.getClass().getName() + " is returning null"); //$NON-NLS-1$//$NON-NLS-2$
100: return;
101: }
102: FeatureStore resource = layer.getResource(FeatureStore.class,
103: null);
104: Feature feature2 = editFeature.get(monitor);
105:
106: FidFilter fidFilter = FilterFactoryFinder.createFilterFactory()
107: .createFidFilter(feature2.getID());
108:
109: this .oldValue = feature2.getAttribute(xpath);
110: feature2.setAttribute(xpath, value);
111:
112: AttributeType attributeType = layer.getSchema()
113: .getAttributeType(xpath);
114: resource.modifyFeatures(attributeType, value, fidFilter);
115: }
116:
117: /**
118: * @see net.refractions.udig.project.internal.command.UndoableCommand#rollback()
119: */
120: public void rollback(IProgressMonitor monitor) throws Exception {
121: Feature feature = editFeature.get(monitor);
122: feature.setAttribute(xpath, oldValue);
123: ILayer layer = editLayer.get(monitor);
124: FeatureStore resource = layer.getResource(FeatureStore.class,
125: null);
126: AttributeType attributeType = layer.getSchema()
127: .getAttributeType(xpath);
128: FidFilter createFidFilter = FilterFactoryFinder
129: .createFilterFactory().createFidFilter(feature.getID());
130: resource.modifyFeatures(attributeType, oldValue,
131: createFidFilter);
132: }
133:
134: /**
135: * @see net.refractions.udig.project.command.MapCommand#getName()
136: */
137: public String getName() {
138: return MessageFormat.format(
139: Messages.SetAttributeCommand_setFeatureAttribute,
140: new Object[] { xpath });
141: }
142:
143: @Override
144: public void setMap(IMap map) {
145: super.setMap(map);
146: }
147: }
|