1 11 package org.eclipse.jdt.internal.corext.fix; 12 13 import org.eclipse.text.edits.MultiTextEdit; 14 import org.eclipse.text.edits.TextEdit; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IStatus; 18 19 import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup; 20 import org.eclipse.ltk.core.refactoring.GroupCategory; 21 import org.eclipse.ltk.core.refactoring.GroupCategorySet; 22 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 23 import org.eclipse.ltk.core.refactoring.TextChange; 24 25 import org.eclipse.jdt.core.ICompilationUnit; 26 import org.eclipse.jdt.core.ISourceRange; 27 import org.eclipse.jdt.core.dom.CompilationUnit; 28 import org.eclipse.jdt.core.search.TypeNameMatch; 29 30 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; 31 import org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation; 32 import org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery; 33 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange; 34 import org.eclipse.jdt.internal.corext.util.Messages; 35 36 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 37 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 38 39 public class ImportsFix extends AbstractFix { 40 41 private static final class AmbiguousImportException extends RuntimeException { 42 private static final long serialVersionUID= 1L; 43 } 44 45 public static IFix createCleanUp(final CompilationUnit cu, CodeGenerationSettings settings, boolean organizeImports, RefactoringStatus status) throws CoreException { 46 if (!organizeImports) 47 return null; 48 49 IChooseImportQuery query= new IChooseImportQuery() { 50 public TypeNameMatch[] chooseImports(TypeNameMatch[][] openChoices, ISourceRange[] ranges) { 51 throw new AmbiguousImportException(); 52 } 53 }; 54 OrganizeImportsOperation op= new OrganizeImportsOperation((ICompilationUnit)cu.getJavaElement(), cu, settings.importIgnoreLowercase, false, false, query); 55 final TextEdit edit; 56 try { 57 edit= op.createTextEdit(null); 58 } catch (AmbiguousImportException e) { 59 status.addInfo(Messages.format(ActionMessages.OrganizeImportsAction_multi_error_unresolvable, getLocationString(cu))); 60 return null; 61 } 62 63 if (op.getParseError() != null) { 64 status.addInfo(Messages.format(ActionMessages.OrganizeImportsAction_multi_error_parse, getLocationString(cu))); 65 return null; 66 } 67 68 if (edit == null || (edit instanceof MultiTextEdit && edit.getChildrenSize() == 0)) 69 return null; 70 71 return new IFix() { 72 73 public TextChange createChange() throws CoreException { 74 CompilationUnitChange result= new CompilationUnitChange(getDescription(), getCompilationUnit()); 75 result.setEdit(edit); 76 String label= getDescription(); 77 result.addTextEditGroup(new CategorizedTextEditGroup(label, new GroupCategorySet(new GroupCategory(label, label, label)))); 78 return result; 79 } 80 81 public ICompilationUnit getCompilationUnit() { 82 return (ICompilationUnit)cu.getJavaElement(); 83 } 84 85 public String getDescription() { 86 return FixMessages.ImportsFix_OrganizeImports_Description; 87 } 88 89 public IStatus getStatus() { 90 return StatusInfo.OK_STATUS; 91 } 92 }; 93 } 94 95 private static String getLocationString(final CompilationUnit cu) { 96 return cu.getJavaElement().getPath().makeRelative().toString(); 97 } 98 99 protected ImportsFix(String name, CompilationUnit compilationUnit, IFixRewriteOperation[] fixRewriteOperations) { 100 super(name, compilationUnit, fixRewriteOperations); 101 } 102 } 103 | Popular Tags |