1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import org.eclipse.core.resources.IFile; 14 15 import org.eclipse.jface.dialogs.MessageDialog; 16 17 import org.eclipse.ui.IEditorDescriptor; 18 import org.eclipse.ui.IEditorInput; 19 import org.eclipse.ui.IEditorPart; 20 import org.eclipse.ui.IEditorReference; 21 import org.eclipse.ui.IEditorRegistry; 22 import org.eclipse.ui.IReusableEditor; 23 import org.eclipse.ui.IViewReference; 24 import org.eclipse.ui.IWorkbenchPage; 25 import org.eclipse.ui.IWorkbenchPartReference; 26 import org.eclipse.ui.PartInitException; 27 import org.eclipse.ui.part.FileEditorInput; 28 29 import org.eclipse.ui.ide.IDE; 30 31 import org.eclipse.search.ui.NewSearchUI; 32 import org.eclipse.search.ui.text.Match; 33 34 import org.eclipse.jdt.core.IClassFile; 35 import org.eclipse.jdt.core.ICompilationUnit; 36 import org.eclipse.jdt.core.IJavaElement; 37 import org.eclipse.jdt.core.IMember; 38 import org.eclipse.jdt.core.JavaModelException; 39 40 import org.eclipse.jdt.ui.JavaUI; 41 42 import org.eclipse.jdt.internal.ui.JavaPlugin; 43 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; 44 import org.eclipse.jdt.internal.ui.javaeditor.InternalClassFileEditorInput; 45 46 public class JavaSearchEditorOpener { 47 48 private IEditorReference fReusedEditor; 49 50 public IEditorPart openElement(Object element) throws PartInitException, JavaModelException { 51 IWorkbenchPage wbPage= JavaPlugin.getActivePage(); 52 if (NewSearchUI.reuseEditor()) 53 return showWithReuse(element, wbPage); 54 else 55 return showWithoutReuse(element, wbPage); 56 } 57 58 public IEditorPart openMatch(Match match) throws PartInitException, JavaModelException { 59 Object element= getElementToOpen(match); 60 return openElement(element); 61 } 62 63 protected Object getElementToOpen(Match match) { 64 return match.getElement(); 65 } 66 67 private IEditorPart showWithoutReuse(Object element, IWorkbenchPage wbPage) throws PartInitException, JavaModelException { 68 return EditorUtility.openInEditor(element, false); 69 } 70 71 private IEditorPart showWithReuse(Object element, IWorkbenchPage wbPage) throws JavaModelException, PartInitException { 72 IFile file= getFile(element); 73 if (file != null) { 74 String editorID= getEditorID(file); 75 return showInEditor(wbPage, new FileEditorInput(file), editorID); 76 } else { 77 IClassFile cf= getClassFile(element); 78 if (cf != null) 79 return showInEditor(wbPage, new InternalClassFileEditorInput(cf), JavaUI.ID_CF_EDITOR); 80 } 81 return null; 82 } 83 84 private IFile getFile(Object element) throws JavaModelException { 85 if (element instanceof IFile) 86 return (IFile) element; 87 if (element instanceof IJavaElement) { 88 IJavaElement jElement= (IJavaElement) element; 89 ICompilationUnit cu= (ICompilationUnit) jElement.getAncestor(IJavaElement.COMPILATION_UNIT); 90 if (cu != null) { 91 return (IFile) cu.getCorrespondingResource(); 92 } 93 IClassFile cf= (IClassFile) jElement.getAncestor(IJavaElement.CLASS_FILE); 94 if (cf != null) 95 return (IFile) cf.getCorrespondingResource(); 96 } 97 return null; 98 } 99 100 private String getEditorID(IFile file) throws PartInitException { 101 IEditorDescriptor desc= IDE.getEditorDescriptor(file); 102 if (desc == null) 103 return JavaPlugin.getDefault().getWorkbench().getEditorRegistry().findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID).getId(); 104 else 105 return desc.getId(); 106 } 107 108 private IEditorPart showInEditor(IWorkbenchPage page, IEditorInput input, String editorId) { 109 IEditorPart editor= page.findEditor(input); 110 if (editor != null) { 111 page.bringToTop(editor); 112 return editor; 113 } 114 IEditorReference reusedEditorRef= fReusedEditor; 115 if (reusedEditorRef != null) { 116 boolean isOpen= reusedEditorRef.getEditor(false) != null; 117 boolean canBeReused= isOpen && !reusedEditorRef.isDirty() && !reusedEditorRef.isPinned(); 118 if (canBeReused) { 119 boolean showsSameInputType= reusedEditorRef.getId().equals(editorId); 120 if (!showsSameInputType) { 121 if (isOkToClose(page)) page.closeEditors(new IEditorReference[] { reusedEditorRef }, false); 123 fReusedEditor= null; 124 } else { 125 editor= reusedEditorRef.getEditor(true); 126 if (editor instanceof IReusableEditor) { 127 ((IReusableEditor) editor).setInput(input); 128 page.bringToTop(editor); 129 return editor; 130 } 131 } 132 } 133 } 134 try { 136 editor= page.openEditor(input, editorId, false); 137 if (editor instanceof IReusableEditor) { 138 IEditorReference reference= (IEditorReference) page.getReference(editor); 139 fReusedEditor= reference; 140 } else { 141 fReusedEditor= null; 142 } 143 return editor; 144 } catch (PartInitException ex) { 145 MessageDialog.openError(JavaPlugin.getActiveWorkbenchShell(), SearchMessages.Search_Error_openEditor_title, SearchMessages.Search_Error_openEditor_message); 146 return null; 147 } 148 } 149 150 private IClassFile getClassFile(Object element) { 151 if (!(element instanceof IJavaElement)) 152 return null; 153 if (element instanceof IClassFile) 154 return (IClassFile) element; 155 IJavaElement jElement= (IJavaElement) element; 156 if (jElement instanceof IMember) 157 return ((IMember) jElement).getClassFile(); 158 return null; 159 } 160 161 private boolean isOkToClose(IWorkbenchPage page) { 162 IWorkbenchPartReference searchViewRef= page.getActivePartReference(); 163 return searchViewRef == null || 164 !NewSearchUI.SEARCH_VIEW_ID.equals(searchViewRef.getId()) || 165 !((IViewReference) searchViewRef).isFastView(); 166 } 167 168 } 169 | Popular Tags |