1 19 package org.netbeans.modules.localhistory.ui.actions; 20 21 import org.netbeans.modules.localhistory.ui.view.ShowLocalHistoryAction; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.util.Set ; 27 import org.netbeans.modules.localhistory.LocalHistory; 28 import org.netbeans.modules.localhistory.store.StoreEntry; 29 import org.netbeans.modules.versioning.spi.VCSContext; 30 import org.netbeans.modules.versioning.util.FlatFolder; 31 import org.openide.ErrorManager; 32 import org.openide.filesystems.FileAlreadyLockedException; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.nodes.Node; 36 import org.openide.util.HelpCtx; 37 import org.openide.util.NbBundle; 38 import org.openide.util.RequestProcessor; 39 import org.openide.util.actions.NodeAction; 40 41 44 public class RevertDeletedAction extends NodeAction { 45 46 47 public RevertDeletedAction() { 48 setIcon(null); 49 putValue("noIconInMenu", Boolean.TRUE); } 51 52 protected void performAction(final Node[] activatedNodes) { 53 54 RequestProcessor.getDefault().post(new Runnable () { 55 public void run() { 56 VCSContext ctx = VCSContext.forNodes(activatedNodes); 57 Set <File > rootSet = ctx.getRootFiles(); 58 if(rootSet == null || rootSet.size() < 1) { 59 return; 60 } 61 for (File file : rootSet) { 62 if(file instanceof FlatFolder) { 63 revert(file); 64 } else { 65 revertRecursively(file); 66 } 67 } 68 } 69 }); 70 } 71 72 private void revertRecursively(File file) { 73 revert(file); 74 File [] files = file.listFiles(); 75 for(File f : files) { 76 if(f.isDirectory()) { 77 revertRecursively(f); 78 } 79 } 80 } 81 82 private void revert(File file) { 83 StoreEntry[] entries = LocalHistory.getInstance().getLocalHistoryStore().getDeletedFiles(file); 84 for(StoreEntry se : entries) { 85 revert(se); 86 } 87 } 88 89 protected boolean enable(Node[] activatedNodes) { 90 VCSContext ctx = VCSContext.forNodes(activatedNodes); 91 Set <File > rootSet = ctx.getRootFiles(); 92 if(rootSet == null || rootSet.size() < 1) { 93 return false; 94 } 95 for (File file : rootSet) { 96 if(file != null && !file.isDirectory()) { 97 return false; 98 } 99 } 100 return true; 101 } 102 103 public String getName() { 104 return NbBundle.getMessage(this.getClass(), "CTL_ShowRevertDeleted"); 105 } 106 107 public HelpCtx getHelpCtx() { 108 return new HelpCtx(ShowLocalHistoryAction.class); 109 } 110 111 private static void revert(StoreEntry se) { 112 File file = se.getFile(); 113 File storeFile = se.getStoreFile(); 114 115 InputStream is = null; 116 OutputStream os = null; 117 try { 118 if(!storeFile.isFile()) { 119 FileUtil.createFolder(file); 120 } else { 121 FileObject fo = FileUtil.createData(file); 122 123 os = getOutputStream(fo); 124 is = se.getStoreFileInputStream(); 125 FileUtil.copy(is, os); 126 } 127 } catch (Exception e) { 128 ErrorManager.getDefault().notify(ErrorManager.ERROR, e); 129 return; 130 } finally { 131 try { 132 if(os != null) { os.close(); } 133 if(is != null) { is.close(); } 134 } catch (IOException e) {} 135 } 136 } 137 138 private static OutputStream getOutputStream(FileObject fo) throws FileAlreadyLockedException, IOException , InterruptedException { 139 int retry = 0; 140 while (true) { 141 try { 142 return fo.getOutputStream(); 143 } catch (IOException ioe) { 144 retry++; 145 if (retry > 7) { 146 throw ioe; 147 } 148 Thread.sleep(retry * 30); 149 } 150 } 151 } 152 153 } 154 | Popular Tags |