1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IMarker; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.jdt.core.IClassFile; 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.IMember; 20 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 21 import org.eclipse.jdt.internal.ui.JavaPlugin; 22 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; 23 import org.eclipse.jdt.internal.ui.javaeditor.InternalClassFileEditorInput; 24 import org.eclipse.jdt.internal.ui.util.SelectionUtil; 25 import org.eclipse.jdt.ui.IPackagesViewPart; 26 import org.eclipse.jdt.ui.JavaUI; 27 import org.eclipse.jface.action.Action; 28 import org.eclipse.jface.dialogs.MessageDialog; 29 import org.eclipse.search.ui.ISearchResultView; 30 import org.eclipse.search.ui.ISearchResultViewEntry; 31 import org.eclipse.search.ui.NewSearchUI; 32 import org.eclipse.search.ui.SearchUI; 33 import org.eclipse.ui.IEditorInput; 34 import org.eclipse.ui.IEditorPart; 35 import org.eclipse.ui.IEditorReference; 36 import org.eclipse.ui.IReusableEditor; 37 import org.eclipse.ui.IViewPart; 38 import org.eclipse.ui.IWorkbenchPage; 39 import org.eclipse.ui.PartInitException; 40 import org.eclipse.ui.help.WorkbenchHelp; 41 import org.eclipse.ui.ide.IDE; 42 import org.eclipse.ui.part.FileEditorInput; 43 44 public class GotoMarkerAction extends Action { 45 46 private IEditorPart fEditor; 47 48 public GotoMarkerAction(){ 49 WorkbenchHelp.setHelp(this, IJavaHelpContextIds.GOTO_MARKER_ACTION); 50 } 51 52 public void run() { 53 ISearchResultView view= SearchUI.getSearchResultView(); 54 Object element= SelectionUtil.getSingleElement(view.getSelection()); 55 if (element instanceof ISearchResultViewEntry) { 56 ISearchResultViewEntry entry= (ISearchResultViewEntry)element; 57 show(entry.getSelectedMarker()); 58 } 59 } 60 61 private void show(IMarker marker) { 62 IResource resource= marker.getResource(); 63 if (resource == null || !resource.exists()) 64 return; 65 IWorkbenchPage wbPage= JavaPlugin.getActivePage(); 66 IJavaElement javaElement= SearchUtil.getJavaElement(marker); 67 68 if (javaElement != null && javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) 69 gotoPackagesView(javaElement, wbPage); 70 else { 71 if (NewSearchUI.reuseEditor()) 72 showWithReuse(marker, resource, javaElement, wbPage); 73 else 74 showWithoutReuse(marker, javaElement, wbPage); 75 } 76 } 77 78 private void showWithoutReuse(IMarker marker, IJavaElement javaElement, IWorkbenchPage wbPage) { 79 IEditorPart editor= null; 80 try { 81 Object objectToOpen= javaElement; 82 if (objectToOpen == null) 83 objectToOpen= marker.getResource(); 84 editor= EditorUtility.openInEditor(objectToOpen, false); 85 } catch (CoreException ex) { 86 MessageDialog.openError(JavaPlugin.getActiveWorkbenchShell(), SearchMessages.getString("Search.Error.openEditor.title"), SearchMessages.getString("Search.Error.openEditor.message")); } 88 if (editor != null) 89 IDE.gotoMarker(editor, marker); 90 } 91 92 private void showWithReuse(IMarker marker, IResource resource, IJavaElement javaElement, IWorkbenchPage wbPage) { 93 if (javaElement == null || !isBinary(javaElement)) { 94 if (resource instanceof IFile) 95 showInEditor(marker, wbPage, new FileEditorInput((IFile)resource), JavaUI.ID_CU_EDITOR); 96 } 97 else { 98 IClassFile cf= getClassFile(javaElement); 99 if (cf != null) 100 showInEditor(marker, wbPage, new InternalClassFileEditorInput(cf), JavaUI.ID_CF_EDITOR); 101 } 102 } 103 104 private boolean isPinned(IEditorPart editor) { 105 if (editor == null) 106 return false; 107 108 IEditorReference[] editorRefs= editor.getEditorSite().getPage().getEditorReferences(); 109 int i= 0; 110 while (i < editorRefs.length) { 111 if (editor.equals(editorRefs[i].getEditor(false))) 112 return editorRefs[i].isPinned(); 113 i++; 114 } 115 return false; 116 } 117 118 private void showInEditor(IMarker marker, IWorkbenchPage page, IEditorInput input, String editorId) { 119 IEditorPart editor= page.findEditor(input); 120 if (editor != null) 121 page.bringToTop(editor); 122 else { 123 boolean isOpen= false; 124 if (fEditor != null) { 125 IEditorReference[] parts= page.getEditorReferences(); 126 int i= 0; 127 while (!isOpen && i < parts.length) 128 isOpen= fEditor == parts[i++].getEditor(false); 129 } 130 131 boolean canBeReused= isOpen && !fEditor.isDirty() && !isPinned(fEditor); 132 boolean showsSameInputType= fEditor != null && fEditor.getSite().getId().equals(editorId); 133 if (canBeReused && !showsSameInputType) { 134 page.closeEditor(fEditor, false); 135 fEditor= null; 136 } 137 138 if (canBeReused && showsSameInputType) { 139 ((IReusableEditor)fEditor).setInput(input); 140 page.bringToTop(fEditor); 141 editor= fEditor; 142 } else { 143 try { 144 editor= page.openEditor(input, editorId, false); 145 if (editor instanceof IReusableEditor) 146 fEditor= editor; 147 else 148 fEditor= null; 149 } catch (PartInitException ex) { 150 MessageDialog.openError(JavaPlugin.getActiveWorkbenchShell(), SearchMessages.getString("Search.Error.openEditor.title"), SearchMessages.getString("Search.Error.openEditor.message")); return; 152 } 153 } 154 } 155 156 if (editor != null) { 157 IDE.gotoMarker(editor, marker); 158 } 159 } 160 161 private void gotoPackagesView(IJavaElement javaElement, IWorkbenchPage wbPage) { 162 try { 163 IViewPart view= wbPage.showView(JavaUI.ID_PACKAGES); 164 if (view instanceof IPackagesViewPart) 165 ((IPackagesViewPart)view).selectAndReveal(javaElement); 166 } catch (PartInitException ex) { 167 MessageDialog.openError(JavaPlugin.getActiveWorkbenchShell(), SearchMessages.getString("Search.Error.openEditor.title"), SearchMessages.getString("Search.Error.openEditor.message")); } 169 } 170 171 private IClassFile getClassFile(IJavaElement jElement) { 172 if (jElement instanceof IMember) 173 return ((IMember)jElement).getClassFile(); 174 return null; 175 } 176 177 private boolean isBinary(IJavaElement jElement) { 178 if (jElement instanceof IMember) 179 return ((IMember)jElement).isBinary(); 180 return false; 181 } 182 } 183 | Popular Tags |