KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > ModifierChangeCompletionProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc 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; // is same CU
58
} else {
59             selectionDescription= new TextEditGroup("selection"); // in different CU, needs selection //$NON-NLS-1$
60
//setSelectionDescription(selectionDescription);
61
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)) { // split
74
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)) { // split
80
VariableDeclarationRewrite.rewriteModifiers(varDecl, new VariableDeclarationFragment[] {fragment}, fIncludedModifiers, fExcludedModifiers, rewrite, selectionDescription);
81                         return rewrite;
82                     }
83                 } else if (parent instanceof VariableDeclarationExpression) {
84                     // can't separate
85
}
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