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.io.IOException;
046: import java.util.Collection;
047: import java.util.List;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050: import javax.lang.model.element.Element;
051: import javax.lang.model.element.TypeElement;
052: import org.netbeans.api.java.source.ClasspathInfo;
053: import org.netbeans.api.java.source.CompilationController;
054: import org.netbeans.api.java.source.CompilationInfo;
055: import org.netbeans.api.java.source.JavaSource;
056: import org.netbeans.api.java.source.Task;
057: import org.netbeans.api.java.source.TreePathHandle;
058: import org.netbeans.modules.refactoring.api.Problem;
059: import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
060: import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
061: import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
062: import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
063: import org.netbeans.modules.web.api.webmodule.WebModule;
064: import org.openide.filesystems.FileObject;
065: import org.openide.text.PositionBounds;
066: import org.openide.util.Lookup;
067: import org.openide.util.lookup.Lookups;
068:
069: /**
070: *
071: * @author Petr Pisl
072: */
073: public class JSFSafeDeletePlugin implements RefactoringPlugin {
074:
075: /** This one is important creature - makes sure that cycles between plugins won't appear */
076: private static ThreadLocal semafor = new ThreadLocal();
077:
078: private static final Logger LOGGER = Logger
079: .getLogger(JSFSafeDeletePlugin.class.getName());
080:
081: private final SafeDeleteRefactoring refactoring;
082:
083: /** Creates a new instance of JSFWhereUsedPlugin */
084: public JSFSafeDeletePlugin(SafeDeleteRefactoring refactoring) {
085: this .refactoring = refactoring;
086: }
087:
088: public Problem preCheck() {
089: return null;
090: }
091:
092: public Problem checkParameters() {
093: return null;
094: }
095:
096: public Problem fastCheckParameters() {
097: return null;
098: }
099:
100: public void cancelRequest() {
101:
102: }
103:
104: public Problem prepare(RefactoringElementsBag refactoringElements) {
105: if (semafor.get() == null) {
106: semafor.set(new Object());
107: Collection<? extends TreePathHandle> treePathHandles = refactoring
108: .getRefactoringSource().lookupAll(
109: TreePathHandle.class);
110: WebModule webModule;
111:
112: if (treePathHandles != null) {
113: for (TreePathHandle treePathHandle : treePathHandles) {
114: if (treePathHandle.getKind() == Kind.CLASS) {
115: webModule = WebModule
116: .getWebModule(treePathHandle
117: .getFileObject());
118: if (webModule != null) {
119: CompilationInfo info = JSFRefactoringUtils
120: .getCompilationInfo(refactoring,
121: treePathHandle
122: .getFileObject());
123: if (info != null) {
124: Element resElement = treePathHandle
125: .resolveElement(info);
126: TypeElement type = (TypeElement) resElement;
127: String fqcn = type.getQualifiedName()
128: .toString();
129: List<Occurrences.OccurrenceItem> items = Occurrences
130: .getAllOccurrences(webModule,
131: fqcn, null);
132: for (Occurrences.OccurrenceItem item : items) {
133: refactoringElements
134: .add(
135: refactoring,
136: new JSFSafeDeleteClassElement(
137: item));
138: }
139: }
140: }
141: }
142: }
143: }
144: semafor.set(null);
145: }
146: return null;
147: }
148:
149: public static class JSFSafeDeleteClassElement extends
150: SimpleRefactoringElementImplementation {
151: private final Occurrences.OccurrenceItem item;
152:
153: JSFSafeDeleteClassElement(Occurrences.OccurrenceItem item) {
154: this .item = item;
155: }
156:
157: public String getText() {
158: return getDisplayText();
159: }
160:
161: public String getDisplayText() {
162: return item.getSafeDeleteMessage();
163: }
164:
165: public void performChange() {
166: item.performSafeDelete();
167: }
168:
169: public Lookup getLookup() {
170: return Lookups.singleton(item.getFacesConfig());
171: }
172:
173: public FileObject getParentFile() {
174: return item.getFacesConfig();
175: }
176:
177: public PositionBounds getPosition() {
178: return item.getChangePosition();
179: }
180:
181: @Override
182: public void undoChange() {
183: item.undoSafeDelete();
184: }
185:
186: }
187: }
|