KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > EditorAreaDropAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.ide;
13
14 import org.eclipse.core.filesystem.EFS;
15 import org.eclipse.core.filesystem.IFileStore;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.swt.dnd.DND;
23 import org.eclipse.swt.dnd.DropTargetAdapter;
24 import org.eclipse.swt.dnd.DropTargetEvent;
25 import org.eclipse.swt.dnd.FileTransfer;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.ui.IEditorDescriptor;
28 import org.eclipse.ui.IEditorInput;
29 import org.eclipse.ui.IEditorPart;
30 import org.eclipse.ui.IEditorRegistry;
31 import org.eclipse.ui.IWorkbenchPage;
32 import org.eclipse.ui.IWorkbenchWindow;
33 import org.eclipse.ui.PartInitException;
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.ide.IDE;
36 import org.eclipse.ui.part.EditorInputTransfer;
37 import org.eclipse.ui.part.FileEditorInput;
38 import org.eclipse.ui.part.MarkerTransfer;
39 import org.eclipse.ui.part.ResourceTransfer;
40
41 /**
42  * An editor area drop adapter to handle transfer types
43  * <code>EditorInputTransfer</code>, <code>MarkerTransfer</code>,
44  * and <code>ResourceTransfer</code>.
45  */

46 public class EditorAreaDropAdapter extends DropTargetAdapter {
47     private IWorkbenchWindow window;
48
49     /**
50      * Constructs a new EditorAreaDropAdapter.
51      * @param window the workbench window
52      */

53     public EditorAreaDropAdapter(IWorkbenchWindow window) {
54         this.window = window;
55     }
56
57     public void dragEnter(DropTargetEvent event) {
58         // always indicate a copy
59
event.detail = DND.DROP_COPY;
60         event.feedback = DND.FEEDBACK_NONE;
61     }
62
63     public void dragOver(DropTargetEvent event) {
64         // always indicate a copy
65
event.detail = DND.DROP_COPY;
66         event.feedback = DND.FEEDBACK_NONE;
67     }
68
69     public void dragOperationChanged(DropTargetEvent event) {
70         // always indicate a copy
71
event.detail = DND.DROP_COPY;
72         event.feedback = DND.FEEDBACK_NONE;
73     }
74
75     public void drop(final DropTargetEvent event) {
76         Display d = window.getShell().getDisplay();
77         final IWorkbenchPage page = window.getActivePage();
78         if (page != null) {
79             d.asyncExec(new Runnable JavaDoc() {
80                 public void run() {
81                     asyncDrop(event, page);
82                 }
83             });
84         }
85     }
86
87     private void asyncDrop(DropTargetEvent event, IWorkbenchPage page) {
88
89         /* Open Editor for generic IEditorInput */
90         if (EditorInputTransfer.getInstance().isSupportedType(
91                 event.currentDataType)) {
92             /* event.data is an array of EditorInputData, which contains an IEditorInput and
93              * the corresponding editorId */

94             Assert.isTrue(event.data instanceof EditorInputTransfer.EditorInputData[]);
95             EditorInputTransfer.EditorInputData[] editorInputs = (EditorInputTransfer.EditorInputData []) event.data;
96             for (int i = 0; i < editorInputs.length; i++) {
97                 IEditorInput editorInput = editorInputs[i].input;
98                 String JavaDoc editorId = editorInputs[i].editorId;
99                 openNonExternalEditor(page, editorInput, editorId);
100             }
101         }
102
103         /* Open Editor for Marker (e.g. Tasks, Bookmarks, etc) */
104         else if (MarkerTransfer.getInstance().isSupportedType(
105                 event.currentDataType)) {
106             Assert.isTrue(event.data instanceof IMarker[]);
107             IMarker[] markers = (IMarker[]) event.data;
108             for (int i = 0; i < markers.length; i++) {
109                 openNonExternalEditor(page, markers[i]);
110             }
111         }
112
113         /* Open Editor for resource */
114         else if (ResourceTransfer.getInstance().isSupportedType(
115                 event.currentDataType)) {
116             Assert.isTrue(event.data instanceof IResource[]);
117             IResource[] files = (IResource[]) event.data;
118             for (int i = 0; i < files.length; i++) {
119                 if (files[i] instanceof IFile) {
120                     IFile file = (IFile) files[i];
121                     openNonExternalEditor(page, file);
122                 }
123             }
124         }
125
126         /* Open Editor for file from local file system */
127         else if (FileTransfer.getInstance().isSupportedType(
128                 event.currentDataType)) {
129             Assert.isTrue(event.data instanceof String JavaDoc[]);
130             String JavaDoc[] paths = (String JavaDoc[]) event.data;
131             for (int i = 0; i < paths.length; i++) {
132                 IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(paths[i]));
133                 try {
134                     IDE.openEditorOnFileStore(page, fileStore);
135                 } catch (PartInitException e) {
136                     // silently ignore problems opening the editor
137
}
138             }
139         }
140
141     }
142
143     /**
144      * Opens an editor for the given file on the given workbench page in response
145      * to a drop on the workbench editor area. In contrast to other ways of opening
146      * an editor, we never open an external editor in this case (since external
147      * editors appear in their own window and not in the editor area).
148      * The operation fails silently if there is no suitable editor to open.
149      *
150      * @param page the workbench page
151      * @param file the file to open
152      * @return the editor part that was opened, or <code>null</code> if no editor
153      * was opened
154      */

155     private IEditorPart openNonExternalEditor(IWorkbenchPage page, IFile file) {
156         IEditorPart result;
157         try {
158             // find out which editor we would normal open
159
IEditorDescriptor defaultEditorDesc = IDE.getDefaultEditor(file);
160             if (defaultEditorDesc != null
161                     && !defaultEditorDesc.isOpenExternal()) {
162                 // open an internal or in-place editor
163
result = IDE.openEditor(page, file, true);
164             } else {
165                 // never open an external editor in response to a drop
166
// check the OS for in-place editor (OLE on Win32)
167
IEditorRegistry editorReg = PlatformUI.getWorkbench()
168                         .getEditorRegistry();
169                 IEditorDescriptor editorDesc = null;
170                 if (editorReg.isSystemInPlaceEditorAvailable(file.getName())) {
171                     editorDesc = editorReg
172                             .findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID);
173                 }
174
175                 // next lookup the default text editor
176
if (editorDesc == null) {
177                     editorDesc = editorReg
178                             .findEditor(IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID);
179                 }
180
181                 // if no valid editor found, bail out
182
if (editorDesc == null) {
183                     throw new PartInitException(IDEWorkbenchMessages.IDE_noFileEditorFound);
184                 }
185
186                 // open the editor on the file
187
result = page.openEditor(new FileEditorInput(file), editorDesc
188                         .getId(), true);
189             }
190         } catch (PartInitException e) {
191             // silently ignore problems opening the editor
192
result = null;
193         }
194         return result;
195     }
196
197     /**
198      * Opens an editor for the given marker on the given workbench page in response
199      * to a drop on the workbench editor area. In contrast to other ways of opening
200      * an editor, we never open an external editor in this case (since external
201      * editors appear in their own window and not in the editor area).
202      * The operation fails silently if there is no suitable editor to open.
203      *
204      * @param page the workbench page
205      * @param marker the marker to open
206      * @return the editor part that was opened, or <code>null</code> if no editor
207      * was opened
208      */

209     private IEditorPart openNonExternalEditor(IWorkbenchPage page,
210             IMarker marker) {
211         IEditorPart result;
212         try {
213             // get the marker resource file
214
if (!(marker.getResource() instanceof IFile)) {
215                 return null;
216             }
217             IFile file = (IFile) marker.getResource();
218
219             // get the preferred editor id from the marker
220
IEditorDescriptor editorDesc = null;
221             try {
222                 String JavaDoc editorID = (String JavaDoc) marker
223                         .getAttribute(IDE.EDITOR_ID_ATTR);
224                 if (editorID != null) {
225                     IEditorRegistry editorReg = PlatformUI.getWorkbench()
226                             .getEditorRegistry();
227                     editorDesc = editorReg.findEditor(editorID);
228                 }
229             } catch (CoreException e) {
230                 // ignore problems with getting the marker
231
}
232
233             // open the editor on the marker resource file
234
if (editorDesc != null && !editorDesc.isOpenExternal()) {
235                 result = page.openEditor(new FileEditorInput(file), editorDesc
236                         .getId(), true);
237             } else {
238                 result = openNonExternalEditor(page, file);
239             }
240
241             // get the editor to update its position based on the marker
242
if (result != null) {
243                 IDE.gotoMarker(result, marker);
244             }
245
246         } catch (PartInitException e) {
247             // silently ignore problems opening the editor
248
result = null;
249         }
250         return result;
251     }
252
253     /**
254      * Opens an editor for the given editor input and editor id combination on the
255      * given workbench page in response to a drop on the workbench editor area.
256      * In contrast to other ways of opening an editor, we never open an external
257      * editor in this case (since external editors appear in their own window and
258      * not in the editor area). The operation fails silently if the editor
259      * cannot be opened.
260      *
261      * @param page the workbench page
262      * @param editorInput the editor input
263      * @param editorId the editor id
264      * @return the editor part that was opened, or <code>null</code> if no editor
265      * was opened
266      */

267     private IEditorPart openNonExternalEditor(IWorkbenchPage page,
268             IEditorInput editorInput, String JavaDoc editorId) {
269         IEditorPart result;
270         try {
271             IEditorRegistry editorReg = PlatformUI.getWorkbench()
272                     .getEditorRegistry();
273             IEditorDescriptor editorDesc = editorReg.findEditor(editorId);
274             if (editorDesc != null && !editorDesc.isOpenExternal()) {
275                 result = page.openEditor(editorInput, editorId);
276             } else {
277                 result = null;
278             }
279         } catch (PartInitException e) {
280             // silently ignore problems opening the editor
281
result = null;
282         }
283         return result;
284     }
285
286 }
287
Popular Tags