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.project.internal.commands.edit;
016:
017: import java.util.List;
018:
019: import net.refractions.udig.project.IEditManager;
020: import net.refractions.udig.project.ILayer;
021: import net.refractions.udig.project.command.AbstractCommand;
022: import net.refractions.udig.project.internal.Layer;
023: import net.refractions.udig.project.internal.Messages;
024:
025: import org.eclipse.core.runtime.IProgressMonitor;
026: import org.geotools.data.FeatureEvent;
027: import org.geotools.data.FeatureStore;
028: import org.geotools.feature.Feature;
029: import org.geotools.filter.Filter;
030:
031: import com.vividsolutions.jts.geom.Envelope;
032:
033: /**
034: * Deletes a set of features based on a filter.
035: *
036: * @author jones
037: * @since 1.0.0
038: */
039: public class DeleteManyFeaturesCommand extends AbstractCommand {
040:
041: private ILayer ilayer;
042: private Filter filter;
043:
044: public DeleteManyFeaturesCommand(ILayer layer, Filter filter) {
045: this .ilayer = layer;
046: this .filter = filter;
047: }
048:
049: public void run(IProgressMonitor monitor) throws Exception {
050: FeatureStore fs = ilayer.getResource(FeatureStore.class,
051: monitor);
052: Layer layer = null;
053: if (ilayer instanceof Layer) {
054: layer = (Layer) ilayer;
055: }
056: int events = 0;
057: try {
058: if (layer != null) {
059: layer.eSetDeliver(false);
060: events = layer.getFeatureChanges().size();
061: }
062: fs.removeFeatures(filter);
063: IEditManager editManager = getMap().getEditManager();
064: Feature editFeature = editManager.getEditFeature();
065: if (editFeature != null
066: && editManager.getEditLayer() == ilayer
067: && filter.contains(editFeature))
068: getMap().getEditManagerInternal().setEditFeature(null,
069: null);
070: } finally {
071: fireFeatureChangeEvent(layer, events);
072: }
073: }
074:
075: private void fireFeatureChangeEvent(Layer layer, int events) {
076: if (layer != null) {
077: layer.eSetDeliver(true);
078: List<FeatureEvent> tmp = layer.getFeatureChanges();
079: List<FeatureEvent> eventList = tmp.subList(events, tmp
080: .size());
081: Envelope bounds = new Envelope();
082: for (FeatureEvent event : eventList) {
083: if (bounds.isNull())
084: bounds.init(event.getBounds());
085: else
086: bounds.expandToInclude(event.getBounds());
087: }
088: if (!eventList.isEmpty()) {
089: FeatureEvent event = eventList.get(0);
090: tmp.add(new FeatureEvent(event.getFeatureSource(),
091: event.getEventType(), bounds));
092: }
093: }
094: }
095:
096: public String getName() {
097: return Messages.DeleteManyFeaturesCommand_name;
098: }
099:
100: }
|