KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > handlers > ShowResourceByPathHandler


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.ide.handlers;
13
14 import org.eclipse.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.ui.IPageLayout;
21 import org.eclipse.ui.IViewPart;
22 import org.eclipse.ui.IWorkbenchPage;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.handlers.HandlerUtil;
26 import org.eclipse.ui.part.ISetSelectionTarget;
27
28 /**
29  * A command handler to show a resource in the Navigator view given the resource
30  * path.
31  *
32  * @since 3.2
33  */

34 public class ShowResourceByPathHandler extends AbstractHandler {
35
36     private static final String JavaDoc PARAM_ID_RESOURCE_PATH = "resourcePath"; //$NON-NLS-1$
37

38     public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
39
40         IResource resource = (IResource) event
41                 .getObjectParameterForExecution(PARAM_ID_RESOURCE_PATH);
42
43         IWorkbenchWindow activeWindow = HandlerUtil
44                 .getActiveWorkbenchWindowChecked(event);
45
46         IWorkbenchPage activePage = activeWindow.getActivePage();
47         if (activePage == null) {
48             throw new ExecutionException("no active workbench page"); //$NON-NLS-1$
49
}
50
51         try {
52             IViewPart view = activePage.showView(IPageLayout.ID_RES_NAV);
53             if (view instanceof ISetSelectionTarget) {
54                 ISelection selection = new StructuredSelection(resource);
55                 ((ISetSelectionTarget) view).selectReveal(selection);
56             }
57         } catch (PartInitException e) {
58             throw new ExecutionException("error showing resource in navigator"); //$NON-NLS-1$
59
}
60
61         return null;
62     }
63
64 }
65
Popular Tags