01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.project.ui.internal.dragdrop;
16:
17: import java.util.Collection;
18: import java.util.Collections;
19:
20: import net.refractions.udig.project.IMap;
21: import net.refractions.udig.project.internal.Layer;
22: import net.refractions.udig.project.internal.Map;
23: import net.refractions.udig.project.internal.commands.AddLayersCommand;
24: import net.refractions.udig.project.internal.commands.DeleteLayersCommand;
25: import net.refractions.udig.project.ui.ApplicationGIS;
26: import net.refractions.udig.project.ui.internal.LayersView;
27: import net.refractions.udig.project.ui.internal.MapEditor;
28: import net.refractions.udig.ui.IDropAction;
29:
30: import org.eclipse.core.runtime.IProgressMonitor;
31:
32: /**
33: * Moves layers from one map to another when destination is a map or LayersView or MapEditor.
34: *
35: * @author Jesse
36: * @since 1.1.0
37: */
38: public class MoveLayerDropAction extends IDropAction {
39:
40: @Override
41: public boolean accept() {
42: Layer layer = toLayer(this );
43: if (layer == null)
44: return false;
45: Object destination2 = getDestination();
46: if (destination2 instanceof LayersView
47: || destination2 instanceof MapEditor) {
48: destination2 = ApplicationGIS.getActiveMap();
49: }
50: return layer.getMap() != destination2;
51: }
52:
53: @SuppressWarnings("unchecked")
54: static Layer toLayer(IDropAction action) {
55: Layer layer;
56: if (action.getData() instanceof Layer) {
57: layer = (Layer) action.getData();
58: } else {
59: Collection<Layer> layers = (Collection<Layer>) action
60: .getData();
61: layer = layers.iterator().next();
62: // check that all layers are from same map
63: for (Layer layer2 : layers) {
64: if (layer2.getMap() != layer)
65: return null;
66: }
67: }
68: return layer;
69: }
70:
71: @Override
72: public void perform(IProgressMonitor monitor) {
73: Object data2 = getData();
74: IMap map;
75: if (getDestination() instanceof LayersView
76: || getDestination() instanceof MapEditor) {
77: map = ApplicationGIS.getActiveMap();
78: } else {
79: map = (Map) getDestination();
80: }
81:
82: Collection<Layer> layers = toCollection(data2);
83: map.sendCommandASync(new AddLayersCommand(layers));
84: layers.iterator().next().getMap().sendCommandASync(
85: new DeleteLayersCommand(layers.toArray(new Layer[0])));
86: }
87:
88: @SuppressWarnings("unchecked")
89: static Collection<Layer> toCollection(Object data2) {
90: Collection<Layer> layers;
91: if (data2 instanceof Layer) {
92: layers = Collections.singleton((Layer) data2);
93: } else {
94: layers = (Collection<Layer>) data2;
95: }
96: return layers;
97: }
98:
99: }
|