2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.versioning.system.cvss.ui.actions.tag;
44
import org.netbeans.modules.versioning.system.cvss.ui.actions.AbstractSystemAction;
45
import org.netbeans.modules.versioning.system.cvss.ui.actions.add.AddExecutor;
46
import org.netbeans.modules.versioning.system.cvss.ui.actions.update.UpdateExecutor;
47
import org.netbeans.modules.versioning.system.cvss.*;
48
import org.netbeans.modules.versioning.system.cvss.util.Context;
49
import org.netbeans.modules.versioning.util.Utils;
50
import org.netbeans.lib.cvsclient.command.update.UpdateCommand;
51
import org.netbeans.lib.cvsclient.command.add.AddCommand;
52
import org.netbeans.lib.cvsclient.command.GlobalOptions;
53
import org.openide.util.NbBundle;
54
import org.openide.util.HelpCtx;
55
import org.openide.util.RequestProcessor;
56
import org.openide.DialogDescriptor;
57
import org.openide.DialogDisplayer;
58
import org.openide.ErrorManager;
59
import org.openide.nodes.Node;
62
import java.awt.Dialog;
64
import java.io.FileWriter;
65
import java.io.IOException;
66
import java.text.MessageFormat;
70
* Performs the CVS 'update -r branch' command on selected nodes.
72
* @author Maros Sandor
74
public class SwitchBranchAction extends AbstractSystemAction {
76
private static final int enabledForStatus = FileInformation.STATUS_VERSIONED_MERGE
77
| FileInformation.STATUS_VERSIONED_MODIFIEDINREPOSITORY
78
| FileInformation.STATUS_VERSIONED_MODIFIEDLOCALLY
79
| FileInformation.STATUS_VERSIONED_REMOVEDINREPOSITORY
80
| FileInformation.STATUS_VERSIONED_UPTODATE;
82
protected String getBaseName(Node [] activatedNodes) {
83
return "CTL_MenuItem_SwitchBranch"; // NOI18N
86
protected int getFileEnabledStatus() {
87
return enabledForStatus;
90
public void performCvsAction(final Node[] nodes) {
91
final Context context = getContext(nodes);
92
String title = MessageFormat.format(NbBundle.getBundle(SwitchBranchAction.class).getString("CTL_SwitchBranchDialog_Title"),
93
new Object[] { getContextDisplayName(nodes) });
95
final SwitchBranchPanel settings = new SwitchBranchPanel(context.getFiles());
97
JButton swich = new JButton(NbBundle.getMessage(SwitchBranchAction.class, "CTL_SwitchBranchDialog_Action_Switch"));
98
swich.setToolTipText(NbBundle.getMessage(SwitchBranchAction.class, "TT_SwitchBranchDialog_Action_Switch"));
99
DialogDescriptor descriptor = new DialogDescriptor(
103
new Object [] { swich, DialogDescriptor.CANCEL_OPTION },
105
DialogDescriptor.DEFAULT_ALIGN,
106
new HelpCtx(SwitchBranchAction.class),
108
descriptor.setClosingOptions(null);
110
Dialog dialog = DialogDisplayer.getDefault().createDialog(descriptor);
111
dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(SwitchBranchAction.class, "ACSD_SwitchBranchDialog"));
112
dialog.setVisible(true);
113
if (descriptor.getValue() != swich) return;
115
settings.saveSettings();
117
RequestProcessor.getDefault().post(new Runnable() {
119
switchBranch(nodes, context, settings);
124
protected boolean asynchronous() {
128
private void switchBranch(Node [] nodes, Context context, final SwitchBranchPanel settings) {
130
List newFolders = new ArrayList();
131
List others = new ArrayList();
132
FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache();
134
ExecutorGroup group = new ExecutorGroup(getRunningName(nodes));
135
File [][] flatRecursive = Utils.splitFlatOthers(context.getRootFiles());
136
if (flatRecursive[0].length > 0) {
137
File[] flat = flatRecursive[0];
138
UpdateCommand cmd = new UpdateCommand();
139
if (settings.isSwitchToTrunk()) {
140
cmd.setResetStickyOnes(true);
142
cmd.setUpdateByRevision(settings.getBranchName());
145
cmd.setBuildDirectories(true);
146
cmd.setPruneDirectories(true);
147
cmd.setRecursive(false);
150
GlobalOptions options = CvsVersioningSystem.createGlobalOptions();
151
if (context.getExclusions().size() > 0) {
152
options.setExclusions(context.getExclusions().toArray(new File[context.getExclusions().size()]));
155
group.addExecutors(UpdateExecutor.splitCommand(cmd, CvsVersioningSystem.getInstance(), options, getContextDisplayName(nodes)));
158
if (flatRecursive[1].length > 0) {
159
File [] roots = flatRecursive[1];
160
for (int i = 0; i < roots.length; i++) {
161
File root = roots[i];
162
// FIXME this check fails on workdir root, it's incorectly recognides as locally new
163
// console: cvs [add aborted]: there is no version here; do 'cvs checkout' first
165
if (root.isDirectory() && cache.getStatus(root).getStatus() == FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY) {
166
newFolders.add(root);
172
// Special treatment for Locally New folders. Ww cannot switch them to branch with the Update command.
173
// Workaround: add the folder to CVS, then manually create CVS/Tag inside
174
if (newFolders.size() > 0) {
175
AddCommand acmd = new AddCommand();
176
final File [] files = (File[]) newFolders.toArray(new File[newFolders.size()]);
177
acmd.setFiles(files);
178
group.addExecutors(AddExecutor.splitCommand(acmd, CvsVersioningSystem.getInstance(), null));
179
Runnable action = new Runnable() {
181
if (settings.isSwitchToTrunk()) {
182
setSticky(files, null);
184
setSticky(files, settings.getBranchName());
188
group.addBarrier(action);
189
others.addAll(newFolders);
192
if (others.size() > 0) {
193
UpdateCommand cmd = new UpdateCommand();
194
if (settings.isSwitchToTrunk()) {
195
cmd.setResetStickyOnes(true);
197
cmd.setUpdateByRevision(settings.getBranchName());
200
cmd.setBuildDirectories(true);
201
cmd.setPruneDirectories(true);
202
cmd.setFiles((File[]) others.toArray(new File[others.size()]));
204
GlobalOptions options = CvsVersioningSystem.createGlobalOptions();
205
if (context.getExclusions().size() > 0) {
206
options.setExclusions(context.getExclusions().toArray(new File[context.getExclusions().size()]));
209
group.addExecutors(UpdateExecutor.splitCommand(cmd, CvsVersioningSystem.getInstance(), options, getContextDisplayName(nodes)));
215
private void setSticky(File[] files, String sticky) {
216
for (int i = 0; i < files.length; i++) {
217
File file = files[i];
219
setSticky(file, sticky);
220
} catch (IOException e) {
221
ErrorManager.getDefault().notify(e);
227
* Creates CVS/Tag file under the given directory with Tsticky string. If sticky is null, it deleted CVS/Tag file.
229
* @param file directory to tag
230
* @param sticky sitcky tag to use
231
* @throws IOException if some I/O operation fails
233
private void setSticky(File file, String sticky) throws IOException {
234
File tag = new File(file, "CVS/Tag"); // NOI18N
236
if ("HEAD".equals(sticky)) { // NOI18N
239
if (sticky != null) {
240
FileWriter w = new FileWriter(tag);
241
w.write("T"); // NOI18N
243
w.write(System.getProperty("line.separator")); // NOI18N