1 11 12 package org.eclipse.jdt.internal.ui.text.correction; 13 14 import org.eclipse.text.edits.TextEditGroup; 15 16 import org.eclipse.swt.graphics.Image; 17 18 import org.eclipse.jdt.core.ICompilationUnit; 19 import org.eclipse.jdt.core.dom.AST; 20 import org.eclipse.jdt.core.dom.ASTNode; 21 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; 22 import org.eclipse.jdt.core.dom.Block; 23 import org.eclipse.jdt.core.dom.CompilationUnit; 24 import org.eclipse.jdt.core.dom.FieldDeclaration; 25 import org.eclipse.jdt.core.dom.IBinding; 26 import org.eclipse.jdt.core.dom.VariableDeclarationExpression; 27 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 28 import org.eclipse.jdt.core.dom.VariableDeclarationStatement; 29 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 30 31 import org.eclipse.jdt.internal.corext.dom.ModifierRewrite; 32 import org.eclipse.jdt.internal.corext.dom.VariableDeclarationRewrite; 33 34 public class ModifierChangeCompletionProposal extends LinkedCorrectionProposal { 35 36 private IBinding fBinding; 37 private ASTNode fNode; 38 private int fIncludedModifiers; 39 private int fExcludedModifiers; 40 41 public ModifierChangeCompletionProposal(String label, ICompilationUnit targetCU, IBinding binding, ASTNode node, int includedModifiers, int excludedModifiers, int relevance, Image image) { 42 super(label, targetCU, null, relevance, image); 43 fBinding= binding; 44 fNode= node; 45 fIncludedModifiers= includedModifiers; 46 fExcludedModifiers= excludedModifiers; 47 } 48 49 protected ASTRewrite getRewrite() { 50 CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fNode); 51 ASTNode boundNode= astRoot.findDeclaringNode(fBinding); 52 ASTNode declNode= null; 53 54 TextEditGroup selectionDescription= null; 55 56 if (boundNode != null) { 57 declNode= boundNode; } else { 59 selectionDescription= new TextEditGroup("selection"); CompilationUnit newRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null); 62 declNode= newRoot.findDeclaringNode(fBinding.getKey()); 63 } 64 if (declNode != null) { 65 AST ast= declNode.getAST(); 66 ASTRewrite rewrite= ASTRewrite.create(ast); 67 68 if (declNode.getNodeType() == ASTNode.VARIABLE_DECLARATION_FRAGMENT) { 69 VariableDeclarationFragment fragment= (VariableDeclarationFragment)declNode; 70 ASTNode parent= declNode.getParent(); 71 if (parent instanceof FieldDeclaration) { 72 FieldDeclaration fieldDecl= (FieldDeclaration) parent; 73 if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) { VariableDeclarationRewrite.rewriteModifiers(fieldDecl, new VariableDeclarationFragment[] {fragment}, fIncludedModifiers, fExcludedModifiers, rewrite, selectionDescription); 75 return rewrite; 76 } 77 } else if (parent instanceof VariableDeclarationStatement) { 78 VariableDeclarationStatement varDecl= (VariableDeclarationStatement) parent; 79 if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) { VariableDeclarationRewrite.rewriteModifiers(varDecl, new VariableDeclarationFragment[] {fragment}, fIncludedModifiers, fExcludedModifiers, rewrite, selectionDescription); 81 return rewrite; 82 } 83 } else if (parent instanceof VariableDeclarationExpression) { 84 } 86 declNode= parent; 87 } 88 ModifierRewrite listRewrite= ModifierRewrite.create(rewrite, declNode); 89 listRewrite.setModifiers(fIncludedModifiers, fExcludedModifiers, selectionDescription); 90 return rewrite; 91 } 92 return null; 93 } 94 95 96 } 97 | Popular Tags |