KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
22 import java.net.URL JavaDoc;
23 import org.netbeans.modules.refactoring.api.SingleCopyRefactoring;
24 import org.netbeans.modules.refactoring.api.Problem;
25 import org.netbeans.modules.refactoring.api.RefactoringSession;
26 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
27 import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
28 import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImpl;
29 import org.openide.ErrorManager;
30 import org.openide.filesystems.FileObject;
31 import org.openide.loaders.DataFolder;
32 import org.openide.loaders.DataObject;
33 import org.openide.text.PositionBounds;
34
35 /**
36  *
37  * @author Jan Becicka
38  */

39 public class FileCopyPlugin implements RefactoringPlugin {
40     private SingleCopyRefactoring refactoring;
41     
42     /** Creates a new instance of WhereUsedQuery */
43     public FileCopyPlugin(SingleCopyRefactoring refactoring) {
44         this.refactoring = refactoring;
45     }
46     
47     public Problem preCheck() {
48         return null;
49     }
50     
51     public Problem prepare(RefactoringElementsBag elements) {
52         elements.add(refactoring, new CopyFile(refactoring.getRefactoringSource().lookup(FileObject.class), elements.getSession()));
53         return null;
54     }
55     
56     public Problem fastCheckParameters() {
57         return null;
58     }
59     
60     public Problem checkParameters() {
61         return null;
62     }
63     
64     public void cancelRequest() {
65     }
66     
67     private class CopyFile extends SimpleRefactoringElementImpl {
68         
69         private FileObject fo;
70         private RefactoringSession session;
71         private DataObject newOne;
72         public CopyFile(FileObject fo, RefactoringSession session) {
73             this.fo = fo;
74             this.session = session;
75         }
76         public String JavaDoc getText() {
77             return "Copy file " + fo.getNameExt();
78         }
79         
80         public String JavaDoc getDisplayText() {
81             return getText();
82         }
83         
84         public void performChange() {
85             try {
86                 FileObject fo = FileHandlingFactory.getOrCreateFolder(refactoring.getTarget().lookup(URL JavaDoc.class));
87                 FileObject source = refactoring.getRefactoringSource().lookup(FileObject.class);
88                 DataObject dob = DataObject.find(source);
89                 newOne = dob.copy(DataFolder.findFolder(fo));
90                 newOne.rename(refactoring.getNewName());
91                 refactoring.getContext().add(newOne.getPrimaryFile());
92             } catch (Exception JavaDoc ioe) {
93                 ErrorManager.getDefault().notify(ioe);
94             }
95         }
96         
97         @Override JavaDoc
98         public void undoChange() {
99             try {
100                 newOne.delete();
101             } catch (IOException JavaDoc ex) {
102                 ErrorManager.getDefault().notify(ex);
103             }
104         }
105         
106         public Object JavaDoc getComposite() {
107             return fo;
108         }
109         
110         public FileObject getParentFile() {
111             return fo;
112         }
113         
114         public PositionBounds getPosition() {
115             return null;
116         }
117     }
118 }
119
Popular Tags