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.actions;
16:
17: import java.util.ArrayList;
18: import java.util.Collection;
19: import java.util.List;
20:
21: import net.refractions.udig.catalog.IGeoResource;
22: import net.refractions.udig.catalog.IService;
23: import net.refractions.udig.project.IProject;
24: import net.refractions.udig.project.internal.Project;
25: import net.refractions.udig.project.ui.ApplicationGIS;
26: import net.refractions.udig.ui.IDropAction;
27: import net.refractions.udig.ui.ViewerDropLocation;
28:
29: import org.eclipse.core.runtime.IProgressMonitor;
30:
31: /**
32: * Handles Layers and IGeoResources being dropped on a Project. It will create a map and add the layer/resource to the
33: * map.
34: *
35: * @author Jesse
36: * @since 1.1.0
37: */
38: public class OnProjectDropAction extends IDropAction {
39:
40: @SuppressWarnings("unchecked")
41: @Override
42: public boolean accept() {
43: if (getViewerLocation() == ViewerDropLocation.NONE)
44: return false;
45: if (!(getDestination() instanceof Project))
46: return false;
47:
48: if (isLegalType(getData()))
49: return true;
50:
51: if (getData() instanceof Collection) {
52: Collection<Object> coll = (Collection<Object>) getData();
53: for (Object object : coll) {
54: if (isLegalType(object))
55: return true;
56: }
57: }
58: return false;
59: }
60:
61: private boolean isLegalType(Object obj) {
62: if (obj instanceof IGeoResource)
63: return true;
64: if (obj instanceof IService)
65: return true;
66: return false;
67: }
68:
69: @SuppressWarnings("unchecked")
70: @Override
71: public void perform(IProgressMonitor monitor) {
72: if (!accept())
73: throw new IllegalStateException(
74: "the data or destination is not legal"); //$NON-NLS-1$
75: List<IGeoResource> resources = new ArrayList<IGeoResource>();
76:
77: if (getData() instanceof IGeoResource) {
78: resources.add((IGeoResource) getData());
79: } else if (getData() instanceof IService) {
80: resources.addAll(MapDropAction.toResources(monitor,
81: getData(), getClass()));
82: } else {
83: List<Object> list = (List<Object>) getData();
84: for (Object object : list) {
85: if (object instanceof IGeoResource) {
86: resources.add((IGeoResource) object);
87: } else if (object instanceof IService) {
88: Collection<IGeoResource> toResources = MapDropAction
89: .toResources(monitor, object, getClass());
90: resources.addAll(toResources);
91: }
92: }
93: }
94:
95: ApplicationGIS.createAndOpenMap(resources,
96: (IProject) getDestination());
97: }
98:
99: }
|