1 19 package org.netbeans.modules.refactoring.plugins; 20 21 import java.io.IOException ; 22 import java.net.URL ; 23 import org.netbeans.modules.refactoring.spi.Transaction; 24 import org.netbeans.modules.refactoring.api.MoveRefactoring; 25 import org.netbeans.modules.refactoring.api.Problem; 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.filesystems.FileUtil; 31 import org.openide.loaders.DataFolder; 32 import org.openide.loaders.DataObject; 33 import org.openide.loaders.DataObjectNotFoundException; 34 import org.openide.text.PositionBounds; 35 36 40 public class FileMovePlugin implements RefactoringPlugin { 41 private MoveRefactoring refactoring; 42 43 44 public FileMovePlugin(MoveRefactoring refactoring) { 45 this.refactoring = refactoring; 46 } 47 48 public Problem preCheck() { 49 return null; 50 } 51 52 public Problem prepare(RefactoringElementsBag elements) { 53 for (FileObject o: refactoring.getRefactoringSource().lookupAll(FileObject.class)) { 54 elements.add(refactoring, new MoveFile(o, elements)); 55 } 56 return null; 57 } 58 59 public Problem fastCheckParameters() { 60 return null; 61 } 62 63 public Problem checkParameters() { 64 return null; 65 } 66 67 public void cancelRequest() { 68 } 69 70 private class MoveFile extends SimpleRefactoringElementImpl { 71 72 private FileObject fo; 73 private RefactoringElementsBag session; 74 public MoveFile(FileObject fo, RefactoringElementsBag session) { 75 this.fo = fo; 76 this.session = session; 77 } 78 public String getText() { 79 return "Move file " + fo.getNameExt(); 80 } 81 82 public String getDisplayText() { 83 return getText(); 84 } 85 86 public void performChange() { 87 session.registerFileChange(new Transaction() { 88 DataFolder sourceFolder; 89 DataObject source; 90 public void commit() { 91 try { 92 FileObject target = FileHandlingFactory.getOrCreateFolder(refactoring.getTarget().lookup(URL .class)); 93 DataFolder targetFolder = DataFolder.findFolder(target); 94 if (!fo.isValid()) { 95 fo = FileUtil.toFileObject(FileUtil.toFile(fo)); 96 } 97 source = DataObject.find(fo); 98 sourceFolder = source.getFolder(); 99 source.move(targetFolder); 100 } catch (DataObjectNotFoundException ex) { 101 ex.printStackTrace(); 102 } catch (IOException ex) { 103 ex.printStackTrace(); 104 } 105 } 106 public void rollback() { 107 try { 108 source.move(sourceFolder); 109 } catch (DataObjectNotFoundException ex) { 110 ex.printStackTrace(); 111 } catch (IOException ex) { 112 ex.printStackTrace(); 113 } 114 } 115 }); 116 } 117 118 public Object getComposite() { 119 return fo; 120 } 121 122 public FileObject getParentFile() { 123 return fo; 124 } 125 126 public PositionBounds getPosition() { 127 return null; 128 } 129 } 130 } 131 | Popular Tags |