KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.sun.source.tree.CompilationUnitTree;
21 import com.sun.source.util.TreePath;
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import org.netbeans.api.java.source.CancellableTask;
28 import org.netbeans.api.java.source.JavaSource;
29 import org.netbeans.api.java.source.ModificationResult;
30 import org.netbeans.api.java.source.WorkingCopy;
31 import org.netbeans.api.project.FileOwnerQuery;
32 import org.netbeans.modules.refactoring.api.Problem;
33 import org.netbeans.modules.refactoring.api.SingleCopyRefactoring;
34 import org.netbeans.modules.refactoring.java.RetoucheUtils;
35 import org.netbeans.modules.refactoring.java.plugins.JavaRefactoringPlugin;
36 import org.netbeans.modules.refactoring.java.ui.tree.ElementGripFactory;
37 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
38 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
39 import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImpl;
40 import org.openide.ErrorManager;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.URLMapper;
43 import org.openide.text.PositionBounds;
44 import org.openide.util.NbBundle;
45 import org.openide.util.Utilities;
46
47
48 /** Plugin that implements the core functionality of Copy Class Refactoring.
49  *
50  * @author Jan Becicka
51  */

52 public class CopyClassRefactoringPlugin extends JavaRefactoringPlugin {
53     /** Reference to the parent refactoring instance */
54     private final SingleCopyRefactoring refactoring;
55     
56     /** Creates a new instance of PullUpRefactoringPlugin
57      * @param refactoring Parent refactoring instance.
58      */

59     CopyClassRefactoringPlugin(SingleCopyRefactoring refactoring) {
60         this.refactoring = refactoring;
61     }
62     
63     /** Checks pre-conditions of the refactoring.
64      * @return Problems found or <code>null</code>.
65      */

66     public Problem preCheck() {
67         return null;
68     }
69     
70     public Problem fastCheckParameters() {
71         if (!Utilities.isJavaIdentifier(refactoring.getNewName())) {
72             String JavaDoc msg = new MessageFormat JavaDoc(NbBundle.getMessage(CopyClassRefactoringPlugin.class, "ERR_InvalidIdentifier")).format(
73                 new Object JavaDoc[] {refactoring.getNewName()}
74             );
75             return createProblem(null, true, msg);
76         }
77         URL JavaDoc target = refactoring.getTarget().lookup(URL JavaDoc.class);
78         String JavaDoc targetPackageName = RetoucheUtils.getPackageName(target);
79         if (!RetoucheUtils.isValidPackageName(targetPackageName)) {
80             String JavaDoc msg = new MessageFormat JavaDoc(NbBundle.getMessage(CopyClassRefactoringPlugin.class, "ERR_InvalidPackage")).format(
81                 new Object JavaDoc[] {targetPackageName}
82             );
83             return createProblem(null, true, msg);
84         }
85         String JavaDoc name = targetPackageName.replace('.','/') + '/' + refactoring.getNewName() + ".java"; // NOI18N
86
FileObject fo = URLMapper.findFileObject(target);
87         if (fo==null) {
88             return null;
89         }
90         if (fo.getFileObject(refactoring.getNewName(), (refactoring.getRefactoringSource().lookup(FileObject.class)).getExt()) != null)
91             return createProblem(null, true, new MessageFormat JavaDoc(NbBundle.getMessage(CopyClassRefactoringPlugin.class, "ERR_ClassToMoveClashes")).format(new Object JavaDoc[]{refactoring.getNewName()}));
92         return null;
93     }
94
95     public Problem checkParameters() {
96         return null;
97     }
98
99     public Problem prepare(RefactoringElementsBag refactoringElements) {
100         refactoringElements.add(refactoring, new CopyClass());
101         return null;
102     }
103     
104 private class CopyClass extends SimpleRefactoringElementImpl implements RefactoringElementImplementation{
105         
106         public CopyClass () {
107         }
108         
109         public String JavaDoc getText() {
110             return getDisplayText ();
111         }
112     
113         public String JavaDoc getDisplayText() {
114             return new MessageFormat JavaDoc (NbBundle.getMessage(CopyClassRefactoringPlugin.class, "TXT_CopyClassToPackage")).format ( // NOI18N
115
new Object JavaDoc[] {refactoring.getNewName(), getTargetPackageName(), getParentFile().getName()}
116             );
117         }
118
119         public Object JavaDoc getComposite() {
120             return getParentFile();
121         }
122
123         public PositionBounds getPosition() {
124             return null;
125         }
126         public String JavaDoc getTargetPackageName() {
127             return "test";
128         }
129
130         public void performChange() {
131             try {
132                 FileObject fo = RetoucheUtils.getOrCreateFolder(refactoring.getTarget().lookup(URL JavaDoc.class));
133                 FileObject source = refactoring.getRefactoringSource().lookup(FileObject.class);
134                 String JavaDoc oldPackage = RetoucheUtils.getPackageName(source.getParent());
135                 
136                 FileObject newOne = refactoring.getContext().lookup(FileObject.class);
137                 final Collection JavaDoc<ModificationResult> results = processFiles(
138                         Collections.singleton(newOne),
139                         new UpdateReferences(
140                         !fo.equals(source.getParent()) &&
141                         FileOwnerQuery.getOwner(fo).equals(FileOwnerQuery.getOwner(source))
142                         , oldPackage));
143                 results.iterator().next().commit();
144             } catch (Exception JavaDoc ioe) {
145                 ErrorManager.getDefault().notify(ioe);
146             }
147             
148         }
149
150         public FileObject getParentFile() {
151             return refactoring.getRefactoringSource().lookup(FileObject.class);
152         }
153     }
154     
155     private class UpdateReferences implements CancellableTask<WorkingCopy> {
156
157         private boolean insertImport;
158         private String JavaDoc oldPackage;
159         public UpdateReferences(boolean insertImport, String JavaDoc oldPackage) {
160             this.insertImport = insertImport;
161             this.oldPackage = oldPackage;
162         }
163
164         public void cancel() {
165         }
166
167         public void run(WorkingCopy compiler) throws IOException JavaDoc {
168             compiler.toPhase(JavaSource.Phase.RESOLVED);
169             CompilationUnitTree cu = compiler.getCompilationUnit();
170             if (cu == null) {
171                 ErrorManager.getDefault().log(ErrorManager.ERROR, "compiler.getCompilationUnit() is null " + compiler);
172                 return;
173             }
174             
175             CopyTransformer findVisitor = new CopyTransformer(compiler, refactoring.getNewName(), insertImport, oldPackage);
176             findVisitor.scan(compiler.getCompilationUnit(), null);
177
178             for (TreePath tree : findVisitor.getUsages()) {
179                     ElementGripFactory.getDefault().put(compiler.getFileObject(), tree, compiler);
180           }
181             fireProgressListenerStep();
182         }
183     }
184 }
185
Popular Tags