1 19 20 package org.netbeans.modules.ruby.railsprojects; 21 22 import java.awt.event.ItemEvent ; 23 import java.awt.event.ItemListener ; 24 import java.io.BufferedReader ; 25 import java.io.File ; 26 import java.io.FileReader ; 27 import java.io.IOException ; 28 import java.io.Reader ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import javax.swing.ComboBoxModel ; 32 import javax.swing.DefaultComboBoxModel ; 33 import javax.swing.JPanel ; 34 import javax.swing.event.ChangeEvent ; 35 import javax.swing.event.ChangeListener ; 36 import javax.swing.event.DocumentEvent ; 37 import javax.swing.event.DocumentListener ; 38 import org.jdesktop.layout.GroupLayout; 39 import org.netbeans.modules.ruby.rubyproject.api.RubyInstallation; 40 import org.openide.util.Exceptions; 41 import org.openide.util.NbBundle; 42 43 54 public class GeneratorPanel extends javax.swing.JPanel { 55 private ChangeListener changeListener; 56 57 58 public GeneratorPanel(Generator generator) { 59 initComponents(); 60 if (generator != Generator.NONE) { 61 typeCombo.setSelectedItem(generator.name()); 62 } 63 showGenerator(generator); 64 typeCombo.addItemListener(new ItemListener () { 65 public void itemStateChanged(ItemEvent e) { 66 if (e.getStateChange() == ItemEvent.SELECTED) { 67 showGenerator(Generator.valueOf(e.getItem().toString())); 68 } 69 } 70 }); 71 nameText.requestFocus(); 72 nameText.getDocument().addDocumentListener(new DocumentListener () { 73 public void insertUpdate(DocumentEvent ev) { 74 if (changeListener != null) { 75 changeListener.stateChanged(new ChangeEvent (ev)); 76 } 77 } 78 public void removeUpdate(DocumentEvent ev) { 79 if (changeListener != null) { 80 changeListener.stateChanged(new ChangeEvent (ev)); 81 } 82 } 83 84 public void changedUpdate(DocumentEvent ev) { 85 } 86 }); 87 } 88 89 void setForcing(boolean forcing) { 90 overwriteRadio.setSelected(forcing); 91 skipRadio.setSelected(!forcing); 92 } 93 94 void setPretend(boolean pretend) { 95 pretendCB.setSelected(pretend); 96 } 97 98 private ComboBoxModel getTypeModel() { 99 Generator[] generators = Generator.values(); 101 List <String > generatorNames = new ArrayList <String >(); 105 for (Generator generator : generators) { 106 if (generator == Generator.NONE) { 107 continue; 108 } 109 generatorNames.add(generator.name()); 110 } 111 112 DefaultComboBoxModel model = new DefaultComboBoxModel (generatorNames.toArray()); 113 return model; 114 } 115 116 public String getName() { 117 return nameText.getText().trim(); 118 } 119 120 public String getType() { 121 Object o = typeCombo.getSelectedItem(); 122 123 return o != null ? o.toString() : ""; 124 } 125 126 public boolean isForce() { 127 return overwriteRadio.isSelected(); 128 } 129 130 public boolean isPretend() { 131 return pretendCB.isSelected(); 132 } 133 134 private void showGenerator(Generator generator) { 135 switch (generator) { 136 case controller: setOptions("Views", null); break; case scaffold: setOptions("ScaffControllerName", "ScaffoldActions"); break; case mailer: setOptions("Views", null); break; case web_service: setOptions("ApiMethods", null); break; case other: setOptions("Arg1", "Arg2"); break; case model: case integration_test: case migration: case session_migration: case plugin: default: setOptions(null, null); break; 147 } 148 149 showUsage(generator); 150 } 151 152 private void showUsage(Generator generator) { 153 156 usageText.setText(""); 157 158 String version = RubyInstallation.getInstance().getVersion("rails"); if (version == null) { 161 return; 162 } 163 164 String rubyLib = RubyInstallation.getInstance().getRubyLib(); 165 File gemDir = new File (rubyLib+File.separator+"ruby"+File.separator+"gems"+File.separator+ 166 RubyInstallation.RUBY_RELEASE+File.separator+"gems"+File.separator); 167 File generatorDir = new File (gemDir, "rails" + "-" + version + File.separator + 169 "lib" + File.separator + "rails_generator" + File.separator + "generators" + File.separator + 170 "components" + File.separator + generator.name()); 171 File usageFile = new File (generatorDir, "USAGE"); 172 if (!usageFile.exists()) { 173 return; 174 } 175 176 StringBuilder sb = new StringBuilder (); 177 try { 178 BufferedReader fr = new BufferedReader (new FileReader (usageFile)); 179 while (true) { 180 String line = fr.readLine(); 181 if (line == null) { 182 break; 183 } 184 sb.append(line); 185 sb.append("\n"); 186 } 187 188 usageText.setText(sb.toString()); 189 usageText.getCaret().setDot(0); 190 } catch (IOException ioe) { 191 Exceptions.printStackTrace(ioe); 192 } 193 } 194 195 private void setOptions(String firstParameterKey, String secondParameterKey) { 196 boolean visible = firstParameterKey != null; 197 parameter1Label.setVisible(visible); 198 parameter1Text.setVisible(visible); 199 if (visible) { 200 parameter1Label.setText(NbBundle.getMessage(GeneratorPanel.class, firstParameterKey)); 201 } 202 203 visible = secondParameterKey != null; 204 parameter2Label.setVisible(visible); 205 parameter2Text.setVisible(visible); 206 if (visible) { 207 parameter2Label.setText(NbBundle.getMessage(GeneratorPanel.class, secondParameterKey)); 208 } 209 210 invalidate(); 211 revalidate(); 212 repaint(); 213 } 214 215 public String [] getFirstParameterList() { 216 if (parameter1Text.isVisible()) { 217 return parameter1Text.getText().replace(',', ' ').trim().split(" "); 219 } else { 220 return null; 221 } 222 } 223 224 public String [] getSecondParameterList() { 225 if (parameter2Text.isVisible()) { 226 return parameter2Text.getText().replace(',', ' ').trim().split(" "); 227 } else { 228 return null; 229 } 230 } 231 232 public void setChangeListener (ChangeListener l) { 233 changeListener = l; 234 } 235 236 241 private void initComponents() { 243 java.awt.GridBagConstraints gridBagConstraints; 244 245 forceGroup = new javax.swing.ButtonGroup (); 246 generateLabel = new javax.swing.JLabel (); 247 pretendCB = new javax.swing.JCheckBox (); 248 typeCombo = new javax.swing.JComboBox (); 249 skipRadio = new javax.swing.JRadioButton (); 250 overwriteRadio = new javax.swing.JRadioButton (); 251 optionsPanel = new javax.swing.JPanel (); 252 nameLabel = new javax.swing.JLabel (); 253 nameText = new javax.swing.JTextField (); 254 parameter1Label = new javax.swing.JLabel (); 255 parameter1Text = new javax.swing.JTextField (); 256 parameter2Label = new javax.swing.JLabel (); 257 parameter2Text = new javax.swing.JTextField (); 258 forceLabel = new javax.swing.JLabel (); 259 jSeparator1 = new javax.swing.JSeparator (); 260 jScrollPane1 = new javax.swing.JScrollPane (); 261 usageText = new javax.swing.JTextArea (); 262 jSeparator2 = new javax.swing.JSeparator (); 263 264 generateLabel.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.generateLabel.text")); 266 pretendCB.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.pretendCB.text")); pretendCB.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 268 pretendCB.setMargin(new java.awt.Insets (0, 0, 0, 0)); 269 270 typeCombo.setModel(getTypeModel()); 271 272 forceGroup.add(skipRadio); 273 skipRadio.setSelected(true); 274 skipRadio.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.skipRadio.text")); skipRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 276 skipRadio.setMargin(new java.awt.Insets (0, 0, 0, 0)); 277 278 forceGroup.add(overwriteRadio); 279 overwriteRadio.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.overwriteRadio.text")); overwriteRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 281 overwriteRadio.setMargin(new java.awt.Insets (0, 0, 0, 0)); 282 283 optionsPanel.setLayout(new java.awt.GridBagLayout ()); 284 285 nameLabel.setText(NbBundle.getMessage(GeneratorPanel.class, "Name")); gridBagConstraints = new java.awt.GridBagConstraints (); 287 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 288 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 11); 289 optionsPanel.add(nameLabel, gridBagConstraints); 290 291 nameText.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.nameText.text")); gridBagConstraints = new java.awt.GridBagConstraints (); 293 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 294 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 295 gridBagConstraints.weightx = 1.0; 296 optionsPanel.add(nameText, gridBagConstraints); 297 298 parameter1Label.setText("jLabel2"); 299 gridBagConstraints = new java.awt.GridBagConstraints (); 300 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 301 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 11); 302 optionsPanel.add(parameter1Label, gridBagConstraints); 303 304 parameter1Text.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.parameter1Text.text")); gridBagConstraints = new java.awt.GridBagConstraints (); 306 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 307 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 308 gridBagConstraints.weightx = 1.0; 309 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 310 optionsPanel.add(parameter1Text, gridBagConstraints); 311 312 parameter2Label.setText("jLabel1"); 313 gridBagConstraints = new java.awt.GridBagConstraints (); 314 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 315 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 11); 316 optionsPanel.add(parameter2Label, gridBagConstraints); 317 318 parameter2Text.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.parameter2Text.text")); gridBagConstraints = new java.awt.GridBagConstraints (); 320 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 321 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 322 gridBagConstraints.weightx = 1.0; 323 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 324 optionsPanel.add(parameter2Text, gridBagConstraints); 325 326 forceLabel.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.forceLabel.text")); 328 usageText.setColumns(20); 329 usageText.setEditable(false); 330 usageText.setRows(5); 331 jScrollPane1.setViewportView(usageText); 332 333 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 334 this.setLayout(layout); 335 layout.setHorizontalGroup( 336 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 337 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 338 .addContainerGap() 339 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 340 .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE) 341 .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE) 342 .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() 343 .add(forceLabel) 344 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 345 .add(skipRadio) 346 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 347 .add(overwriteRadio)) 348 .add(optionsPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE) 349 .add(org.jdesktop.layout.GroupLayout.LEADING, pretendCB) 350 .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() 351 .add(generateLabel) 352 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 353 .add(typeCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 354 .add(org.jdesktop.layout.GroupLayout.LEADING, jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE)) 355 .addContainerGap()) 356 ); 357 layout.setVerticalGroup( 358 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 359 .add(layout.createSequentialGroup() 360 .addContainerGap() 361 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 362 .add(generateLabel) 363 .add(typeCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 364 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 365 .add(optionsPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 366 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 367 .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 368 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 369 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 370 .add(forceLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 371 .add(skipRadio) 372 .add(overwriteRadio)) 373 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 374 .add(pretendCB) 375 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 376 .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 377 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 378 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE) 379 .addContainerGap()) 380 ); 381 } 383 384 private javax.swing.ButtonGroup forceGroup; 386 private javax.swing.JLabel forceLabel; 387 private javax.swing.JLabel generateLabel; 388 private javax.swing.JScrollPane jScrollPane1; 389 private javax.swing.JSeparator jSeparator1; 390 private javax.swing.JSeparator jSeparator2; 391 private javax.swing.JLabel nameLabel; 392 private javax.swing.JTextField nameText; 393 private javax.swing.JPanel optionsPanel; 394 private javax.swing.JRadioButton overwriteRadio; 395 private javax.swing.JLabel parameter1Label; 396 private javax.swing.JTextField parameter1Text; 397 private javax.swing.JLabel parameter2Label; 398 private javax.swing.JTextField parameter2Text; 399 private javax.swing.JCheckBox pretendCB; 400 private javax.swing.JRadioButton skipRadio; 401 private javax.swing.JComboBox typeCombo; 402 private javax.swing.JTextArea usageText; 403 405 } 406 | Popular Tags |