KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 org.eclipse.text.edits.ReplaceEdit;
14 import org.eclipse.text.edits.TextEdit;
15
16 import org.eclipse.core.runtime.CoreException;
17
18 import org.eclipse.jface.text.IDocument;
19
20 import org.eclipse.jdt.core.ICompilationUnit;
21
22 import org.eclipse.jdt.core.dom.ASTNode;
23 import org.eclipse.jdt.core.dom.CompilationUnit;
24 import org.eclipse.jdt.core.dom.SimpleName;
25
26 import org.eclipse.jdt.internal.corext.dom.LinkedNodeFinder;
27 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
28 import org.eclipse.jdt.internal.ui.JavaPlugin;
29 import org.eclipse.jdt.internal.ui.JavaPluginImages;
30 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
31 /**
32  *
33  */

34 public class RenameNodeCompletionProposal extends CUCorrectionProposal {
35
36     private String JavaDoc fNewName;
37     private int fOffset;
38     private int fLength;
39
40     public RenameNodeCompletionProposal(String JavaDoc name, ICompilationUnit cu, int offset, int length, String JavaDoc newName, int relevance) {
41         super(name, cu, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE));
42         fOffset= offset;
43         fLength= length;
44         fNewName= newName;
45     }
46
47     /*(non-Javadoc)
48      * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jface.text.IDocument)
49      */

50     protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
51         super.addEdits(doc, root);
52
53         // build a full AST
54
CompilationUnit unit= JavaPlugin.getDefault().getASTProvider().getAST(getCompilationUnit(), ASTProvider.WAIT_YES, null);
55
56         ASTNode name= NodeFinder.perform(unit, fOffset, fLength);
57         if (name instanceof SimpleName) {
58
59             SimpleName[] names= LinkedNodeFinder.findByProblems(unit, (SimpleName) name);
60             if (names != null) {
61                 for (int i= 0; i < names.length; i++) {
62                     SimpleName curr= names[i];
63                     root.addChild(new ReplaceEdit(curr.getStartPosition(), curr.getLength(), fNewName));
64                 }
65                 return;
66             }
67         }
68         root.addChild(new ReplaceEdit(fOffset, fLength, fNewName));
69     }
70 }
71
Popular Tags