1 19 package org.netbeans.modules.refactoring.java.plugins; 20 import java.io.IOException ; 21 import java.text.MessageFormat ; 22 import java.util.StringTokenizer ; 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 47 public class PackageRename implements RefactoringPluginFactory{ 48 49 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 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 newName = refactoring.getNewName(); 81 if (!RetoucheUtils.isValidPackageName(newName)) { 82 String msg = new MessageFormat (NbBundle.getMessage(RenameRefactoringPlugin.class, "ERR_InvalidPackage")).format( 83 new Object [] {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 msg = new MessageFormat (NbBundle.getMessage(RenameRefactoringPlugin.class,"ERR_PackageExists")).format( 91 new Object [] {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 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 getText() { 126 return "Rename file " + folder.getNameExt(); 127 } 128 129 public String 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 ("not implemented"); 141 } 142 }); 143 } 144 145 public Object 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 160 public void setName() { 161 String name = refactoring.getNewName(); 162 if (oldName.equals(name)) { 163 return; 164 } 165 name = name.replace('.','/')+'/'; oldName = oldName.replace('.','/')+'/'; 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); String commonPrefix = index == -1 ? null : oldName.substring(0,index); 181 String toCreate = (index+1 == name.length()) ? "" : name.substring(index+1); try { 183 FileObject commonFolder = commonPrefix == null ? this.root : this.root.getFileObject(commonPrefix); 184 FileObject destination = commonFolder; 185 StringTokenizer dtk = new StringTokenizer (toCreate,"/"); while (dtk.hasMoreTokens()) { 187 String 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 ioe) { 213 ErrorManager.getDefault().notify(ioe); 214 } 215 } 216 } 217 } 218 } 219 | Popular Tags |