1 11 package org.eclipse.jdt.internal.ui.refactoring.actions; 12 13 import org.eclipse.jdt.core.IClassFile; 14 import org.eclipse.jdt.core.ICompilationUnit; 15 import org.eclipse.jdt.core.IJavaElement; 16 import org.eclipse.jdt.core.IType; 17 import org.eclipse.jdt.core.JavaModelException; 18 19 import org.eclipse.jface.text.ITextSelection; 20 21 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 22 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 23 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 24 25 28 public class RefactoringActions { 29 30 39 public static IType getEnclosingOrPrimaryType(JavaTextSelection selection) throws JavaModelException { 40 final IJavaElement element= selection.resolveEnclosingElement(); 41 if (element != null) 42 return convertToEnclosingOrPrimaryType(element); 43 return null; 44 } 45 public static IType getEnclosingOrPrimaryType(JavaEditor editor) throws JavaModelException { 46 return convertToEnclosingOrPrimaryType(SelectionConverter.resolveEnclosingElement( 47 editor, (ITextSelection)editor.getSelectionProvider().getSelection())); 48 } 49 50 private static IType convertToEnclosingOrPrimaryType(IJavaElement element) throws JavaModelException { 51 if (element instanceof IType) 52 return (IType)element; 53 IType result= (IType)element.getAncestor(IJavaElement.TYPE); 54 if (result != null) 55 return result; 56 if (element instanceof ICompilationUnit) 57 return ((ICompilationUnit)element).findPrimaryType(); 58 if (element instanceof IClassFile) 59 return ((IClassFile)element).getType(); 60 return null; 61 } 62 63 70 public static IType getEnclosingType(JavaTextSelection selection) throws JavaModelException { 71 return convertToEnclosingType(selection.resolveEnclosingElement()); 72 } 73 public static IType getEnclosingType(JavaEditor editor) throws JavaModelException { 74 return convertToEnclosingType(SelectionConverter.resolveEnclosingElement( 75 editor, (ITextSelection)editor.getSelectionProvider().getSelection())); 76 } 77 78 private static IType convertToEnclosingType(IJavaElement element) { 79 if (element == null) 80 return null; 81 if (! (element instanceof IType)) 82 element= element.getAncestor(IJavaElement.TYPE); 83 return (IType)element; 84 } 85 } 86 | Popular Tags |