KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > GeneratorPanel


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.ruby.railsprojects;
21
22 import java.awt.event.ItemEvent JavaDoc;
23 import java.awt.event.ItemListener JavaDoc;
24 import java.io.BufferedReader JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.ComboBoxModel JavaDoc;
32 import javax.swing.DefaultComboBoxModel JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.event.ChangeEvent JavaDoc;
35 import javax.swing.event.ChangeListener JavaDoc;
36 import javax.swing.event.DocumentEvent JavaDoc;
37 import javax.swing.event.DocumentListener JavaDoc;
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 /**
44  * TODO: Do some checking of the arguments. For example, the documentation says
45  * to not suffix plugins with the name "Plugin", or test with "Test", so I
46  * should enforce that. The docs also give some clues about what you should be
47  * typing; it would be nice to include this documentation right there in the
48  * dialog, or perhaps through a validator.
49  * TODO: I should use the args-splitting logic from Utilities here such that
50  * the usage examples work better
51  *
52  * @author Tor Norbye
53  */

54 public class GeneratorPanel extends javax.swing.JPanel JavaDoc {
55     private ChangeListener JavaDoc changeListener;
56
57     /** Creates new form GeneratorPanel */
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 JavaDoc() {
65             public void itemStateChanged(ItemEvent JavaDoc 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 JavaDoc() {
73             public void insertUpdate(DocumentEvent JavaDoc ev) {
74                 if (changeListener != null) {
75                     changeListener.stateChanged(new ChangeEvent JavaDoc(ev));
76                 }
77             }
78             public void removeUpdate(DocumentEvent JavaDoc ev) {
79                 if (changeListener != null) {
80                     changeListener.stateChanged(new ChangeEvent JavaDoc(ev));
81                 }
82             }
83             
84             public void changedUpdate(DocumentEvent JavaDoc 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 JavaDoc getTypeModel() {
99         // TODO - Show localized name in the combo, not just the generator mode name
100
Generator[] generators = Generator.values();
101         // TODO: Generate this list from
102
// ./lib/ruby/gems/1.8/gems/rails-1.1.6/lib/rails_generator/generators/components
103
// ALSO: Pull in the USAGE documentation right there in the dialog!
104
List JavaDoc<String JavaDoc> generatorNames = new ArrayList JavaDoc<String JavaDoc>();
105         for (Generator generator : generators) {
106             if (generator == Generator.NONE) {
107                 continue;
108             }
109             generatorNames.add(generator.name());
110         }
111         
112         DefaultComboBoxModel JavaDoc model = new DefaultComboBoxModel JavaDoc(generatorNames.toArray());
113         return model;
114     }
115     
116     public String JavaDoc getName() {
117         return nameText.getText().trim();
118     }
119     
120     public String JavaDoc getType() {
121         Object JavaDoc 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; // NOI18N
137
case scaffold: setOptions("ScaffControllerName", "ScaffoldActions"); break; // NOI18N
138
case mailer: setOptions("Views", null); break; // NOI18N
139
case web_service: setOptions("ApiMethods", null); break; // NOI18N
140
case other: setOptions("Arg1", "Arg2"); break; // NOI18N
141
case model: // No parameters
142
case integration_test: // No parameters
143
case migration: // No Parameters
144
case session_migration: // No Parameters
145
case plugin: // Has "--with-generator" flag - should I support that?
146
default: setOptions(null, null); break;
147         }
148         
149         showUsage(generator);
150     }
151     
152     private void showUsage(Generator generator) {
153         // Look up the Rails directory and read the USAGE file, then stick
154
// it into the usageText.
155

156         usageText.setText("");
157         
158         // Generator dir
159
String JavaDoc version = RubyInstallation.getInstance().getVersion("rails"); // NOI18N
160
if (version == null) {
161             return;
162         }
163
164         String JavaDoc rubyLib = RubyInstallation.getInstance().getRubyLib();
165         File JavaDoc gemDir = new File JavaDoc(rubyLib+File.separator+"ruby"+File.separator+"gems"+File.separator+
166                 RubyInstallation.RUBY_RELEASE+File.separator+"gems"+File.separator);
167         //Example: jruby-0.9.2/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/rails_generator/generators/components
168
File JavaDoc generatorDir = new File JavaDoc(gemDir, "rails" + "-" + version + File.separator +
169                 "lib" + File.separator + "rails_generator" + File.separator + "generators" + File.separator +
170                 "components" + File.separator + generator.name());
171         File JavaDoc usageFile = new File JavaDoc(generatorDir, "USAGE");
172         if (!usageFile.exists()) {
173             return;
174         }
175         
176         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
177         try {
178             BufferedReader JavaDoc fr = new BufferedReader JavaDoc(new FileReader JavaDoc(usageFile));
179             while (true) {
180                 String JavaDoc 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 JavaDoc ioe) {
191             Exceptions.printStackTrace(ioe);
192         }
193     }
194     
195     private void setOptions(String JavaDoc firstParameterKey, String JavaDoc 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 JavaDoc[] getFirstParameterList() {
216         if (parameter1Text.isVisible()) {
217             // Change commas to spaces since lists are space separated, not comma separated
218
return parameter1Text.getText().replace(',', ' ').trim().split(" ");
219         } else {
220             return null;
221         }
222     }
223     
224     public String JavaDoc[] 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 JavaDoc l) {
233         changeListener = l;
234     }
235     
236     /** This method is called from within the constructor to
237      * initialize the form.
238      * WARNING: Do NOT modify this code. The content of this method is
239      * always regenerated by the Form Editor.
240      */

241     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
242
private void initComponents() {
243         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
244
245         forceGroup = new javax.swing.ButtonGroup JavaDoc();
246         generateLabel = new javax.swing.JLabel JavaDoc();
247         pretendCB = new javax.swing.JCheckBox JavaDoc();
248         typeCombo = new javax.swing.JComboBox JavaDoc();
249         skipRadio = new javax.swing.JRadioButton JavaDoc();
250         overwriteRadio = new javax.swing.JRadioButton JavaDoc();
251         optionsPanel = new javax.swing.JPanel JavaDoc();
252         nameLabel = new javax.swing.JLabel JavaDoc();
253         nameText = new javax.swing.JTextField JavaDoc();
254         parameter1Label = new javax.swing.JLabel JavaDoc();
255         parameter1Text = new javax.swing.JTextField JavaDoc();
256         parameter2Label = new javax.swing.JLabel JavaDoc();
257         parameter2Text = new javax.swing.JTextField JavaDoc();
258         forceLabel = new javax.swing.JLabel JavaDoc();
259         jSeparator1 = new javax.swing.JSeparator JavaDoc();
260         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
261         usageText = new javax.swing.JTextArea JavaDoc();
262         jSeparator2 = new javax.swing.JSeparator JavaDoc();
263
264         generateLabel.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.generateLabel.text")); // NOI18N
265

266         pretendCB.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.pretendCB.text")); // NOI18N
267
pretendCB.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
268         pretendCB.setMargin(new java.awt.Insets JavaDoc(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")); // NOI18N
275
skipRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
276         skipRadio.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
277
278         forceGroup.add(overwriteRadio);
279         overwriteRadio.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.overwriteRadio.text")); // NOI18N
280
overwriteRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
281         overwriteRadio.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
282
283         optionsPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
284
285         nameLabel.setText(NbBundle.getMessage(GeneratorPanel.class, "Name")); // NOI18N
286
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
287         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
288         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 11);
289         optionsPanel.add(nameLabel, gridBagConstraints);
290
291         nameText.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.nameText.text")); // NOI18N
292
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc();
300         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
301         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 11);
302         optionsPanel.add(parameter1Label, gridBagConstraints);
303
304         parameter1Text.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.parameter1Text.text")); // NOI18N
305
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(12, 0, 0, 0);
310         optionsPanel.add(parameter1Text, gridBagConstraints);
311
312         parameter2Label.setText("jLabel1");
313         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
314         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
315         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 11);
316         optionsPanel.add(parameter2Label, gridBagConstraints);
317
318         parameter2Text.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.parameter2Text.text")); // NOI18N
319
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(12, 0, 0, 0);
324         optionsPanel.add(parameter2Text, gridBagConstraints);
325
326         forceLabel.setText(org.openide.util.NbBundle.getMessage(GeneratorPanel.class, "GeneratorPanel.forceLabel.text")); // NOI18N
327

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     }// </editor-fold>//GEN-END:initComponents
382

383     
384     // Variables declaration - do not modify//GEN-BEGIN:variables
385
private javax.swing.ButtonGroup JavaDoc forceGroup;
386     private javax.swing.JLabel JavaDoc forceLabel;
387     private javax.swing.JLabel JavaDoc generateLabel;
388     private javax.swing.JScrollPane JavaDoc jScrollPane1;
389     private javax.swing.JSeparator JavaDoc jSeparator1;
390     private javax.swing.JSeparator JavaDoc jSeparator2;
391     private javax.swing.JLabel JavaDoc nameLabel;
392     private javax.swing.JTextField JavaDoc nameText;
393     private javax.swing.JPanel JavaDoc optionsPanel;
394     private javax.swing.JRadioButton JavaDoc overwriteRadio;
395     private javax.swing.JLabel JavaDoc parameter1Label;
396     private javax.swing.JTextField JavaDoc parameter1Text;
397     private javax.swing.JLabel JavaDoc parameter2Label;
398     private javax.swing.JTextField JavaDoc parameter2Text;
399     private javax.swing.JCheckBox JavaDoc pretendCB;
400     private javax.swing.JRadioButton JavaDoc skipRadio;
401     private javax.swing.JComboBox JavaDoc typeCombo;
402     private javax.swing.JTextArea JavaDoc usageText;
403     // End of variables declaration//GEN-END:variables
404

405 }
406
Popular Tags