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.actions;
016:
017: import java.io.File;
018: import java.io.IOException;
019: import java.net.MalformedURLException;
020: import java.net.URL;
021:
022: import net.refractions.udig.project.ILayer;
023: import net.refractions.udig.project.IMap;
024: import net.refractions.udig.project.IProjectElement;
025: import net.refractions.udig.project.internal.ProjectPlugin;
026: import net.refractions.udig.project.ui.ApplicationGIS;
027: import net.refractions.udig.project.ui.internal.LayersView;
028: import net.refractions.udig.project.ui.internal.MapEditor;
029: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
030: import net.refractions.udig.project.ui.render.displayAdapter.ViewportPane;
031: import net.refractions.udig.ui.IDropAction;
032:
033: import org.eclipse.core.runtime.IProgressMonitor;
034: import org.eclipse.emf.ecore.EObject;
035: import org.eclipse.swt.dnd.DropTarget;
036: import org.eclipse.swt.widgets.Control;
037:
038: /**
039: * Opens a map when dropped on an IEditorPart
040: *
041: * @author jones
042: * @since 1.1.0
043: */
044: public class DropMap extends IDropAction {
045:
046: @Override
047: public boolean accept() {
048:
049: if (getDestination() instanceof ILayer) {
050: Control control = ((DropTarget) getEvent().widget)
051: .getControl();
052: if (control != LayersView.getViewer().getControl()
053: && !(control instanceof ViewportPane))
054: return false;
055: }
056:
057: if (getData() instanceof IProjectElement) {
058: return true;
059: }
060:
061: // we want to open the project element if one of it children is dragged
062: if (getData() instanceof EObject
063: && !(getDestination() instanceof ILayer)) {
064: EObject eobj = (EObject) getData();
065: while (eobj != null && !(eobj instanceof IProjectElement)) {
066: eobj = eobj.eContainer();
067: }
068:
069: if (eobj == null)
070: return false;
071:
072: // if layer dropped in layers view when map is open then we want use another action
073: if (getData() instanceof ILayer
074: && getDestination() instanceof LayersView
075: && ApplicationGIS.getActiveMap() != null) {
076: return false;
077: }
078: // layer droppe in map if special too
079: if (getData() instanceof ILayer
080: && getDestination() instanceof MapEditor) {
081: return false;
082: }
083:
084: return true;
085: }
086: URL url = null;
087: if (getData() instanceof String) {
088: String string = (String) getData();
089: try {
090: url = new URL(string);
091: } catch (MalformedURLException e) {
092: // guess it is not a URL
093: }
094: if (url == null) {
095: File file = new File(string);
096: if (!file.exists()) {
097: return false;
098: }
099: }
100: }
101:
102: if (getData() instanceof URL) {
103: url = (URL) getData();
104: }
105:
106: if (url != null) {
107: String fileString = url.getFile();
108: try {
109: File file = new File(fileString);
110: if (file.exists() && fileString.endsWith(".umap")) //$NON-NLS-1$
111: return true;
112:
113: return false;
114: } catch (Exception e) {
115: // ok not a file either
116: return false;
117: }
118: }
119: return false;
120: }
121:
122: @Override
123: public void perform(IProgressMonitor monitor) {
124: if (getData() instanceof EObject
125: && !(getDestination() instanceof ILayer)) {
126: EObject eobj = (EObject) getData();
127: while (eobj != null && !(eobj instanceof IProjectElement)) {
128: eobj = eobj.eContainer();
129: }
130: ApplicationGIS.openProjectElement((IProjectElement) eobj,
131: false);
132: }
133: URL url = null;
134: if (getData() instanceof IProjectElement) {
135: ApplicationGIS.openProjectElement(
136: (IProjectElement) getData(), false);
137: } else if (getData() instanceof String) {
138: String string = (String) getData();
139: try {
140: url = new URL(string);
141: } catch (MalformedURLException e) {
142: // guess it is not a URL
143: File file = new File(string);
144: if (file.exists())
145: try {
146: url = file.toURL();
147: } catch (MalformedURLException e1) {
148: // oh well
149: }
150: }
151:
152: } else if (getData() instanceof URL) {
153: url = (URL) getData();
154: }
155:
156: if (url != null) {
157: String fileString;
158: try {
159: fileString = url.getFile();
160: File file = new File(fileString);
161: if (!file.exists() || !fileString.endsWith(".umap")) { //$NON-NLS-1$
162: ProjectUIPlugin
163: .log(
164: "Some how accept() accepted: " + getData(), new Exception()); //$NON-NLS-1$
165: return;
166: }
167: } catch (Exception e) {
168: // ok not a file either
169: ProjectUIPlugin
170: .log(
171: "Some how accept() accepted: " + getData(), new Exception()); //$NON-NLS-1$
172: return;
173: }
174:
175: IProjectElement elem;
176: try {
177: elem = ApplicationGIS.loadProjectElement(url,
178: ApplicationGIS.getActiveProject());
179: if (elem instanceof IProjectElement)
180: ApplicationGIS.openProjectElement(
181: (IProjectElement) elem, false);
182: } catch (IllegalArgumentException e) {
183: ProjectPlugin.log("", e); //$NON-NLS-1$
184: } catch (IOException e) {
185: ProjectPlugin.log("", e); //$NON-NLS-1$
186: }
187: }
188: }
189:
190: }
|