1 11 package org.eclipse.jdt.internal.corext.refactoring.base; 12 13 import org.eclipse.jdt.core.IClassFile; 14 import org.eclipse.jdt.core.ICompilationUnit; 15 import org.eclipse.jdt.core.IImportDeclaration; 16 import org.eclipse.jdt.core.IMember; 17 import org.eclipse.jdt.core.IMethod; 18 import org.eclipse.jdt.core.ISourceRange; 19 import org.eclipse.jdt.core.ITypeRoot; 20 import org.eclipse.jdt.core.JavaModelException; 21 import org.eclipse.jdt.core.dom.ASTNode; 22 import org.eclipse.jdt.core.dom.IMethodBinding; 23 24 import org.eclipse.jdt.internal.corext.SourceRange; 25 import org.eclipse.jdt.internal.corext.dom.Selection; 26 import org.eclipse.ltk.core.refactoring.RefactoringStatusContext; 27 28 32 public abstract class JavaStatusContext extends RefactoringStatusContext { 33 34 private static class MemberSourceContext extends JavaStatusContext { 35 private IMember fMember; 36 private MemberSourceContext(IMember member) { 37 fMember= member; 38 } 39 public boolean isBinary() { 40 return fMember.isBinary(); 41 } 42 public ICompilationUnit getCompilationUnit() { 43 return fMember.getCompilationUnit(); 44 } 45 public IClassFile getClassFile() { 46 return fMember.getClassFile(); 47 } 48 public ISourceRange getSourceRange() { 49 try { 50 return fMember.getSourceRange(); 51 } catch (JavaModelException e) { 52 return new SourceRange(0,0); 53 } 54 } 55 } 56 57 private static class ImportDeclarationSourceContext extends JavaStatusContext { 58 private IImportDeclaration fImportDeclartion; 59 private ImportDeclarationSourceContext(IImportDeclaration declaration) { 60 fImportDeclartion= declaration; 61 } 62 public boolean isBinary() { 63 return false; 64 } 65 public ICompilationUnit getCompilationUnit() { 66 return (ICompilationUnit)fImportDeclartion.getParent().getParent(); 67 } 68 public IClassFile getClassFile() { 69 return null; 70 } 71 public ISourceRange getSourceRange() { 72 try { 73 return fImportDeclartion.getSourceRange(); 74 } catch (JavaModelException e) { 75 return new SourceRange(0,0); 76 } 77 } 78 } 79 80 private static class CompilationUnitSourceContext extends JavaStatusContext { 81 private ICompilationUnit fCUnit; 82 private ISourceRange fSourceRange; 83 private CompilationUnitSourceContext(ICompilationUnit cunit, ISourceRange range) { 84 fCUnit= cunit; 85 fSourceRange= range; 86 if (fSourceRange == null) 87 fSourceRange= new SourceRange(0,0); 88 } 89 public boolean isBinary() { 90 return false; 91 } 92 public ICompilationUnit getCompilationUnit() { 93 return fCUnit; 94 } 95 public IClassFile getClassFile() { 96 return null; 97 } 98 public ISourceRange getSourceRange() { 99 return fSourceRange; 100 } 101 public String toString() { 102 return getSourceRange() + " in " + super.toString(); } 104 } 105 106 private static class ClassFileSourceContext extends JavaStatusContext { 107 private IClassFile fClassFile; 108 private ISourceRange fSourceRange; 109 private ClassFileSourceContext(IClassFile classFile, ISourceRange range) { 110 fClassFile= classFile; 111 fSourceRange= range; 112 if (fSourceRange == null) 113 fSourceRange= new SourceRange(0,0); 114 } 115 public boolean isBinary() { 116 return true; 117 } 118 public ICompilationUnit getCompilationUnit() { 119 return null; 120 } 121 public IClassFile getClassFile() { 122 return fClassFile; 123 } 124 public ISourceRange getSourceRange() { 125 return fSourceRange; 126 } 127 public String toString() { 128 return getSourceRange() + " in " + super.toString(); } 130 } 131 132 140 public static RefactoringStatusContext create(IMember member) { 141 if (member == null || !member.exists()) 142 return null; 143 return new MemberSourceContext(member); 144 } 145 146 154 public static RefactoringStatusContext create(IImportDeclaration declaration) { 155 if (declaration == null || !declaration.exists()) 156 return null; 157 return new ImportDeclarationSourceContext(declaration); 158 } 159 160 167 public static RefactoringStatusContext create(IMethodBinding method) { 168 return create((IMethod) method.getJavaElement()); 169 } 170 171 178 public static RefactoringStatusContext create(ITypeRoot typeRoot) { 179 return create(typeRoot, (ISourceRange)null); 180 } 181 182 191 public static RefactoringStatusContext create(ITypeRoot typeRoot, ISourceRange range) { 192 if (typeRoot instanceof ICompilationUnit) 193 return new CompilationUnitSourceContext((ICompilationUnit) typeRoot, range); 194 else if (typeRoot instanceof IClassFile) 195 return new ClassFileSourceContext((IClassFile) typeRoot, range); 196 else 197 return null; 198 } 199 200 209 public static RefactoringStatusContext create(ITypeRoot typeRoot, ASTNode node) { 210 ISourceRange range= null; 211 if (node != null) 212 range= new SourceRange(node.getStartPosition(), node.getLength()); 213 return create(typeRoot, range); 214 } 215 216 225 public static RefactoringStatusContext create(ITypeRoot typeRoot, Selection selection) { 226 ISourceRange range= null; 227 if (selection != null) 228 range= new SourceRange(selection.getOffset(), selection.getLength()); 229 return create(typeRoot, range); 230 } 231 232 238 public abstract boolean isBinary(); 239 240 246 public abstract ICompilationUnit getCompilationUnit(); 247 248 254 public abstract IClassFile getClassFile(); 255 256 261 public abstract ISourceRange getSourceRange(); 262 263 266 public Object getCorrespondingElement() { 267 if (isBinary()) 268 return getClassFile(); 269 else 270 return getCompilationUnit(); 271 } 272 } 273 274 | Popular Tags |