KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > update > GetCleanAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
40
41 /**
42  * Revert modifications action.
43  *
44  * @author Maros Sandor
45  */

46 public class GetCleanAction extends AbstractSystemAction {
47
48     protected String JavaDoc getBaseName(Node [] activatedNodes) {
49         return "CTL_MenuItem_GetClean"; // NOI18N
50
}
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 JavaDoc() {
63             public void run() {
64                 revertModifications(nodes);
65             }
66         });
67     }
68
69     private static boolean confirmed(File file, String JavaDoc revision) {
70         String JavaDoc 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 JavaDoc 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     /**
108      * Overwrites given file with its specified revision. Revision number and sticky information in Entries is NOT modified,
109      * only the content is overwritten.
110      *
111      * @param file the file to overwrite
112      * @param revision revision to get
113      */

114     public static void rollback(File file, String JavaDoc 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     /**
128      * If the revision is BASE and there is no Entry for the file, then the file is backed up and deleted.
129      */

130     private static void rollback(File file, String JavaDoc 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             // non-fatal, we have no entry for this file
138
}
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                 // 'atomic' action >>>
148
backup(file, entry);
149                 try {
150 // CvsVersioningSystem.ignoreFilesystemEvents(true);
151
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 // CvsVersioningSystem.ignoreFilesystemEvents(false);
187
}
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                 // 'atomic' action <<<
194
} else {
195                 if (group.isCancelled()) {
196                     return;
197                 }
198                 // locally delete? NOt yet there seems to be bug in checkout -p
199
Logger.getLogger(GetCleanAction.class.getName()).severe("Unable to revert changes in " + file.getName() + "; checkout failed");
200             }
201         } catch (Exception JavaDoc e) {
202             if (e.getCause() instanceof InterruptedException JavaDoc) {
203                 // command aborted
204
} 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; // nothing to backup (avoid creating unnecessary directories in FileUtils.copyFile)
221
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             // ignore
231
}
232     }
233 }
234
Popular Tags