1 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 46 public class EditorAreaDropAdapter extends DropTargetAdapter { 47 private IWorkbenchWindow window; 48 49 53 public EditorAreaDropAdapter(IWorkbenchWindow window) { 54 this.window = window; 55 } 56 57 public void dragEnter(DropTargetEvent event) { 58 event.detail = DND.DROP_COPY; 60 event.feedback = DND.FEEDBACK_NONE; 61 } 62 63 public void dragOver(DropTargetEvent event) { 64 event.detail = DND.DROP_COPY; 66 event.feedback = DND.FEEDBACK_NONE; 67 } 68 69 public void dragOperationChanged(DropTargetEvent event) { 70 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 () { 80 public void run() { 81 asyncDrop(event, page); 82 } 83 }); 84 } 85 } 86 87 private void asyncDrop(DropTargetEvent event, IWorkbenchPage page) { 88 89 90 if (EditorInputTransfer.getInstance().isSupportedType( 91 event.currentDataType)) { 92 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 editorId = editorInputs[i].editorId; 99 openNonExternalEditor(page, editorInput, editorId); 100 } 101 } 102 103 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 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 127 else if (FileTransfer.getInstance().isSupportedType( 128 event.currentDataType)) { 129 Assert.isTrue(event.data instanceof String []); 130 String [] paths = (String []) 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 } 138 } 139 } 140 141 } 142 143 155 private IEditorPart openNonExternalEditor(IWorkbenchPage page, IFile file) { 156 IEditorPart result; 157 try { 158 IEditorDescriptor defaultEditorDesc = IDE.getDefaultEditor(file); 160 if (defaultEditorDesc != null 161 && !defaultEditorDesc.isOpenExternal()) { 162 result = IDE.openEditor(page, file, true); 164 } else { 165 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 if (editorDesc == null) { 177 editorDesc = editorReg 178 .findEditor(IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID); 179 } 180 181 if (editorDesc == null) { 183 throw new PartInitException(IDEWorkbenchMessages.IDE_noFileEditorFound); 184 } 185 186 result = page.openEditor(new FileEditorInput(file), editorDesc 188 .getId(), true); 189 } 190 } catch (PartInitException e) { 191 result = null; 193 } 194 return result; 195 } 196 197 209 private IEditorPart openNonExternalEditor(IWorkbenchPage page, 210 IMarker marker) { 211 IEditorPart result; 212 try { 213 if (!(marker.getResource() instanceof IFile)) { 215 return null; 216 } 217 IFile file = (IFile) marker.getResource(); 218 219 IEditorDescriptor editorDesc = null; 221 try { 222 String editorID = (String ) 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 } 232 233 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 if (result != null) { 243 IDE.gotoMarker(result, marker); 244 } 245 246 } catch (PartInitException e) { 247 result = null; 249 } 250 return result; 251 } 252 253 267 private IEditorPart openNonExternalEditor(IWorkbenchPage page, 268 IEditorInput editorInput, String 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 result = null; 282 } 283 return result; 284 } 285 286 } 287 | Popular Tags |