KickJava   Java API By Example, From Geeks To Geeks.

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


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.spi.Transaction;
24 import org.netbeans.modules.refactoring.api.MoveRefactoring;
25 import org.netbeans.modules.refactoring.api.Problem;
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.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.loaders.DataFolder;
32 import org.openide.loaders.DataObject;
33 import org.openide.loaders.DataObjectNotFoundException;
34 import org.openide.text.PositionBounds;
35
36 /**
37  *
38  * @author Jan Becicka
39  */

40 public class FileMovePlugin implements RefactoringPlugin {
41     private MoveRefactoring refactoring;
42     
43     /** Creates a new instance of WhereUsedQuery */
44     public FileMovePlugin(MoveRefactoring refactoring) {
45         this.refactoring = refactoring;
46     }
47     
48     public Problem preCheck() {
49         return null;
50     }
51     
52     public Problem prepare(RefactoringElementsBag elements) {
53         for (FileObject o: refactoring.getRefactoringSource().lookupAll(FileObject.class)) {
54                 elements.add(refactoring, new MoveFile(o, elements));
55         }
56         return null;
57     }
58     
59     public Problem fastCheckParameters() {
60         return null;
61     }
62         
63     public Problem checkParameters() {
64         return null;
65     }
66
67     public void cancelRequest() {
68     }
69     
70     private class MoveFile extends SimpleRefactoringElementImpl {
71         
72         private FileObject fo;
73         private RefactoringElementsBag session;
74         public MoveFile(FileObject fo, RefactoringElementsBag session) {
75             this.fo = fo;
76             this.session = session;
77         }
78         public String JavaDoc getText() {
79             return "Move file " + fo.getNameExt();
80         }
81
82         public String JavaDoc getDisplayText() {
83             return getText();
84         }
85
86         public void performChange() {
87             session.registerFileChange(new Transaction() {
88                 DataFolder sourceFolder;
89                 DataObject source;
90                 public void commit() {
91                     try {
92                         FileObject target = FileHandlingFactory.getOrCreateFolder(refactoring.getTarget().lookup(URL JavaDoc.class));
93                         DataFolder targetFolder = DataFolder.findFolder(target);
94                         if (!fo.isValid()) {
95                             fo = FileUtil.toFileObject(FileUtil.toFile(fo));
96                         }
97                         source = DataObject.find(fo);
98                         sourceFolder = source.getFolder();
99                         source.move(targetFolder);
100                     } catch (DataObjectNotFoundException ex) {
101                         ex.printStackTrace();
102                     } catch (IOException JavaDoc ex) {
103                         ex.printStackTrace();
104                     }
105                 }
106                 public void rollback() {
107                     try {
108                         source.move(sourceFolder);
109                     } catch (DataObjectNotFoundException ex) {
110                         ex.printStackTrace();
111                     } catch (IOException JavaDoc ex) {
112                         ex.printStackTrace();
113                     }
114                 }
115             });
116          }
117
118         public Object JavaDoc getComposite() {
119             return fo;
120         }
121
122         public FileObject getParentFile() {
123             return fo;
124         }
125
126         public PositionBounds getPosition() {
127             return null;
128         }
129     }
130 }
131
Popular Tags