KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > tag > SwitchBranchAction


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.system.cvss.ui.actions.tag;
21
22 import org.netbeans.modules.versioning.system.cvss.ui.actions.AbstractSystemAction;
23 import org.netbeans.modules.versioning.system.cvss.ui.actions.add.AddExecutor;
24 import org.netbeans.modules.versioning.system.cvss.ui.actions.update.UpdateExecutor;
25 import org.netbeans.modules.versioning.system.cvss.*;
26 import org.netbeans.modules.versioning.system.cvss.util.Context;
27 import org.netbeans.modules.versioning.util.Utils;
28 import org.netbeans.lib.cvsclient.command.update.UpdateCommand;
29 import org.netbeans.lib.cvsclient.command.add.AddCommand;
30 import org.netbeans.lib.cvsclient.command.GlobalOptions;
31 import org.openide.util.NbBundle;
32 import org.openide.util.HelpCtx;
33 import org.openide.util.RequestProcessor;
34 import org.openide.DialogDescriptor;
35 import org.openide.DialogDisplayer;
36 import org.openide.ErrorManager;
37 import org.openide.nodes.Node;
38
39 import javax.swing.*;
40 import java.awt.Dialog JavaDoc;
41 import java.io.File JavaDoc;
42 import java.io.FileWriter JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.text.MessageFormat JavaDoc;
45 import java.util.*;
46
47 /**
48  * Performs the CVS 'update -r branch' command on selected nodes.
49  *
50  * @author Maros Sandor
51  */

52 public class SwitchBranchAction extends AbstractSystemAction {
53
54     private static final int enabledForStatus = FileInformation.STATUS_VERSIONED_MERGE
55                     | FileInformation.STATUS_VERSIONED_MODIFIEDINREPOSITORY
56                     | FileInformation.STATUS_VERSIONED_MODIFIEDLOCALLY
57                     | FileInformation.STATUS_VERSIONED_REMOVEDINREPOSITORY
58                     | FileInformation.STATUS_VERSIONED_UPTODATE;
59     
60     protected String JavaDoc getBaseName(Node [] activatedNodes) {
61         return "CTL_MenuItem_SwitchBranch"; // NOI18N
62
}
63
64     protected int getFileEnabledStatus() {
65         return enabledForStatus;
66     }
67
68     public void performCvsAction(final Node[] nodes) {
69         final Context context = getContext(nodes);
70         String JavaDoc title = MessageFormat.format(NbBundle.getBundle(SwitchBranchAction.class).getString("CTL_SwitchBranchDialog_Title"),
71                                          new Object JavaDoc[] { getContextDisplayName(nodes) });
72         
73         final SwitchBranchPanel settings = new SwitchBranchPanel(context.getFiles());
74
75         JButton swich = new JButton(NbBundle.getMessage(SwitchBranchAction.class, "CTL_SwitchBranchDialog_Action_Switch"));
76         swich.setToolTipText(NbBundle.getMessage(SwitchBranchAction.class, "TT_SwitchBranchDialog_Action_Switch"));
77         DialogDescriptor descriptor = new DialogDescriptor(
78                 settings,
79                 title,
80                 true,
81                 new Object JavaDoc [] { swich, DialogDescriptor.CANCEL_OPTION },
82                 swich,
83                 DialogDescriptor.DEFAULT_ALIGN,
84                 new HelpCtx(SwitchBranchAction.class),
85                 null);
86         descriptor.setClosingOptions(null);
87
88         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(descriptor);
89         dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(SwitchBranchAction.class, "ACSD_SwitchBranchDialog"));
90         dialog.setVisible(true);
91         if (descriptor.getValue() != swich) return;
92
93         settings.saveSettings();
94         
95         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
96             public void run() {
97                 switchBranch(nodes, context, settings);
98             }
99         });
100     }
101
102     protected boolean asynchronous() {
103         return false;
104     }
105
106     private void switchBranch(Node [] nodes, Context context, final SwitchBranchPanel settings) {
107         
108         List newFolders = new ArrayList();
109         List others = new ArrayList();
110         FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache();
111
112         ExecutorGroup group = new ExecutorGroup(getRunningName(nodes));
113         File JavaDoc [][] flatRecursive = Utils.splitFlatOthers(context.getRootFiles());
114         if (flatRecursive[0].length > 0) {
115             File JavaDoc[] flat = flatRecursive[0];
116             UpdateCommand cmd = new UpdateCommand();
117             if (settings.isSwitchToTrunk()) {
118                 cmd.setResetStickyOnes(true);
119             } else {
120                 cmd.setUpdateByRevision(settings.getBranchName());
121             }
122
123             cmd.setBuildDirectories(true);
124             cmd.setPruneDirectories(true);
125             cmd.setRecursive(false);
126             cmd.setFiles(flat);
127
128             GlobalOptions options = CvsVersioningSystem.createGlobalOptions();
129             if (context.getExclusions().size() > 0) {
130                 options.setExclusions(context.getExclusions().toArray(new File JavaDoc[context.getExclusions().size()]));
131             }
132
133             group.addExecutors(UpdateExecutor.splitCommand(cmd, CvsVersioningSystem.getInstance(), options, getContextDisplayName(nodes)));
134
135         }
136         if (flatRecursive[1].length > 0) {
137             File JavaDoc [] roots = flatRecursive[1];
138             for (int i = 0; i < roots.length; i++) {
139                 File JavaDoc root = roots[i];
140                 // FIXME this check fails on workdir root, it's incorectly recognides as locally new
141
// console: cvs [add aborted]: there is no version here; do 'cvs checkout' first
142
// see #64103
143
if (root.isDirectory() && cache.getStatus(root).getStatus() == FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY) {
144                     newFolders.add(root);
145                 } else {
146                     others.add(root);
147                 }
148             }
149
150             // Special treatment for Locally New folders. Ww cannot switch them to branch with the Update command.
151
// Workaround: add the folder to CVS, then manually create CVS/Tag inside
152
if (newFolders.size() > 0) {
153                 AddCommand acmd = new AddCommand();
154                 final File JavaDoc [] files = (File JavaDoc[]) newFolders.toArray(new File JavaDoc[newFolders.size()]);
155                 acmd.setFiles(files);
156                 group.addExecutors(AddExecutor.splitCommand(acmd, CvsVersioningSystem.getInstance(), null));
157                 Runnable JavaDoc action = new Runnable JavaDoc() {
158                     public void run() {
159                         if (settings.isSwitchToTrunk()) {
160                             setSticky(files, null);
161                         } else {
162                             setSticky(files, settings.getBranchName());
163                         }
164                     }
165                 };
166                 group.addBarrier(action);
167                 others.addAll(newFolders);
168             }
169
170             if (others.size() > 0) {
171                 UpdateCommand cmd = new UpdateCommand();
172                 if (settings.isSwitchToTrunk()) {
173                     cmd.setResetStickyOnes(true);
174                 } else {
175                     cmd.setUpdateByRevision(settings.getBranchName());
176                 }
177
178                 cmd.setBuildDirectories(true);
179                 cmd.setPruneDirectories(true);
180                 cmd.setFiles((File JavaDoc[]) others.toArray(new File JavaDoc[others.size()]));
181
182                 GlobalOptions options = CvsVersioningSystem.createGlobalOptions();
183                 if (context.getExclusions().size() > 0) {
184                     options.setExclusions(context.getExclusions().toArray(new File JavaDoc[context.getExclusions().size()]));
185                 }
186
187                 group.addExecutors(UpdateExecutor.splitCommand(cmd, CvsVersioningSystem.getInstance(), options, getContextDisplayName(nodes)));
188             }
189         }
190         group.execute();
191     }
192
193     private void setSticky(File JavaDoc[] files, String JavaDoc sticky) {
194         for (int i = 0; i < files.length; i++) {
195             File JavaDoc file = files[i];
196             try {
197                 setSticky(file, sticky);
198             } catch (IOException JavaDoc e) {
199                 ErrorManager.getDefault().notify(e);
200             }
201         }
202     }
203
204     /**
205      * Creates CVS/Tag file under the given directory with Tsticky string. If sticky is null, it deleted CVS/Tag file.
206      *
207      * @param file directory to tag
208      * @param sticky sitcky tag to use
209      * @throws IOException if some I/O operation fails
210      */

211     private void setSticky(File JavaDoc file, String JavaDoc sticky) throws IOException JavaDoc {
212         File JavaDoc tag = new File JavaDoc(file, "CVS/Tag"); // NOI18N
213
tag.delete();
214         if ("HEAD".equals(sticky)) { // NOI18N
215
return;
216         }
217         if (sticky != null) {
218             FileWriter JavaDoc w = new FileWriter JavaDoc(tag);
219             w.write("T"); // NOI18N
220
w.write(sticky);
221             w.write(System.getProperty("line.separator")); // NOI18N
222
w.close();
223         }
224     }
225 }
226
Popular Tags