KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.modules.refactoring.java.plugins;
21
22 import org.netbeans.api.fileinfo.NonRecursiveFolder;
23 import org.netbeans.api.java.source.TreePathHandle;
24 import org.netbeans.modules.refactoring.java.RetoucheUtils;
25 import org.netbeans.modules.refactoring.api.*;
26 import org.netbeans.modules.refactoring.java.api.ExtractInterfaceRefactoring;
27 import org.netbeans.modules.refactoring.java.api.PullUpRefactoring;
28 import org.netbeans.modules.refactoring.java.api.PushDownRefactoring;
29 import org.netbeans.modules.refactoring.spi.*;
30 import org.openide.filesystems.FileObject;
31 import org.openide.util.Lookup;
32
33 /**
34  *
35  * @author Jan Becicka
36  */

37 public class JavaRefactoringsFactory implements RefactoringPluginFactory {
38    
39     public RefactoringPlugin createInstance(AbstractRefactoring refactoring) {
40         Lookup look = refactoring.getRefactoringSource();
41         FileObject file = look.lookup(FileObject.class);
42         NonRecursiveFolder folder = look.lookup(NonRecursiveFolder.class);
43         TreePathHandle handle = look.lookup(TreePathHandle.class);
44         if (refactoring instanceof WhereUsedQuery) {
45             if (handle!=null) {
46                 return new JavaWhereUsedQueryPlugin((WhereUsedQuery) refactoring);
47             }
48         } else if (refactoring instanceof RenameRefactoring) {
49             if (handle!=null || RetoucheUtils.isJavaFile(file)) {
50                 //rename java file, class, method etc..
51
return new RenameRefactoringPlugin((RenameRefactoring)refactoring);
52             } else if (file!=null && RetoucheUtils.isOnSourceClasspath(file) && file.isFolder()) {
53                 //rename folder
54
return new MoveRefactoringPlugin((RenameRefactoring)refactoring);
55             } else if (folder!=null && RetoucheUtils.isOnSourceClasspath(folder.getFolder())) {
56                 //rename package
57
return new MoveRefactoringPlugin((RenameRefactoring)refactoring);
58             }
59         } else if (refactoring instanceof SafeDeleteRefactoring) {
60             //TODO: should be implemented better
61
if (checkSafeDelete(refactoring.getRefactoringSource())) {
62                 return new SafeDeleteRefactoringPlugin((SafeDeleteRefactoring)refactoring);
63             }
64         } else if (refactoring instanceof MoveRefactoring) {
65             return new MoveRefactoringPlugin((MoveRefactoring) refactoring);
66         } else if (refactoring instanceof SingleCopyRefactoring) {
67             return new CopyClassRefactoringPlugin((SingleCopyRefactoring) refactoring);
68         } else if (refactoring instanceof ExtractInterfaceRefactoring) {
69             return new ExtractInterfaceRefactoringPlugin((ExtractInterfaceRefactoring) refactoring);
70         } else if (refactoring instanceof PullUpRefactoring) {
71             return new PullUpRefactoringPlugin((PullUpRefactoring)refactoring);
72         } else if (refactoring instanceof PushDownRefactoring) {
73             return new PushDownRefactoringPlugin((PushDownRefactoring) refactoring);
74         }
75         return null;
76     }
77
78     //TODO: should be implemented better
79
private boolean checkSafeDelete(Lookup object) {
80         boolean a=false;
81         for (FileObject f:object.lookupAll(FileObject.class)) {
82             a=true;
83             if (!RetoucheUtils.isJavaFile(f)) {
84                 return false;
85             }
86         }
87         if (object.lookup(TreePathHandle.class)!=null)
88             return true;
89         
90         return a;
91     }
92 }
93
Popular Tags