1 19 20 package org.netbeans.modules.versioning.system.cvss.ui.actions.update; 21 22 import org.netbeans.modules.versioning.system.cvss.ui.actions.AbstractSystemAction; 23 import org.netbeans.modules.versioning.system.cvss.*; 24 import org.netbeans.modules.versioning.system.cvss.util.Utils; 25 import org.netbeans.lib.cvsclient.file.FileUtils; 26 import org.netbeans.lib.cvsclient.admin.Entry; 27 import org.netbeans.lib.cvsclient.admin.AdminHandler; 28 import org.openide.util.NbBundle; 29 import org.openide.util.RequestProcessor; 30 import org.openide.ErrorManager; 31 import org.openide.DialogDisplayer; 32 import org.openide.NotifyDescriptor; 33 import org.openide.nodes.Node; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileLock; 37 38 import java.io.*; 39 import java.util.logging.Logger ; 40 41 46 public class GetCleanAction extends AbstractSystemAction { 47 48 protected String getBaseName(Node [] activatedNodes) { 49 return "CTL_MenuItem_GetClean"; } 51 52 protected boolean enable(Node[] nodes) { 53 return CvsVersioningSystem.getInstance().getFileTableModel(Utils.getCurrentContext(nodes), FileInformation.STATUS_LOCAL_CHANGE).getNodes().length > 0; 54 } 55 56 protected boolean asynchronous() { 57 return false; 58 } 59 60 public void performCvsAction(final Node[] nodes) { 61 if (!confirmed(null, null)) return; 62 RequestProcessor.getDefault().post(new Runnable () { 63 public void run() { 64 revertModifications(nodes); 65 } 66 }); 67 } 68 69 private static boolean confirmed(File file, String revision) { 70 String message; 71 if (file == null || revision == null) { 72 message = NbBundle.getMessage(GetCleanAction.class, "CTL_RevertModifications_Prompt"); 73 } else { 74 message = NbBundle.getMessage(GetCleanAction.class, "CTL_RevertModifications_Prompt2", file.getName(), revision); 75 } 76 NotifyDescriptor descriptor = new NotifyDescriptor( 77 message, 78 NbBundle.getMessage(GetCleanAction.class, "CTL_RevertModifications_Title"), 79 NotifyDescriptor.YES_NO_OPTION, 80 NotifyDescriptor.WARNING_MESSAGE, 81 null, 82 null 83 ); 84 Object option = DialogDisplayer.getDefault().notify(descriptor); 85 return option == NotifyDescriptor.YES_OPTION; 86 } 87 88 private void revertModifications(Node[] nodes) { 89 ExecutorGroup group = new ExecutorGroup(NbBundle.getMessage(GetCleanAction.class, "CTL_RevertModifications_Progress")); 90 try { 91 group.progress(NbBundle.getMessage(GetCleanAction.class, "CTL_RevertModifications_ProgressPrepare")); 92 FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache(); 93 File [] files = cache.listFiles(getContext(nodes), FileInformation.STATUS_LOCAL_CHANGE & FileInformation.STATUS_IN_REPOSITORY); 94 for (int i = 0; i < files.length; i++) { 95 File file = files[i]; 96 rollback(file, VersionsCache.REVISION_BASE, group); 97 } 98 for (int i = 0; i < files.length; i++) { 99 refresh(files[i]); 100 } 101 } finally { 102 group.executed(); 103 } 104 105 } 106 107 114 public static void rollback(File file, String revision) { 115 if (!confirmed(file, revision)) return; 116 rollback(file, revision, null); 117 refresh(file); 118 } 119 120 private static void refresh(File file) { 121 FileObject fo = FileUtil.toFileObject(file); 122 if (fo != null) { 123 fo.refresh(); 124 } 125 } 126 127 130 private static void rollback(File file, String revision, ExecutorGroup group) { 131 FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache(); 132 AdminHandler ah = CvsVersioningSystem.getInstance().getAdminHandler(); 133 Entry entry = null; 134 try { 135 entry = ah.getEntry(file); 136 } catch (IOException e) { 137 } 139 if ((entry == null || entry.isNewUserFile()) && revision.equals(VersionsCache.REVISION_BASE)) { 140 backup(file, entry); 141 file.delete(); 142 return; 143 } 144 try { 145 File cleanFile = VersionsCache.getInstance().getRemoteFile(file, revision, group); 146 if (cleanFile != null) { 147 backup(file, entry); 149 try { 150 FileObject target; 152 if (file.exists() == false) { 153 File dir = file.getParentFile(); 154 FileObject folder = Utils.mkfolders(dir); 155 target = folder.createData(file.getName()); 156 } else { 157 target = FileUtil.toFileObject(file); 158 } 159 InputStream in = null; 160 OutputStream out = null; 161 FileLock lock = null; 162 try { 163 in = new FileInputStream(cleanFile); 164 lock = target.lock(); 165 out = target.getOutputStream(lock); 166 copyStream(in, out); 167 } finally { 168 if (in != null) { 169 try { 170 in.close(); 171 } catch (IOException alreadyClosed) { 172 } 173 } 174 if (out != null) { 175 try { 176 out.close(); 177 } catch (IOException alreadyClosed) { 178 } 179 } 180 if (lock != null) { 181 lock.releaseLock(); 182 } 183 } 184 185 } finally { 186 } 188 if (entry != null && entry.isUserFileToBeRemoved()) { 189 entry.setRevision(entry.getRevision().substring(1)); 190 ah.setEntry(file, entry); 191 } 192 cache.refresh(file, revision == VersionsCache.REVISION_BASE ? FileStatusCache.REPOSITORY_STATUS_UPTODATE : FileStatusCache.REPOSITORY_STATUS_UNKNOWN); 193 } else { 195 if (group.isCancelled()) { 196 return; 197 } 198 Logger.getLogger(GetCleanAction.class.getName()).severe("Unable to revert changes in " + file.getName() + "; checkout failed"); 200 } 201 } catch (Exception e) { 202 if (e.getCause() instanceof InterruptedException ) { 203 } else { 205 ErrorManager.getDefault().notify(e); 206 } 207 } 208 } 209 210 private static void copyStream(InputStream in, OutputStream out) throws IOException { 211 byte [] buffer = new byte[4096]; 212 for (;;) { 213 int n = in.read(buffer, 0, 4096); 214 if (n < 0) return; 215 out.write(buffer, 0, n); 216 } 217 } 218 219 private static void backup(File file, Entry entry) { 220 if (!file.isFile()) return; try { 222 File backup; 223 if (entry != null) { 224 backup = new File(file.getParentFile(), ".#" + file.getName() + "." + entry.getRevision()); 225 } else { 226 backup = new File(file.getParentFile(), ".#" + file.getName() + "." + "LOCAL"); 227 } 228 FileUtils.copyFile(file, backup); 229 } catch (IOException e) { 230 } 232 } 233 } 234 | Popular Tags |