KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22
23 import org.eclipse.swt.widgets.Shell;
24
25 import org.eclipse.jface.dialogs.ErrorDialog;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.jface.util.OpenStrategy;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.StructuredSelection;
32
33 import org.eclipse.ui.IEditorPart;
34 import org.eclipse.ui.IWorkbenchPage;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.PartInitException;
37 import org.eclipse.ui.texteditor.ITextEditor;
38
39 import org.eclipse.jdt.core.IJavaElement;
40 import org.eclipse.jdt.core.IMember;
41 import org.eclipse.jdt.core.IMethod;
42 import org.eclipse.jdt.core.ISourceRange;
43 import org.eclipse.jdt.core.JavaModelException;
44
45 import org.eclipse.jdt.ui.JavaUI;
46
47 import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
48 import org.eclipse.jdt.internal.ui.JavaPlugin;
49 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
50 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
51 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
52 import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchy;
53 import org.eclipse.jdt.internal.corext.callhierarchy.CallLocation;
54 import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;
55 import org.eclipse.jdt.internal.corext.util.Messages;
56
57 public class CallHierarchyUI {
58     private static final int DEFAULT_MAX_CALL_DEPTH= 10;
59     private static final String JavaDoc PREF_MAX_CALL_DEPTH = "PREF_MAX_CALL_DEPTH"; //$NON-NLS-1$
60

61     private static CallHierarchyUI fgInstance;
62
63     private CallHierarchyUI() {
64         // Do nothing
65
}
66
67     public static CallHierarchyUI getDefault() {
68         if (fgInstance == null) {
69             fgInstance = new CallHierarchyUI();
70         }
71
72         return fgInstance;
73     }
74
75     /**
76      * Returns the maximum tree level allowed
77      * @return int
78      */

79     public int getMaxCallDepth() {
80         int maxCallDepth;
81         
82         IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore();
83         maxCallDepth = settings.getInt(PREF_MAX_CALL_DEPTH);
84         if (maxCallDepth < 1 || maxCallDepth > 99) {
85             maxCallDepth= DEFAULT_MAX_CALL_DEPTH;
86         }
87
88         return maxCallDepth;
89     }
90
91     public void setMaxCallDepth(int maxCallDepth) {
92         IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore();
93         settings.setValue(PREF_MAX_CALL_DEPTH, maxCallDepth);
94     }
95     
96     public static void jumpToMember(IJavaElement element) {
97         if (element != null) {
98             try {
99                 JavaUI.openInEditor(element, true, true);
100             } catch (JavaModelException e) {
101                 JavaPlugin.log(e);
102             } catch (PartInitException e) {
103                 JavaPlugin.log(e);
104             }
105         }
106     }
107
108     public static void jumpToLocation(CallLocation callLocation) {
109         try {
110             IEditorPart methodEditor = JavaUI.openInEditor(callLocation.getMember(), false, false);
111             if (methodEditor instanceof ITextEditor) {
112                 ITextEditor editor = (ITextEditor) methodEditor;
113                 editor.selectAndReveal(callLocation.getStart(),
114                     (callLocation.getEnd() - callLocation.getStart()));
115             }
116         } catch (JavaModelException e) {
117             JavaPlugin.log(e);
118         } catch (PartInitException e) {
119             JavaPlugin.log(e);
120         }
121     }
122
123     /**
124      * @return <code>true</code> iff no error occurred while trying to
125      * open the editor, <code>false</code> iff an error dialog was raised.
126      */

127     public static boolean openInEditor(Object JavaDoc element, Shell shell, String JavaDoc title) {
128         CallLocation callLocation= CallHierarchy.getCallLocation(element);
129         
130         try {
131             IMember enclosingMember;
132             int selectionStart;
133             int selectionLength;
134             
135             if (callLocation != null) {
136                 enclosingMember= callLocation.getMember();
137                 selectionStart= callLocation.getStart();
138                 selectionLength= callLocation.getEnd() - selectionStart;
139             } else if (element instanceof MethodWrapper) {
140                 enclosingMember= ((MethodWrapper) element).getMember();
141                 ISourceRange selectionRange= enclosingMember.getNameRange();
142                 if (selectionRange == null)
143                     selectionRange= enclosingMember.getSourceRange();
144                 if (selectionRange == null)
145                     return true;
146                 selectionStart= selectionRange.getOffset();
147                 selectionLength= selectionRange.getLength();
148             } else {
149                 return true;
150             }
151     
152             boolean activateOnOpen = OpenStrategy.activateOnOpen();
153
154             IEditorPart methodEditor = JavaUI.openInEditor(enclosingMember, activateOnOpen, false);
155             if (methodEditor instanceof ITextEditor) {
156                 ITextEditor editor = (ITextEditor) methodEditor;
157                 editor.selectAndReveal(selectionStart, selectionLength);
158             }
159             return true;
160         } catch (JavaModelException e) {
161             JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(),
162                     IJavaStatusConstants.INTERNAL_ERROR,
163                     CallHierarchyMessages.CallHierarchyUI_open_in_editor_error_message, e));
164
165             ErrorDialog.openError(shell, title,
166                 CallHierarchyMessages.CallHierarchyUI_open_in_editor_error_message,
167                 e.getStatus());
168             return false;
169         } catch (PartInitException x) {
170             String JavaDoc name;
171             if (callLocation != null)
172                 name= callLocation.getCalledMember().getElementName();
173             else if (element instanceof MethodWrapper)
174                 name= ((MethodWrapper) element).getName();
175             else
176                 name= ""; //$NON-NLS-1$
177
MessageDialog.openError(shell, title,
178                 Messages.format(
179                     CallHierarchyMessages.CallHierarchyUI_open_in_editor_error_messageArgs,
180                     new String JavaDoc[] { name, x.getMessage() }));
181             return false;
182         }
183     }
184
185     public static IEditorPart isOpenInEditor(Object JavaDoc elem) {
186         IJavaElement javaElement= null;
187         if (elem instanceof MethodWrapper) {
188             javaElement= ((MethodWrapper) elem).getMember();
189         } else if (elem instanceof CallLocation) {
190             javaElement= ((CallLocation) elem).getCalledMember();
191         }
192         if (javaElement != null) {
193             return EditorUtility.isOpenInEditor(javaElement);
194         }
195         return null;
196     }
197
198     /**
199      * Converts the input to a possible input candidates
200      */

201     public static IJavaElement[] getCandidates(Object JavaDoc input) {
202         if (!(input instanceof IJavaElement)) {
203             return null;
204         }
205         IJavaElement elem= (IJavaElement) input;
206         if (elem.getElementType() == IJavaElement.METHOD) {
207             return new IJavaElement[] { elem };
208         }
209         return null;
210     }
211     
212     public static CallHierarchyViewPart open(IJavaElement[] candidates, IWorkbenchWindow window) {
213         Assert.isTrue(candidates != null && candidates.length != 0);
214             
215         IJavaElement input= null;
216         if (candidates.length > 1) {
217             String JavaDoc title= CallHierarchyMessages.CallHierarchyUI_selectionDialog_title;
218             String JavaDoc message= CallHierarchyMessages.CallHierarchyUI_selectionDialog_message;
219             input= SelectionConverter.selectJavaElement(candidates, window.getShell(), title, message);
220         } else {
221             input= candidates[0];
222         }
223         if (input == null)
224             return null;
225             
226         return openInViewPart(window, input);
227     }
228     
229     private static CallHierarchyViewPart openInViewPart(IWorkbenchWindow window, IJavaElement input) {
230         IWorkbenchPage page= window.getActivePage();
231         try {
232             CallHierarchyViewPart result= (CallHierarchyViewPart)page.showView(CallHierarchyViewPart.ID_CALL_HIERARCHY);
233             result.setMethod((IMethod)input);
234             return result;
235         } catch (CoreException e) {
236             ExceptionHandler.handle(e, window.getShell(),
237                 CallHierarchyMessages.CallHierarchyUI_error_open_view, e.getMessage());
238         }
239         return null;
240     }
241     
242     /**
243      * Converts an ISelection (containing MethodWrapper instances) to an ISelection
244      * with the MethodWrapper's replaced by their corresponding IMembers. If the selection
245      * contains elements which are not MethodWrapper instances or not already IMember instances
246      * they are discarded.
247      * @param selection The selection to convert.
248      * @return An ISelection containing IMember's in place of MethodWrapper instances.
249      */

250     static ISelection convertSelection(ISelection selection) {
251         if (selection.isEmpty()) {
252             return selection;
253         }
254         
255         if (selection instanceof IStructuredSelection) {
256             IStructuredSelection structuredSelection= (IStructuredSelection) selection;
257             List JavaDoc javaElements= new ArrayList JavaDoc();
258             for (Iterator JavaDoc iter= structuredSelection.iterator(); iter.hasNext();) {
259                 Object JavaDoc element= iter.next();
260                 if (element instanceof MethodWrapper) {
261                     IMember member= ((MethodWrapper)element).getMember();
262                     if (member != null) {
263                         javaElements.add(member);
264                     }
265                 } else if (element instanceof IMember) {
266                     javaElements.add(element);
267                 } else if (element instanceof CallLocation) {
268                     IMember member = ((CallLocation) element).getMember();
269                     javaElements.add(member);
270                 }
271             }
272             return new StructuredSelection(javaElements);
273         }
274         return StructuredSelection.EMPTY;
275     }
276 }
277
Popular Tags