1 19 20 package org.netbeans.modules.web.jsf.refactoring; 21 22 23 import com.sun.source.tree.Tree.Kind; 24 import java.io.IOException ; 25 import java.util.List ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 import javax.lang.model.element.Element; 29 import javax.lang.model.element.TypeElement; 30 import org.netbeans.api.java.source.ClasspathInfo; 31 import org.netbeans.api.java.source.CompilationController; 32 import org.netbeans.api.java.source.CompilationInfo; 33 import org.netbeans.api.java.source.JavaSource; 34 import org.netbeans.api.java.source.TreePathHandle; 35 import org.netbeans.modules.j2ee.common.source.AbstractTask; 36 import org.netbeans.modules.refactoring.api.Problem; 37 import org.netbeans.modules.refactoring.api.WhereUsedQuery; 38 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 39 import org.netbeans.modules.refactoring.spi.RefactoringPlugin; 40 import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImpl; 41 import org.netbeans.modules.web.api.webmodule.WebModule; 42 import org.openide.filesystems.FileObject; 43 import org.openide.text.PositionBounds; 44 45 49 public class JSFWhereUsedPlugin implements RefactoringPlugin{ 50 51 52 private static ThreadLocal semafor = new ThreadLocal (); 53 54 private static final Logger LOGGER = Logger.getLogger(JSFWhereUsedPlugin.class.getName()); 55 56 private final WhereUsedQuery refactoring; 57 private TreePathHandle treePathHandle = null; 58 59 60 public JSFWhereUsedPlugin(WhereUsedQuery refactoring) { 61 this.refactoring = refactoring; 62 } 63 64 public Problem preCheck() { 65 return null; 66 } 67 68 public Problem checkParameters() { 69 return null; 70 } 71 72 public Problem fastCheckParameters() { 73 return null; 74 } 75 76 public void cancelRequest() { 77 } 78 79 public Problem prepare(RefactoringElementsBag refactoringElements) { 80 if (semafor.get() == null) { 81 semafor.set(new Object ()); 82 Object element = refactoring.getRefactoringSource().lookup(Object .class); 84 LOGGER.fine("Prepare refactoring: " + element); 86 if (element instanceof TreePathHandle) { 87 treePathHandle = (TreePathHandle)element; 88 if (treePathHandle != null && treePathHandle.getKind() == Kind.CLASS){ 89 WebModule webModule = WebModule.getWebModule(treePathHandle.getFileObject()); 90 if (webModule != null){ 91 CompilationInfo info = refactoring.getContext().lookup(CompilationInfo.class); 92 if (refactoring.getContext().lookup(CompilationInfo.class) == null){ 93 final ClasspathInfo cpInfo = refactoring.getContext().lookup(ClasspathInfo.class); 94 JavaSource source = JavaSource.create(cpInfo, new FileObject[]{treePathHandle.getFileObject()}); 95 try{ 96 source.runUserActionTask(new AbstractTask<CompilationController>() { 97 98 public void run(CompilationController co) throws Exception { 99 co.toPhase(JavaSource.Phase.RESOLVED); 100 refactoring.getContext().add(co); 101 } 102 }, false); 103 } catch (IOException ex) { 104 LOGGER.log(Level.WARNING, "Exception in JSFWhereUsedPlugin", ex); 105 } 106 } 107 info = refactoring.getContext().lookup(CompilationInfo.class); 108 Element resElement = treePathHandle.resolveElement(info); 109 TypeElement type = (TypeElement) resElement; 110 String fqnc = type.getQualifiedName().toString(); 111 List <Occurrences.OccurrenceItem> items = Occurrences.getAllOccurrences(webModule, fqnc,""); 112 for (Occurrences.OccurrenceItem item : items) { 113 refactoringElements.add(refactoring, new JSFWhereUsedElement(item)); 114 } 115 } 116 } 117 } 118 semafor.set(null); 119 } 120 return null; 121 } 122 123 public class JSFWhereUsedElement extends SimpleRefactoringElementImpl { 124 125 private final Occurrences.OccurrenceItem item; 126 127 public JSFWhereUsedElement(Occurrences.OccurrenceItem item){ 128 this.item = item; 129 } 130 131 public String getText() { 132 return getDisplayText(); 133 } 134 135 public String getDisplayText() { 136 return item.getWhereUsedMessage(); 137 } 138 139 public void performChange() { 140 } 141 142 public Element getJavaElement() { 143 return null; 144 } 145 146 public FileObject getParentFile() { 147 return item.getFacesConfig(); 148 } 149 150 public PositionBounds getPosition() { 151 return item.getClassDefinitionPosition(); 152 } 153 154 public Object getComposite() { 155 return item.getFacesConfig(); 156 } 157 158 } 159 } 160 | Popular Tags |