|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2000, 2008 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 | +package ts.eclipse.ide.internal.ui.search; |
| 12 | + |
| 13 | +import java.util.HashMap; |
| 14 | + |
| 15 | +import org.eclipse.core.runtime.CoreException; |
| 16 | + |
| 17 | +import org.eclipse.core.resources.IFile; |
| 18 | +import org.eclipse.core.resources.IMarker; |
| 19 | + |
| 20 | +import org.eclipse.ui.IEditorDescriptor; |
| 21 | +import org.eclipse.ui.IEditorInput; |
| 22 | +import org.eclipse.ui.IEditorPart; |
| 23 | +import org.eclipse.ui.IEditorReference; |
| 24 | +import org.eclipse.ui.IEditorRegistry; |
| 25 | +import org.eclipse.ui.IReusableEditor; |
| 26 | +import org.eclipse.ui.IWorkbenchPage; |
| 27 | +import org.eclipse.ui.PartInitException; |
| 28 | +import org.eclipse.ui.ide.IDE; |
| 29 | +import org.eclipse.ui.part.FileEditorInput; |
| 30 | + |
| 31 | +import org.eclipse.ui.texteditor.ITextEditor; |
| 32 | + |
| 33 | +import org.eclipse.search.internal.ui.SearchMessages; |
| 34 | +import org.eclipse.search.internal.ui.SearchPlugin; |
| 35 | +import org.eclipse.search.ui.NewSearchUI; |
| 36 | + |
| 37 | +public class EditorOpener { |
| 38 | + |
| 39 | + private IEditorReference fReusedEditor; |
| 40 | + |
| 41 | + public IEditorPart open(IWorkbenchPage wbPage, IFile file, boolean activate) throws PartInitException { |
| 42 | + if (NewSearchUI.reuseEditor()) |
| 43 | + return showWithReuse(file, wbPage, getEditorID(file), activate); |
| 44 | + return showWithoutReuse(file, wbPage, getEditorID(file), activate); |
| 45 | + } |
| 46 | + |
| 47 | + public IEditorPart openAndSelect(IWorkbenchPage wbPage, IFile file, int offset, int length, boolean activate) throws PartInitException { |
| 48 | + String editorId= null; |
| 49 | + IEditorDescriptor desc= IDE.getEditorDescriptor(file); |
| 50 | + if (desc == null || !desc.isInternal()) { |
| 51 | + editorId= "org.eclipse.ui.DefaultTextEditor"; //$NON-NLS-1$ |
| 52 | + } else { |
| 53 | + editorId= desc.getId(); |
| 54 | + } |
| 55 | + |
| 56 | + IEditorPart editor; |
| 57 | + if (NewSearchUI.reuseEditor()) { |
| 58 | + editor= showWithReuse(file, wbPage, editorId, activate); |
| 59 | + } else { |
| 60 | + editor= showWithoutReuse(file, wbPage, editorId, activate); |
| 61 | + } |
| 62 | + |
| 63 | + if (editor instanceof ITextEditor) { |
| 64 | + ITextEditor textEditor= (ITextEditor) editor; |
| 65 | + textEditor.selectAndReveal(offset, length); |
| 66 | + } else if (editor != null) { |
| 67 | + showWithMarker(editor, file, offset, length); |
| 68 | + } |
| 69 | + return editor; |
| 70 | + } |
| 71 | + |
| 72 | + private IEditorPart showWithoutReuse(IFile file, IWorkbenchPage wbPage, String editorID, boolean activate) throws PartInitException { |
| 73 | + return IDE.openEditor(wbPage, file, editorID, activate); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + private String getEditorID(IFile file) throws PartInitException { |
| 78 | + IEditorDescriptor desc= IDE.getEditorDescriptor(file); |
| 79 | + if (desc == null) |
| 80 | + return SearchPlugin.getDefault().getWorkbench().getEditorRegistry().findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID).getId(); |
| 81 | + return desc.getId(); |
| 82 | + } |
| 83 | + |
| 84 | + private IEditorPart showWithReuse(IFile file, IWorkbenchPage page, String editorId, boolean activate) throws PartInitException { |
| 85 | + IEditorInput input= new FileEditorInput(file); |
| 86 | + IEditorPart editor= page.findEditor(input); |
| 87 | + if (editor != null) { |
| 88 | + page.bringToTop(editor); |
| 89 | + if (activate) { |
| 90 | + page.activate(editor); |
| 91 | + } |
| 92 | + return editor; |
| 93 | + } |
| 94 | + IEditorReference reusedEditorRef= fReusedEditor; |
| 95 | + if (reusedEditorRef != null) { |
| 96 | + boolean isOpen= reusedEditorRef.getEditor(false) != null; |
| 97 | + boolean canBeReused= isOpen && !reusedEditorRef.isDirty() && !reusedEditorRef.isPinned(); |
| 98 | + if (canBeReused) { |
| 99 | + boolean showsSameInputType= reusedEditorRef.getId().equals(editorId); |
| 100 | + if (!showsSameInputType) { |
| 101 | + page.closeEditors(new IEditorReference[] { reusedEditorRef }, false); |
| 102 | + fReusedEditor= null; |
| 103 | + } else { |
| 104 | + editor= reusedEditorRef.getEditor(true); |
| 105 | + if (editor instanceof IReusableEditor) { |
| 106 | + ((IReusableEditor) editor).setInput(input); |
| 107 | + page.bringToTop(editor); |
| 108 | + if (activate) { |
| 109 | + page.activate(editor); |
| 110 | + } |
| 111 | + return editor; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + editor= page.openEditor(input, editorId, activate); |
| 117 | + if (editor instanceof IReusableEditor) { |
| 118 | + IEditorReference reference= (IEditorReference) page.getReference(editor); |
| 119 | + fReusedEditor= reference; |
| 120 | + } else { |
| 121 | + fReusedEditor= null; |
| 122 | + } |
| 123 | + return editor; |
| 124 | + } |
| 125 | + |
| 126 | + private void showWithMarker(IEditorPart editor, IFile file, int offset, int length) throws PartInitException { |
| 127 | + IMarker marker= null; |
| 128 | + try { |
| 129 | + marker= file.createMarker(NewSearchUI.SEARCH_MARKER); |
| 130 | + HashMap attributes= new HashMap(4); |
| 131 | + attributes.put(IMarker.CHAR_START, new Integer(offset)); |
| 132 | + attributes.put(IMarker.CHAR_END, new Integer(offset + length)); |
| 133 | + marker.setAttributes(attributes); |
| 134 | + IDE.gotoMarker(editor, marker); |
| 135 | + } catch (CoreException e) { |
| 136 | + throw new PartInitException(SearchMessages.FileSearchPage_error_marker, e); |
| 137 | + } finally { |
| 138 | + if (marker != null) |
| 139 | + try { |
| 140 | + marker.delete(); |
| 141 | + } catch (CoreException e) { |
| 142 | + // ignore |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments