001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.jsf.refactoring;
043:
044: import com.sun.source.tree.Tree.Kind;
045: import java.util.List;
046: import java.util.logging.Logger;
047: import javax.lang.model.element.Element;
048: import javax.lang.model.element.TypeElement;
049: import org.netbeans.api.java.source.CompilationInfo;
050: import org.netbeans.api.java.source.TreePathHandle;
051: import org.netbeans.modules.refactoring.api.Problem;
052: import org.netbeans.modules.refactoring.api.WhereUsedQuery;
053: import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
054: import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
055: import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
056: import org.netbeans.modules.web.api.webmodule.WebModule;
057: import org.openide.filesystems.FileObject;
058: import org.openide.text.PositionBounds;
059: import org.openide.util.Lookup;
060: import org.openide.util.lookup.Lookups;
061:
062: /**
063: *
064: * @author Petr Pisl
065: */
066: public class JSFWhereUsedPlugin implements RefactoringPlugin {
067:
068: /** This one is important creature - makes sure that cycles between plugins won't appear */
069: private static ThreadLocal semafor = new ThreadLocal();
070:
071: private static final Logger LOGGER = Logger
072: .getLogger(JSFWhereUsedPlugin.class.getName());
073:
074: private final WhereUsedQuery refactoring;
075: private TreePathHandle treePathHandle = null;
076:
077: /** Creates a new instance of JSFWhereUsedPlugin */
078: public JSFWhereUsedPlugin(WhereUsedQuery refactoring) {
079: this .refactoring = refactoring;
080: }
081:
082: public Problem preCheck() {
083: return null;
084: }
085:
086: public Problem checkParameters() {
087: return null;
088: }
089:
090: public Problem fastCheckParameters() {
091: return null;
092: }
093:
094: public void cancelRequest() {
095: }
096:
097: public Problem prepare(RefactoringElementsBag refactoringElements) {
098: if (semafor.get() == null) {
099: semafor.set(new Object());
100: //TODO: should be improved.
101: Object element = refactoring.getRefactoringSource().lookup(
102: Object.class);
103: LOGGER.fine("Prepare refactoring: " + element); // NOI18N
104:
105: if (element instanceof TreePathHandle) {
106: treePathHandle = (TreePathHandle) element;
107: if (treePathHandle != null
108: && treePathHandle.getKind() == Kind.CLASS) {
109: WebModule webModule = WebModule
110: .getWebModule(treePathHandle
111: .getFileObject());
112: if (webModule != null) {
113: CompilationInfo info = JSFRefactoringUtils
114: .getCompilationInfo(refactoring,
115: treePathHandle.getFileObject());
116: if (info != null) {
117: Element resElement = treePathHandle
118: .resolveElement(info);
119: TypeElement type = (TypeElement) resElement;
120: String fqnc = type.getQualifiedName()
121: .toString();
122: List<Occurrences.OccurrenceItem> items = Occurrences
123: .getAllOccurrences(webModule, fqnc,
124: "");
125: for (Occurrences.OccurrenceItem item : items) {
126: refactoringElements.add(refactoring,
127: new JSFWhereUsedElement(item));
128: }
129: }
130: }
131: }
132: }
133: semafor.set(null);
134: }
135: return null;
136: }
137:
138: public class JSFWhereUsedElement extends
139: SimpleRefactoringElementImplementation {
140:
141: private final Occurrences.OccurrenceItem item;
142:
143: public JSFWhereUsedElement(Occurrences.OccurrenceItem item) {
144: this .item = item;
145: }
146:
147: public String getText() {
148: return getDisplayText();
149: }
150:
151: public String getDisplayText() {
152: return item.getWhereUsedMessage();
153: }
154:
155: public void performChange() {
156: }
157:
158: public Element getJavaElement() {
159: return null;
160: }
161:
162: public FileObject getParentFile() {
163: return item.getFacesConfig();
164: }
165:
166: public PositionBounds getPosition() {
167: return item.getChangePosition();
168: }
169:
170: public Lookup getLookup() {
171: return Lookups.singleton(item.getFacesConfig());
172: }
173:
174: }
175: }
|