1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import java.util.Iterator; 14 15 import org.eclipse.jdt.core.IClassFile; 16 import org.eclipse.jdt.core.ICodeAssist; 17 import org.eclipse.jdt.core.ICompilationUnit; 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.ISourceRange; 20 import org.eclipse.jdt.core.ISourceReference; 21 import org.eclipse.jdt.core.IType; 22 import org.eclipse.jdt.core.JavaModelException; 23 24 import org.eclipse.swt.widgets.Shell; 25 26 import org.eclipse.jface.text.ITextSelection; 27 import org.eclipse.jface.viewers.ISelection; 28 import org.eclipse.jface.viewers.ISelectionProvider; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.viewers.StructuredSelection; 31 32 import org.eclipse.ui.IEditorInput; 33 import org.eclipse.ui.IWorkbenchPart; 34 35 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 36 import org.eclipse.jdt.internal.ui.JavaPlugin; 37 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; 38 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 39 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 40 41 import org.eclipse.jdt.ui.IWorkingCopyManager; 42 43 public class SelectionConverter { 44 45 private static final IJavaElement[] EMPTY_RESULT= new IJavaElement[0]; 46 47 private SelectionConverter() { 48 } 50 51 62 public static IStructuredSelection getStructuredSelection(IWorkbenchPart part) throws JavaModelException { 63 if (part instanceof JavaEditor) 64 return new StructuredSelection(codeResolve((JavaEditor)part)); 65 ISelectionProvider provider= part.getSite().getSelectionProvider(); 66 if (provider != null) { 67 ISelection selection= provider.getSelection(); 68 if (selection instanceof IStructuredSelection) 69 return (IStructuredSelection)selection; 70 } 71 return StructuredSelection.EMPTY; 72 } 73 74 75 80 public static IJavaElement[] getElements(IStructuredSelection selection) { 81 if (!selection.isEmpty()) { 82 IJavaElement[] result= new IJavaElement[selection.size()]; 83 int i= 0; 84 for (Iterator iter= selection.iterator(); iter.hasNext(); i++) { 85 Object element= iter.next(); 86 if (!(element instanceof IJavaElement)) 87 return EMPTY_RESULT; 88 result[i]= (IJavaElement)element; 89 } 90 return result; 91 } 92 return EMPTY_RESULT; 93 } 94 95 public static boolean canOperateOn(JavaEditor editor) { 96 if (editor == null) 97 return false; 98 return getInput(editor) != null; 99 100 } 101 102 107 public static IJavaElement[] codeResolveOrInput(JavaEditor editor) throws JavaModelException { 108 IJavaElement input= getInput(editor); 109 ITextSelection selection= (ITextSelection)editor.getSelectionProvider().getSelection(); 110 IJavaElement[] result= codeResolve(input, selection); 111 if (result.length == 0) { 112 result= new IJavaElement[] {input}; 113 } 114 return result; 115 } 116 117 public static IJavaElement[] codeResolveOrInputHandled(JavaEditor editor, Shell shell, String title) { 118 try { 119 return codeResolveOrInput(editor); 120 } catch(JavaModelException e) { 121 ExceptionHandler.handle(e, shell, title, ActionMessages.SelectionConverter_codeResolve_failed); 122 } 123 return null; 124 } 125 126 132 public static IJavaElement codeResolveOrInput(JavaEditor editor, Shell shell, String title, String message) throws JavaModelException { 133 IJavaElement[] elements= codeResolveOrInput(editor); 134 if (elements == null || elements.length == 0) 135 return null; 136 IJavaElement candidate= elements[0]; 137 if (elements.length > 1) { 138 candidate= OpenActionUtil.selectJavaElement(elements, shell, title, message); 139 } 140 return candidate; 141 } 142 143 public static IJavaElement codeResolveOrInputHandled(JavaEditor editor, Shell shell, String title, String message) { 144 try { 145 return codeResolveOrInput(editor, shell, title, message); 146 } catch (JavaModelException e) { 147 ExceptionHandler.handle(e, shell, title, ActionMessages.SelectionConverter_codeResolveOrInput_failed); 148 } 149 return null; 150 } 151 152 public static IJavaElement[] codeResolve(JavaEditor editor) throws JavaModelException { 153 return codeResolve(getInput(editor), (ITextSelection)editor.getSelectionProvider().getSelection()); 154 } 155 156 161 public static IJavaElement codeResolve(JavaEditor editor, Shell shell, String title, String message) throws JavaModelException { 162 IJavaElement[] elements= codeResolve(editor); 163 if (elements == null || elements.length == 0) 164 return null; 165 IJavaElement candidate= elements[0]; 166 if (elements.length > 1) { 167 candidate= OpenActionUtil.selectJavaElement(elements, shell, title, message); 168 } 169 return candidate; 170 } 171 172 public static IJavaElement[] codeResolveHandled(JavaEditor editor, Shell shell, String title) { 173 try { 174 return codeResolve(editor); 175 } catch (JavaModelException e) { 176 ExceptionHandler.handle(e, shell, title, ActionMessages.SelectionConverter_codeResolve_failed); 177 } 178 return null; 179 } 180 181 public static IJavaElement getElementAtOffset(JavaEditor editor) throws JavaModelException { 182 return getElementAtOffset(getInput(editor), (ITextSelection)editor.getSelectionProvider().getSelection()); 183 } 184 185 public static IType getTypeAtOffset(JavaEditor editor) throws JavaModelException { 186 IJavaElement element= SelectionConverter.getElementAtOffset(editor); 187 IType type= (IType)element.getAncestor(IJavaElement.TYPE); 188 if (type == null) { 189 ICompilationUnit unit= SelectionConverter.getInputAsCompilationUnit(editor); 190 if (unit != null) 191 type= unit.findPrimaryType(); 192 } 193 return type; 194 } 195 196 public static IJavaElement getInput(JavaEditor editor) { 197 if (editor == null) 198 return null; 199 IEditorInput input= editor.getEditorInput(); 200 if (input instanceof IClassFileEditorInput) 201 return ((IClassFileEditorInput)input).getClassFile(); 202 IWorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager(); 203 return manager.getWorkingCopy(input); 204 } 205 206 public static ICompilationUnit getInputAsCompilationUnit(JavaEditor editor) { 207 Object editorInput= SelectionConverter.getInput(editor); 208 if (editorInput instanceof ICompilationUnit) 209 return (ICompilationUnit)editorInput; 210 else 211 return null; 212 } 213 214 public static IJavaElement[] codeResolve(IJavaElement input, ITextSelection selection) throws JavaModelException { 215 if (input instanceof ICodeAssist) { 216 if (input instanceof ICompilationUnit) { 217 JavaModelUtil.reconcile((ICompilationUnit) input); 218 } 219 IJavaElement[] elements= ((ICodeAssist)input).codeSelect(selection.getOffset(), selection.getLength()); 220 if (elements != null && elements.length > 0) 221 return elements; 222 } 223 return EMPTY_RESULT; 224 } 225 226 public static IJavaElement getElementAtOffset(IJavaElement input, ITextSelection selection) throws JavaModelException { 227 if (input instanceof ICompilationUnit) { 228 ICompilationUnit cunit= (ICompilationUnit) input; 229 JavaModelUtil.reconcile(cunit); 230 IJavaElement ref= cunit.getElementAt(selection.getOffset()); 231 if (ref == null) 232 return input; 233 else 234 return ref; 235 } else if (input instanceof IClassFile) { 236 IJavaElement ref= ((IClassFile)input).getElementAt(selection.getOffset()); 237 if (ref == null) 238 return input; 239 else 240 return ref; 241 } 242 return null; 243 } 244 245 256 public static IJavaElement resolveEnclosingElement(JavaEditor editor, ITextSelection selection) throws JavaModelException { 257 return resolveEnclosingElement(getInput(editor), selection); 258 } 259 260 public static IJavaElement resolveEnclosingElement(IJavaElement input, ITextSelection selection) throws JavaModelException { 261 IJavaElement atOffset= null; 262 if (input instanceof ICompilationUnit) { 263 ICompilationUnit cunit= (ICompilationUnit)input; 264 JavaModelUtil.reconcile(cunit); 265 atOffset= cunit.getElementAt(selection.getOffset()); 266 } else if (input instanceof IClassFile) { 267 IClassFile cfile= (IClassFile)input; 268 atOffset= cfile.getElementAt(selection.getOffset()); 269 } else { 270 return null; 271 } 272 if (atOffset == null) { 273 return input; 274 } else { 275 int selectionEnd= selection.getOffset() + selection.getLength(); 276 IJavaElement result= atOffset; 277 if (atOffset instanceof ISourceReference) { 278 ISourceRange range= ((ISourceReference)atOffset).getSourceRange(); 279 while (range.getOffset() + range.getLength() < selectionEnd) { 280 result= result.getParent(); 281 if (! (result instanceof ISourceReference)) { 282 result= input; 283 break; 284 } 285 range= ((ISourceReference)result).getSourceRange(); 286 } 287 } 288 return result; 289 } 290 } 291 } 292 | Popular Tags |