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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039: package org.netbeans.modules.refactoring.java.ui;
040:
041: import com.sun.source.tree.CompilationUnitTree;
042: import com.sun.source.tree.Tree;
043: import com.sun.source.util.TreePath;
044: import java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.Collection;
047: import java.util.HashSet;
048: import javax.lang.model.element.Element;
049: import javax.lang.model.element.Modifier;
050: import javax.swing.JOptionPane;
051: import org.netbeans.api.java.source.CancellableTask;
052: import org.netbeans.api.java.source.CompilationController;
053: import org.netbeans.api.java.source.CompilationInfo;
054: import org.netbeans.api.java.source.JavaSource;
055: import org.netbeans.api.java.source.JavaSource.Phase;
056: import org.netbeans.api.java.source.TreePathHandle;
057: import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
058: import org.netbeans.modules.refactoring.spi.ui.UI;
059: import org.openide.filesystems.FileObject;
060: import org.openide.loaders.DataObject;
061: import org.openide.nodes.Node;
062: import org.openide.util.NbBundle;
063: import org.openide.windows.TopComponent;
064:
065: /**
066: * A task that extracts tree path handles representing java types within a package
067: *
068: * @author Bharath Ravi Kumar
069: */
070: abstract class PackagetoTreePathHandleTask implements Runnable,
071: CancellableTask<CompilationController> {
072:
073: private static final String JAVA_EXTENSION = "java";
074: public CompilationInfo cinfo;
075: private final Collection<FileObject> javaFileObjects = new HashSet<FileObject>();
076: private final Collection<TreePathHandle> handles = new ArrayList<TreePathHandle>();
077:
078: public PackagetoTreePathHandleTask(Collection<? extends Node> nodes) {
079: for (Node packageNode : nodes) {
080: DataObject dataObject = packageNode.getLookup().lookup(
081: DataObject.class);
082: FileObject primaryFileObject = dataObject.getPrimaryFile();
083: javaFileObjects
084: .addAll(findJavaSourceFiles(primaryFileObject));
085: }
086: }
087:
088: public void cancel() {
089: }
090:
091: public void run(CompilationController info) throws Exception {
092: //TODO:Should this be a WeakReference?
093: info.toPhase(Phase.ELEMENTS_RESOLVED);
094: cinfo = info;
095: CompilationUnitTree unit = info.getCompilationUnit();
096: for (Tree tree : unit.getTypeDecls()) {
097: Element element = info.getTrees().getElement(
098: TreePath.getPath(unit, tree));
099: if (element == null
100: || !(element.getKind().isClass() || element
101: .getKind().isInterface())) {
102: // syntax errors #111195
103: continue;
104: }
105: //TODO:Revisit this check
106: if (!element.getModifiers().contains(Modifier.PRIVATE)) {
107: TreePathHandle typeHandle = TreePathHandle.create(
108: TreePath.getPath(unit, tree), info);
109: handles.add(typeHandle);
110: }
111: }
112:
113: }
114:
115: public void run() {
116:
117: for (FileObject javaFileObject : javaFileObjects) {
118: JavaSource source = JavaSource
119: .forFileObject(javaFileObject);
120: assert source != null;
121: try {
122: source.runUserActionTask(this , false);
123: } catch (IllegalArgumentException ex) {
124: ex.printStackTrace();
125: } catch (IOException ex) {
126: ex.printStackTrace();
127: }
128: }
129:
130: TopComponent activetc = TopComponent.getRegistry()
131: .getActivated();
132:
133: RefactoringUI ui = createRefactoringUI(handles, cinfo);
134: if (ui != null) {
135: UI.openRefactoringUI(ui, activetc);
136: } else {
137: JOptionPane.showMessageDialog(null, NbBundle.getMessage(
138: RefactoringActionsProvider.class,
139: "ERR_CannotRenameKeyword"));
140: }
141: }
142:
143: protected final FileObject[] getFileHandles() {
144: return javaFileObjects.toArray(new FileObject[0]);
145: }
146:
147: protected abstract RefactoringUI createRefactoringUI(
148: Collection<TreePathHandle> handles, CompilationInfo info);
149:
150: public static Collection<FileObject> findJavaSourceFiles(
151: FileObject pkg) {
152: Collection<FileObject> javaSrcFiles = new ArrayList<FileObject>();
153: addSourcesInPackage(pkg, javaSrcFiles);
154: return javaSrcFiles;
155: }
156:
157: private static void addSourcesInPackage(FileObject pkgFileObject,
158: Collection<FileObject> javaSrcFiles) {
159: for (FileObject childFileObject : pkgFileObject.getChildren()) {
160: if (childFileObject.isData()
161: && JAVA_EXTENSION.equalsIgnoreCase(childFileObject
162: .getExt())) {
163: javaSrcFiles.add(childFileObject);
164: }
165: //We do not recursively delete subpackages
166: // else if (childFileObject.isFolder()) {
167: // addSourcesInPackage(childFileObject, javaSrcFiles);
168: // }
169: }
170: }
171: }
|