1 19 20 package org.netbeans.modules.ruby.railsprojects.ui.customizer; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.awt.Font ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.awt.event.MouseEvent ; 28 import java.io.File ; 29 import java.text.Collator ; 30 import java.util.Comparator ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import java.util.SortedSet ; 35 import java.util.TreeSet ; 36 import javax.swing.DefaultComboBoxModel ; 37 import javax.swing.DefaultListCellRenderer ; 38 import javax.swing.JButton ; 39 import javax.swing.JFileChooser ; 40 import javax.swing.JLabel ; 41 import javax.swing.JList ; 42 import javax.swing.JPanel ; 43 import javax.swing.JTextField ; 44 import javax.swing.event.ChangeEvent ; 45 import javax.swing.event.ChangeListener ; 46 import javax.swing.event.DocumentEvent ; 47 import javax.swing.event.DocumentListener ; 48 import org.netbeans.modules.ruby.railsprojects.RailsProject; 49 import org.netbeans.modules.ruby.railsprojects.SourceRoots; 50 import org.openide.DialogDescriptor; 51 import org.openide.DialogDisplayer; 52 import org.openide.NotifyDescriptor; 53 import org.openide.awt.MouseUtils; 54 import org.openide.filesystems.FileUtil; 55 import org.openide.util.HelpCtx; 56 import org.openide.util.NbBundle; 57 import org.openide.util.Utilities; 58 59 public class CustomizerRun extends JPanel implements HelpCtx.Provider { 60 61 private RailsProject project; 62 63 private JTextField [] data; 64 private JLabel [] dataLabels; 65 private String [] keys; 66 private Map <String ,Map <String ,String >> configs; 67 RailsProjectProperties uiProperties; 68 69 public CustomizerRun( RailsProjectProperties uiProperties ) { 70 this.uiProperties = uiProperties; 71 initComponents(); 72 73 this.project = uiProperties.getProject(); 74 75 configs = uiProperties.RUN_CONFIGS; 76 77 data = new JTextField [] { 78 jTextFieldMainClass, 79 jTextFieldArgs, 80 jTextVMOptions, 81 jTextWorkingDirectory, 82 }; 83 dataLabels = new JLabel [] { 84 jLabelMainClass, 85 jLabelArgs, 86 jLabelVMOptions, 87 jLabelWorkingDirectory, 88 }; 89 keys = new String [] { 90 RailsProjectProperties.MAIN_CLASS, 91 RailsProjectProperties.APPLICATION_ARGS, 92 RailsProjectProperties.RUN_JVM_ARGS, 93 RailsProjectProperties.RUN_WORK_DIR, 94 }; 95 assert data.length == keys.length; 96 97 configChanged(uiProperties.activeConfig); 98 99 configCombo.setRenderer(new DefaultListCellRenderer () { 100 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 101 String config = (String ) value; 102 String label; 103 if (config == null) { 104 label = null; 106 } else if (config.length() > 0) { 107 Map <String ,String > m = configs.get(config); 108 label = m != null ? m.get("$label") : null; 109 if (label == null) { 110 label = config; 111 } 112 } else { 113 label = NbBundle.getMessage(CustomizerRun.class, "CustomizerRun.default"); 114 } 115 return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus); 116 } 117 }); 118 119 for (int i = 0; i < data.length; i++) { 120 final JTextField field = data[i]; 121 final String prop = keys[i]; 122 final JLabel label = dataLabels[i]; 123 field.getDocument().addDocumentListener(new DocumentListener () { 124 Font basefont = label.getFont(); 125 Font boldfont = basefont.deriveFont(Font.BOLD); 126 { 127 updateFont(); 128 } 129 public void insertUpdate(DocumentEvent e) { 130 changed(); 131 } 132 public void removeUpdate(DocumentEvent e) { 133 changed(); 134 } 135 public void changedUpdate(DocumentEvent e) {} 136 void changed() { 137 String config = (String ) configCombo.getSelectedItem(); 138 if (config.length() == 0) { 139 config = null; 140 } 141 String v = field.getText(); 142 if (v != null && config != null && v.equals(configs.get(null).get(prop))) { 143 v = null; 145 } 146 configs.get(config).put(prop, v); 147 updateFont(); 148 } 149 void updateFont() { 150 String v = field.getText(); 151 String config = (String ) configCombo.getSelectedItem(); 152 if (config.length() == 0) { 153 config = null; 154 } 155 String def = configs.get(null).get(prop); 156 label.setFont(config != null && !Utilities.compareObjects(v != null ? v : "", def != null ? def : "") ? boldfont : basefont); 157 } 158 }); 159 } 160 } 161 162 public HelpCtx getHelpCtx() { 163 return new HelpCtx( CustomizerRun.class ); 164 } 165 166 171 private void initComponents() { 173 java.awt.GridBagConstraints gridBagConstraints; 174 175 configSep = new javax.swing.JSeparator (); 176 configPanel = new javax.swing.JPanel (); 177 configLabel = new javax.swing.JLabel (); 178 configCombo = new javax.swing.JComboBox (); 179 configNew = new javax.swing.JButton (); 180 configDel = new javax.swing.JButton (); 181 mainPanel = new javax.swing.JPanel (); 182 jLabelMainClass = new javax.swing.JLabel (); 183 jTextFieldMainClass = new javax.swing.JTextField (); 184 jButtonMainClass = new javax.swing.JButton (); 185 jLabelArgs = new javax.swing.JLabel (); 186 jTextFieldArgs = new javax.swing.JTextField (); 187 jLabelWorkingDirectory = new javax.swing.JLabel (); 188 jTextWorkingDirectory = new javax.swing.JTextField (); 189 jButtonWorkingDirectoryBrowse = new javax.swing.JButton (); 190 jLabelVMOptions = new javax.swing.JLabel (); 191 jTextVMOptions = new javax.swing.JTextField (); 192 jLabelVMOptionsExample = new javax.swing.JLabel (); 193 194 setLayout(new java.awt.GridBagLayout ()); 195 gridBagConstraints = new java.awt.GridBagConstraints (); 196 gridBagConstraints.gridx = 0; 197 gridBagConstraints.gridy = 1; 198 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 199 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 200 gridBagConstraints.insets = new java.awt.Insets (6, 0, 6, 0); 201 add(configSep, gridBagConstraints); 202 203 configPanel.setLayout(new java.awt.GridBagLayout ()); 204 205 configLabel.setLabelFor(configCombo); 206 org.openide.awt.Mnemonics.setLocalizedText(configLabel, org.openide.util.NbBundle.getMessage(CustomizerRun.class, "CustomizerRun.configLabel")); gridBagConstraints = new java.awt.GridBagConstraints (); 208 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 209 gridBagConstraints.insets = new java.awt.Insets (2, 0, 2, 0); 210 configPanel.add(configLabel, gridBagConstraints); 211 212 configCombo.setModel(new javax.swing.DefaultComboBoxModel (new String [] { "<default>" })); 213 configCombo.addActionListener(new java.awt.event.ActionListener () { 214 public void actionPerformed(java.awt.event.ActionEvent evt) { 215 configComboActionPerformed(evt); 216 } 217 }); 218 gridBagConstraints = new java.awt.GridBagConstraints (); 219 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 220 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 221 gridBagConstraints.weightx = 1.0; 222 gridBagConstraints.insets = new java.awt.Insets (2, 6, 2, 0); 223 configPanel.add(configCombo, gridBagConstraints); 224 225 org.openide.awt.Mnemonics.setLocalizedText(configNew, org.openide.util.NbBundle.getMessage(CustomizerRun.class, "CustomizerRun.configNew")); configNew.addActionListener(new java.awt.event.ActionListener () { 227 public void actionPerformed(java.awt.event.ActionEvent evt) { 228 configNewActionPerformed(evt); 229 } 230 }); 231 gridBagConstraints = new java.awt.GridBagConstraints (); 232 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 233 gridBagConstraints.insets = new java.awt.Insets (2, 6, 2, 0); 234 configPanel.add(configNew, gridBagConstraints); 235 236 org.openide.awt.Mnemonics.setLocalizedText(configDel, org.openide.util.NbBundle.getMessage(CustomizerRun.class, "CustomizerRun.configDelete")); configDel.addActionListener(new java.awt.event.ActionListener () { 238 public void actionPerformed(java.awt.event.ActionEvent evt) { 239 configDelActionPerformed(evt); 240 } 241 }); 242 gridBagConstraints = new java.awt.GridBagConstraints (); 243 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 244 gridBagConstraints.insets = new java.awt.Insets (2, 6, 2, 0); 245 configPanel.add(configDel, gridBagConstraints); 246 247 gridBagConstraints = new java.awt.GridBagConstraints (); 248 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 249 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 250 gridBagConstraints.insets = new java.awt.Insets (6, 0, 6, 0); 251 add(configPanel, gridBagConstraints); 252 253 mainPanel.setLayout(new java.awt.GridBagLayout ()); 254 255 jLabelMainClass.setLabelFor(jTextFieldMainClass); 256 org.openide.awt.Mnemonics.setLocalizedText(jLabelMainClass, org.openide.util.NbBundle.getMessage(CustomizerRun.class, "LBL_CustomizeRun_Run_MainClass_JLabel")); gridBagConstraints = new java.awt.GridBagConstraints (); 258 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 259 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 260 mainPanel.add(jLabelMainClass, gridBagConstraints); 261 gridBagConstraints = new java.awt.GridBagConstraints (); 262 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 263 gridBagConstraints.weightx = 1.0; 264 gridBagConstraints.insets = new java.awt.Insets (0, 12, 5, 0); 265 mainPanel.add(jTextFieldMainClass, gridBagConstraints); 266 jTextFieldMainClass.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(CustomizerRun.class).getString("AD_jTextFieldMainClass")); 268 org.openide.awt.Mnemonics.setLocalizedText(jButtonMainClass, org.openide.util.NbBundle.getMessage(CustomizerRun.class, "LBL_CustomizeRun_Run_MainClass_JButton")); gridBagConstraints = new java.awt.GridBagConstraints (); 270 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 271 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 272 gridBagConstraints.insets = new java.awt.Insets (0, 6, 5, 0); 273 mainPanel.add(jButtonMainClass, gridBagConstraints); 274 jButtonMainClass.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(CustomizerRun.class).getString("AD_jButtonMainClass")); 276 jLabelArgs.setLabelFor(jTextFieldArgs); 277 org.openide.awt.Mnemonics.setLocalizedText(jLabelArgs, org.openide.util.NbBundle.getMessage(CustomizerRun.class, "LBL_CustomizeRun_Run_Args_JLabel")); gridBagConstraints = new java.awt.GridBagConstraints (); 279 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 280 gridBagConstraints.insets = new java.awt.Insets (0, 0, 12, 0); 281 mainPanel.add(jLabelArgs, gridBagConstraints); 282 gridBagConstraints = new java.awt.GridBagConstraints (); 283 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 284 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 285 gridBagConstraints.weightx = 1.0; 286 gridBagConstraints.insets = new java.awt.Insets (0, 12, 12, 0); 287 mainPanel.add(jTextFieldArgs, gridBagConstraints); 288 jTextFieldArgs.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(CustomizerRun.class).getString("AD_jTextFieldArgs")); 290 jLabelWorkingDirectory.setLabelFor(jTextWorkingDirectory); 291 org.openide.awt.Mnemonics.setLocalizedText(jLabelWorkingDirectory, org.openide.util.NbBundle.getMessage(CustomizerRun.class, "LBL_CustomizeRun_Run_Working_Directory")); gridBagConstraints = new java.awt.GridBagConstraints (); 293 gridBagConstraints.gridy = 2; 294 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 295 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 296 mainPanel.add(jLabelWorkingDirectory, gridBagConstraints); 297 gridBagConstraints = new java.awt.GridBagConstraints (); 298 gridBagConstraints.gridy = 2; 299 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 300 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 301 gridBagConstraints.weightx = 1.0; 302 gridBagConstraints.insets = new java.awt.Insets (0, 12, 5, 0); 303 mainPanel.add(jTextWorkingDirectory, gridBagConstraints); 304 java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/ruby/railsprojects/ui/customizer/Bundle"); jTextWorkingDirectory.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_CustomizeRun_Run_Working_Directory")); 307 org.openide.awt.Mnemonics.setLocalizedText(jButtonWorkingDirectoryBrowse, org.openide.util.NbBundle.getMessage(CustomizerRun.class, "LBL_CustomizeRun_Run_Working_Directory_Browse")); jButtonWorkingDirectoryBrowse.addActionListener(new java.awt.event.ActionListener () { 309 public void actionPerformed(java.awt.event.ActionEvent evt) { 310 jButtonWorkingDirectoryBrowseActionPerformed(evt); 311 } 312 }); 313 gridBagConstraints = new java.awt.GridBagConstraints (); 314 gridBagConstraints.gridy = 2; 315 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 316 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 317 gridBagConstraints.insets = new java.awt.Insets (0, 6, 5, 0); 318 mainPanel.add(jButtonWorkingDirectoryBrowse, gridBagConstraints); 319 jButtonWorkingDirectoryBrowse.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_CustomizeRun_Run_Working_Directory_Browse")); 321 jLabelVMOptions.setLabelFor(jTextVMOptions); 322 org.openide.awt.Mnemonics.setLocalizedText(jLabelVMOptions, org.openide.util.NbBundle.getMessage(CustomizerRun.class, "LBL_CustomizeRun_Run_VM_Options")); gridBagConstraints = new java.awt.GridBagConstraints (); 324 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 325 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 326 mainPanel.add(jLabelVMOptions, gridBagConstraints); 327 gridBagConstraints = new java.awt.GridBagConstraints (); 328 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 329 gridBagConstraints.weightx = 1.0; 330 gridBagConstraints.insets = new java.awt.Insets (0, 12, 0, 0); 331 mainPanel.add(jTextVMOptions, gridBagConstraints); 332 jTextVMOptions.getAccessibleContext().setAccessibleDescription(bundle.getString("AD_CustomizeRun_Run_VM_Options")); 334 jLabelVMOptionsExample.setLabelFor(jTextFieldMainClass); 335 org.openide.awt.Mnemonics.setLocalizedText(jLabelVMOptionsExample, org.openide.util.NbBundle.getMessage(CustomizerRun.class, "LBL_CustomizeRun_Run_VM_Options_Example")); gridBagConstraints = new java.awt.GridBagConstraints (); 337 gridBagConstraints.gridx = 1; 338 gridBagConstraints.gridy = 4; 339 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 340 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 341 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 342 gridBagConstraints.weighty = 1.0; 343 gridBagConstraints.insets = new java.awt.Insets (0, 12, 12, 0); 344 mainPanel.add(jLabelVMOptionsExample, gridBagConstraints); 345 jLabelVMOptionsExample.getAccessibleContext().setAccessibleDescription(bundle.getString("LBL_CustomizeRun_Run_VM_Options_Example")); 347 gridBagConstraints = new java.awt.GridBagConstraints (); 348 gridBagConstraints.gridx = 0; 349 gridBagConstraints.gridy = 2; 350 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 351 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 352 gridBagConstraints.weightx = 1.0; 353 gridBagConstraints.weighty = 1.0; 354 gridBagConstraints.insets = new java.awt.Insets (6, 0, 6, 0); 355 add(mainPanel, gridBagConstraints); 356 } 358 private void configDelActionPerformed(java.awt.event.ActionEvent evt) { String config = (String ) configCombo.getSelectedItem(); 360 assert config != null; 361 configs.put(config, null); 362 configChanged(null); 363 uiProperties.activeConfig = null; 364 } 366 private void configNewActionPerformed(java.awt.event.ActionEvent evt) { NotifyDescriptor.InputLine d = new NotifyDescriptor.InputLine( 368 NbBundle.getMessage(CustomizerRun.class, "CustomizerRun.input.prompt"), 369 NbBundle.getMessage(CustomizerRun.class, "CustomizerRun.input.title")); 370 if (DialogDisplayer.getDefault().notify(d) != NotifyDescriptor.OK_OPTION) { 371 return; 372 } 373 String name = d.getInputText(); 374 String config = name.replaceAll("[^a-zA-Z0-9_.-]", "_"); if (configs.get(config) != null) { 376 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 377 NbBundle.getMessage(CustomizerRun.class, "CustomizerRun.input.duplicate", config), 378 NotifyDescriptor.WARNING_MESSAGE)); 379 return; 380 } 381 Map <String ,String > m = new HashMap <String ,String >(); 382 if (!name.equals(config)) { 383 m.put("$label", name); } 385 configs.put(config, m); 386 configChanged(config); 387 uiProperties.activeConfig = config; 388 } 390 private void configComboActionPerformed(java.awt.event.ActionEvent evt) { String config = (String ) configCombo.getSelectedItem(); 392 if (config.length() == 0) { 393 config = null; 394 } 395 configChanged(config); 396 uiProperties.activeConfig = config; 397 } 399 private void jButtonWorkingDirectoryBrowseActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser (); 401 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 402 chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY); 403 chooser.setMultiSelectionEnabled(false); 404 405 String workDir = jTextWorkingDirectory.getText(); 406 if (workDir.equals("")) { 407 workDir = FileUtil.toFile(project.getProjectDirectory()).getAbsolutePath(); 408 } 409 chooser.setSelectedFile(new File (workDir)); 410 chooser.setDialogTitle(NbBundle.getMessage(CustomizerRun.class, "LBL_CustomizeRun_Run_Working_Directory_Browse_Title")); 411 if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) { File file = FileUtil.normalizeFile(chooser.getSelectedFile()); 413 jTextWorkingDirectory.setText(file.getAbsolutePath()); 414 } 415 } 417 private void configChanged(String activeConfig) { 418 DefaultComboBoxModel model = new DefaultComboBoxModel (); 419 model.addElement(""); 420 SortedSet <String > alphaConfigs = new TreeSet <String >(new Comparator <String >() { 421 Collator coll = Collator.getInstance(); 422 public int compare(String s1, String s2) { 423 return coll.compare(label(s1), label(s2)); 424 } 425 private String label(String c) { 426 Map <String ,String > m = configs.get(c); 427 String label = m.get("$label"); return label != null ? label : c; 429 } 430 }); 431 for (Map.Entry <String ,Map <String ,String >> entry : configs.entrySet()) { 432 String config = entry.getKey(); 433 if (config != null && entry.getValue() != null) { 434 alphaConfigs.add(config); 435 } 436 } 437 for (String c : alphaConfigs) { 438 model.addElement(c); 439 } 440 configCombo.setModel(model); 441 configCombo.setSelectedItem(activeConfig != null ? activeConfig : ""); 442 Map <String ,String > m = configs.get(activeConfig); 443 Map <String ,String > def = configs.get(null); 444 if (m != null) { 445 for (int i = 0; i < data.length; i++) { 446 String v = m.get(keys[i]); 447 if (v == null) { 448 v = def.get(keys[i]); 450 } 451 data[i].setText(v); 452 } 453 } configDel.setEnabled(activeConfig != null); 455 } 456 457 458 private javax.swing.JComboBox configCombo; 460 private javax.swing.JButton configDel; 461 private javax.swing.JLabel configLabel; 462 private javax.swing.JButton configNew; 463 private javax.swing.JPanel configPanel; 464 private javax.swing.JSeparator configSep; 465 private javax.swing.JButton jButtonMainClass; 466 private javax.swing.JButton jButtonWorkingDirectoryBrowse; 467 private javax.swing.JLabel jLabelArgs; 468 private javax.swing.JLabel jLabelMainClass; 469 private javax.swing.JLabel jLabelVMOptions; 470 private javax.swing.JLabel jLabelVMOptionsExample; 471 private javax.swing.JLabel jLabelWorkingDirectory; 472 private javax.swing.JTextField jTextFieldArgs; 473 private javax.swing.JTextField jTextFieldMainClass; 474 private javax.swing.JTextField jTextVMOptions; 475 private javax.swing.JTextField jTextWorkingDirectory; 476 private javax.swing.JPanel mainPanel; 477 479 480 } 481 | Popular Tags |