KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.update.UpdateExecutor;
24 import org.netbeans.modules.versioning.system.cvss.FileInformation;
25 import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem;
26 import org.netbeans.modules.versioning.system.cvss.ExecutorSupport;
27 import org.netbeans.modules.versioning.system.cvss.ExecutorGroup;
28 import org.netbeans.modules.versioning.system.cvss.util.Context;
29 import org.netbeans.lib.cvsclient.command.update.UpdateCommand;
30 import org.netbeans.lib.cvsclient.command.tag.RtagCommand;
31 import org.netbeans.lib.cvsclient.command.GlobalOptions;
32 import org.openide.util.NbBundle;
33 import org.openide.util.RequestProcessor;
34 import org.openide.util.HelpCtx;
35 import org.openide.DialogDescriptor;
36 import org.openide.DialogDisplayer;
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.text.MessageFormat JavaDoc;
43
44 /**
45  * Performs the CVS 'update -j' command on selected nodes.
46  *
47  * @author Maros Sandor
48  */

49 public class MergeBranchAction extends AbstractSystemAction {
50
51     private static final int enabledForStatus = FileInformation.STATUS_VERSIONED_MERGE
52                     | FileInformation.STATUS_VERSIONED_MODIFIEDINREPOSITORY
53                     | FileInformation.STATUS_VERSIONED_MODIFIEDLOCALLY
54                     | FileInformation.STATUS_VERSIONED_REMOVEDINREPOSITORY
55                     | FileInformation.STATUS_VERSIONED_UPTODATE;
56     
57     protected String JavaDoc getBaseName(Node [] activatedNodes) {
58         return "CTL_MenuItem_MergeBranch"; // NOI18N
59
}
60
61     protected int getFileEnabledStatus() {
62         return enabledForStatus;
63     }
64
65     protected int getDirectoryEnabledStatus() {
66         return FileInformation.STATUS_MANAGED & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED & ~FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY;
67     }
68     
69     public void performCvsAction(Node[] nodes) {
70         Context context = getContext(nodes);
71
72         String JavaDoc title = MessageFormat.format(NbBundle.getBundle(MergeBranchAction.class).getString("CTL_MergeBranchDialog_Title"), getContextDisplayName(nodes)); // NOI18N
73

74         MergePanel settings = new MergePanel(context.getFiles());
75
76         JButton merge = new JButton(NbBundle.getMessage(MergeBranchAction.class, "CTL_MergeBranchDialog_Action_Merge")); // NOI18N
77
settings.putClientProperty("OKButton", merge); // NOI18N
78
merge.setToolTipText(NbBundle.getMessage(MergeBranchAction.class, "TT_MergeBranchDialog_Action_Merge")); // NOI18N
79
JButton cancel = new JButton(NbBundle.getMessage(MergeBranchAction.class, "CTL_MergeBranchDialog_Action_Cancel")); // NOI18N
80
cancel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MergeBranchAction.class, "ACSD_MergeBranchDialog_Action_Cancel")); // NOI18N
81
DialogDescriptor descriptor = new DialogDescriptor(
82                 settings,
83                 title,
84                 true,
85                 new Object JavaDoc [] { merge, cancel },
86                 merge,
87                 DialogDescriptor.DEFAULT_ALIGN,
88                 new HelpCtx(MergeBranchAction.class),
89                 null);
90         descriptor.setClosingOptions(null);
91
92         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(descriptor);
93         dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(MergeBranchAction.class, "ACSD_MergeBranchDialog")); // NOI18N
94
dialog.setVisible(true);
95         if (descriptor.getValue() != merge) return;
96
97         settings.saveSettings();
98        
99         RequestProcessor.getDefault().post(new MergeBranchExecutor(context, settings, getRunningName(nodes), getContextDisplayName(nodes)));
100     }
101
102     protected boolean asynchronous() {
103         return false;
104     }
105
106     /**
107      * Runnable that executes actions specified in the MergeBranch settings panel.
108      */

109     private static class MergeBranchExecutor implements Runnable JavaDoc {
110
111         private final Context context;
112         private final MergePanel settings;
113         private final String JavaDoc contextName;
114         private String JavaDoc temporaryTag;
115         private String JavaDoc name;
116
117         public MergeBranchExecutor(Context context, MergePanel settings, String JavaDoc name, String JavaDoc contextName) {
118             this.context = context;
119             this.settings = settings;
120             this.name = name;
121             this.contextName = contextName;
122         }
123
124         public void run() {
125             final ExecutorGroup group = new ExecutorGroup(name);
126             if (settings.isTaggingAfterMerge()) {
127                 temporaryTag = settings.getAfterMergeTagName() + "_tempheadmarker"; // NOI18N
128
final ExecutorSupport[] tmpTagging = tagHeadTemporary();
129                 group.addExecutors(tmpTagging);
130                 Runnable JavaDoc cleanup = new Runnable JavaDoc() {
131                     public void run() {
132                         if (tmpTagging == null || ExecutorSupport.wait(tmpTagging)) {
133                             group.addCleanups(removeTagHeadTemporary());
134                         }
135                     }
136                 };
137                 group.addBarrier(cleanup);
138                 group.addExecutors(update());
139                 group.addBarrier(null);
140                 group.addExecutors(tag());
141             } else {
142                 group.addExecutors(update());
143             }
144             group.execute();
145         }
146
147         /**
148          * If user requests the merge to start at a specific tag:
149          * cvs update -j merge_tag -j my_branch
150          * If user requests to merge everything from the common ancestor revision:
151          * cvs update -j my_branch
152          * Branch_tag may be HEAD if we merge from trunk.
153          */

154         private UpdateExecutor [] update() {
155             UpdateCommand cmd = new UpdateCommand();
156
157             String JavaDoc headTag;
158             if (temporaryTag != null) {
159                 headTag = temporaryTag;
160             } else if (settings.isMergingFromTrunk()) {
161                 headTag = "HEAD"; // NOI18N
162
} else if (settings.isMergingFromBranch()) {
163                 headTag = settings.getBranchName();
164             } else {
165                 headTag = settings.getEndTagName();
166             }
167             
168             GlobalOptions options = CvsVersioningSystem.createGlobalOptions();
169             if (context.getExclusions().size() > 0) {
170                 options.setExclusions((File JavaDoc[]) context.getExclusions().toArray(new File JavaDoc[context.getExclusions().size()]));
171             }
172             
173             if (settings.isUsingMergeTag()) {
174                 cmd.setMergeRevision1(settings.getMergeTagName());
175                 cmd.setMergeRevision2(headTag);
176             } else {
177                 cmd.setMergeRevision1(headTag);
178             }
179             cmd.setFiles(context.getRootFiles());
180             cmd.setBuildDirectories(true);
181             cmd.setPruneDirectories(true);
182         
183             return UpdateExecutor.splitCommand(cmd, CvsVersioningSystem.getInstance(), options, contextName);
184         }
185
186         /**
187          * Tags the head of branch we merged from:
188          * cvs rtag -F -r my_branch merge_tag module1 module2 ...
189          */

190         private RTagExecutor[] tag() {
191             RtagCommand cmd = new RtagCommand();
192         
193             cmd.setOverrideExistingTag(true);
194             cmd.setTagByRevision(temporaryTag);
195             cmd.setTag(settings.getAfterMergeTagName());
196         
197             return RTagExecutor.splitCommand(cmd, context.getFiles(), null);
198         }
199
200         /**
201          * Places or removes a temporary tag at the head of the branch we merge from:
202          * cvs rtag -F -r my_branch temporary_tag module1 module2 ...
203          */

204         private RTagExecutor[] tagHeadTemporary() {
205             RtagCommand cmd = new RtagCommand();
206         
207             cmd.setOverrideExistingTag(true);
208             cmd.setTagByRevision(settings.isMergingFromTrunk() ? "HEAD" : settings.getBranchName()); // NOI18N
209
cmd.setTag(temporaryTag);
210
211             return RTagExecutor.splitCommand(cmd, context.getFiles(), null);
212         }
213
214         private RTagExecutor [] removeTagHeadTemporary() {
215             RtagCommand cmd = new RtagCommand();
216                     
217             cmd.setDeleteTag(true);
218             cmd.setTag(temporaryTag);
219         
220             return RTagExecutor.splitCommand(cmd, context.getFiles(), null);
221         }
222     }
223 }
224
Popular Tags