1 11 package org.eclipse.search.internal.ui.text; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IMarker; 15 import org.eclipse.core.resources.IResource; 16 17 import org.eclipse.jface.action.Action; 18 import org.eclipse.jface.viewers.ISelection; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 21 import org.eclipse.ui.IEditorDescriptor; 22 import org.eclipse.ui.IEditorInput; 23 import org.eclipse.ui.IEditorPart; 24 import org.eclipse.ui.IEditorReference; 25 import org.eclipse.ui.IEditorRegistry; 26 import org.eclipse.ui.IReusableEditor; 27 import org.eclipse.ui.IWorkbenchPage; 28 import org.eclipse.ui.PartInitException; 29 import org.eclipse.ui.ide.IDE; 30 import org.eclipse.ui.part.FileEditorInput; 31 32 import org.eclipse.search.ui.ISearchResultView; 33 import org.eclipse.search.ui.ISearchResultViewEntry; 34 import org.eclipse.search.ui.SearchUI; 35 36 import org.eclipse.search.internal.ui.SearchMessages; 37 import org.eclipse.search.internal.ui.SearchPlugin; 38 import org.eclipse.search.internal.ui.util.ExceptionHandler; 39 40 class GotoMarkerAction extends Action { 41 42 private IEditorPart fEditor; 43 44 public void run() { 45 ISearchResultView view= SearchUI.getSearchResultView(); 46 ISelection selection= view.getSelection(); 47 Object element= null; 48 if (selection instanceof IStructuredSelection) 49 element= ((IStructuredSelection)selection).getFirstElement(); 50 if (element instanceof ISearchResultViewEntry) { 51 ISearchResultViewEntry entry= (ISearchResultViewEntry)element; 52 show(entry.getSelectedMarker()); 53 } 54 } 55 56 private void show(IMarker marker) { 57 if (SearchUI.reuseEditor()) 58 showWithReuse(marker); 59 else 60 showWithoutReuse(marker); 61 } 62 63 private void showWithReuse(IMarker marker) { 64 IWorkbenchPage page= SearchPlugin.getActivePage(); 65 IResource resource= marker.getResource(); 66 if (page == null || !(resource instanceof IFile)) 67 return; 68 69 IEditorInput input= new FileEditorInput((IFile)resource); 70 String editorId= null; 71 IEditorDescriptor desc= IDE.getDefaultEditor((IFile)resource); 72 if (desc == null) 73 editorId= SearchPlugin.getDefault().getWorkbench().getEditorRegistry().findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID).getId(); 74 else 75 editorId= desc.getId(); 76 77 IEditorPart editor= page.findEditor(input); 78 if (editor != null) 79 page.bringToTop(editor); 80 else { 81 boolean isOpen= false; 82 if (fEditor != null) { 83 IEditorReference[] parts= page.getEditorReferences(); 84 int i= 0; 85 while (!isOpen && i < parts.length) 86 isOpen= fEditor == parts[i++].getEditor(false); 87 } 88 89 boolean canBeReused= isOpen && !fEditor.isDirty() && !isPinned(fEditor); 90 boolean showsSameInputType= fEditor != null && fEditor.getSite().getId().equals(editorId); 91 92 if (canBeReused && !showsSameInputType) { 93 page.closeEditor(fEditor, false); 94 fEditor= null; 95 } 96 97 if (canBeReused && showsSameInputType) { 98 ((IReusableEditor)fEditor).setInput(input); 99 page.bringToTop(fEditor); 100 editor= fEditor; 101 } else 102 try { 103 editor= page.openEditor(input, editorId, false); 104 if (editor instanceof IReusableEditor) 105 fEditor= editor; 106 else 107 fEditor= null; 108 } catch (PartInitException ex) { 109 ExceptionHandler.handle(ex, SearchMessages.getString("Search.Error.openEditor.title"), SearchMessages.getString("Search.Error.openEditor.message")); return; 111 } 112 } 113 114 if (editor != null) { 115 IDE.gotoMarker(editor, marker); 116 } 117 } 118 119 private boolean isPinned(IEditorPart editor) { 120 if (editor == null) 121 return false; 122 123 IEditorReference[] editorRefs= editor.getEditorSite().getPage().getEditorReferences(); 124 int i= 0; 125 while (i < editorRefs.length) { 126 if (editor.equals(editorRefs[i].getEditor(false))) 127 return editorRefs[i].isPinned(); 128 i++; 129 } 130 return false; 131 } 132 133 private void showWithoutReuse(IMarker marker) { 134 IWorkbenchPage page= SearchPlugin.getActivePage(); 135 if (page == null) 136 return; 137 138 try { 139 IDE.openEditor(page, marker, false); 140 } catch (PartInitException ex) { 141 ExceptionHandler.handle(ex, SearchMessages.getString("Search.Error.openEditor.title"), SearchMessages.getString("Search.Error.openEditor.message")); return; 143 } 144 } 145 } 146 | Popular Tags |