1 19 20 package org.netbeans.modules.versioning.system.cvss.ui.actions.commit; 21 22 import java.util.prefs.PreferenceChangeEvent ; 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 ; 36 import javax.swing.event.TableModelEvent ; 37 import javax.swing.event.DocumentListener ; 38 import javax.swing.event.DocumentEvent ; 39 import java.awt.Dimension ; 40 import java.awt.Toolkit ; 41 import java.awt.Cursor ; 42 import java.awt.event.MouseEvent ; 43 import java.awt.event.MouseAdapter ; 44 import java.util.prefs.PreferenceChangeListener ; 45 import java.util.*; 46 import java.io.File ; 47 import java.io.StringWriter ; 48 import java.io.FileReader ; 49 import java.io.IOException ; 50 51 56 public class CommitSettings extends javax.swing.JPanel implements PreferenceChangeListener , TableModelListener , DocumentListener { 57 58 static final String COLUMN_NAME_NAME = "name"; static final String COLUMN_NAME_STICKY = "sticky"; static final String COLUMN_NAME_STATUS = "status"; static final String COLUMN_NAME_ACTION = "action"; static final String COLUMN_NAME_PATH = "path"; 64 static final Object EVENT_SETTINGS_CHANGED = new Object (); 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 ss = Toolkit.getDefaultToolkit().getScreenSize(); 88 setPreferredSize(new Dimension (ss.width / 2, ss.height / 5 * 2)); 89 init(); 90 } 91 92 97 void setColumns(String [] cols) { 98 commitTable.setColumns(cols); 99 } 100 101 106 void setNodes(CvsFileNode[] nodes) { 107 commitTable.setNodes(nodes); 108 } 109 110 public String 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(); } 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 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(""); messageErrorLabel.setText(""); jScrollPane1.setMinimumSize(jScrollPane1.getPreferredSize()); 145 commitTable = new CommitTable(jLabel3); 146 java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 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 > 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 () { 164 public void mouseClicked(MouseEvent e) { 165 onBrowseRecentMessages(); 166 } 167 }); 168 templateLink.addMouseListener(new MouseAdapter () { 169 public void mouseClicked(MouseEvent e) { 170 loadTemplate(false); 171 } 172 }); 173 taMessage.getDocument().addDocumentListener(this); 174 onCommitMessageChanged(); 175 } 176 177 public void insertUpdate(DocumentEvent e) { 178 onCommitMessageChanged(); 179 } 180 181 public void removeUpdate(DocumentEvent e) { 182 onCommitMessageChanged(); 183 } 184 185 public void changedUpdate(DocumentEvent 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(""); } 195 } 196 197 private void loadTemplate(boolean quiet) { 198 CommitFile [] files = getCommitFiles(); 199 for (CommitFile commitFile : files) { 200 File file = commitFile.getNode().getFile(); 201 File templateFile = new File (file.getParentFile(), CvsVersioningSystem.FILENAME_CVS + "/Template"); if (templateFile.canRead()) { 203 StringWriter sw = new StringWriter (); 204 try { 205 Utils.copyStreamsCloseAll(sw, new FileReader (templateFile)); 206 String message = sw.toString(); 207 if (message.trim().length() == 0) { 208 NotifyDescriptor nd = new NotifyDescriptor(NbBundle.getMessage(CommitSettings.class, "CTL_LoadTemplate_Empty"), NbBundle.getMessage(CommitSettings.class, "CTL_LoadTemplate_Title"), 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 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"), NbBundle.getMessage(CommitSettings.class, "CTL_LoadTemplate_Title"), NotifyDescriptor.DEFAULT_OPTION, NotifyDescriptor.ERROR_MESSAGE, null, null); DialogDisplayer.getDefault().notify(nd); 224 } 225 } 226 227 private void onBrowseRecentMessages() { 228 String message = StringSelector.select(NbBundle.getMessage(CommitSettings.class, "CTL_RecentMessages_Prompt"), NbBundle.getMessage(CommitSettings.class, "CTL_RecentMessages_Title"), Utils.getStringList(CvsModuleConfig.getDefault().getPreferences(), CommitAction.RECENT_COMMIT_MESSAGES)); 231 if (message != null) { 232 taMessage.replaceSelection(message); 233 } 234 } 235 236 void setErrorLabel(String htmlErrorLabel) { 237 errorLabel.setText(htmlErrorLabel); 238 revalidate(); 239 } 240 241 246 private void initComponents() { 248 java.awt.GridBagConstraints gridBagConstraints; 249 250 jLabel2 = new javax.swing.JLabel (); 251 recentLink = new javax.swing.JLabel (); 252 templateLink = new javax.swing.JLabel (); 253 jScrollPane1 = new javax.swing.JScrollPane (); 254 taMessage = new org.netbeans.modules.versioning.system.cvss.ui.components.KTextArea(); 255 jLabel3 = new javax.swing.JLabel (); 256 errorLabel = new javax.swing.JLabel (); 257 messageErrorLabel = new javax.swing.JLabel (); 258 259 setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 0, 11)); 260 setLayout(new java.awt.GridBagLayout ()); 261 262 jLabel2.setLabelFor(taMessage); 263 java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/versioning/system/cvss/ui/actions/commit/Bundle"); org.openide.awt.Mnemonics.setLocalizedText(jLabel2, bundle.getString("CTL_CommitForm_Message")); jLabel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 2, 0)); 266 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (0, 0, 0, 5); 273 add(jLabel2, gridBagConstraints); 274 275 recentLink.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); 276 recentLink.setIcon(new javax.swing.ImageIcon (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")); recentLink.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 2, 8)); 279 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (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")); templateLink.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 2, 0)); 289 gridBagConstraints = new java.awt.GridBagConstraints (); 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")); 304 gridBagConstraints = new java.awt.GridBagConstraints (); 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")); jLabel3.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 0, 0, 0)); 315 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (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 (); 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 (); 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 } 344 345 private javax.swing.JLabel errorLabel; 347 private javax.swing.JLabel jLabel2; 348 private javax.swing.JLabel jLabel3; 349 private javax.swing.JScrollPane jScrollPane1; 350 private javax.swing.JLabel messageErrorLabel; 351 private javax.swing.JLabel recentLink; 352 private org.netbeans.modules.versioning.system.cvss.ui.components.KTextArea taMessage; 353 private javax.swing.JLabel templateLink; 354 356 public void tableChanged(TableModelEvent 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 |