KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > nls > AccessorClassModifier


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 package org.eclipse.jdt.internal.corext.refactoring.nls;
12
13 import com.ibm.icu.text.Collator;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.text.edits.MultiTextEdit;
21 import org.eclipse.text.edits.TextEdit;
22 import org.eclipse.text.edits.TextEditGroup;
23
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Status;
28
29 import org.eclipse.core.filebuffers.FileBuffers;
30 import org.eclipse.core.filebuffers.ITextFileBuffer;
31 import org.eclipse.core.filebuffers.ITextFileBufferManager;
32 import org.eclipse.core.filebuffers.LocationKind;
33
34 import org.eclipse.jface.text.Document;
35 import org.eclipse.jface.text.IDocument;
36
37 import org.eclipse.ltk.core.refactoring.Change;
38 import org.eclipse.ltk.core.refactoring.TextChange;
39
40 import org.eclipse.jdt.core.ICompilationUnit;
41 import org.eclipse.jdt.core.dom.AST;
42 import org.eclipse.jdt.core.dom.ASTNode;
43 import org.eclipse.jdt.core.dom.ASTVisitor;
44 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
45 import org.eclipse.jdt.core.dom.CompilationUnit;
46 import org.eclipse.jdt.core.dom.FieldDeclaration;
47 import org.eclipse.jdt.core.dom.Modifier;
48 import org.eclipse.jdt.core.dom.TypeDeclaration;
49 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
50 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
51 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
52
53 import org.eclipse.jdt.internal.corext.dom.GenericVisitor;
54 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange;
55 import org.eclipse.jdt.internal.corext.util.Messages;
56
57 import org.eclipse.jdt.ui.JavaUI;
58
59 import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
60 import org.eclipse.jdt.internal.ui.JavaPlugin;
61 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
62
63 public class AccessorClassModifier {
64
65     private CompilationUnit fRoot;
66     private AST fAst;
67     private ASTRewrite fASTRewrite;
68     private ListRewrite fListRewrite;
69     private ICompilationUnit fCU;
70     private List JavaDoc fFields;
71
72     private AccessorClassModifier(ICompilationUnit cu) throws CoreException {
73
74         fCU= cu;
75         
76         fRoot= JavaPlugin.getDefault().getASTProvider().getAST(cu, ASTProvider.WAIT_YES, null);
77         fAst= fRoot.getAST();
78         fASTRewrite= ASTRewrite.create(fAst);
79         
80         AbstractTypeDeclaration parent= null;
81         if (fRoot.types().size() > 0) {
82             parent= (AbstractTypeDeclaration)fRoot.types().get(0);
83             fFields= new ArrayList JavaDoc();
84             parent.accept(new GenericVisitor() {
85                 /**
86                  * {@inheritDoc}
87                  */

88                 public boolean visit(FieldDeclaration node) {
89                     int modifiers= node.getModifiers();
90                     if (!Modifier.isPublic(modifiers))
91                         return false;
92                     
93                     if (!Modifier.isStatic(modifiers))
94                         return false;
95                     
96                     List JavaDoc fragments= node.fragments();
97                     if (fragments.size() != 1)
98                         return false;
99                     
100                     VariableDeclarationFragment fragment= (VariableDeclarationFragment)fragments.get(0);
101                     if (fragment.getInitializer() != null)
102                         return false;
103                     
104                     fFields.add(node);
105                     return false;
106                 }
107             });
108             fListRewrite= fASTRewrite.getListRewrite(parent, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
109         } else {
110             IStatus status= new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IJavaStatusConstants.INTERNAL_ERROR, NLSMessages.AccessorClassModifier_missingType, null);
111             throw new CoreException(status);
112         }
113     }
114     
115     private TextEdit getTextEdit() throws CoreException {
116         IDocument document= null;
117         
118         ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
119         IPath path= fCU.getPath();
120         
121         if (manager != null && path != null) {
122             manager.connect(path, LocationKind.NORMALIZE, null);
123             try {
124                 ITextFileBuffer buffer= manager.getTextFileBuffer(path, LocationKind.NORMALIZE);
125                 if (buffer != null)
126                     document= buffer.getDocument();
127             } finally {
128                 manager.disconnect(path, LocationKind.NORMALIZE, null);
129             }
130         }
131         
132         if (document == null)
133             document= new Document(fCU.getSource());
134          
135         return fASTRewrite.rewriteAST(document, fCU.getJavaProject().getOptions(true));
136     }
137
138     public static Change create(ICompilationUnit cu, NLSSubstitution[] substitutions) throws CoreException {
139         
140         Map JavaDoc newKeyToSubstMap= NLSPropertyFileModifier.getNewKeyToSubstitutionMap(substitutions);
141         Map JavaDoc oldKeyToSubstMap= NLSPropertyFileModifier.getOldKeyToSubstitutionMap(substitutions);
142
143         AccessorClassModifier sourceModification= new AccessorClassModifier(cu);
144
145         String JavaDoc message= Messages.format(NLSMessages.NLSSourceModifier_change_description, cu.getElementName());
146
147         TextChange change= new CompilationUnitChange(message, cu);
148         MultiTextEdit multiTextEdit= new MultiTextEdit();
149         change.setEdit(multiTextEdit);
150         
151         for (int i= 0; i < substitutions.length; i++) {
152             NLSSubstitution substitution= substitutions[i];
153             if (NLSPropertyFileModifier.doRemove(substitution, newKeyToSubstMap, oldKeyToSubstMap)) {
154                 sourceModification.removeKey(substitution, change);
155             }
156         }
157         for (int i= 0; i < substitutions.length; i++) {
158             NLSSubstitution substitution= substitutions[i];
159             if (substitution.isKeyRename() && NLSPropertyFileModifier.doReplace(substitution, newKeyToSubstMap, oldKeyToSubstMap)) {
160                 sourceModification.renameKey(substitution, change);
161             }
162         }
163         for (int i= 0; i < substitutions.length; i++) {
164             NLSSubstitution substitution= substitutions[i];
165             if (NLSPropertyFileModifier.doInsert(substitution, newKeyToSubstMap, oldKeyToSubstMap)) {
166                 sourceModification.addKey(substitution, change);
167             }
168         }
169         
170         if (change.getChangeGroups().length == 0)
171             return null;
172         
173         change.addEdit(sourceModification.getTextEdit());
174         
175         return change;
176     }
177     
178     private void removeKey(NLSSubstitution sub, TextChange change) throws CoreException {
179         ASTNode node= findField(fRoot, sub.getKey());
180         if (node == null)
181             return;
182         
183         String JavaDoc name= Messages.format(NLSMessages.AccessorClassModifier_remove_entry, sub.getKey());
184         TextEditGroup editGroup= new TextEditGroup(name);
185         fListRewrite.remove(node, editGroup);
186         change.addTextEditGroup(editGroup);
187         fFields.remove(node);
188     }
189     
190     private void renameKey(NLSSubstitution sub, TextChange change) throws CoreException {
191         ASTNode node= findField(fRoot, sub.getInitialKey());
192         if (node == null)
193             return;
194         
195         String JavaDoc name= Messages.format(NLSMessages.AccessorClassModifier_replace_entry, sub.getKey());
196         TextEditGroup editGroup= new TextEditGroup(name);
197         fListRewrite.remove(node, editGroup);
198         fFields.remove(node);
199         
200         addKey(sub, change, editGroup);
201         
202         change.addTextEditGroup(editGroup);
203     }
204     
205     private ASTNode findField(ASTNode astRoot, final String JavaDoc name) {
206         
207         class STOP_VISITING extends RuntimeException JavaDoc {
208             private static final long serialVersionUID= 1L;
209         }
210         
211         final ASTNode[] result= new ASTNode[1];
212         
213         try {
214             astRoot.accept(new ASTVisitor() {
215                 
216                 public boolean visit(VariableDeclarationFragment node) {
217                     if (name.equals(node.getName().getFullyQualifiedName())) {
218                         result[0]= node.getParent();
219                         throw new STOP_VISITING();
220                     }
221                     return true;
222                 }
223             });
224         } catch (STOP_VISITING ex) {
225             // stop visiting AST
226
}
227         
228         return result[0];
229     }
230     
231     private void addKey(NLSSubstitution sub, TextChange change) throws CoreException {
232         String JavaDoc name= Messages.format(NLSMessages.AccessorClassModifier_add_entry, sub.getKey());
233         TextEditGroup editGroup= new TextEditGroup(name);
234         change.addTextEditGroup(editGroup);
235         addKey(sub, change, editGroup);
236     }
237         
238     private void addKey(NLSSubstitution sub, TextChange change, TextEditGroup editGroup) throws CoreException {
239         
240         if (fListRewrite == null)
241             return;
242         
243         String JavaDoc key= sub.getKey();
244         FieldDeclaration fieldDeclaration= getNewFinalStringFieldDeclaration(key);
245
246         Iterator JavaDoc iter= fFields.iterator();
247         int insertionPosition= 0;
248         if (iter.hasNext()) {
249             Collator collator= Collator.getInstance();
250             FieldDeclaration existingFieldDecl= (FieldDeclaration)iter.next();
251             VariableDeclarationFragment fragment= (VariableDeclarationFragment)existingFieldDecl.fragments().get(0);
252             String JavaDoc identifier= fragment.getName().getIdentifier();
253             if (collator.compare(key, identifier) != 1) {
254                 insertionPosition= 0;
255                 fListRewrite.insertBefore(fieldDeclaration, existingFieldDecl, editGroup);
256             } else {
257                 insertionPosition++;
258                 while (iter.hasNext()) {
259                     FieldDeclaration next= (FieldDeclaration)iter.next();
260                     fragment= (VariableDeclarationFragment)next.fragments().get(0);
261                     identifier= fragment.getName().getIdentifier();
262                     if (collator.compare(key, identifier) == -1) {
263                         break;
264                     }
265                     insertionPosition++;
266                     existingFieldDecl= next;
267                 }
268                 fListRewrite.insertAfter(fieldDeclaration, existingFieldDecl, editGroup);
269             }
270         } else {
271             insertionPosition= 0;
272             fListRewrite.insertLast(fieldDeclaration, editGroup);
273         }
274         fFields.add(insertionPosition, fieldDeclaration);
275     }
276
277     private FieldDeclaration getNewFinalStringFieldDeclaration(String JavaDoc name) {
278         VariableDeclarationFragment variableDeclarationFragment= fAst.newVariableDeclarationFragment();
279         variableDeclarationFragment.setName(fAst.newSimpleName(name));
280         
281         FieldDeclaration fieldDeclaration= fAst.newFieldDeclaration(variableDeclarationFragment);
282         fieldDeclaration.setType(fAst.newSimpleType(fAst.newSimpleName("String"))); //$NON-NLS-1$
283
fieldDeclaration.modifiers().add(fAst.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
284         fieldDeclaration.modifiers().add(fAst.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
285         
286         return fieldDeclaration;
287     }
288
289 }
290
Popular Tags