KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > plugins > FileHandlingFactory


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.plugins;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URISyntaxException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Collection JavaDoc;
26 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
27 import org.netbeans.modules.refactoring.api.MoveRefactoring;
28 import org.netbeans.modules.refactoring.api.RenameRefactoring;
29 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
30 import org.netbeans.modules.refactoring.api.SingleCopyRefactoring;
31 import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
32 import org.netbeans.modules.refactoring.spi.RefactoringPluginFactory;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.filesystems.URLMapper;
36
37 /**
38  *
39  * @author Jan Becicka
40  */

41 public class FileHandlingFactory implements RefactoringPluginFactory {
42    
43     public RefactoringPlugin createInstance(AbstractRefactoring refactoring) {
44         Collection JavaDoc<? extends FileObject> o = refactoring.getRefactoringSource().lookupAll(FileObject.class);
45         if (refactoring instanceof RenameRefactoring) {
46             if (!o.isEmpty()) {
47                 return new FileRenamePlugin((RenameRefactoring) refactoring);
48             }
49         } else if (refactoring instanceof MoveRefactoring) {
50             if (!o.isEmpty()) {
51                 return new FileMovePlugin((MoveRefactoring) refactoring);
52             }
53         } else if (refactoring instanceof SafeDeleteRefactoring) {
54             if (!o.isEmpty()) {
55                 return new FileDeletePlugin((SafeDeleteRefactoring) refactoring);
56             }
57         } else if (refactoring instanceof SingleCopyRefactoring) {
58             if (!o.isEmpty()) {
59                 return new FileCopyPlugin((SingleCopyRefactoring) refactoring);
60             }
61         }
62         return null;
63     }
64     
65         /**
66      * creates or finds FileObject according to
67      * @param url
68      * @return FileObject
69      */

70     static FileObject getOrCreateFolder(URL JavaDoc url) throws IOException JavaDoc {
71         try {
72             FileObject result = URLMapper.findFileObject(url);
73             if (result != null)
74                 return result;
75             File JavaDoc f = new File JavaDoc(url.toURI());
76             
77             result = FileUtil.createFolder(f);
78             return result;
79         } catch (URISyntaxException JavaDoc ex) {
80             throw (IOException JavaDoc) new IOException JavaDoc().initCause(ex);
81         }
82     }
83
84 }
85
Popular Tags