1 11 package org.eclipse.jdt.internal.ui.infoviews; 12 13 import org.eclipse.jface.text.ITextSelection; 14 15 import org.eclipse.ui.IEditorInput; 16 17 import org.eclipse.jdt.core.IClassFile; 18 import org.eclipse.jdt.core.ICodeAssist; 19 import org.eclipse.jdt.core.ICompilationUnit; 20 import org.eclipse.jdt.core.IJavaElement; 21 import org.eclipse.jdt.core.JavaModelException; 22 23 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 24 25 import org.eclipse.jdt.ui.IWorkingCopyManager; 26 27 import org.eclipse.jdt.internal.ui.JavaPlugin; 28 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; 29 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 30 31 36 class TextSelectionConverter { 37 38 39 private static final IJavaElement[] EMPTY_RESULT= new IJavaElement[0]; 40 41 42 private TextSelectionConverter() { 43 } 44 45 53 public static IJavaElement[] codeResolve(JavaEditor editor, ITextSelection selection) throws JavaModelException { 54 return codeResolve(getInput(editor), selection); 55 } 56 57 66 public static IJavaElement getElementAtOffset(JavaEditor editor, ITextSelection selection) throws JavaModelException { 67 return getElementAtOffset(getInput(editor), selection); 68 } 69 70 72 private static IJavaElement getInput(JavaEditor editor) { 73 if (editor == null) 74 return null; 75 IEditorInput input= editor.getEditorInput(); 76 if (input instanceof IClassFileEditorInput) 77 return ((IClassFileEditorInput)input).getClassFile(); 78 IWorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager(); 79 return manager.getWorkingCopy(input); 80 } 81 82 private static IJavaElement[] codeResolve(IJavaElement input, ITextSelection selection) throws JavaModelException { 83 if (input instanceof ICodeAssist) { 84 if (input instanceof ICompilationUnit) { 85 ICompilationUnit cunit= (ICompilationUnit)input; 86 if (cunit.isWorkingCopy()) 87 JavaModelUtil.reconcile(cunit); 88 } 89 IJavaElement[] elements= ((ICodeAssist)input).codeSelect(selection.getOffset(), selection.getLength()); 90 if (elements != null && elements.length > 0) 91 return elements; 92 } 93 return EMPTY_RESULT; 94 } 95 96 private static IJavaElement getElementAtOffset(IJavaElement input, ITextSelection selection) throws JavaModelException { 97 if (input instanceof ICompilationUnit) { 98 ICompilationUnit cunit= (ICompilationUnit)input; 99 if (cunit.isWorkingCopy()) 100 JavaModelUtil.reconcile(cunit); 101 IJavaElement ref= cunit.getElementAt(selection.getOffset()); 102 if (ref == null) 103 return input; 104 else 105 return ref; 106 } else if (input instanceof IClassFile) { 107 IJavaElement ref= ((IClassFile)input).getElementAt(selection.getOffset()); 108 if (ref == null) 109 return input; 110 else 111 return ref; 112 } 113 return null; 114 } 115 } 116 | Popular Tags |