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.ArrayList;
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.tool.edit.internal.Messages;
023: import net.refractions.udig.tools.edit.support.EditBlackboard;
024:
025: import org.eclipse.core.runtime.IProgressMonitor;
026: import org.eclipse.core.runtime.SubProgressMonitor;
027: import org.geotools.data.DefaultQuery;
028: import org.geotools.data.FeatureStore;
029: import org.geotools.data.Query;
030: import org.geotools.feature.Feature;
031: import org.geotools.feature.FeatureCollection;
032: import org.geotools.feature.FeatureIterator;
033: import org.geotools.filter.Filter;
034:
035: /**
036: * Add all the features to the EditBlackboard that are contained in the filter.
037: *
038: * @author jones
039: * @since 1.1.0
040: */
041: public class AddFeaturesCommand extends AbstractCommand implements
042: UndoableMapCommand {
043:
044: private Filter filter;
045: private ILayer layer;
046: private EditBlackboard bb;
047: private ArrayList<UndoableMapCommand> commands;
048:
049: /**
050: * Create new instance
051: * @param blackboard blackboard to add selected features
052: * @param layer layer used to obtain features. Must have a FeatureStore resource.
053: * @param filter filter used to select features
054: */
055: public AddFeaturesCommand(EditBlackboard blackboard, ILayer layer,
056: Filter filter) {
057: if (!layer.hasResource(FeatureStore.class))
058: throw new IllegalArgumentException(
059: "Layer must have a FeatureStore resource"); //$NON-NLS-1$
060: this .filter = filter;
061: this .layer = layer;
062: this .bb = blackboard;
063: }
064:
065: public void run(IProgressMonitor monitor) throws Exception {
066: monitor.beginTask(Messages.AddFeaturesCommand_taskMessage, 10);
067: monitor.worked(1);
068:
069: FeatureStore store = layer.getResource(FeatureStore.class,
070: new SubProgressMonitor(monitor, 2));
071: Query query = new DefaultQuery(layer.getSchema().getTypeName(),
072: filter, new String[] { layer.getSchema()
073: .getDefaultGeometry().getName() });
074: FeatureCollection features = store.getFeatures(query);
075:
076: FeatureIterator iter = features.features();
077: try {
078: commands = new ArrayList<UndoableMapCommand>();
079: while (iter.hasNext()) {
080: Feature feature = iter.next();
081: commands.add(new AddGeomCommand(bb, feature));
082: }
083:
084: float index = 0;
085: float inc = (float) 7 / commands.size();
086: for (UndoableMapCommand command : commands) {
087: command.setMap(getMap());
088: index += inc;
089: SubProgressMonitor subProgressMonitor = new SubProgressMonitor(
090: monitor, (int) index);
091: command.run(subProgressMonitor);
092: subProgressMonitor.done();
093: if (index > 1) {
094: index = 0;
095: }
096: }
097: } finally {
098: features.close(iter);
099: }
100:
101: monitor.done();
102:
103: }
104:
105: public String getName() {
106: return null;
107: }
108:
109: public void rollback(IProgressMonitor monitor) throws Exception {
110: monitor.beginTask(Messages.AddFeaturesCommand_undoTaskMessage,
111: commands.size() * 2);
112:
113: for (int i = commands.size() - 1; i > -1; i--) {
114: SubProgressMonitor subProgressMonitor = new SubProgressMonitor(
115: monitor, 2);
116: commands.get(i).rollback(subProgressMonitor);
117: subProgressMonitor.done();
118: }
119: monitor.done();
120: }
121:
122: }
|