1 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 ; 27 import java.util.List ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 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 50 51 public class JSFRenamePlugin implements RefactoringPlugin { 53 54 55 private static ThreadLocal semafor = new ThreadLocal (); 56 private TreePathHandle treePathHandle = null; 57 58 private static final Logger LOGGER = Logger.getLogger(JSFRenamePlugin.class.getName()); 59 60 private final RenameRefactoring refactoring; 61 62 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 ()); 88 Object element = refactoring.getRefactoringSource().lookup(Object .class); 90 LOGGER.fine("Prepare refactoring: " + element); 92 if (element instanceof FileObject){ 93 JavaSource source = JavaSource.forFileObject((FileObject) element); 94 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 { 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 ex) { 110 LOGGER.log(Level.WARNING, "Exception in JSFRenamePlugin", ex); 111 } catch (IOException 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 oldFQN = type.getQualifiedName().toString(); 127 String newFQN = renameClass(oldFQN, refactoring.getNewName()); 128 List <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 143 private static boolean isEmpty(String str){ 144 return str == null || "".equals(str.trim()); 145 } 146 147 153 private static String renameClass(String originalFullyQualifiedName, String newName){ 154 if (isEmpty(originalFullyQualifiedName) || isEmpty(newName)){ 155 throw new IllegalArgumentException ("Old and new name of the class must be given."); 156 } 157 int lastDot = originalFullyQualifiedName.lastIndexOf('.'); 158 if (lastDot <= 0){ 159 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 getText() { 173 return getDisplayText(); 174 } 175 176 public String getDisplayText() { 177 return item.getRenameMessage(); 178 } 179 180 public void performChange() { 181 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 getComposite() { 201 return item.getFacesConfig(); 202 } 203 } 204 205 } 206 | Popular Tags |