bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package org.eclipse.wst.jsdt.web.ui.actions; |
| 5 | |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 6 | import org.eclipse.core.resources.IProject; |
| 7 | import org.eclipse.core.resources.IResource; |
| 8 | import org.eclipse.core.runtime.IPath; |
| 9 | import org.eclipse.core.runtime.Path; |
| 10 | import org.eclipse.jface.action.IAction; |
| 11 | import org.eclipse.jface.viewers.ISelection; |
| 12 | import org.eclipse.jface.viewers.StructuredSelection; |
| 13 | import org.eclipse.ui.IPageLayout; |
| 14 | import org.eclipse.ui.IViewPart; |
| 15 | import org.eclipse.ui.IWorkbenchPage; |
| 16 | import org.eclipse.ui.PartInitException; |
| 17 | import org.eclipse.ui.part.ISetSelectionTarget; |
| 18 | import org.eclipse.wst.jsdt.core.IJavaElement; |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 19 | import org.eclipse.wst.jsdt.internal.ui.util.ExceptionHandler; |
| 20 | |
| 21 | /** |
| 22 | * @author childsb |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 23 | * |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 24 | */ |
| 25 | public class ShowInNavigatorAction extends JsElementActionProxy { |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 26 | /* |
| 27 | * (non-Javadoc) |
| 28 | * |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 29 | * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) |
| 30 | */ |
| 31 | private IResource getHostResource(IJavaElement virtualElement) { |
| 32 | IProject project = virtualElement.getJavaProject().getProject(); |
| 33 | IPath path = new Path(virtualElement.getHostPath().getPath()); |
| 34 | IResource host = project.getWorkspace().getRoot().findMember(path); |
| 35 | return host; |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 36 | } |
| 37 | |
bchilds | c25d89a | 2007-06-20 17:12:18 +0000 | [diff] [blame] | 38 | |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 39 | public void run(IAction action) { |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 40 | IJavaElement elements[] = JsElementActionProxy.getJsElementsFromSelection(getCurrentSelection()); |
| 41 | if (elements == null || elements.length == 0) { |
| 42 | return; |
| 43 | } |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 44 | IResource resource = null; |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 45 | if (elements[0].isVirtual()) { |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 46 | resource = getHostResource(elements[0]); |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 47 | } else { |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 48 | resource = elements[0].getResource(); |
| 49 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 50 | if (resource == null) { |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 51 | return; |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 52 | } |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 53 | try { |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 54 | IWorkbenchPage page = targetWorkbenchPart.getSite().getPage(); |
| 55 | IViewPart view = page.showView(IPageLayout.ID_RES_NAV); |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 56 | if (view instanceof ISetSelectionTarget) { |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 57 | ISelection selection = new StructuredSelection(resource); |
| 58 | ((ISetSelectionTarget) view).selectReveal(selection); |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 59 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 60 | } catch (PartInitException e) { |
bchilds | 3e12098 | 2007-11-06 21:49:40 +0000 | [diff] [blame] | 61 | ExceptionHandler.handle(e, targetWorkbenchPart.getSite().getShell(), Messages.getString("ShowInNavigatorAction.0"), Messages.getString("ShowInNavigatorAction.1") + e); //$NON-NLS-1$ //$NON-NLS-2$ |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
bchilds | c25d89a | 2007-06-20 17:12:18 +0000 | [diff] [blame] | 65 | |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 66 | public void selectionChanged(IAction action, ISelection selection) { |
| 67 | setSelection(selection); |
| 68 | IJavaElement elements[] = JsElementActionProxy.getJsElementsFromSelection(getCurrentSelection()); |
| 69 | for (int i = 0; i < elements.length; i++) { |
| 70 | if (elements[i].isVirtual()) { |
| 71 | IResource resource = getHostResource(elements[i]); |
| 72 | if (resource == null || !resource.exists()) { |
| 73 | action.setEnabled(false); |
| 74 | } |
| 75 | } |
bchilds | 73b8893 | 2007-06-11 20:45:50 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |