blob: bd1a2c72d5671f11585872259d03114647f85d9c [file] [log] [blame]
bchilds73b88932007-06-11 20:45:50 +00001/**
2 *
3 */
4package org.eclipse.wst.jsdt.web.ui.actions;
5
bchilds73b88932007-06-11 20:45:50 +00006import org.eclipse.core.resources.IProject;
7import org.eclipse.core.resources.IResource;
8import org.eclipse.core.runtime.IPath;
9import org.eclipse.core.runtime.Path;
10import org.eclipse.jface.action.IAction;
11import org.eclipse.jface.viewers.ISelection;
12import org.eclipse.jface.viewers.StructuredSelection;
13import org.eclipse.ui.IPageLayout;
14import org.eclipse.ui.IViewPart;
15import org.eclipse.ui.IWorkbenchPage;
16import org.eclipse.ui.PartInitException;
17import org.eclipse.ui.part.ISetSelectionTarget;
18import org.eclipse.wst.jsdt.core.IJavaElement;
bchilds73b88932007-06-11 20:45:50 +000019import org.eclipse.wst.jsdt.internal.ui.util.ExceptionHandler;
20
21/**
22 * @author childsb
bchildsa1181702007-06-14 21:47:04 +000023 *
bchilds73b88932007-06-11 20:45:50 +000024 */
25public class ShowInNavigatorAction extends JsElementActionProxy {
bchildsa1181702007-06-14 21:47:04 +000026 /*
27 * (non-Javadoc)
28 *
bchilds73b88932007-06-11 20:45:50 +000029 * @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;
bchilds73b88932007-06-11 20:45:50 +000036 }
37
bchildsc25d89a2007-06-20 17:12:18 +000038
bchilds73b88932007-06-11 20:45:50 +000039 public void run(IAction action) {
bchildsa1181702007-06-14 21:47:04 +000040 IJavaElement elements[] = JsElementActionProxy.getJsElementsFromSelection(getCurrentSelection());
41 if (elements == null || elements.length == 0) {
42 return;
43 }
bchilds73b88932007-06-11 20:45:50 +000044 IResource resource = null;
bchildsa1181702007-06-14 21:47:04 +000045 if (elements[0].isVirtual()) {
bchilds73b88932007-06-11 20:45:50 +000046 resource = getHostResource(elements[0]);
bchildsa1181702007-06-14 21:47:04 +000047 } else {
bchilds73b88932007-06-11 20:45:50 +000048 resource = elements[0].getResource();
49 }
bchildsa1181702007-06-14 21:47:04 +000050 if (resource == null) {
bchilds73b88932007-06-11 20:45:50 +000051 return;
bchildsa1181702007-06-14 21:47:04 +000052 }
bchilds73b88932007-06-11 20:45:50 +000053 try {
bchildsa1181702007-06-14 21:47:04 +000054 IWorkbenchPage page = targetWorkbenchPart.getSite().getPage();
55 IViewPart view = page.showView(IPageLayout.ID_RES_NAV);
bchilds73b88932007-06-11 20:45:50 +000056 if (view instanceof ISetSelectionTarget) {
bchildsa1181702007-06-14 21:47:04 +000057 ISelection selection = new StructuredSelection(resource);
58 ((ISetSelectionTarget) view).selectReveal(selection);
bchilds73b88932007-06-11 20:45:50 +000059 }
bchildsa1181702007-06-14 21:47:04 +000060 } catch (PartInitException e) {
bchilds3e120982007-11-06 21:49:40 +000061 ExceptionHandler.handle(e, targetWorkbenchPart.getSite().getShell(), Messages.getString("ShowInNavigatorAction.0"), Messages.getString("ShowInNavigatorAction.1") + e); //$NON-NLS-1$ //$NON-NLS-2$
bchildsa1181702007-06-14 21:47:04 +000062 }
63 }
64
bchildsc25d89a2007-06-20 17:12:18 +000065
bchildsa1181702007-06-14 21:47:04 +000066 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 }
bchilds73b88932007-06-11 20:45:50 +000076 }
77 }
78}