1 11 12 package org.eclipse.jdt.internal.ui.text.correction; 13 14 import org.eclipse.text.edits.DeleteEdit; 15 import org.eclipse.text.edits.InsertEdit; 16 import org.eclipse.text.edits.ReplaceEdit; 17 import org.eclipse.text.edits.TextEdit; 18 19 import org.eclipse.core.runtime.CoreException; 20 21 import org.eclipse.jface.text.IDocument; 22 23 import org.eclipse.jdt.core.ICompilationUnit; 24 import org.eclipse.jdt.core.IPackageDeclaration; 25 import org.eclipse.jdt.core.IPackageFragment; 26 import org.eclipse.jdt.core.ISourceRange; 27 import org.eclipse.jdt.core.JavaModelException; 28 29 import org.eclipse.jdt.ui.text.java.IProblemLocation; 30 31 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; 32 import org.eclipse.jdt.internal.corext.util.Messages; 33 34 import org.eclipse.jdt.internal.ui.JavaPlugin; 35 import org.eclipse.jdt.internal.ui.JavaPluginImages; 36 37 public class CorrectPackageDeclarationProposal extends CUCorrectionProposal { 38 39 private IProblemLocation fLocation; 40 41 public CorrectPackageDeclarationProposal(ICompilationUnit cu, IProblemLocation location, int relevance) { 42 super(CorrectionMessages.CorrectPackageDeclarationProposal_name, cu, relevance, 43 JavaPluginImages.get(JavaPluginImages.IMG_OBJS_PACKDECL)); 44 fLocation= location; 45 } 46 47 50 protected void addEdits(IDocument doc, TextEdit root) throws CoreException { 51 super.addEdits(doc, root); 52 53 ICompilationUnit cu= getCompilationUnit(); 54 55 IPackageFragment parentPack= (IPackageFragment) cu.getParent(); 56 IPackageDeclaration[] decls= cu.getPackageDeclarations(); 57 58 if (parentPack.isDefaultPackage() && decls.length > 0) { 59 for (int i= 0; i < decls.length; i++) { 60 ISourceRange range= decls[i].getSourceRange(); 61 root.addChild(new DeleteEdit(range.getOffset(), range.getLength())); 62 } 63 return; 64 } 65 if (!parentPack.isDefaultPackage() && decls.length == 0) { 66 String lineDelim= StubUtility.getLineDelimiterUsed(cu); 67 String str= "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim; root.addChild(new InsertEdit(0, str)); 69 return; 70 } 71 72 root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName())); 73 } 74 75 78 public String getDisplayString() { 79 ICompilationUnit cu= getCompilationUnit(); 80 IPackageFragment parentPack= (IPackageFragment) cu.getParent(); 81 try { 82 IPackageDeclaration[] decls= cu.getPackageDeclarations(); 83 if (parentPack.isDefaultPackage() && decls.length > 0) { 84 return Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_remove_description, decls[0].getElementName()); 85 } 86 if (!parentPack.isDefaultPackage() && decls.length == 0) { 87 return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_add_description, parentPack.getElementName())); 88 } 89 } catch(JavaModelException e) { 90 JavaPlugin.log(e); 91 } 92 return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_change_description, parentPack.getElementName())); 93 } 94 } 95 | Popular Tags |