KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > update > RevertModificationsAction


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.subversion.ui.update;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.*;
25
26 import org.netbeans.modules.subversion.*;
27 import org.netbeans.modules.subversion.Subversion;
28 import org.netbeans.modules.subversion.client.SvnClient;
29 import org.netbeans.modules.subversion.client.SvnProgressSupport;
30 import org.netbeans.modules.subversion.ui.actions.ContextAction;
31 import org.netbeans.modules.subversion.util.*;
32 import org.netbeans.modules.subversion.util.Context;
33 import org.netbeans.modules.versioning.util.Utils;
34 import org.openide.ErrorManager;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.filesystems.FileStateInvalidException;
38 import org.openide.nodes.Node;
39 import org.tigris.subversion.svnclientadapter.ISVNInfo;
40 import org.tigris.subversion.svnclientadapter.SVNClientException;
41 import org.tigris.subversion.svnclientadapter.SVNRevision;
42 import org.tigris.subversion.svnclientadapter.SVNUrl;
43
44 /**
45  * Reverts local changes.
46  *
47  * @author Petr Kuzel
48  */

49 public class RevertModificationsAction extends ContextAction {
50     
51     /** Creates a new instance of RevertModificationsAction */
52     public RevertModificationsAction() {
53     }
54
55     protected String JavaDoc getBaseName(Node[] activatedNodes) {
56         return "CTL_MenuItem_Revert"; // NOI18N
57
}
58
59     protected int getFileEnabledStatus() {
60         return FileInformation.STATUS_VERSIONED & ~FileInformation.STATUS_VERSIONED_NEWINREPOSITORY;
61     }
62
63     protected int getDirectoryEnabledStatus() {
64         return FileInformation.STATUS_VERSIONED & ~FileInformation.STATUS_VERSIONED_NEWINREPOSITORY;
65     }
66
67     protected void performContextAction(final Node[] nodes) {
68         final Context ctx = getContext(nodes);
69         final File JavaDoc root = ctx.getRootFiles()[0];
70         final SVNUrl url = SvnUtils.getRepositoryRootUrl(root);
71         final RepositoryFile repositoryFile = new RepositoryFile(url, url, SVNRevision.HEAD);
72         
73         final RevertModifications revertModifications = new RevertModifications(repositoryFile);
74         if(!revertModifications.showDialog()) {
75             return;
76         }
77
78         ContextAction.ProgressSupport support = new ContextAction.ProgressSupport(this, nodes) {
79             public void perform() {
80                 performRevert(ctx, revertModifications, this);
81             }
82         };
83         support.start(createRequestProcessor(nodes));
84     }
85         
86     /** Recursive revert */
87     public static void performRevert(Context ctx, RevertModifications revertModifications, SvnProgressSupport support) {
88         SvnClient client;
89         try {
90             client = Subversion.getInstance().getClient(ctx, support);
91         } catch (SVNClientException ex) {
92             ErrorManager.getDefault().notify(ex);
93             return;
94         }
95
96         File JavaDoc files[] = ctx.getFiles();
97         File JavaDoc[][] split = Utils.splitFlatOthers(files);
98         for (int c = 0; c<split.length; c++) {
99             if(support.isCanceled()) {
100                 return;
101             }
102             files = split[c];
103             boolean recursive = c == 1;
104             if (recursive == false) {
105                 files = SvnUtils.flatten(files, FileInformation.STATUS_REVERTIBLE_CHANGE);
106             }
107
108             try {
109                 RevertModifications.RevisionInterval revisions = revertModifications.getRevisionInterval();
110                 if(revisions != null) {
111                     for (int i= 0; i<files.length; i++) {
112                         if(support.isCanceled()) {
113                             return;
114                         }
115                         SVNUrl url = SvnUtils.getRepositoryUrl(files[i]);
116                         revisions = recountStartRevision(client, url, revisions);
117                         client.merge(url, revisions.endRevision, url, revisions.startRevision, files[i], false, recursive);
118                     }
119                 } else {
120                     if(support.isCanceled()) {
121                         return;
122                     }
123                     if(files.length > 0 ) {
124                         client.revert(files, recursive);
125                     }
126                 }
127             } catch (SVNClientException ex) {
128                 support.annotate(ex);
129             }
130         }
131
132         if(support.isCanceled()) {
133             return;
134         }
135                 
136         if(revertModifications.revertNewFiles()) {
137             File JavaDoc[] newfiles = Subversion.getInstance().getStatusCache().listFiles(ctx.getRootFiles(), FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY | FileInformation.STATUS_VERSIONED_ADDEDLOCALLY);
138             for (int i = 0; i < newfiles.length; i++) {
139                 FileObject fo = FileUtil.toFileObject(newfiles[i]);
140                 try {
141                     if(fo != null) {
142                         fo.delete();
143                     }
144                 }
145                 catch (IOException JavaDoc ex) {
146                     ErrorManager.getDefault().notify(ex);
147                 }
148             }
149         }
150     }
151
152     /**
153      * Folders that were resurrected by "Revert Delete" have not really been created because they already existed.
154      * Therefore we must refresh their status manually.
155      *
156      * @param file
157      */

158     private static void refreshRecursively(File JavaDoc file) {
159         File JavaDoc [] files = file.listFiles();
160         if (files != null) {
161             for (File JavaDoc child : files) {
162                 refreshRecursively(child);
163             }
164         }
165         FileStatusCache cache = Subversion.getInstance().getStatusCache();
166         cache.refreshCached(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN);
167     }
168
169     private static void addFileSystem(Set filesystems, File JavaDoc file) {
170         FileObject fo;
171         for (;;) {
172             fo = FileUtil.toFileObject(file);
173             if (fo != null) break;
174             file = file.getParentFile();
175             if (file == null) return;
176         }
177         try {
178             filesystems.add(fo.getFileSystem());
179         } catch (FileStateInvalidException e) {
180             // ignore invalid filesystems
181
}
182     }
183     
184     private static RevertModifications.RevisionInterval recountStartRevision(SvnClient client, SVNUrl repository, RevertModifications.RevisionInterval ret) throws SVNClientException {
185         if(ret.startRevision.equals(SVNRevision.HEAD)) {
186             ISVNInfo info = client.getInfo(repository);
187             ret.startRevision = info.getRevision();
188         }
189         long start = Long.parseLong(ret.startRevision.toString());
190         if(start > 0) {
191             start = start - 1;
192         }
193         ret.startRevision = new SVNRevision.Number(start);
194         return ret;
195     }
196
197 }
198
Popular Tags