KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > ShowInNavigatorViewAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.jdt.ui.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
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 /**
42  * Reveals the selected element in the resource navigator view.
43  * <p>
44  * Action is applicable to structured selections containing Java element
45  * or resources.
46  *
47  * <p>
48  * This class may be instantiated; it is not intended to be subclassed.
49  * </p>
50  *
51  * @since 2.0
52  */

53 public class ShowInNavigatorViewAction extends SelectionDispatchAction {
54
55     private JavaEditor fEditor;
56     
57     /**
58      * Creates a new <code>ShowInNavigatorViewAction</code>. The action requires
59      * that the selection provided by the site's selection provider is of type
60      * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
61      *
62      * @param site the site providing context information for this action
63      */

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     /**
71      * Note: This constructor is for internal use only. Clients should not call this constructor.
72      * @param editor the Java editor
73      */

74     public ShowInNavigatorViewAction(JavaEditor editor) {
75         this(editor.getEditorSite());
76         fEditor= editor;
77         setEnabled(SelectionConverter.canOperateOn(fEditor));
78     }
79     
80     /* (non-Javadoc)
81      * Method declared on SelectionDispatchAction.
82      */

83     public void selectionChanged(ITextSelection selection) {
84     }
85
86     /* (non-Javadoc)
87      * Method declared on SelectionDispatchAction.
88      */

89     public void selectionChanged(IStructuredSelection selection) {
90         setEnabled(getResource(selection) != null);
91     }
92
93     /* (non-Javadoc)
94      * Method declared on SelectionDispatchAction.
95      */

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 JavaDoc e) {
115             ExceptionHandler.handle(e, getDialogTitle(), ActionMessages.SelectionConverter_codeResolve_failed);
116         } catch (InterruptedException JavaDoc e) {
117             // cancelled
118
}
119     }
120     
121     /* (non-Javadoc)
122      * Method declared on SelectionDispatchAction.
123      */

124     public void run(IStructuredSelection selection) {
125         run(getResource(selection));
126     }
127     
128     /*
129      * No Javadoc. The method should be internal but can't be changed since
130      * we shipped it with a public visibility
131      */

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 JavaDoc 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 JavaDoc getDialogTitle() {
170         return ActionMessages.ShowInNavigatorView_dialog_title;
171     }
172 }
173
Popular Tags