1 11 package org.eclipse.ui.views.navigator; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.resources.IMarker; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.IAdaptable; 21 import org.eclipse.jface.dialogs.ErrorDialog; 22 import org.eclipse.jface.viewers.ISelection; 23 import org.eclipse.jface.viewers.ISelectionProvider; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.jface.viewers.StructuredSelection; 26 import org.eclipse.ui.IPageLayout; 27 import org.eclipse.ui.IViewPart; 28 import org.eclipse.ui.IWorkbenchPage; 29 import org.eclipse.ui.PartInitException; 30 import org.eclipse.ui.actions.SelectionProviderAction; 31 import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages; 32 import org.eclipse.ui.part.ISetSelectionTarget; 33 34 40 public class ShowInNavigatorAction extends SelectionProviderAction { 41 private IWorkbenchPage page; 42 43 49 public ShowInNavigatorAction(IWorkbenchPage page, ISelectionProvider viewer) { 50 super(viewer, ResourceNavigatorMessages.ShowInNavigator_text); 51 Assert.isNotNull(page); 52 this.page = page; 53 setDescription(ResourceNavigatorMessages.ShowInNavigator_toolTip); 54 page.getWorkbenchWindow().getWorkbench().getHelpSystem().setHelp(this, 55 INavigatorHelpContextIds.SHOW_IN_NAVIGATOR_ACTION); 56 } 57 58 63 List getResources(IStructuredSelection selection) { 64 List v = new ArrayList (); 65 for (Iterator i = selection.iterator(); i.hasNext();) { 66 Object o = i.next(); 67 if (o instanceof IResource) { 68 v.add(o); 69 } else if (o instanceof IMarker) { 70 IResource resource = ((IMarker) o).getResource(); 71 v.add(resource); 72 } else if (o instanceof IAdaptable) { 73 IResource resource = (IResource) ((IAdaptable) o) 74 .getAdapter(IResource.class); 75 if (resource != null) { 76 v.add(resource); 77 } 78 } 79 } 80 return v; 81 } 82 83 87 91 public void run() { 92 List v = getResources(getStructuredSelection()); 93 if (v.isEmpty()) { 94 return; 95 } 96 try { 97 IViewPart view = page.showView(IPageLayout.ID_RES_NAV); 98 if (view instanceof ISetSelectionTarget) { 99 ISelection selection = new StructuredSelection(v); 100 ((ISetSelectionTarget) view).selectReveal(selection); 101 } 102 } catch (PartInitException e) { 103 ErrorDialog.openError(page.getWorkbenchWindow().getShell(), 104 ResourceNavigatorMessages.ShowInNavigator_errorMessage, 105 e.getMessage(), e.getStatus()); 106 } 107 } 108 109 113 public void selectionChanged(IStructuredSelection selection) { 114 setEnabled(!getResources(selection).isEmpty()); 115 } 116 } 117 | Popular Tags |