~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to form/src/org/netbeans/modules/form/RADComponentRenameRefactoringSupport.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
package org.netbeans.modules.form;
 
42
 
 
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;
 
66
 
 
67
/**
 
68
 * Class used to hook form component/variables renaming into refactoring.
 
69
 * @author Wade Chandler
 
70
 * @version 1.0
 
71
 */
 
72
public class RADComponentRenameRefactoringSupport {
 
73
    private String newName = "";
 
74
    private RADComponent component = null;
 
75
 
 
76
    public RADComponent getComponent() {
 
77
        return component;
 
78
    }
 
79
 
 
80
    public void setComponent(RADComponent component) {
 
81
        this.component = component;
 
82
    }
 
83
 
 
84
    /**
 
85
     * Creates a new instance of support.
 
86
     * @param newName the new name of the field being set
 
87
     */
 
88
    public RADComponentRenameRefactoringSupport(String newName) {
 
89
        this.newName = newName;
 
90
    }
 
91
    
 
92
    private static class MemberVisitor
 
93
            extends TreePathScanner<Void, Void>
 
94
            implements CancellableTask<CompilationController>{
 
95
        
 
96
        private CompilationInfo info;
 
97
        private String member = null;
 
98
        private TreePathHandle handle = null;
 
99
        
 
100
        public TreePathHandle getHandle() {
 
101
            return handle;
 
102
        }
 
103
        
 
104
        public void setHandle(TreePathHandle handle) {
 
105
            this.handle = handle;
 
106
        }
 
107
        
 
108
        public MemberVisitor(CompilationInfo info, String member) {
 
109
            this.info = info;
 
110
            this.member = member;
 
111
        }
 
112
        
 
113
        @Override
 
114
        public Void visitClass(ClassTree t, Void v) {
 
115
            List<Tree> members = (List<Tree>) t.getMembers();
 
116
            Iterator<Tree> it = members.iterator();
 
117
            while(it.hasNext()){
 
118
                Tree tr = it.next();
 
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);
 
126
                    }
 
127
                } // TODO also need to check local variables in initComponents
 
128
            }
 
129
            return null;
 
130
        }
 
131
        
 
132
        public void cancel() {
 
133
        }
 
134
        
 
135
        public void run(CompilationController parameter) throws IOException {
 
136
            this.info = parameter;
 
137
            parameter.toPhase(Phase.ELEMENTS_RESOLVED);
 
138
            this.scan(parameter.getCompilationUnit(), null);
 
139
        }
 
140
    }
 
141
    
 
142
    /**
 
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.
 
148
     */
 
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());
 
153
        if(dao!=null){
 
154
            //we should be able to
 
155
            JavaSource js = JavaSource.forFileObject(dao.getPrimaryFile());
 
156
            try {
 
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...
 
162
                    return;
 
163
                }
 
164
                FormEditorSupport fes = dao.getFormEditorSupport();
 
165
                if (fes.isModified()) {
 
166
                    fes.saveDocument();
 
167
                }
 
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.");
 
175
                }
 
176
                
 
177
                Problem p = null;
 
178
                
 
179
                if( (!(pre!=null&&pre.isFatal())) && !emptyOrWhite(newName) ){
 
180
                    refactoring.setNewName(newName);
 
181
                    p = refactoring.prepare(renameSession);
 
182
                }
 
183
                
 
184
                if( (!(p!=null&&p.isFatal())) && !emptyOrWhite(newName) ){
 
185
                    renameSession.doRefactoring(true);
 
186
                }
 
187
            } catch (IOException e) {
 
188
                Logger.getLogger("global").log(Level.SEVERE, e.getMessage(), e);
 
189
            }
 
190
        }
 
191
    }
 
192
    
 
193
    private static boolean emptyOrWhite(String s){
 
194
        if(s==null){
 
195
            return true;
 
196
        }
 
197
        if(s.trim().length()<0){
 
198
            return true;
 
199
        }
 
200
        return false;
 
201
    }
 
202
    
 
203
}