KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > refactoring > JSFRenamePlugin


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.jsf.refactoring;
21
22
23 import com.sun.source.tree.CompilationUnitTree;
24 import com.sun.source.tree.Tree.Kind;
25 import com.sun.source.util.TreePath;
26 import java.io.IOException JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import javax.lang.model.element.Element;
31 import javax.lang.model.element.TypeElement;
32 import org.netbeans.api.java.source.CompilationController;
33 import org.netbeans.api.java.source.CompilationInfo;
34 import org.netbeans.api.java.source.JavaSource;
35 import org.netbeans.api.java.source.TreePathHandle;
36 import org.netbeans.modules.j2ee.common.source.AbstractTask;
37 import org.netbeans.modules.refactoring.api.Problem;
38 import org.netbeans.modules.refactoring.api.RenameRefactoring;
39 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
40 import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
41 import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImpl;
42 import org.netbeans.modules.web.api.webmodule.WebModule;
43 import org.openide.filesystems.FileObject;
44 import org.openide.text.PositionBounds;
45
46 /**
47  *
48  * @author Petr Pisl
49  */

50
51 //TODO need to be handled the rename packages for manged beans
52
public class JSFRenamePlugin implements RefactoringPlugin {
53     
54     /** This one is important creature - makes sure that cycles between plugins won't appear */
55     private static ThreadLocal JavaDoc semafor = new ThreadLocal JavaDoc();
56     private TreePathHandle treePathHandle = null;
57     
58     private static final Logger JavaDoc LOGGER = Logger.getLogger(JSFRenamePlugin.class.getName());
59     
60     private final RenameRefactoring refactoring;
61     
62     /** Creates a new instance of WicketRenameRefactoringPlugin */
63     public JSFRenamePlugin(RenameRefactoring refactoring) {
64         this.refactoring = refactoring;
65     }
66     
67     public Problem preCheck() {
68         LOGGER.fine("preCheck() called.");
69         return null;
70     }
71     
72     public Problem checkParameters() {
73         LOGGER.fine("checkParameters() called.");
74         return null;
75     }
76     
77     public Problem fastCheckParameters() {
78         LOGGER.fine("fastCheckParameters() called.");
79         return null;
80     }
81     
82     public void cancelRequest() {
83     }
84     
85     public Problem prepare(RefactoringElementsBag refactoringElements) {
86         if (semafor.get() == null) {
87             semafor.set(new Object JavaDoc());
88             //TODO: Lookup stuff should be improved
89
Object JavaDoc element = refactoring.getRefactoringSource().lookup(Object JavaDoc.class);
90             LOGGER.fine("Prepare refactoring: " + element); // NOI18N
91

92             if (element instanceof FileObject){
93                 JavaSource source = JavaSource.forFileObject((FileObject) element);
94                 // Can be null, if it is just folder. Should be handled as well and found
95
// whether is not a part of a package name.
96
if (source != null){
97                     try {
98                         source.runUserActionTask(new AbstractTask<CompilationController>() {
99                             public void cancel() {
100                             }
101
102                             public void run(CompilationController co) throws Exception JavaDoc {
103                                 co.toPhase(JavaSource.Phase.RESOLVED);
104                                 CompilationUnitTree cut = co.getCompilationUnit();
105                                 treePathHandle = TreePathHandle.create(TreePath.getPath(cut, cut.getTypeDecls().get(0)), co);
106                                 refactoring.getContext().add(co);
107                             }
108                         }, false);
109                     } catch (IllegalArgumentException JavaDoc ex) {
110                         LOGGER.log(Level.WARNING, "Exception in JSFRenamePlugin", ex);
111                     } catch (IOException JavaDoc ex) {
112                         LOGGER.log(Level.WARNING, "Exception in JSFRenamePlugin", ex);
113                     }
114                 }
115             }
116             else
117                 if (element instanceof TreePathHandle)
118                     treePathHandle = (TreePathHandle)element;
119             
120             if (treePathHandle != null && treePathHandle.getKind() == Kind.CLASS){
121                 WebModule webModule = WebModule.getWebModule(treePathHandle.getFileObject());
122                 if (webModule != null){
123                     CompilationInfo info = refactoring.getContext().lookup(CompilationInfo.class);
124                     Element resElement = treePathHandle.resolveElement(info);
125                     TypeElement type = (TypeElement) resElement;
126                     String JavaDoc oldFQN = type.getQualifiedName().toString();
127                     String JavaDoc newFQN = renameClass(oldFQN, refactoring.getNewName());
128                     List JavaDoc <Occurrences.OccurrenceItem> items = Occurrences.getAllOccurrences(webModule, oldFQN, newFQN);
129                     for (Occurrences.OccurrenceItem item : items) {
130                         refactoringElements.add(refactoring, new JSFConfigRenameClassElement(item));
131                     }
132                 }
133             }
134
135             semafor.set(null);
136         }
137         return null;
138     }
139     
140     /**
141      * @return true if given str is null or empty.
142      */

143     private static boolean isEmpty(String JavaDoc str){
144         return str == null || "".equals(str.trim());
145     }
146     
147     /**
148      * Constructs new name for given class.
149      * @param originalFullyQualifiedName old fully qualified name of the class.
150      * @param newName new unqualified name of the class.
151      * @return new fully qualified name of the class.
152      */

153     private static String JavaDoc renameClass(String JavaDoc originalFullyQualifiedName, String JavaDoc newName){
154         if (isEmpty(originalFullyQualifiedName) || isEmpty(newName)){
155             throw new IllegalArgumentException JavaDoc("Old and new name of the class must be given.");
156         }
157         int lastDot = originalFullyQualifiedName.lastIndexOf('.');
158         if (lastDot <= 0){
159             // no package
160
return newName;
161         }
162         return originalFullyQualifiedName.substring(0, lastDot + 1) + newName;
163     }
164     
165     public static class JSFConfigRenameClassElement extends SimpleRefactoringElementImpl {
166         private final Occurrences.OccurrenceItem item;
167         
168         JSFConfigRenameClassElement(Occurrences.OccurrenceItem item){
169             this.item = item;
170         }
171         
172         public String JavaDoc getText() {
173             return getDisplayText();
174         }
175         
176         public String JavaDoc getDisplayText() {
177             return item.getRenameMessage();
178         }
179         
180         public void performChange() {
181             //JavaMetamodel.getManager().registerExtChange(this);
182
LOGGER.fine("JSFConfigRenameClassElement.performChange()");
183             item.performRename();
184         }
185         
186         public void undoExternalChange() {
187             item.undoRename();
188         }
189         
190         
191         
192         public FileObject getParentFile() {
193             return item.getFacesConfig();
194         }
195         
196         public PositionBounds getPosition() {
197             return item.getClassDefinitionPosition();
198         }
199         
200         public Object JavaDoc getComposite() {
201             return item.getFacesConfig();
202         }
203     }
204     
205 }
206
Popular Tags