KickJava   Java API By Example, From Geeks To Geeks.

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


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.Tree.Kind;
24 import java.io.IOException JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
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 /**
46  *
47  * @author Petr Pisl
48  */

49 public class JSFWhereUsedPlugin implements RefactoringPlugin{
50     
51     /** This one is important creature - makes sure that cycles between plugins won't appear */
52     private static ThreadLocal JavaDoc semafor = new ThreadLocal JavaDoc();
53     
54     private static final Logger JavaDoc LOGGER = Logger.getLogger(JSFWhereUsedPlugin.class.getName());
55     
56     private final WhereUsedQuery refactoring;
57     private TreePathHandle treePathHandle = null;
58     
59     /** Creates a new instance of JSFWhereUsedPlugin */
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 JavaDoc());
82             //TODO: should be improved.
83
Object JavaDoc element = refactoring.getRefactoringSource().lookup(Object JavaDoc.class);
84             LOGGER.fine("Prepare refactoring: " + element); // NOI18N
85

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 JavaDoc {
99                                         co.toPhase(JavaSource.Phase.RESOLVED);
100                                         refactoring.getContext().add(co);
101                                     }
102                                 }, false);
103                             } catch (IOException JavaDoc 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 JavaDoc fqnc = type.getQualifiedName().toString();
111                         List JavaDoc <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 JavaDoc getText() {
132             return getDisplayText();
133         }
134         
135         public String JavaDoc 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 JavaDoc getComposite() {
155             return item.getFacesConfig();
156         }
157         
158     }
159 }
160
Popular Tags