1 19 package org.netbeans.modules.refactoring.plugins; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import org.netbeans.modules.refactoring.spi.Transaction; 24 import org.netbeans.modules.refactoring.api.Problem; 25 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring; 26 import org.netbeans.modules.refactoring.spi.BackupFacility; 27 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 28 import org.netbeans.modules.refactoring.spi.RefactoringPlugin; 29 import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImpl; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.FileUtil; 32 import org.openide.loaders.DataObject; 33 import org.openide.loaders.DataObjectNotFoundException; 34 import org.openide.text.PositionBounds; 35 36 37 41 public class FileDeletePlugin implements RefactoringPlugin { 42 private SafeDeleteRefactoring refactoring; 43 44 45 public FileDeletePlugin(SafeDeleteRefactoring refactoring) { 46 this.refactoring = refactoring; 47 } 48 49 public Problem preCheck() { 50 return null; 51 } 52 53 public Problem prepare(RefactoringElementsBag elements) { 54 for (FileObject fo: refactoring.getRefactoringSource().lookupAll(FileObject.class)) { 55 elements.add(refactoring, new DeleteFile(fo, elements)); 56 } 57 return null; 58 } 59 60 public Problem fastCheckParameters() { 61 return null; 62 } 63 64 public Problem checkParameters() { 65 return null; 66 } 67 68 public void cancelRequest() { 69 } 70 71 private class DeleteFile extends SimpleRefactoringElementImpl { 72 73 private FileObject fo; 74 private RefactoringElementsBag session; 75 public DeleteFile(FileObject fo, RefactoringElementsBag session) { 76 this.fo = fo; 77 this.session = session; 78 } 79 public String getText() { 80 return "Delete file " + fo.getNameExt(); 81 } 82 83 public String getDisplayText() { 84 return getText(); 85 } 86 87 public void performChange() { 88 session.registerFileChange(new Transaction() { 89 BackupFacility.Handle id; 90 public void commit () { 91 try { 92 if (!fo.isValid()) { 93 fo = FileUtil.toFileObject(new File (fo.getPath())); 94 } 95 96 id = BackupFacility.getDefault().backup(fo); 97 DataObject.find(fo).delete(); 98 } catch (DataObjectNotFoundException ex) { 99 ex.printStackTrace(); 100 } catch (IOException ex) { 101 ex.printStackTrace(); 102 } 103 } 104 105 public void rollback() { 106 try { 107 id.restore(); 108 } catch (IOException ex) { 109 ex.printStackTrace(); 110 } 111 } 112 }); 113 } 114 115 public Object getComposite() { 116 return fo; 117 } 118 119 public FileObject getParentFile() { 120 return fo; 121 } 122 123 public PositionBounds getPosition() { 124 return null; 125 } 126 } 127 128 } 129 | Popular Tags |