KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
23 import org.netbeans.modules.subversion.ui.actions.ContextAction;
24 import org.netbeans.modules.subversion.util.Context;
25 import org.netbeans.modules.subversion.*;
26 import java.io.File JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import org.netbeans.modules.subversion.client.SvnClient;
30 import org.netbeans.modules.subversion.client.SvnProgressSupport;
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.openide.awt.StatusDisplayer;
36 import org.openide.util.RequestProcessor;
37 import org.tigris.subversion.svnclientadapter.ISVNStatus;
38 import org.tigris.subversion.svnclientadapter.SVNClientException;
39 import org.tigris.subversion.svnclientadapter.SVNRevision;
40 import org.tigris.subversion.svnclientadapter.SVNUrl;
41 import org.tigris.subversion.svnclientadapter.utils.SVNStatusUtils;
42
43 /**
44  * Update action
45  *
46  * @author Petr Kuzel
47  */

48 public class UpdateAction extends ContextAction {
49
50     protected String JavaDoc getBaseName(Node[] nodes) {
51         return "CTL_MenuItem_Update"; // NOI18N
52
}
53
54     protected int getFileEnabledStatus() {
55         return FileInformation.STATUS_IN_REPOSITORY;
56     }
57
58     protected int getDirectoryEnabledStatus() {
59         return FileInformation.STATUS_MANAGED
60              & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED
61              & ~FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY;
62     }
63     
64     protected void performContextAction(Node[] nodes) {
65         performUpdate(nodes);
66     }
67
68     public void performUpdate(final Node[] nodes) {
69         // FIXME add shalow logic allowing to ignore nested projects
70
// look into CVS, it's very tricky:
71
// project1/
72
// nbbuild/ (project1)
73
// project2/
74
// src/ (project1)
75
// test/ (project1 but imagine it's in repository, to be updated )
76
// Is there a way how to update project1 without updating project2?
77
final Context ctx = getContext(nodes);
78         ContextAction.ProgressSupport support = new ContextAction.ProgressSupport(this, nodes) {
79             public void perform() {
80                 update(ctx, this);
81             }
82         };
83         support.start(createRequestProcessor(nodes));
84     }
85
86     private static void update(Context ctx, SvnProgressSupport progress) {
87
88         File JavaDoc[] roots = ctx.getRootFiles();
89         final SVNUrl repositoryUrl = SvnUtils.getRepositoryRootUrl(roots[0]);
90         
91         FileStatusCache cache = Subversion.getInstance().getStatusCache();
92         cache.refreshCached(ctx);
93         File JavaDoc[][] split = Utils.splitFlatOthers(roots);
94         final List JavaDoc<File JavaDoc> recursiveFiles = new ArrayList JavaDoc<File JavaDoc>();
95         final List JavaDoc<File JavaDoc> flatFiles = new ArrayList JavaDoc<File JavaDoc>();
96         
97         // recursive files
98
for (int i = 0; i<split[1].length; i++) {
99             recursiveFiles.add(split[1][i]);
100         }
101         // flat files
102
//File[] flatRoots = SvnUtils.flatten(split[0], getDirectoryEnabledStatus());
103
for (int i= 0; i<split[0].length; i++) {
104             flatFiles.add(split[0][i]);
105         }
106         
107         
108         SvnClient client;
109         try {
110             client = Subversion.getInstance().getClient(repositoryUrl);
111         } catch (SVNClientException ex) {
112             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not hapen
113
return;
114         }
115
116         try {
117             updateRoots(recursiveFiles, progress, client, true);
118             if(progress.isCanceled()) {
119                 return;
120             }
121             updateRoots(flatFiles, progress, client, false);
122         } catch (SVNClientException e1) {
123             progress.annotate(e1);
124         }
125     }
126
127     private static void updateRoots(List JavaDoc<File JavaDoc> roots, SvnProgressSupport support, SvnClient client, boolean recursive) throws SVNClientException {
128         boolean conflict = false;
129 roots_loop:
130         for (Iterator JavaDoc<File JavaDoc> it = roots.iterator(); it.hasNext();) {
131             File JavaDoc root = it.next();
132             if(support.isCanceled()) {
133                 break;
134             }
135             client.update(root, SVNRevision.HEAD, recursive);
136             ISVNStatus status[] = client.getStatus(root, true, false);
137             for (int k = 0; k<status.length; k++) {
138                 ISVNStatus s = status[k];
139                 if (SVNStatusUtils.isTextConflicted(s) || SVNStatusUtils.isPropConflicted(s)) {
140                     conflict = true;
141                     break roots_loop;
142                 }
143             }
144         }
145         if (conflict) {
146             StatusDisplayer.getDefault().setStatusText(org.openide.util.NbBundle.getMessage(UpdateAction.class, "MSG_Update_Conflicts")); // NOI18N
147
} else {
148             StatusDisplayer.getDefault().setStatusText(org.openide.util.NbBundle.getMessage(UpdateAction.class, "MSG_Update_Completed")); // NOI18N
149
}
150         return;
151     }
152
153     public static void performUpdate(final Context context) {
154         if (context == null || context.getRoots().size() == 0) {
155             return;
156         }
157         SVNUrl repository = getSvnUrl(context);
158         RequestProcessor rp = Subversion.getInstance().getRequestProcessor(repository);
159         SvnProgressSupport support = new SvnProgressSupport() {
160             public void perform() {
161                 update(context, this);
162             }
163         };
164         support.start(rp, repository, org.openide.util.NbBundle.getMessage(UpdateAction.class, "MSG_Update_Progress")); // NOI18N
165
}
166
167 }
168
Popular Tags