2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.form;
43
import com.sun.source.tree.ClassTree;
44
import com.sun.source.tree.Tree;
45
import com.sun.source.util.TreePath;
46
import com.sun.source.util.TreePathScanner;
47
import com.sun.source.util.Trees;
48
import java.io.IOException;
49
import java.util.Iterator;
50
import java.util.List;
51
import java.util.logging.Level;
52
import java.util.logging.Logger;
53
import javax.lang.model.element.Element;
54
import org.netbeans.api.java.source.CancellableTask;
55
import org.netbeans.api.java.source.CompilationController;
56
import org.netbeans.api.java.source.CompilationInfo;
57
import org.netbeans.api.java.source.JavaSource;
58
import org.netbeans.api.java.source.JavaSource.Phase;
59
import org.netbeans.api.java.source.TreePathHandle;
60
import org.netbeans.modules.refactoring.api.Problem;
61
import org.netbeans.modules.refactoring.api.RefactoringSession;
62
import org.netbeans.modules.refactoring.api.RenameRefactoring;
63
import org.openide.loaders.DataObject;
64
import org.openide.util.Lookup;
65
import org.openide.util.lookup.Lookups;
68
* Class used to hook form component/variables renaming into refactoring.
69
* @author Wade Chandler
72
public class RADComponentRenameRefactoringSupport {
73
private String newName = "";
74
private RADComponent component = null;
76
public RADComponent getComponent() {
80
public void setComponent(RADComponent component) {
81
this.component = component;
85
* Creates a new instance of support.
86
* @param newName the new name of the field being set
88
public RADComponentRenameRefactoringSupport(String newName) {
89
this.newName = newName;
92
private static class MemberVisitor
93
extends TreePathScanner<Void, Void>
94
implements CancellableTask<CompilationController>{
96
private CompilationInfo info;
97
private String member = null;
98
private TreePathHandle handle = null;
100
public TreePathHandle getHandle() {
104
public void setHandle(TreePathHandle handle) {
105
this.handle = handle;
108
public MemberVisitor(CompilationInfo info, String member) {
110
this.member = member;
114
public Void visitClass(ClassTree t, Void v) {
115
List<Tree> members = (List<Tree>) t.getMembers();
116
Iterator<Tree> it = members.iterator();
119
if (tr.getKind() == Tree.Kind.VARIABLE) {
120
Trees trees = info.getTrees();
121
TreePath path = new TreePath(getCurrentPath(), tr);
122
Element el = trees.getElement(path);
123
String sname = el.getSimpleName().toString();
124
if(sname.equals(this.member)){
125
this.handle = TreePathHandle.create(path, info);
127
} // TODO also need to check local variables in initComponents
132
public void cancel() {
135
public void run(CompilationController parameter) throws IOException {
136
this.info = parameter;
137
parameter.toPhase(Phase.ELEMENTS_RESOLVED);
138
this.scan(parameter.getCompilationUnit(), null);
143
* Method used to perform the actual refactoring. This method will use the oldName to locate the
144
* elemnent/field needing renamed. It will then setup the correct Java infrastructure path
145
* for refactoring to use. Currently no UI besides gather the user variable is used. If found at
146
* some point people are using component fields outside of the forms and this is very slow then
147
* we can add better UI support.
149
public void doRenameRefactoring(){
150
//now we need to get into the Java phase and get a TreePathHandle
151
//to the field we are renaming. We can then kick off a RenameRefactoring
152
FormDataObject dao = FormEditor.getFormDataObject(this.component.getFormModel());
154
//we should be able to
155
JavaSource js = JavaSource.forFileObject(dao.getPrimaryFile());
157
MemberVisitor visitor = new MemberVisitor(null, component.getName());
158
js.runUserActionTask(visitor, true);
159
if(visitor.getHandle()==null){
160
//this would only happen if setName were called without the correct component being
161
//selected some how...
164
FormEditorSupport fes = dao.getFormEditorSupport();
165
if (fes.isModified()) {
168
//ok, so we are now ready to actually setup our RenameRefactoring...we need the element TreePathHandle
169
Lookup rnl = Lookups.singleton(visitor.getHandle());
170
RefactoringSession renameSession = RefactoringSession.create("Change variable name");//NOI18N
171
RenameRefactoring refactoring = new RenameRefactoring(rnl);
172
Problem pre = refactoring.preCheck();
173
if(pre!=null&&pre.isFatal()){
174
Logger.getLogger("global").log(Level.WARNING, "There were problems trying to perform the refactoring.");
179
if( (!(pre!=null&&pre.isFatal())) && !emptyOrWhite(newName) ){
180
refactoring.setNewName(newName);
181
p = refactoring.prepare(renameSession);
184
if( (!(p!=null&&p.isFatal())) && !emptyOrWhite(newName) ){
185
renameSession.doRefactoring(true);
187
} catch (IOException e) {
188
Logger.getLogger("global").log(Level.SEVERE, e.getMessage(), e);
193
private static boolean emptyOrWhite(String s){
197
if(s.trim().length()<0){