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: import java.util.HashSet;
20:
21: import net.refractions.udig.project.internal.Project;
22: import net.refractions.udig.project.internal.ProjectElement;
23: import net.refractions.udig.ui.IDropAction;
24:
25: import org.eclipse.core.runtime.IProgressMonitor;
26: import org.eclipse.emf.ecore.EObject;
27:
28: /**
29: * Move Project elements between projects
30: *
31: * @author Jesse
32: * @since 1.1.0
33: */
34: public class MoveProjectElement extends IDropAction {
35:
36: @Override
37: public boolean accept() {
38: Collection<ProjectElement> elements = toProjectElements();
39: if (elements == null)
40: return false;
41:
42: return true;
43: }
44:
45: private Collection<ProjectElement> toProjectElements() {
46: if (getData() instanceof ProjectElement) {
47: ProjectElement element = (ProjectElement) getData();
48: return Collections.singleton(element);
49: }
50:
51: if (getData() instanceof EObject) {
52: EObject eobj = (EObject) getData();
53: while (eobj != null && !(eobj instanceof ProjectElement))
54: eobj = eobj.eContainer();
55:
56: if (eobj instanceof ProjectElement)
57: return Collections.singleton((ProjectElement) eobj);
58:
59: return null;
60: }
61:
62: if (getData() instanceof Collection) {
63: Collection<ProjectElement> elements = new HashSet<ProjectElement>();
64: Collection data = (Collection) getData();
65: for (Object object : data) {
66: if (object instanceof ProjectElement) {
67: ProjectElement element = (ProjectElement) object;
68: elements.add(element);
69: }
70:
71: if (object instanceof EObject) {
72: EObject eobj = (EObject) object;
73: while (eobj != null
74: && !(eobj instanceof ProjectElement))
75: eobj = eobj.eContainer();
76:
77: if (eobj instanceof ProjectElement)
78: elements.add((ProjectElement) eobj);
79: }
80: }
81:
82: if (!elements.isEmpty())
83: return elements;
84: }
85: return null;
86: }
87:
88: @Override
89: public void perform(IProgressMonitor monitor) {
90: Collection<ProjectElement> elements = toProjectElements();
91:
92: Project destination = (Project) getDestination();
93:
94: destination.getElementsInternal().addAll(elements);
95: }
96:
97: }
|