KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.modules.refactoring.spi.Transaction;
23 import org.netbeans.modules.refactoring.api.Problem;
24 import org.netbeans.modules.refactoring.api.RefactoringSession;
25 import org.netbeans.modules.refactoring.api.RenameRefactoring;
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.loaders.DataObject;
31 import org.openide.loaders.DataObjectNotFoundException;
32 import org.openide.text.PositionBounds;
33
34 /**
35  *
36  * @author Jan Becicka
37  */

38 public class FileRenamePlugin implements RefactoringPlugin {
39     private RenameRefactoring refactoring;
40     
41     /** Creates a new instance of WhereUsedQuery */
42     public FileRenamePlugin(RenameRefactoring refactoring) {
43         this.refactoring = refactoring;
44     }
45     
46     public Problem preCheck() {
47         return null;
48     }
49     
50     public Problem prepare(RefactoringElementsBag elements) {
51         elements.add(refactoring, new RenameFile(refactoring.getRefactoringSource().lookup(FileObject.class), elements));
52         return null;
53     }
54     
55     public Problem fastCheckParameters() {
56         return null;
57     }
58         
59     public Problem checkParameters() {
60         return null;
61     }
62
63     public void cancelRequest() {
64     }
65     
66     private class RenameFile extends SimpleRefactoringElementImpl {
67         
68         private FileObject fo;
69         private RefactoringElementsBag bag;
70         public RenameFile(FileObject fo, RefactoringElementsBag bag) {
71             this.fo = fo;
72             this.bag = bag;
73         }
74         public String JavaDoc getText() {
75             return "Rename file " + fo.getNameExt();
76         }
77
78         public String JavaDoc getDisplayText() {
79             return getText();
80         }
81
82         public void performChange() {
83             bag.registerFileChange(new Transaction() {
84                 private String JavaDoc oldName;
85                 public void commit() {
86                     try {
87                         oldName = fo.getName();
88                         DataObject.find(fo).rename(refactoring.getNewName());
89                     } catch (DataObjectNotFoundException ex) {
90                         ex.printStackTrace();
91                     } catch (IOException JavaDoc ex) {
92                         ex.printStackTrace();
93                     }
94                 }
95                 
96                 public void rollback() {
97                     try {
98                         DataObject.find(fo).rename(oldName);
99                     } catch (DataObjectNotFoundException ex) {
100                         ex.printStackTrace();
101                     } catch (IOException JavaDoc ex) {
102                         ex.printStackTrace();
103                     }
104                 }
105             });
106          }
107
108         public Object JavaDoc getComposite() {
109             return fo;
110         }
111
112         public FileObject getParentFile() {
113             return fo;
114         }
115
116         public PositionBounds getPosition() {
117             return null;
118         }
119     }
120
121 }
122
Popular Tags