1 19 package org.netbeans.modules.refactoring.plugins; 20 21 import java.io.IOException ; 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 38 public class FileRenamePlugin implements RefactoringPlugin { 39 private RenameRefactoring refactoring; 40 41 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 getText() { 75 return "Rename file " + fo.getNameExt(); 76 } 77 78 public String getDisplayText() { 79 return getText(); 80 } 81 82 public void performChange() { 83 bag.registerFileChange(new Transaction() { 84 private String 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 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 ex) { 102 ex.printStackTrace(); 103 } 104 } 105 }); 106 } 107 108 public Object 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 |