KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.jdt.internal.ui.text.correction;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
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.CompilationUnit;
23 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
24
25 import org.eclipse.jdt.internal.corext.dom.LinkedNodeFinder;
26 import org.eclipse.jdt.internal.corext.util.Messages;
27
28 import org.eclipse.jdt.ui.text.java.IInvocationContext;
29
30 import org.eclipse.jdt.internal.ui.JavaPluginImages;
31
32 /**
33  * Renames the primary type to be compatible with the name of the compilation unit.
34  * All constructors and local references to the type are renamed as well.
35   */

36 public class CorrectMainTypeNameProposal extends ASTRewriteCorrectionProposal {
37
38     private final String JavaDoc fOldName;
39     private final String JavaDoc fNewName;
40     private final IInvocationContext fContext;
41
42     /**
43      * Constructor for CorrectTypeNameProposal.
44      */

45     public CorrectMainTypeNameProposal(ICompilationUnit cu, IInvocationContext context, String JavaDoc oldTypeName, String JavaDoc newTypeName, int relevance) {
46         super("", cu, null, relevance, null); //$NON-NLS-1$
47
fContext= context;
48
49         setDisplayName(Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_renametype_description, newTypeName));
50         setImage(JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
51
52         fOldName= oldTypeName;
53         fNewName= newTypeName;
54     }
55
56     /* (non-Javadoc)
57      * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
58      */

59     protected ASTRewrite getRewrite() throws CoreException {
60         CompilationUnit astRoot= fContext.getASTRoot();
61
62         AST ast= astRoot.getAST();
63         ASTRewrite rewrite= ASTRewrite.create(ast);
64
65         AbstractTypeDeclaration decl= findTypeDeclaration(astRoot.types(), fOldName);
66         if (decl != null) {
67             ASTNode[] sameNodes= LinkedNodeFinder.findByNode(astRoot, decl.getName());
68             for (int i= 0; i < sameNodes.length; i++) {
69                 rewrite.replace(sameNodes[i], ast.newSimpleName(fNewName), null);
70             }
71         }
72         return rewrite;
73     }
74
75     private AbstractTypeDeclaration findTypeDeclaration(List JavaDoc types, String JavaDoc name) {
76         for (Iterator JavaDoc iter= types.iterator(); iter.hasNext();) {
77             AbstractTypeDeclaration decl= (AbstractTypeDeclaration) iter.next();
78             if (name.equals(decl.getName().getIdentifier())) {
79                 return decl;
80             }
81         }
82         return null;
83     }
84
85 }
86
Popular Tags