1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.resources.IResource; 16 17 import org.eclipse.jface.viewers.ISelection; 18 import org.eclipse.jface.viewers.IStructuredSelection; 19 import org.eclipse.jface.viewers.StructuredSelection; 20 21 import org.eclipse.jface.text.ITextSelection; 22 23 import org.eclipse.ui.IPageLayout; 24 import org.eclipse.ui.IViewPart; 25 import org.eclipse.ui.IWorkbenchPage; 26 import org.eclipse.ui.IWorkbenchSite; 27 import org.eclipse.ui.PartInitException; 28 import org.eclipse.ui.PlatformUI; 29 import org.eclipse.ui.part.ISetSelectionTarget; 30 31 import org.eclipse.jdt.core.ICompilationUnit; 32 import org.eclipse.jdt.core.IJavaElement; 33 34 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 35 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 36 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 37 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 38 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 39 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 40 41 53 public class ShowInNavigatorViewAction extends SelectionDispatchAction { 54 55 private JavaEditor fEditor; 56 57 64 public ShowInNavigatorViewAction(IWorkbenchSite site) { 65 super(site); 66 setText(ActionMessages.ShowInNavigatorView_label); 67 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SHOW_IN_NAVIGATOR_VIEW_ACTION); 68 } 69 70 74 public ShowInNavigatorViewAction(JavaEditor editor) { 75 this(editor.getEditorSite()); 76 fEditor= editor; 77 setEnabled(SelectionConverter.canOperateOn(fEditor)); 78 } 79 80 83 public void selectionChanged(ITextSelection selection) { 84 } 85 86 89 public void selectionChanged(IStructuredSelection selection) { 90 setEnabled(getResource(selection) != null); 91 } 92 93 96 public void run(ITextSelection selection) { 97 IJavaElement input= SelectionConverter.getInput(fEditor); 98 if (!ActionUtil.isProcessable(getShell(), input)) 99 return; 100 101 102 try { 103 IJavaElement[] elements= SelectionConverter.codeResolveOrInputForked(fEditor); 104 if (elements == null || elements.length == 0) 105 return; 106 107 IJavaElement candidate= elements[0]; 108 if (elements.length > 1) { 109 candidate= SelectionConverter.selectJavaElement(elements, getShell(), getDialogTitle(), ActionMessages.ShowInNavigatorView_dialog_message); 110 } 111 if (candidate != null) { 112 run(getResource(candidate)); 113 } 114 } catch (InvocationTargetException e) { 115 ExceptionHandler.handle(e, getDialogTitle(), ActionMessages.SelectionConverter_codeResolve_failed); 116 } catch (InterruptedException e) { 117 } 119 } 120 121 124 public void run(IStructuredSelection selection) { 125 run(getResource(selection)); 126 } 127 128 132 public void run(IResource resource) { 133 if (resource == null) 134 return; 135 try { 136 IWorkbenchPage page= getSite().getWorkbenchWindow().getActivePage(); 137 IViewPart view= page.showView(IPageLayout.ID_RES_NAV); 138 if (view instanceof ISetSelectionTarget) { 139 ISelection selection= new StructuredSelection(resource); 140 ((ISetSelectionTarget)view).selectReveal(selection); 141 } 142 } catch(PartInitException e) { 143 ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.ShowInNavigatorView_error_activation_failed); 144 } 145 } 146 147 private IResource getResource(IStructuredSelection selection) { 148 if (selection.size() != 1) 149 return null; 150 Object element= selection.getFirstElement(); 151 if (element instanceof IResource) 152 return (IResource)element; 153 if (element instanceof IJavaElement) 154 return getResource((IJavaElement)element); 155 return null; 156 } 157 158 private IResource getResource(IJavaElement element) { 159 if (element == null) 160 return null; 161 162 element= (IJavaElement) element.getOpenable(); 163 if (element instanceof ICompilationUnit) { 164 element= ((ICompilationUnit) element).getPrimary(); 165 } 166 return element.getResource(); 167 } 168 169 private static String getDialogTitle() { 170 return ActionMessages.ShowInNavigatorView_dialog_title; 171 } 172 } 173 | Popular Tags |