~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to javacvs/cvsmodule/src/org/netbeans/modules/versioning/system/cvss/ui/actions/tag/SwitchBranchAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.versioning.system.cvss.ui.actions.tag;
 
43
 
 
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;
 
60
 
 
61
import javax.swing.*;
 
62
import java.awt.Dialog;
 
63
import java.io.File;
 
64
import java.io.FileWriter;
 
65
import java.io.IOException;
 
66
import java.text.MessageFormat;
 
67
import java.util.*;
 
68
 
 
69
/**
 
70
 * Performs the CVS 'update -r branch' command on selected nodes.
 
71
 * 
 
72
 * @author Maros Sandor
 
73
 */
 
74
public class SwitchBranchAction extends AbstractSystemAction {
 
75
 
 
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;
 
81
    
 
82
    protected String getBaseName(Node [] activatedNodes) {
 
83
        return "CTL_MenuItem_SwitchBranch";  // NOI18N
 
84
    }
 
85
 
 
86
    protected int getFileEnabledStatus() {
 
87
        return enabledForStatus;
 
88
    }
 
89
 
 
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) });
 
94
        
 
95
        final SwitchBranchPanel settings = new SwitchBranchPanel(context.getFiles());
 
96
 
 
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(
 
100
                settings,
 
101
                title,
 
102
                true,
 
103
                new Object [] { swich, DialogDescriptor.CANCEL_OPTION },
 
104
                swich,
 
105
                DialogDescriptor.DEFAULT_ALIGN,
 
106
                new HelpCtx(SwitchBranchAction.class),
 
107
                null);
 
108
        descriptor.setClosingOptions(null);
 
109
 
 
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;
 
114
 
 
115
        settings.saveSettings();
 
116
        
 
117
        RequestProcessor.getDefault().post(new Runnable() {
 
118
            public void run() {
 
119
                switchBranch(nodes, context, settings);
 
120
            }
 
121
        });
 
122
    }
 
123
 
 
124
    protected boolean asynchronous() {
 
125
        return false;
 
126
    }
 
127
 
 
128
    private void switchBranch(Node [] nodes, Context context, final SwitchBranchPanel settings) {
 
129
        
 
130
        List newFolders = new ArrayList();
 
131
        List others = new ArrayList();
 
132
        FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache();
 
133
 
 
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);
 
141
            } else {
 
142
                cmd.setUpdateByRevision(settings.getBranchName());
 
143
            }
 
144
 
 
145
            cmd.setBuildDirectories(true);
 
146
            cmd.setPruneDirectories(true);
 
147
            cmd.setRecursive(false);
 
148
            cmd.setFiles(flat);
 
149
 
 
150
            GlobalOptions options = CvsVersioningSystem.createGlobalOptions();
 
151
            if (context.getExclusions().size() > 0) {
 
152
                options.setExclusions(context.getExclusions().toArray(new File[context.getExclusions().size()]));
 
153
            }
 
154
 
 
155
            group.addExecutors(UpdateExecutor.splitCommand(cmd, CvsVersioningSystem.getInstance(), options, getContextDisplayName(nodes)));
 
156
 
 
157
        }
 
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
 
164
                // see #64103
 
165
                if (root.isDirectory() && cache.getStatus(root).getStatus() == FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY) {
 
166
                    newFolders.add(root);
 
167
                } else {
 
168
                    others.add(root);
 
169
                }
 
170
            }
 
171
 
 
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() {
 
180
                    public void run() {
 
181
                        if (settings.isSwitchToTrunk()) {
 
182
                            setSticky(files, null);
 
183
                        } else {
 
184
                            setSticky(files, settings.getBranchName());
 
185
                        }
 
186
                    }
 
187
                };
 
188
                group.addBarrier(action);
 
189
                others.addAll(newFolders);
 
190
            }
 
191
 
 
192
            if (others.size() > 0) {
 
193
                UpdateCommand cmd = new UpdateCommand();
 
194
                if (settings.isSwitchToTrunk()) {
 
195
                    cmd.setResetStickyOnes(true);
 
196
                } else {
 
197
                    cmd.setUpdateByRevision(settings.getBranchName());
 
198
                }
 
199
 
 
200
                cmd.setBuildDirectories(true);
 
201
                cmd.setPruneDirectories(true);
 
202
                cmd.setFiles((File[]) others.toArray(new File[others.size()]));
 
203
 
 
204
                GlobalOptions options = CvsVersioningSystem.createGlobalOptions();
 
205
                if (context.getExclusions().size() > 0) {
 
206
                    options.setExclusions(context.getExclusions().toArray(new File[context.getExclusions().size()]));
 
207
                }
 
208
 
 
209
                group.addExecutors(UpdateExecutor.splitCommand(cmd, CvsVersioningSystem.getInstance(), options, getContextDisplayName(nodes)));
 
210
            }
 
211
        }
 
212
        group.execute();
 
213
    }
 
214
 
 
215
    private void setSticky(File[] files, String sticky) {
 
216
        for (int i = 0; i < files.length; i++) {
 
217
            File file = files[i];
 
218
            try {
 
219
                setSticky(file, sticky);
 
220
            } catch (IOException e) {
 
221
                ErrorManager.getDefault().notify(e);
 
222
            }
 
223
        }
 
224
    }
 
225
 
 
226
    /**
 
227
     * Creates CVS/Tag file under the given directory with Tsticky string. If sticky is null, it deleted CVS/Tag file. 
 
228
     * 
 
229
     * @param file directory to tag
 
230
     * @param sticky sitcky tag to use
 
231
     * @throws IOException if some I/O operation fails
 
232
     */ 
 
233
    private void setSticky(File file, String sticky) throws IOException {
 
234
        File tag = new File(file, "CVS/Tag");  // NOI18N
 
235
        tag.delete();
 
236
        if ("HEAD".equals(sticky)) {  // NOI18N
 
237
            return;
 
238
        }
 
239
        if (sticky != null) {
 
240
            FileWriter w = new FileWriter(tag);
 
241
            w.write("T");  // NOI18N
 
242
            w.write(sticky);
 
243
            w.write(System.getProperty("line.separator")); // NOI18N
 
244
            w.close();
 
245
        }
 
246
    }
 
247
}