KickJava   Java API By Example, From Geeks To Geeks.

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


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.commit;
21
22 import java.util.prefs.PreferenceChangeEvent JavaDoc;
23
24 import org.netbeans.modules.versioning.system.cvss.*;
25 import org.netbeans.modules.versioning.system.cvss.CvsModuleConfig;
26 import org.netbeans.modules.versioning.util.VersioningListener;
27 import org.netbeans.modules.versioning.util.ListenersSupport;
28 import org.netbeans.modules.versioning.util.StringSelector;
29 import org.netbeans.modules.versioning.util.Utils;
30 import org.openide.ErrorManager;
31 import org.openide.NotifyDescriptor;
32 import org.openide.DialogDisplayer;
33 import org.openide.util.NbBundle;
34
35 import javax.swing.event.TableModelListener JavaDoc;
36 import javax.swing.event.TableModelEvent JavaDoc;
37 import javax.swing.event.DocumentListener JavaDoc;
38 import javax.swing.event.DocumentEvent JavaDoc;
39 import java.awt.Dimension JavaDoc;
40 import java.awt.Toolkit JavaDoc;
41 import java.awt.Cursor JavaDoc;
42 import java.awt.event.MouseEvent JavaDoc;
43 import java.awt.event.MouseAdapter JavaDoc;
44 import java.util.prefs.PreferenceChangeListener JavaDoc;
45 import java.util.*;
46 import java.io.File JavaDoc;
47 import java.io.StringWriter JavaDoc;
48 import java.io.FileReader JavaDoc;
49 import java.io.IOException JavaDoc;
50
51 /**
52  * Customization of commits.
53  *
54  * @author Maros Sandor
55  */

56 public class CommitSettings extends javax.swing.JPanel JavaDoc implements PreferenceChangeListener JavaDoc, TableModelListener JavaDoc, DocumentListener JavaDoc {
57     
58     static final String JavaDoc COLUMN_NAME_NAME = "name"; // NOI18N
59
static final String JavaDoc COLUMN_NAME_STICKY = "sticky"; // NOI18N
60
static final String JavaDoc COLUMN_NAME_STATUS = "status"; // NOI18N
61
static final String JavaDoc COLUMN_NAME_ACTION = "action"; // NOI18N
62
static final String JavaDoc COLUMN_NAME_PATH = "path"; // NOI18N
63

64     static final Object JavaDoc EVENT_SETTINGS_CHANGED = new Object JavaDoc();
65
66     private CommitTable commitTable;
67
68     public static class CommitFile {
69         private final CommitOptions options;
70         private final CvsFileNode node;
71
72         public CommitFile(CvsFileNode node, CommitOptions options) {
73             this.node = node;
74             this.options = options;
75         }
76
77         public CommitOptions getOptions() {
78             return options;
79         }
80
81         public CvsFileNode getNode() {
82             return node;
83         }
84     }
85     
86     public CommitSettings() {
87         Dimension JavaDoc ss = Toolkit.getDefaultToolkit().getScreenSize();
88         setPreferredSize(new Dimension JavaDoc(ss.width / 2, ss.height / 5 * 2));
89         init();
90     }
91
92     /**
93      * Set columns to display in the Commit table.
94      *
95      * @param cols array of column names
96      */

97     void setColumns(String JavaDoc[] cols) {
98         commitTable.setColumns(cols);
99     }
100
101     /**
102      * Set file nodes to display in the Commit table.
103      *
104      * @param nodes array of nodes
105      */

106     void setNodes(CvsFileNode[] nodes) {
107         commitTable.setNodes(nodes);
108     }
109     
110     public String JavaDoc getCommitMessage() {
111         return taMessage.getText();
112     }
113     
114     public void addNotify() {
115         super.addNotify();
116         CvsModuleConfig.getDefault().getPreferences().addPreferenceChangeListener(this);
117         commitTable.getTableModel().addTableModelListener(this);
118         listenerSupport.fireVersioningEvent(EVENT_SETTINGS_CHANGED);
119         taMessage.selectAll();
120         taMessage.requestFocus(); // #67106
121
}
122
123     public void removeNotify() {
124         commitTable.getTableModel().removeTableModelListener(this);
125         CvsModuleConfig.getDefault().getPreferences().removePreferenceChangeListener(this);
126         super.removeNotify();
127     }
128     
129     public void preferenceChange(PreferenceChangeEvent JavaDoc evt) {
130         if (evt.getKey().startsWith(CvsModuleConfig.PROP_COMMIT_EXCLUSIONS)) {
131             commitTable.dataChanged();
132             listenerSupport.fireVersioningEvent(EVENT_SETTINGS_CHANGED);
133         }
134     }
135
136     public CommitFile [] getCommitFiles() {
137         return commitTable.getCommitFiles();
138     }
139     
140     private void init() {
141         initComponents();
142         errorLabel.setText(""); // NOI18N
143
messageErrorLabel.setText(""); // NOI18N
144
jScrollPane1.setMinimumSize(jScrollPane1.getPreferredSize());
145         commitTable = new CommitTable(jLabel3);
146         java.awt.GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
147         gridBagConstraints.gridx = 0;
148         gridBagConstraints.gridy = 3;
149         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
150         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
151         gridBagConstraints.weightx = 1.0;
152         gridBagConstraints.weighty = 1.0;
153         add(commitTable.getComponent(), gridBagConstraints);
154         List<String JavaDoc> messages = Utils.getStringList(CvsModuleConfig.getDefault().getPreferences(), CommitAction.RECENT_COMMIT_MESSAGES);
155         if (messages.size() > 0) {
156             taMessage.setText(messages.get(0));
157         } else {
158             loadTemplate(true);
159         }
160
161         recentLink.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
162         templateLink.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
163         recentLink.addMouseListener(new MouseAdapter JavaDoc() {
164             public void mouseClicked(MouseEvent JavaDoc e) {
165                 onBrowseRecentMessages();
166             }
167         });
168         templateLink.addMouseListener(new MouseAdapter JavaDoc() {
169             public void mouseClicked(MouseEvent JavaDoc e) {
170                 loadTemplate(false);
171             }
172         });
173         taMessage.getDocument().addDocumentListener(this);
174         onCommitMessageChanged();
175     }
176
177     public void insertUpdate(DocumentEvent JavaDoc e) {
178         onCommitMessageChanged();
179     }
180
181     public void removeUpdate(DocumentEvent JavaDoc e) {
182         onCommitMessageChanged();
183     }
184
185     public void changedUpdate(DocumentEvent JavaDoc e) {
186         onCommitMessageChanged();
187     }
188
189     private void onCommitMessageChanged() {
190         if (taMessage.getText().trim().length() == 0) {
191             messageErrorLabel.setText(NbBundle.getMessage(CommitSettings.class,"MSG_CommitForm_MessageEmpty"));
192         } else {
193             messageErrorLabel.setText(""); // NOI18N
194
}
195     }
196     
197     private void loadTemplate(boolean quiet) {
198         CommitFile [] files = getCommitFiles();
199         for (CommitFile commitFile : files) {
200             File JavaDoc file = commitFile.getNode().getFile();
201             File JavaDoc templateFile = new File JavaDoc(file.getParentFile(), CvsVersioningSystem.FILENAME_CVS + "/Template"); // NOI18N
202
if (templateFile.canRead()) {
203                 StringWriter JavaDoc sw = new StringWriter JavaDoc();
204                 try {
205                     Utils.copyStreamsCloseAll(sw, new FileReader JavaDoc(templateFile));
206                     String JavaDoc message = sw.toString();
207                     if (message.trim().length() == 0) {
208                         NotifyDescriptor nd = new NotifyDescriptor(NbBundle.getMessage(CommitSettings.class, "CTL_LoadTemplate_Empty"), // NOI18N
209
NbBundle.getMessage(CommitSettings.class, "CTL_LoadTemplate_Title"), // NOI18N
210
NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.WARNING_MESSAGE, null, NotifyDescriptor.NO_OPTION);
211                         if (DialogDisplayer.getDefault().notify(nd) != NotifyDescriptor.YES_OPTION) return;
212                     }
213                     taMessage.setText(message);
214                 } catch (IOException JavaDoc e) {
215                     if (!quiet) ErrorManager.getDefault().notify(e);
216                 }
217                 return;
218             }
219         }
220         if (!quiet) {
221             NotifyDescriptor nd = new NotifyDescriptor(NbBundle.getMessage(CommitSettings.class, "CTL_LoadTemplate_NoTemplate"), // NOI18N
222
NbBundle.getMessage(CommitSettings.class, "CTL_LoadTemplate_Title"), NotifyDescriptor.DEFAULT_OPTION, NotifyDescriptor.ERROR_MESSAGE, null, null); // NOI18N
223
DialogDisplayer.getDefault().notify(nd);
224         }
225     }
226
227     private void onBrowseRecentMessages() {
228         String JavaDoc message = StringSelector.select(NbBundle.getMessage(CommitSettings.class, "CTL_RecentMessages_Prompt"), // NOI18N
229
NbBundle.getMessage(CommitSettings.class, "CTL_RecentMessages_Title"), // NOI18N
230
Utils.getStringList(CvsModuleConfig.getDefault().getPreferences(), CommitAction.RECENT_COMMIT_MESSAGES));
231         if (message != null) {
232             taMessage.replaceSelection(message);
233         }
234     }
235
236     void setErrorLabel(String JavaDoc htmlErrorLabel) {
237         errorLabel.setText(htmlErrorLabel);
238         revalidate();
239     }
240     
241     /** This method is called from within the constructor to
242      * initialize the form.
243      * WARNING: Do NOT modify this code. The content of this method is
244      * always regenerated by the Form Editor.
245      */

246     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
247
private void initComponents() {
248         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
249
250         jLabel2 = new javax.swing.JLabel JavaDoc();
251         recentLink = new javax.swing.JLabel JavaDoc();
252         templateLink = new javax.swing.JLabel JavaDoc();
253         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
254         taMessage = new org.netbeans.modules.versioning.system.cvss.ui.components.KTextArea();
255         jLabel3 = new javax.swing.JLabel JavaDoc();
256         errorLabel = new javax.swing.JLabel JavaDoc();
257         messageErrorLabel = new javax.swing.JLabel JavaDoc();
258
259         setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 0, 11));
260         setLayout(new java.awt.GridBagLayout JavaDoc());
261
262         jLabel2.setLabelFor(taMessage);
263         java.util.ResourceBundle JavaDoc bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/versioning/system/cvss/ui/actions/commit/Bundle"); // NOI18N
264
org.openide.awt.Mnemonics.setLocalizedText(jLabel2, bundle.getString("CTL_CommitForm_Message")); // NOI18N
265
jLabel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 2, 0));
266         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
267         gridBagConstraints.gridx = 0;
268         gridBagConstraints.gridy = 0;
269         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
270         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
271         gridBagConstraints.weightx = 1.0;
272         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 5);
273         add(jLabel2, gridBagConstraints);
274
275         recentLink.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
276         recentLink.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/org/netbeans/modules/versioning/system/cvss/resources/icons/recent_messages.png")));
277         recentLink.setToolTipText(org.openide.util.NbBundle.getMessage(CommitSettings.class, "CTL_CommitForm_RecentMessages")); // NOI18N
278
recentLink.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 2, 8));
279         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
280         gridBagConstraints.gridx = 1;
281         gridBagConstraints.gridy = 0;
282         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
283         add(recentLink, gridBagConstraints);
284
285         templateLink.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
286         templateLink.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/org/netbeans/modules/versioning/system/cvss/resources/icons/load_template.png")));
287         templateLink.setToolTipText(org.openide.util.NbBundle.getMessage(CommitSettings.class, "TT_CommitForm_LoadTemplate")); // NOI18N
288
templateLink.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 2, 0));
289         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
290         gridBagConstraints.gridx = 2;
291         gridBagConstraints.gridy = 0;
292         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
293         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
294         add(templateLink, gridBagConstraints);
295
296         taMessage.setColumns(30);
297         taMessage.setLineWrap(true);
298         taMessage.setRows(6);
299         taMessage.setTabSize(4);
300         taMessage.setWrapStyleWord(true);
301         jScrollPane1.setViewportView(taMessage);
302         taMessage.getAccessibleContext().setAccessibleDescription(bundle.getString("TT_CommitForm_Message")); // NOI18N
303

304         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
305         gridBagConstraints.gridx = 0;
306         gridBagConstraints.gridy = 1;
307         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
308         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
309         gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
310         gridBagConstraints.weightx = 1.0;
311         add(jScrollPane1, gridBagConstraints);
312
313         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, bundle.getString("CTL_CommitForm_FilesToCommit")); // NOI18N
314
jLabel3.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 0, 0, 0));
315         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
316         gridBagConstraints.gridx = 0;
317         gridBagConstraints.gridy = 2;
318         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
319         gridBagConstraints.anchor = java.awt.GridBagConstraints.FIRST_LINE_START;
320         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 5);
321         add(jLabel3, gridBagConstraints);
322
323         errorLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 0, 0, 0));
324         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
325         gridBagConstraints.gridx = 0;
326         gridBagConstraints.gridy = 4;
327         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
328         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
329         gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
330         gridBagConstraints.weightx = 1.0;
331         add(errorLabel, gridBagConstraints);
332
333         messageErrorLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 0, 0, 0));
334         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
335         gridBagConstraints.gridx = 0;
336         gridBagConstraints.gridy = 5;
337         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
338         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
339         gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
340         gridBagConstraints.weightx = 1.0;
341         add(messageErrorLabel, gridBagConstraints);
342     }// </editor-fold>//GEN-END:initComponents
343

344     
345     // Variables declaration - do not modify//GEN-BEGIN:variables
346
private javax.swing.JLabel JavaDoc errorLabel;
347     private javax.swing.JLabel JavaDoc jLabel2;
348     private javax.swing.JLabel JavaDoc jLabel3;
349     private javax.swing.JScrollPane JavaDoc jScrollPane1;
350     private javax.swing.JLabel JavaDoc messageErrorLabel;
351     private javax.swing.JLabel JavaDoc recentLink;
352     private org.netbeans.modules.versioning.system.cvss.ui.components.KTextArea taMessage;
353     private javax.swing.JLabel JavaDoc templateLink;
354     // End of variables declaration//GEN-END:variables
355

356     public void tableChanged(TableModelEvent JavaDoc e) {
357         listenerSupport.fireVersioningEvent(EVENT_SETTINGS_CHANGED);
358     }
359
360     ListenersSupport listenerSupport = new ListenersSupport(this);
361     public void addVersioningListener(VersioningListener listener) {
362         listenerSupport.addListener(listener);
363     }
364
365     public void removeVersioningListener(VersioningListener listener) {
366         listenerSupport.removeListener(listener);
367     }
368 }
369
Popular Tags