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.ui.internal.dragdrop;
016:
017: import net.refractions.udig.project.ILayer;
018: import net.refractions.udig.project.IMap;
019: import net.refractions.udig.project.command.MapCommand;
020: import net.refractions.udig.project.command.factory.EditCommandFactory;
021: import net.refractions.udig.project.internal.Layer;
022: import net.refractions.udig.project.ui.internal.Messages;
023: import net.refractions.udig.ui.IDropAction;
024:
025: import org.eclipse.core.runtime.IAdaptable;
026: import org.eclipse.core.runtime.IProgressMonitor;
027: import org.geotools.feature.Feature;
028: import org.geotools.filter.Filter;
029: import org.geotools.filter.FilterFactoryFinder;
030:
031: /**
032: * If the filter can adapt to a layer then features from the layer are copied to the destination
033: * layer otherwise the filter is set on the destination as the selection filter.
034: *
035: * @author jones
036: * @since 1.1.0
037: */
038: public class DropFilterAction extends IDropAction {
039:
040: /**
041: *
042: */
043: public DropFilterAction() {
044: super ();
045: }
046:
047: @Override
048: public boolean accept() {
049: return true;
050: }
051:
052: @Override
053: public void perform(IProgressMonitor monitor) {
054: Layer destinationLayer;
055: IMap destinationMap;
056:
057: if (getDestination() instanceof IMap) {
058: destinationMap = (IMap) getDestination();
059: destinationLayer = (Layer) destinationMap.getEditManager()
060: .getSelectedLayer();
061: } else if (getDestination() instanceof Layer) {
062: destinationLayer = (Layer) getDestination();
063: destinationMap = destinationLayer.getMap();
064: } else {
065: return;
066: }
067:
068: if (getData() instanceof IAdaptable) {
069: ILayer layer = (ILayer) ((IAdaptable) getData())
070: .getAdapter(ILayer.class);
071: Filter filter = (Filter) ((IAdaptable) getData())
072: .getAdapter(Filter.class);
073:
074: if (filter == null) {
075: Feature feature = (Feature) ((IAdaptable) getData())
076: .getAdapter(Feature.class);
077: if (feature != null)
078: filter = FilterFactoryFinder.createFilterFactory()
079: .createFidFilter(feature.getID());
080: else
081: return;
082: }
083:
084: if (layer == null) {
085: setSelection(filter == null ? (Filter) getData()
086: : filter, destinationLayer, monitor);
087: } else {
088: MapCommand c = EditCommandFactory.getInstance()
089: .createCopyFeaturesCommand(layer, filter,
090: destinationLayer);
091: destinationMap.sendCommandASync(c);
092: }
093: } else {
094: setSelection((Filter) getData(), destinationLayer, monitor);
095: }
096: }
097:
098: private void setSelection(Filter filter, Layer layer,
099: IProgressMonitor monitor) {
100: monitor.beginTask(Messages.DropFilterAction_taskname, 2);
101: monitor.worked(1);
102: layer.setFilter(filter);
103: monitor.done();
104: }
105:
106: }
|