KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > copy > SwitchToAction


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.copy;
21
22 import java.io.File JavaDoc;
23 import org.netbeans.modules.subversion.FileInformation;
24 import org.netbeans.modules.subversion.FileStatusCache;
25 import org.netbeans.modules.subversion.RepositoryFile;
26 import org.netbeans.modules.subversion.Subversion;
27 import org.netbeans.modules.subversion.client.SvnClient;
28 import org.netbeans.modules.subversion.client.SvnProgressSupport;
29 import org.netbeans.modules.subversion.ui.actions.ContextAction;
30 import org.netbeans.modules.subversion.util.Context;
31 import org.netbeans.modules.subversion.util.SvnUtils;
32 import org.netbeans.modules.versioning.util.Utils;
33 import org.openide.ErrorManager;
34 import org.openide.nodes.Node;
35 import org.tigris.subversion.svnclientadapter.SVNClientException;
36 import org.tigris.subversion.svnclientadapter.SVNRevision;
37 import org.tigris.subversion.svnclientadapter.SVNUrl;
38
39 /**
40  *
41  * @author Petr Kuzel
42  */

43 public class SwitchToAction extends ContextAction {
44     
45     /**
46      * Creates a new instance of SwitchToAction
47      */

48     public SwitchToAction() {
49     }
50
51     protected String JavaDoc getBaseName(Node[] activatedNodes) {
52         return "CTL_MenuItem_Switch"; // NOI18N
53
}
54
55     protected int getFileEnabledStatus() {
56         return FileInformation.STATUS_MANAGED
57              & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED
58              & ~FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY;
59     }
60
61     protected int getDirectoryEnabledStatus() {
62         return FileInformation.STATUS_MANAGED
63              & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED
64              & ~FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY;
65     }
66
67     protected boolean enable(Node[] nodes) {
68         return nodes != null && nodes.length == 1 && getContext(nodes).getRoots().size() > 0;
69     }
70     
71     protected void performContextAction(final Node[] nodes) {
72         Context ctx = getContext(nodes);
73         
74         final File JavaDoc root = ctx.getRootFiles()[0];
75         SVNUrl url = SvnUtils.getRepositoryRootUrl(root);
76         final RepositoryFile repositoryRoot = new RepositoryFile(url, url, SVNRevision.HEAD);
77         File JavaDoc[] files = Subversion.getInstance().getStatusCache().listFiles(ctx, FileInformation.STATUS_LOCAL_CHANGE);
78         boolean hasChanges = files.length > 0;
79
80         final SwitchTo switchTo = new SwitchTo(repositoryRoot, root, hasChanges);
81         if(switchTo.showDialog()) {
82             ContextAction.ProgressSupport support = new ContextAction.ProgressSupport(this, nodes) {
83                 public void perform() {
84                     RepositoryFile toRepositoryFile = switchTo.getRepositoryFile();
85                     performSwitch(toRepositoryFile, root, this);
86                 }
87             };
88             support.start(createRequestProcessor(nodes));
89         }
90     }
91
92     static void performSwitch(RepositoryFile toRepositoryFile, File JavaDoc root, SvnProgressSupport support) {
93         File JavaDoc[][] split = Utils.splitFlatOthers(new File JavaDoc[] {root} );
94         boolean recursive;
95         // there can be only 1 root file
96
if(split[0].length > 0) {
97             recursive = false;
98         } else {
99             recursive = true;
100         }
101
102         try {
103             SvnClient client;
104             try {
105                 client = Subversion.getInstance().getClient(toRepositoryFile.getRepositoryUrl());
106             } catch (SVNClientException ex) {
107                 ErrorManager.getDefault().notify(ex);
108                 return;
109             }
110             // ... and switch
111
client.switchToUrl(root, toRepositoryFile.getFileUrl(), toRepositoryFile.getRevision(), recursive);
112             // XXX this is ugly and expensive! the client should notify (onNotify()) the cache. find out why it doesn't work...
113
refreshRecursively(root); // XXX the same for another implementations like this in the code.... (see SvnUtils.refreshRecursively() )
114
Subversion.getInstance().refreshAllAnnotations();
115         } catch (SVNClientException ex) {
116             support.annotate(ex);
117         }
118     }
119
120     private static void refreshRecursively(File JavaDoc file) {
121         Subversion.getInstance().getStatusCache().refresh(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN);
122         if(!file.isFile()) {
123             File JavaDoc[] files = file.listFiles();
124             for (int i = 0; i < files.length; i++) {
125                 refreshRecursively(files[i]);
126             }
127         }
128     }
129 }
130
Popular Tags