KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > callhierarchy > OpenCallHierarchyAction


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  * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
10  * (report 36180: Callers/Callees view)
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.callhierarchy;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21
22 import org.eclipse.jface.dialogs.ErrorDialog;
23 import org.eclipse.jface.text.ITextSelection;
24
25 import org.eclipse.jface.viewers.ISelectionProvider;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27
28 import org.eclipse.ui.IWorkbenchSite;
29 import org.eclipse.ui.PlatformUI;
30
31 import org.eclipse.jdt.core.IClassFile;
32 import org.eclipse.jdt.core.ICompilationUnit;
33 import org.eclipse.jdt.core.IJavaElement;
34 import org.eclipse.jdt.core.JavaModelException;
35
36 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
37
38 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
39 import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
42 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
43 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
44 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
45 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
46
47 /**
48  * This action opens a call hierarchy on the selected method.
49  * <p>
50  * The action is applicable to selections containing elements of type
51  * <code>IMethod</code>.
52  */

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

64     public OpenCallHierarchyAction(IWorkbenchSite site) {
65         super(site);
66         setText(CallHierarchyMessages.OpenCallHierarchyAction_label);
67         setToolTipText(CallHierarchyMessages.OpenCallHierarchyAction_tooltip);
68         setDescription(CallHierarchyMessages.OpenCallHierarchyAction_description);
69         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CALL_HIERARCHY_OPEN_ACTION);
70
71     }
72     
73     /**
74      * Creates a new <code>OpenCallHierarchyAction</code>. The action requires
75      * that the selection provided by the given selection provider is of type <code>
76      * org.eclipse.jface.viewers.IStructuredSelection</code>.
77      *
78      * @param site the site providing context information for this action
79      * @param provider a special selection provider which is used instead
80      * of the site's selection provider or <code>null</code> to use the site's
81      * selection provider
82      *
83      * @since 3.2
84      * @deprecated Use {@link #setSpecialSelectionProvider(ISelectionProvider)} instead. This API will be
85      * removed after 3.2 M5.
86      */

87     public OpenCallHierarchyAction(IWorkbenchSite site, ISelectionProvider provider) {
88         this(site);
89         setSpecialSelectionProvider(provider);
90     }
91     
92     /**
93      * Note: This constructor is for internal use only. Clients should not call this constructor.
94      */

95     public OpenCallHierarchyAction(JavaEditor editor) {
96         this(editor.getEditorSite());
97         fEditor= editor;
98         setEnabled(SelectionConverter.canOperateOn(fEditor));
99     }
100     
101     /* (non-Javadoc)
102      * Method declared on SelectionDispatchAction.
103      */

104     public void selectionChanged(ITextSelection selection) {
105         // Do nothing
106
}
107
108     /* (non-Javadoc)
109      * Method declared on SelectionDispatchAction.
110      */

111     public void selectionChanged(IStructuredSelection selection) {
112         setEnabled(isEnabled(selection));
113     }
114     
115     private boolean isEnabled(IStructuredSelection selection) {
116         if (selection.size() != 1)
117             return false;
118         Object JavaDoc input= selection.getFirstElement();
119         if (!(input instanceof IJavaElement))
120             return false;
121         switch (((IJavaElement)input).getElementType()) {
122             case IJavaElement.METHOD:
123                 return true;
124             default:
125                 return false;
126         }
127     }
128     
129     /* (non-Javadoc)
130      * Method declared on SelectionDispatchAction.
131      */

132     public void run(ITextSelection selection) {
133         IJavaElement input= SelectionConverter.getInput(fEditor);
134         if (!ActionUtil.isProcessable(getShell(), input))
135             return;
136         
137         try {
138             IJavaElement[] elements= SelectionConverter.codeResolveOrInputForked(fEditor);
139             if (elements == null)
140                 return;
141             List JavaDoc candidates= new ArrayList JavaDoc(elements.length);
142             for (int i= 0; i < elements.length; i++) {
143                 IJavaElement[] resolvedElements= CallHierarchyUI.getCandidates(elements[i]);
144                 if (resolvedElements != null)
145                     candidates.addAll(Arrays.asList(resolvedElements));
146             }
147             if (candidates.isEmpty()) {
148                 IJavaElement enclosingMethod= getEnclosingMethod(input, selection);
149                 if (enclosingMethod != null) {
150                     candidates.add(enclosingMethod);
151                 }
152             }
153             run((IJavaElement[])candidates.toArray(new IJavaElement[candidates.size()]));
154         } catch (InvocationTargetException JavaDoc e) {
155             ExceptionHandler.handle(e, getShell(), getErrorDialogTitle(), ActionMessages.SelectionConverter_codeResolve_failed);
156         } catch (InterruptedException JavaDoc e) {
157             // cancelled
158
}
159     }
160     
161     private IJavaElement getEnclosingMethod(IJavaElement input, ITextSelection selection) {
162         IJavaElement enclosingElement= null;
163         try {
164             switch (input.getElementType()) {
165                 case IJavaElement.CLASS_FILE :
166                     IClassFile classFile= (IClassFile) input.getAncestor(IJavaElement.CLASS_FILE);
167                     if (classFile != null) {
168                         enclosingElement= classFile.getElementAt(selection.getOffset());
169                     }
170                     break;
171                 case IJavaElement.COMPILATION_UNIT :
172                     ICompilationUnit cu= (ICompilationUnit) input.getAncestor(IJavaElement.COMPILATION_UNIT);
173                     if (cu != null) {
174                         enclosingElement= cu.getElementAt(selection.getOffset());
175                     }
176                     break;
177             }
178             if (enclosingElement != null && enclosingElement.getElementType() == IJavaElement.METHOD) {
179                 return enclosingElement;
180             }
181         } catch (JavaModelException e) {
182             JavaPlugin.log(e);
183         }
184
185         return null;
186     }
187
188     /* (non-Javadoc)
189      * Method declared on SelectionDispatchAction.
190      */

191     public void run(IStructuredSelection selection) {
192         if (selection.size() != 1)
193             return;
194         Object JavaDoc input= selection.getFirstElement();
195
196         if (!(input instanceof IJavaElement)) {
197             IStatus status= createStatus(CallHierarchyMessages.OpenCallHierarchyAction_messages_no_java_element);
198             openErrorDialog(status);
199             return;
200         }
201         IJavaElement element= (IJavaElement) input;
202         if (!ActionUtil.isProcessable(getShell(), element))
203             return;
204
205         List JavaDoc result= new ArrayList JavaDoc(1);
206         IStatus status= compileCandidates(result, element);
207         if (status.isOK()) {
208             run((IJavaElement[]) result.toArray(new IJavaElement[result.size()]));
209         } else {
210             openErrorDialog(status);
211         }
212     }
213     
214     private int openErrorDialog(IStatus status) {
215         String JavaDoc message= CallHierarchyMessages.OpenCallHierarchyAction_messages_title;
216         String JavaDoc dialogTitle= getErrorDialogTitle();
217         return ErrorDialog.openError(getShell(), dialogTitle, message, status);
218     }
219
220     private static String JavaDoc getErrorDialogTitle() {
221         return CallHierarchyMessages.OpenCallHierarchyAction_dialog_title;
222     }
223     
224     public void run(IJavaElement[] elements) {
225         if (elements.length == 0) {
226             getShell().getDisplay().beep();
227             return;
228         }
229         CallHierarchyUI.open(elements, getSite().getWorkbenchWindow());
230     }
231     
232     private static IStatus compileCandidates(List JavaDoc result, IJavaElement elem) {
233         IStatus ok= new Status(IStatus.OK, JavaPlugin.getPluginId(), 0, "", null); //$NON-NLS-1$
234
switch (elem.getElementType()) {
235             case IJavaElement.METHOD:
236                 result.add(elem);
237                 return ok;
238         }
239         return createStatus(CallHierarchyMessages.OpenCallHierarchyAction_messages_no_valid_java_element);
240     }
241     
242     private static IStatus createStatus(String JavaDoc message) {
243         return new Status(IStatus.INFO, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, null);
244     }
245 }
246
Popular Tags