KickJava   Java API By Example, From Geeks To Geeks.

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


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.java.plugins;
20 import java.io.IOException JavaDoc;
21 import java.text.MessageFormat JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23 import org.netbeans.api.fileinfo.NonRecursiveFolder;
24 import org.netbeans.api.java.classpath.ClassPath;
25 import org.netbeans.api.java.classpath.ClassPath;
26 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
27 import org.netbeans.modules.refactoring.spi.Transaction;
28 import org.netbeans.modules.refactoring.api.Problem;
29 import org.netbeans.modules.refactoring.api.RefactoringSession;
30 import org.netbeans.modules.refactoring.api.RenameRefactoring;
31 import org.netbeans.modules.refactoring.java.RetoucheUtils;
32 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
33 import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
34 import org.netbeans.modules.refactoring.spi.RefactoringPluginFactory;
35 import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImpl;
36 import org.openide.ErrorManager;
37 import org.openide.filesystems.FileObject;
38 import org.openide.loaders.DataFolder;
39 import org.openide.loaders.DataObject;
40 import org.openide.text.PositionBounds;
41 import org.openide.util.NbBundle;
42
43 /**
44  *
45  * @author Jan Becicka
46  */

47 public class PackageRename implements RefactoringPluginFactory{
48     
49     /** Creates a new instance of PackageRename */
50     public PackageRename() {
51     }
52     
53     public RefactoringPlugin createInstance(AbstractRefactoring refactoring) {
54         if (refactoring instanceof RenameRefactoring) {
55             if (refactoring.getRefactoringSource().lookup(NonRecursiveFolder.class)!=null) {
56                 return new PackageRenamePlugin((RenameRefactoring) refactoring);
57             }
58         }
59         return null;
60     }
61     
62     public class PackageRenamePlugin implements RefactoringPlugin {
63         private RenameRefactoring refactoring;
64         
65         /** Creates a new instance of PackageRenamePlugin */
66         public PackageRenamePlugin(RenameRefactoring refactoring) {
67             this.refactoring = refactoring;
68         }
69         
70         public Problem preCheck() {
71             return null;
72         }
73         
74         public Problem prepare(RefactoringElementsBag elements) {
75             elements.add(refactoring, new RenameNonRecursiveFolder(refactoring.getRefactoringSource().lookup(NonRecursiveFolder.class), elements));
76             return null;
77         }
78         
79         public Problem fastCheckParameters() {
80             String JavaDoc newName = refactoring.getNewName();
81             if (!RetoucheUtils.isValidPackageName(newName)) {
82                 String JavaDoc msg = new MessageFormat JavaDoc(NbBundle.getMessage(RenameRefactoringPlugin.class, "ERR_InvalidPackage")).format(
83                         new Object JavaDoc[] {newName}
84                 );
85                 return new Problem(true, msg);
86             }
87             
88             ClassPath projectClassPath = ClassPath.getClassPath(refactoring.getRefactoringSource().lookup(NonRecursiveFolder.class).getFolder(), ClassPath.SOURCE);
89             if (projectClassPath.findResource(newName.replace('.','/'))!=null) {
90                 String JavaDoc msg = new MessageFormat JavaDoc(NbBundle.getMessage(RenameRefactoringPlugin.class,"ERR_PackageExists")).format(
91                         new Object JavaDoc[] {newName}
92                 );
93                 return new Problem(true, msg);
94             }
95             return null;
96         }
97         
98         public Problem checkParameters() {
99             return null;
100         }
101         
102         public void cancelRequest() {
103         }
104         
105         private class RenameNonRecursiveFolder extends SimpleRefactoringElementImpl {
106             
107             private FileObject folder;
108             private RefactoringElementsBag session;
109             private String JavaDoc oldName;
110             private FileObject root;
111             private DataFolder dataFolder;
112             
113             
114             public RenameNonRecursiveFolder(NonRecursiveFolder nrfo, RefactoringElementsBag session) {
115                 this.folder = nrfo.getFolder();
116                 this.session = session;
117                 ClassPath cp = ClassPath.getClassPath(
118                         folder, ClassPath.SOURCE);
119                 this.oldName = cp.getResourceName(folder, '.', false);
120                 this.root = cp.findOwnerRoot(folder);
121                 dataFolder = DataFolder.findFolder(folder);
122                 
123             }
124             
125             public String JavaDoc getText() {
126                 return "Rename file " + folder.getNameExt();
127             }
128             
129             public String JavaDoc getDisplayText() {
130                 return getText();
131             }
132             
133             public void performChange() {
134                 session.registerFileChange(new Transaction() {
135                     public void commit() {
136                         setName();
137                     }
138                     
139                     public void rollback() {
140                         throw new UnsupportedOperationException JavaDoc("not implemented");
141                     }
142                 });
143             }
144             
145             public Object JavaDoc getComposite() {
146                 return folder.getParent();
147             }
148             
149             public FileObject getParentFile() {
150                 return folder.getParent();
151             }
152             
153             public PositionBounds getPosition() {
154                 return null;
155             }
156             
157             /**
158              *copy paste from PackageViewChildren
159              */

160             public void setName() {
161                 String JavaDoc name = refactoring.getNewName();
162                 if (oldName.equals(name)) {
163                     return;
164                 }
165 // if (!isValidPackageName (name)) {
166
// DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message (
167
// NbBundle.getMessage(PackageViewChildren.class,"MSG_InvalidPackageName"), NotifyDescriptor.INFORMATION_MESSAGE));
168
// return;
169
// }
170
name = name.replace('.','/')+'/'; //NOI18N
171
oldName = oldName.replace('.','/')+'/'; //NOI18N
172
int i;
173                 for (i=0; i<oldName.length() && i< name.length(); i++) {
174                     if (oldName.charAt(i) != name.charAt(i)) {
175                         break;
176                     }
177                 }
178                 i--;
179                 int index = oldName.lastIndexOf('/',i); //NOI18N
180
String JavaDoc commonPrefix = index == -1 ? null : oldName.substring(0,index);
181                 String JavaDoc toCreate = (index+1 == name.length()) ? "" : name.substring(index+1); //NOI18N
182
try {
183                     FileObject commonFolder = commonPrefix == null ? this.root : this.root.getFileObject(commonPrefix);
184                     FileObject destination = commonFolder;
185                     StringTokenizer JavaDoc dtk = new StringTokenizer JavaDoc(toCreate,"/"); //NOI18N
186
while (dtk.hasMoreTokens()) {
187                         String JavaDoc pathElement = dtk.nextToken();
188                         FileObject tmp = destination.getFileObject(pathElement);
189                         if (tmp == null) {
190                             tmp = destination.createFolder(pathElement);
191                         }
192                         destination = tmp;
193                     }
194                     FileObject source = this.dataFolder.getPrimaryFile();
195                     DataFolder sourceFolder = DataFolder.findFolder(source);
196                     DataFolder destinationFolder = DataFolder.findFolder(destination);
197                     DataObject[] children = sourceFolder.getChildren();
198                     for (int j=0; j<children.length; j++) {
199                         if (children[j].getPrimaryFile().isData()) {
200                             children[j].move(destinationFolder);
201                         }
202                     }
203                     while (!commonFolder.equals(source)) {
204                         if (source.getChildren().length==0) {
205                             FileObject tmp = source;
206                             source = source.getParent();
207                             tmp.delete();
208                         } else {
209                             break;
210                         }
211                     }
212                 } catch (IOException JavaDoc ioe) {
213                     ErrorManager.getDefault().notify(ioe);
214                 }
215             }
216         }
217     }
218 }
219
Popular Tags