001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.ide;
011:
012: import org.eclipse.core.filesystem.EFS;
013: import org.eclipse.core.filesystem.IFileStore;
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IMarker;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.runtime.Assert;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.Path;
020: import org.eclipse.swt.dnd.DND;
021: import org.eclipse.swt.dnd.DropTargetAdapter;
022: import org.eclipse.swt.dnd.DropTargetEvent;
023: import org.eclipse.swt.dnd.FileTransfer;
024: import org.eclipse.swt.widgets.Display;
025: import org.eclipse.ui.IEditorDescriptor;
026: import org.eclipse.ui.IEditorInput;
027: import org.eclipse.ui.IEditorPart;
028: import org.eclipse.ui.IEditorRegistry;
029: import org.eclipse.ui.IWorkbenchPage;
030: import org.eclipse.ui.IWorkbenchWindow;
031: import org.eclipse.ui.PartInitException;
032: import org.eclipse.ui.PlatformUI;
033: import org.eclipse.ui.ide.IDE;
034: import org.eclipse.ui.part.EditorInputTransfer;
035: import org.eclipse.ui.part.FileEditorInput;
036: import org.eclipse.ui.part.MarkerTransfer;
037: import org.eclipse.ui.part.ResourceTransfer;
038:
039: /**
040: * An editor area drop adapter to handle transfer types
041: * <code>EditorInputTransfer</code>, <code>MarkerTransfer</code>,
042: * and <code>ResourceTransfer</code>.
043: */
044: public class EditorAreaDropAdapter extends DropTargetAdapter {
045: private IWorkbenchWindow window;
046:
047: /**
048: * Constructs a new EditorAreaDropAdapter.
049: * @param window the workbench window
050: */
051: public EditorAreaDropAdapter(IWorkbenchWindow window) {
052: this .window = window;
053: }
054:
055: public void dragEnter(DropTargetEvent event) {
056: // always indicate a copy
057: event.detail = DND.DROP_COPY;
058: event.feedback = DND.FEEDBACK_NONE;
059: }
060:
061: public void dragOver(DropTargetEvent event) {
062: // always indicate a copy
063: event.detail = DND.DROP_COPY;
064: event.feedback = DND.FEEDBACK_NONE;
065: }
066:
067: public void dragOperationChanged(DropTargetEvent event) {
068: // always indicate a copy
069: event.detail = DND.DROP_COPY;
070: event.feedback = DND.FEEDBACK_NONE;
071: }
072:
073: public void drop(final DropTargetEvent event) {
074: Display d = window.getShell().getDisplay();
075: final IWorkbenchPage page = window.getActivePage();
076: if (page != null) {
077: d.asyncExec(new Runnable() {
078: public void run() {
079: asyncDrop(event, page);
080: }
081: });
082: }
083: }
084:
085: private void asyncDrop(DropTargetEvent event, IWorkbenchPage page) {
086:
087: /* Open Editor for generic IEditorInput */
088: if (EditorInputTransfer.getInstance().isSupportedType(
089: event.currentDataType)) {
090: /* event.data is an array of EditorInputData, which contains an IEditorInput and
091: * the corresponding editorId */
092: Assert
093: .isTrue(event.data instanceof EditorInputTransfer.EditorInputData[]);
094: EditorInputTransfer.EditorInputData[] editorInputs = (EditorInputTransfer.EditorInputData[]) event.data;
095: for (int i = 0; i < editorInputs.length; i++) {
096: IEditorInput editorInput = editorInputs[i].input;
097: String editorId = editorInputs[i].editorId;
098: openNonExternalEditor(page, editorInput, editorId);
099: }
100: }
101:
102: /* Open Editor for Marker (e.g. Tasks, Bookmarks, etc) */
103: else if (MarkerTransfer.getInstance().isSupportedType(
104: event.currentDataType)) {
105: Assert.isTrue(event.data instanceof IMarker[]);
106: IMarker[] markers = (IMarker[]) event.data;
107: for (int i = 0; i < markers.length; i++) {
108: openNonExternalEditor(page, markers[i]);
109: }
110: }
111:
112: /* Open Editor for resource */
113: else if (ResourceTransfer.getInstance().isSupportedType(
114: event.currentDataType)) {
115: Assert.isTrue(event.data instanceof IResource[]);
116: IResource[] files = (IResource[]) event.data;
117: for (int i = 0; i < files.length; i++) {
118: if (files[i] instanceof IFile) {
119: IFile file = (IFile) files[i];
120: openNonExternalEditor(page, file);
121: }
122: }
123: }
124:
125: /* Open Editor for file from local file system */
126: else if (FileTransfer.getInstance().isSupportedType(
127: event.currentDataType)) {
128: Assert.isTrue(event.data instanceof String[]);
129: String[] paths = (String[]) event.data;
130: for (int i = 0; i < paths.length; i++) {
131: IFileStore fileStore = EFS.getLocalFileSystem()
132: .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,
156: IFile file) {
157: IEditorPart result;
158: try {
159: // find out which editor we would normal open
160: IEditorDescriptor defaultEditorDesc = IDE
161: .getDefaultEditor(file);
162: if (defaultEditorDesc != null
163: && !defaultEditorDesc.isOpenExternal()) {
164: // open an internal or in-place editor
165: result = IDE.openEditor(page, file, true);
166: } else {
167: // never open an external editor in response to a drop
168: // check the OS for in-place editor (OLE on Win32)
169: IEditorRegistry editorReg = PlatformUI.getWorkbench()
170: .getEditorRegistry();
171: IEditorDescriptor editorDesc = null;
172: if (editorReg.isSystemInPlaceEditorAvailable(file
173: .getName())) {
174: editorDesc = editorReg
175: .findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID);
176: }
177:
178: // next lookup the default text editor
179: if (editorDesc == null) {
180: editorDesc = editorReg
181: .findEditor(IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID);
182: }
183:
184: // if no valid editor found, bail out
185: if (editorDesc == null) {
186: throw new PartInitException(
187: IDEWorkbenchMessages.IDE_noFileEditorFound);
188: }
189:
190: // open the editor on the file
191: result = page.openEditor(new FileEditorInput(file),
192: editorDesc.getId(), true);
193: }
194: } catch (PartInitException e) {
195: // silently ignore problems opening the editor
196: result = null;
197: }
198: return result;
199: }
200:
201: /**
202: * Opens an editor for the given marker on the given workbench page in response
203: * to a drop on the workbench editor area. In contrast to other ways of opening
204: * an editor, we never open an external editor in this case (since external
205: * editors appear in their own window and not in the editor area).
206: * The operation fails silently if there is no suitable editor to open.
207: *
208: * @param page the workbench page
209: * @param marker the marker to open
210: * @return the editor part that was opened, or <code>null</code> if no editor
211: * was opened
212: */
213: private IEditorPart openNonExternalEditor(IWorkbenchPage page,
214: IMarker marker) {
215: IEditorPart result;
216: try {
217: // get the marker resource file
218: if (!(marker.getResource() instanceof IFile)) {
219: return null;
220: }
221: IFile file = (IFile) marker.getResource();
222:
223: // get the preferred editor id from the marker
224: IEditorDescriptor editorDesc = null;
225: try {
226: String editorID = (String) marker
227: .getAttribute(IDE.EDITOR_ID_ATTR);
228: if (editorID != null) {
229: IEditorRegistry editorReg = PlatformUI
230: .getWorkbench().getEditorRegistry();
231: editorDesc = editorReg.findEditor(editorID);
232: }
233: } catch (CoreException e) {
234: // ignore problems with getting the marker
235: }
236:
237: // open the editor on the marker resource file
238: if (editorDesc != null && !editorDesc.isOpenExternal()) {
239: result = page.openEditor(new FileEditorInput(file),
240: editorDesc.getId(), true);
241: } else {
242: result = openNonExternalEditor(page, file);
243: }
244:
245: // get the editor to update its position based on the marker
246: if (result != null) {
247: IDE.gotoMarker(result, marker);
248: }
249:
250: } catch (PartInitException e) {
251: // silently ignore problems opening the editor
252: result = null;
253: }
254: return result;
255: }
256:
257: /**
258: * Opens an editor for the given editor input and editor id combination on the
259: * given workbench page in response to a drop on the workbench editor area.
260: * In contrast to other ways of opening an editor, we never open an external
261: * editor in this case (since external editors appear in their own window and
262: * not in the editor area). The operation fails silently if the editor
263: * cannot be opened.
264: *
265: * @param page the workbench page
266: * @param editorInput the editor input
267: * @param editorId the editor id
268: * @return the editor part that was opened, or <code>null</code> if no editor
269: * was opened
270: */
271: private IEditorPart openNonExternalEditor(IWorkbenchPage page,
272: IEditorInput editorInput, String editorId) {
273: IEditorPart result;
274: try {
275: IEditorRegistry editorReg = PlatformUI.getWorkbench()
276: .getEditorRegistry();
277: IEditorDescriptor editorDesc = editorReg
278: .findEditor(editorId);
279: if (editorDesc != null && !editorDesc.isOpenExternal()) {
280: result = page.openEditor(editorInput, editorId);
281: } else {
282: result = null;
283: }
284: } catch (PartInitException e) {
285: // silently ignore problems opening the editor
286: result = null;
287: }
288: return result;
289: }
290:
291: }
|