1 12 package org.eclipse.jdt.internal.ui.callhierarchy; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 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 PREF_MAX_CALL_DEPTH = "PREF_MAX_CALL_DEPTH"; 61 private static CallHierarchyUI fgInstance; 62 63 private CallHierarchyUI() { 64 } 66 67 public static CallHierarchyUI getDefault() { 68 if (fgInstance == null) { 69 fgInstance = new CallHierarchyUI(); 70 } 71 72 return fgInstance; 73 } 74 75 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 127 public static boolean openInEditor(Object element, Shell shell, String 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 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= ""; MessageDialog.openError(shell, title, 178 Messages.format( 179 CallHierarchyMessages.CallHierarchyUI_open_in_editor_error_messageArgs, 180 new String [] { name, x.getMessage() })); 181 return false; 182 } 183 } 184 185 public static IEditorPart isOpenInEditor(Object 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 201 public static IJavaElement[] getCandidates(Object 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 title= CallHierarchyMessages.CallHierarchyUI_selectionDialog_title; 218 String 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 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 javaElements= new ArrayList (); 258 for (Iterator iter= structuredSelection.iterator(); iter.hasNext();) { 259 Object 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 |