KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > commit > CommitAction


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.commit;
21
22 import org.openide.util.NbBundle;
23 import org.openide.util.HelpCtx;
24 import org.openide.DialogDescriptor;
25 import org.openide.DialogDisplayer;
26 import org.openide.nodes.Node;
27 import org.netbeans.lib.cvsclient.command.commit.CommitCommand;
28 import org.netbeans.lib.cvsclient.command.add.AddCommand;
29 import org.netbeans.lib.cvsclient.command.KeywordSubstitutionOptions;
30 import org.netbeans.lib.cvsclient.command.remove.RemoveCommand;
31 import org.netbeans.modules.versioning.system.cvss.*;
32 import org.netbeans.modules.versioning.system.cvss.CvsModuleConfig;
33 import org.netbeans.modules.versioning.system.cvss.executor.RemoveExecutor;
34 import org.netbeans.modules.versioning.system.cvss.util.Utils;
35 import org.netbeans.modules.versioning.system.cvss.util.Context;
36 import org.netbeans.modules.versioning.system.cvss.ui.actions.AbstractSystemAction;
37 import org.netbeans.modules.versioning.system.cvss.ui.actions.add.AddExecutor;
38 import org.netbeans.modules.versioning.util.VersioningListener;
39 import org.netbeans.modules.versioning.util.VersioningEvent;
40 import javax.swing.*;
41 import java.awt.Dialog JavaDoc;
42 import java.io.File JavaDoc;
43 import java.text.MessageFormat JavaDoc;
44 import java.util.*;
45 import org.netbeans.modules.versioning.util.DialogBoundsPreserver;
46
47 /**
48  * Represents the "Commit" main/popup action and provides programmatic Commit action upon any context.
49  *
50  * @author Maros Sandor
51  */

52 public class CommitAction extends AbstractSystemAction {
53     
54     static final String JavaDoc RECENT_COMMIT_MESSAGES = "commitAction.commitMessage"; // NOI18N
55

56     public CommitAction() {
57         setIcon(null);
58         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
59
}
60
61     protected String JavaDoc getBaseName(Node [] activatedNodes) {
62         return "CTL_MenuItem_Commit"; // NOI18N
63
}
64
65     protected boolean enable(Node[] nodes) {
66         return CvsVersioningSystem.getInstance().getFileTableModel(Utils.getCurrentContext(nodes), FileInformation.STATUS_LOCAL_CHANGE).getNodes().length > 0;
67     }
68
69     /**
70      * Shows commit dialog UI and handles selected option.
71      */

72     public static void invokeCommit(String JavaDoc contentTitle, Context context, String JavaDoc runningName) {
73         ResourceBundle loc = NbBundle.getBundle(CommitAction.class);
74         if (CvsVersioningSystem.getInstance().getFileTableModel(context, FileInformation.STATUS_LOCAL_CHANGE).getNodes().length == 0) {
75             JOptionPane.showMessageDialog(null, loc.getString("MSG_NoFilesToCommit_Prompt"),
76                                           loc.getString("MSG_NoFilesToCommit_Title"), JOptionPane.INFORMATION_MESSAGE);
77             return;
78         }
79         
80         CommitCommand cmd = new CommitCommand();
81         cmd.setDisplayName(NbBundle.getMessage(CommitAction.class, "BK0001")); // NOI18N
82

83         final CommitSettings settings = new CommitSettings();
84         final JButton commit = new JButton(loc.getString("CTL_CommitForm_Action_Commit"));
85         commit.setToolTipText(NbBundle.getMessage(CommitAction.class, "TT_CommitDialog_Action_Commit")); // NOI18N
86
commit.setEnabled(false);
87         JButton cancel = new JButton(loc.getString("CTL_CommitForm_Action_Cancel"));
88         cancel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommitAction.class, "ACSD_CommitDialog_Action_Cancel")); // NOI18N
89
DialogDescriptor descriptor = new DialogDescriptor(
90                 settings,
91                 MessageFormat.format(loc.getString("CTL_CommitDialog_Title"), contentTitle),
92                 true,
93                 new Object JavaDoc [] { commit, cancel },
94                 commit,
95                 DialogDescriptor.BOTTOM_ALIGN,
96                 null,
97                 null);
98         descriptor.setClosingOptions(null);
99         descriptor.setHelpCtx(new HelpCtx(CommitSettings.class));
100         settings.addVersioningListener(new VersioningListener() {
101             public void versioningEvent(VersioningEvent event) {
102                 refreshCommitDialog(settings, commit);
103             }
104         });
105         setupNodes(settings, context);
106         settings.putClientProperty("contentTitle", contentTitle); // NOI18N
107
settings.putClientProperty("DialogDescriptor", descriptor); // NOI18N
108
Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(descriptor);
109         dialog.addWindowListener(new DialogBoundsPreserver(CvsModuleConfig.getDefault().getPreferences(), "svn.commit.dialog")); // NOI18N
110
dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommitAction.class, "ACSD_CommitDialog")); // NOI18N
111
dialog.setVisible(true);
112         if (descriptor.getValue() != commit) return;
113
114         saveExclusions(settings);
115
116         cmd.setMessage(settings.getCommitMessage());
117         org.netbeans.modules.versioning.util.Utils.insert(CvsModuleConfig.getDefault().getPreferences(), RECENT_COMMIT_MESSAGES, cmd.getMessage(), 20);
118         if (runningName == null) {
119             runningName = NbBundle.getMessage(CommitAction.class, "BK0002"); // NOI18N
120
}
121         ExecutorGroup group = new ExecutorGroup(runningName);
122         addCommit(group, settings);
123         group.execute();
124     }
125     
126     private static void saveExclusions(CommitSettings settings) {
127         CommitSettings.CommitFile [] files = settings.getCommitFiles();
128         for (int i = 0; i < files.length; i++) {
129             CommitSettings.CommitFile file = files[i];
130             if (file.getOptions() == CommitOptions.EXCLUDE) {
131                 CvsModuleConfig.getDefault().addExclusion(file.getNode().getFile());
132             } else {
133                 CvsModuleConfig.getDefault().removeExclusion(file.getNode().getFile());
134             }
135         }
136     }
137
138     private static void setupNodes(CommitSettings settings, Context context) {
139         CvsFileNode [] filesToCommit = CvsVersioningSystem.getInstance().getFileTableModel(context, FileInformation.STATUS_LOCAL_CHANGE).getNodes();
140         settings.setNodes(filesToCommit);
141     }
142
143     /**
144      * User changed a commit action.
145      *
146      * @param settings
147      * @param commit
148      */

149     private static void refreshCommitDialog(CommitSettings settings, JButton commit) {
150         ResourceBundle loc = NbBundle.getBundle(CommitAction.class);
151         CommitSettings.CommitFile [] files = settings.getCommitFiles();
152         Set<String JavaDoc> stickyTags = new HashSet<String JavaDoc>();
153         boolean conflicts = false;
154         for (int i = 0; i < files.length; i++) {
155             CommitSettings.CommitFile file = files[i];
156             if (file.getOptions() == CommitOptions.EXCLUDE) continue;
157             stickyTags.add(Utils.getSticky(file.getNode().getFile()));
158             int status = file.getNode().getInformation().getStatus();
159             if ((status & FileInformation.STATUS_REMOTE_CHANGE) != 0 || status == FileInformation.STATUS_VERSIONED_CONFLICT) {
160                 commit.setEnabled(false);
161                 String JavaDoc msg = (status == FileInformation.STATUS_VERSIONED_CONFLICT) ?
162                         loc.getString("MSG_CommitForm_ErrorConflicts") :
163                         loc.getString("MSG_CommitForm_ErrorRemoteChanges");
164                 settings.setErrorLabel("<html><font color=\"#002080\">" + msg + "</font></html>"); // NOI18N
165
conflicts = true;
166             }
167             stickyTags.add(Utils.getSticky(file.getNode().getFile()));
168         }
169         
170         if (stickyTags.size() > 1) {
171             settings.setColumns(new String JavaDoc [] { CommitSettings.COLUMN_NAME_NAME, CommitSettings.COLUMN_NAME_STICKY, CommitSettings.COLUMN_NAME_STATUS,
172                                                 CommitSettings.COLUMN_NAME_ACTION, CommitSettings.COLUMN_NAME_PATH });
173         } else {
174             settings.setColumns(new String JavaDoc [] { CommitSettings.COLUMN_NAME_NAME, CommitSettings.COLUMN_NAME_STATUS,
175                                                 CommitSettings.COLUMN_NAME_ACTION, CommitSettings.COLUMN_NAME_PATH });
176         }
177         
178         String JavaDoc contentTitle = (String JavaDoc) settings.getClientProperty("contentTitle"); // NOI18N
179
DialogDescriptor dd = (DialogDescriptor) settings.getClientProperty("DialogDescriptor"); // NOI18N
180
String JavaDoc errorLabel;
181         if (stickyTags.size() <= 1) {
182             String JavaDoc stickyTag = stickyTags.size() == 0 ? null : (String JavaDoc) stickyTags.iterator().next();
183             if (stickyTag == null) {
184                 dd.setTitle(MessageFormat.format(loc.getString("CTL_CommitDialog_Title"), contentTitle));
185                 errorLabel = ""; // NOI18N
186
} else {
187                 stickyTag = stickyTag.substring(1);
188                 dd.setTitle(MessageFormat.format(loc.getString("CTL_CommitDialog_Title_Branch"), contentTitle, stickyTag));
189                 String JavaDoc msg = MessageFormat.format(loc.getString("MSG_CommitForm_InfoBranch"), stickyTag);
190                 errorLabel = "<html><font color=\"#002080\">" + msg + "</font></html>"; // NOI18N
191
}
192         } else {
193             dd.setTitle(MessageFormat.format(loc.getString("CTL_CommitDialog_Title_Branches"), contentTitle));
194             String JavaDoc msg = loc.getString("MSG_CommitForm_ErrorMultipleBranches");
195             errorLabel = "<html><font color=\"#CC0000\">" + msg + "</font></html>"; // NOI18N
196
}
197         if (!conflicts) {
198             settings.setErrorLabel(errorLabel);
199             commit.setEnabled(true);
200         }
201     }
202
203     public void performCvsAction(Node[] nodes) {
204         invokeCommit(getContextDisplayName(nodes), getContext(nodes), getRunningName(nodes));
205     }
206
207     protected boolean asynchronous() {
208         return false;
209     }
210     
211     /**
212      * Prepares add/commit actions based on settings in the Commit dialog.
213      *
214      * @param group where commit is added
215      * @param settings user settings
216      */

217     public static void addCommit(ExecutorGroup group, CommitSettings settings) {
218         FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache();
219         CommitSettings.CommitFile [] files = settings.getCommitFiles();
220         List<File JavaDoc> commitBucket = new ArrayList<File JavaDoc>();
221         List<File JavaDoc> addDefaultBucket = new ArrayList<File JavaDoc>();
222         List<File JavaDoc> addKkvBucket = new ArrayList<File JavaDoc>();
223         List<File JavaDoc> addKkvlBucket = new ArrayList<File JavaDoc>();
224         List<File JavaDoc> addKkBucket = new ArrayList<File JavaDoc>();
225         List<File JavaDoc> addKoBucket = new ArrayList<File JavaDoc>();
226         List<File JavaDoc> addKbBucket = new ArrayList<File JavaDoc>();
227         List<File JavaDoc> addKvBucket = new ArrayList<File JavaDoc>();
228         List<File JavaDoc> removeBucket = new ArrayList<File JavaDoc>();
229         for (int i = 0; i < files.length; i++) {
230             CommitSettings.CommitFile file = files[i];
231             if (file.getOptions() == CommitOptions.EXCLUDE) continue;
232             if (file.getOptions() == CommitOptions.ADD_TEXT) {
233                 addDefaultBucket.add(file.getNode().getFile());
234             } else if (file.getOptions() == CommitOptions.ADD_BINARY) {
235                 addKbBucket.add(file.getNode().getFile());
236             } else if (file.getOptions() == CommitOptions.COMMIT_REMOVE) {
237                 int status = cache.getStatus(file.getNode().getFile()).getStatus();
238                 if (status == FileInformation.STATUS_VERSIONED_DELETEDLOCALLY) {
239                     removeBucket.add(file.getNode().getFile());
240                 }
241             }
242             commitBucket.add(file.getNode().getFile());
243         }
244
245         // perform
246
group.addExecutors(createAdd(addDefaultBucket, null));
247         group.addExecutors(createAdd(addKkvBucket, KeywordSubstitutionOptions.DEFAULT));
248         group.addExecutors(createAdd(addKkvlBucket, KeywordSubstitutionOptions.DEFAULT_LOCKER));
249         group.addExecutors(createAdd(addKkBucket, KeywordSubstitutionOptions.ONLY_KEYWORDS));
250         group.addExecutors(createAdd(addKoBucket, KeywordSubstitutionOptions.OLD_VALUES));
251         group.addExecutors(createAdd(addKbBucket, KeywordSubstitutionOptions.BINARY));
252         group.addExecutors(createAdd(addKvBucket, KeywordSubstitutionOptions.ONLY_VALUES));
253         group.addExecutors(createRemove(removeBucket));
254         group.addExecutors(createCommit(commitBucket, settings.getCommitMessage()));
255     }
256
257     private static ExecutorSupport[] createCommit(List<File JavaDoc> bucket, String JavaDoc message) {
258         if (bucket.size() == 0) return null;
259         CommitCommand cmd = new CommitCommand();
260         cmd.setFiles((File JavaDoc []) bucket.toArray(new File JavaDoc[bucket.size()]));
261         cmd.setMessage(org.netbeans.modules.versioning.util.Utils.wordWrap(message, CvsModuleConfig.getDefault().getWrapCommitMessagelength()));
262         return CommitExecutor.splitCommand(cmd, CvsVersioningSystem.getInstance(), null);
263     }
264
265     private static ExecutorSupport[] createRemove(List<File JavaDoc> bucket) {
266         if (bucket.size() == 0) return null;
267         RemoveCommand cmd = new RemoveCommand();
268         cmd.setFiles((File JavaDoc []) bucket.toArray(new File JavaDoc[bucket.size()]));
269         return RemoveExecutor.splitCommand(cmd, CvsVersioningSystem.getInstance(), null);
270     }
271
272     private static ExecutorSupport[] createAdd(List<File JavaDoc> bucket, KeywordSubstitutionOptions option) {
273         if (bucket.size() == 0) return null;
274         AddCommand cmd = new AddCommand();
275         cmd.setFiles((File JavaDoc []) bucket.toArray(new File JavaDoc[bucket.size()]));
276         cmd.setKeywordSubst(option);
277         return AddExecutor.splitCommand(cmd, CvsVersioningSystem.getInstance(), null);
278
279     }
280 }
281
Popular Tags