KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > plugins > JavaRefactoringPlugin


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 package org.netbeans.modules.refactoring.java.plugins;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.lang.model.element.Element;
30 import javax.lang.model.type.TypeKind;
31 import org.netbeans.api.java.classpath.ClassPath;
32 import org.netbeans.api.java.source.CancellableTask;
33 import org.netbeans.api.java.source.ClasspathInfo;
34 import org.netbeans.api.java.source.CompilationInfo;
35 import org.netbeans.api.java.source.JavaSource;
36 import org.netbeans.api.java.source.ModificationResult;
37 import org.netbeans.api.java.source.TreePathHandle;
38 import org.netbeans.api.java.source.WorkingCopy;
39 import org.netbeans.modules.refactoring.spi.*;
40 import org.netbeans.modules.refactoring.api.*;
41 import org.openide.filesystems.FileObject;
42 import org.openide.util.Cancellable;
43 import org.openide.util.NbBundle;
44
45 /**
46  *
47  * @author Jan Becicka
48  */

49 public abstract class JavaRefactoringPlugin extends ProgressProviderAdapter implements RefactoringPlugin {
50
51     protected volatile boolean cancelRequest = false;
52     private volatile CancellableTask currentTask;
53     
54     public void cancelRequest() {
55         cancelRequest = true;
56         if (currentTask!=null) {
57             currentTask.cancel();
58         }
59     }
60     
61     protected static final Problem createProblem(Problem result, boolean isFatal, String JavaDoc message) {
62         Problem problem = new Problem(isFatal, message);
63         if (result == null) {
64             return problem;
65         } else if (isFatal) {
66             problem.setNext(result);
67             return problem;
68         } else {
69             //problem.setNext(result.getNext());
70
//result.setNext(problem);
71

72             // [TODO] performance
73
Problem p = result;
74             while (p.getNext() != null)
75                 p = p.getNext();
76             p.setNext(problem);
77             return result;
78         }
79     }
80
81     /**
82      * Checks if the element is still available. Tests if it is still valid.
83      * (Was not deleted by matching mechanism.)
84      * If element is available, returns null, otherwise it creates problem.
85      * (Helper method for refactoring implementation as this problem is
86      * general for all refactorings.)
87      *
88      * @param e element to check
89      * @return problem message or null if the element is valid
90      */

91     protected static Problem isElementAvail(TreePathHandle e, CompilationInfo info) {
92         if (e==null) {
93             //element is null or is not valid.
94
return new Problem(true, NbBundle.getMessage(JavaRefactoringPlugin.class, "DSC_ElNotAvail")); // NOI18N
95
} else {
96             Element el = e.resolveElement(info);
97             if (el == null || el.asType().getKind() == TypeKind.ERROR) {
98                 return new Problem(true, NbBundle.getMessage(JavaRefactoringPlugin.class, "DSC_ElementNotResolved"));
99             }
100             
101             // element is still available
102
return null;
103         }
104     }
105     
106     private Iterable JavaDoc<? extends List JavaDoc<FileObject>> groupByRoot (Iterable JavaDoc<? extends FileObject> data) {
107         Map JavaDoc<FileObject,List JavaDoc<FileObject>> result = new HashMap JavaDoc<FileObject,List JavaDoc<FileObject>> ();
108         for (FileObject file : data) {
109             ClassPath cp = ClassPath.getClassPath(file, ClassPath.SOURCE);
110             if (cp != null) {
111                 FileObject root = cp.findOwnerRoot(file);
112                 if (root != null) {
113                     List JavaDoc<FileObject> subr = result.get (root);
114                     if (subr == null) {
115                         subr = new LinkedList JavaDoc<FileObject>();
116                         result.put (root,subr);
117                     }
118                     subr.add (file);
119                 }
120             }
121         }
122         return result.values();
123     }
124     
125     protected final Collection JavaDoc<ModificationResult> processFiles(Set JavaDoc<FileObject> files, CancellableTask<WorkingCopy> task) {
126         currentTask = task;
127         Iterable JavaDoc<? extends List JavaDoc<FileObject>> work = groupByRoot (files);
128         Collection JavaDoc<ModificationResult> results = new LinkedList JavaDoc();
129         for (List JavaDoc<FileObject> fos : work) {
130             final JavaSource javaSource = JavaSource.create(ClasspathInfo.create(fos.get(0)), fos);
131             try {
132                 results.add(javaSource.runModificationTask(task));
133             } catch (IOException JavaDoc ex) {
134                 throw (RuntimeException JavaDoc) new RuntimeException JavaDoc().initCause(ex);
135             }
136         }
137         return results;
138     }
139 }
140
Popular Tags