KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > ui > wizards > RailsInstallationPanel


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.ui.wizards;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import javax.swing.event.ChangeEvent JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
27 import org.netbeans.api.options.OptionsDisplayer;
28 import org.netbeans.modules.ruby.rubyproject.api.RubyInstallation;
29 import org.netbeans.modules.ruby.rubyproject.gems.GemManager;
30 import org.netbeans.modules.ruby.rubyproject.gems.GemManager.Gem;
31 import org.openide.WizardDescriptor;
32 import org.openide.WizardValidationException;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
35
36 /**
37  * Wizard panel to handle easy/convenient Rails installations
38  *
39  * @author Tor Norbye
40  */

41 public class RailsInstallationPanel extends javax.swing.JPanel JavaDoc {
42     private Panel firer;
43     private WizardDescriptor wizardDescriptor;
44     
45     /** Creates new form RailsInstallationPanel */
46     public RailsInstallationPanel(Panel panel) {
47         initComponents();
48         this.firer = panel;
49         initComponents();
50         this.setName(NbBundle.getMessage(RailsInstallationPanel.class,"LAB_InstallRails"));
51         this.putClientProperty ("NewProjectWizard_Title", NbBundle.getMessage(RailsInstallationPanel.class,"TXT_RailsInstallation")); // NOI18N
52
updateLabel();
53         
54         String JavaDoc gem = RubyInstallation.getInstance().getGem();
55
56         if (!(new File JavaDoc(gem).exists())) {
57             String JavaDoc msg = NbBundle.getMessage(RailsInstallationPanel.class, "NoGem");
58             railsButton.setEnabled(false);
59             sslButton.setEnabled(false);
60         }
61     }
62     
63     private void updateLabel() {
64         if (RubyInstallation.getInstance().isValidRails(false)) {
65             descLabel.setText(NbBundle.getMessage(RailsInstallationPanel.class, "RailsOk"));
66             railsButton.setText(NbBundle.getMessage(RailsInstallationPanel.class, "UpdateRails"));
67             String JavaDoc version = RubyInstallation.getInstance().getVersion("rails"); // NOI18N
68
if (version == null) {
69                 version = "?";
70             }
71             installedLabel.setText(NbBundle.getMessage(RailsInstallationPanel.class, "RailsVersion", version));
72         } else {
73             descLabel.setText(NbBundle.getMessage(RailsInstallationPanel.class, "NoRails"));
74             railsButton.setText(NbBundle.getMessage(RailsInstallationPanel.class, "InstallRails"));
75             installedLabel.setText("");
76         }
77     }
78     
79     void read (WizardDescriptor settings) {
80         this.wizardDescriptor = settings;
81     }
82         
83     boolean valid (WizardDescriptor settings) {
84         // Make sure we have Rails (and possibly openssl as well)
85
String JavaDoc rails = RubyInstallation.getInstance().getRails();
86         if (rails != null && !(new File JavaDoc(rails).exists())) {
87             String JavaDoc msg = NbBundle.getMessage(RailsInstallationPanel.class, "NotFound", rails);
88             wizardDescriptor.putProperty( "WizardPanel_errorMessage", msg); //NOI18N
89
return false;
90         } else if (rails == null) {
91             String JavaDoc msg = NbBundle.getMessage(RailsInstallationPanel.class, "NoRails");
92             wizardDescriptor.putProperty( "WizardPanel_errorMessage", msg); //NOI18N
93
return false;
94         }
95
96         wizardDescriptor.putProperty( "WizardPanel_errorMessage",""); //NOI18N
97
return true;
98     }
99     
100     void validate (WizardDescriptor d) throws WizardValidationException {
101     }
102     
103     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
104
private void initComponents() {
105
106         railsButton = new javax.swing.JButton JavaDoc();
107         sslButton = new javax.swing.JButton JavaDoc();
108         descLabel = new javax.swing.JLabel JavaDoc();
109         jSeparator1 = new javax.swing.JSeparator JavaDoc();
110         jrubyLabel = new javax.swing.JLabel JavaDoc();
111         jrubySslLabel = new javax.swing.JLabel JavaDoc();
112         proxyButton = new javax.swing.JButton JavaDoc();
113         installedLabel = new javax.swing.JLabel JavaDoc();
114
115         railsButton.setText(org.openide.util.NbBundle.getMessage(RailsInstallationPanel.class, "InstallRails")); // NOI18N
116
railsButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
117             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
118                 railsButtonActionPerformed(evt);
119             }
120         });
121
122         sslButton.setText(org.openide.util.NbBundle.getMessage(RailsInstallationPanel.class, "RailsInstallationPanel.sslButton.text")); // NOI18N
123
sslButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
124             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
125                 sslButtonActionPerformed(evt);
126             }
127         });
128
129         descLabel.setText(org.openide.util.NbBundle.getMessage(RailsInstallationPanel.class, "NoRails")); // NOI18N
130

131         jrubyLabel.setText(org.openide.util.NbBundle.getMessage(RailsInstallationPanel.class, "RailsInstallationPanel.jrubyLabel.text")); // NOI18N
132

133         jrubySslLabel.setText(org.openide.util.NbBundle.getMessage(RailsInstallationPanel.class, "RailsInstallationPanel.jrubySslLabel.text")); // NOI18N
134

135         proxyButton.setText(org.openide.util.NbBundle.getMessage(RailsInstallationPanel.class, "RailsInstallationPanel.proxyButton.text")); // NOI18N
136
proxyButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
137             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
138                 proxyButtonActionPerformed(evt);
139             }
140         });
141
142         installedLabel.setText(org.openide.util.NbBundle.getMessage(RailsInstallationPanel.class, "RailsInstallationPanel.installedLabel.text")); // NOI18N
143

144         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
145         this.setLayout(layout);
146         layout.setHorizontalGroup(
147             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
148             .add(layout.createSequentialGroup()
149                 .addContainerGap()
150                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
151                     .add(descLabel)
152                     .add(layout.createSequentialGroup()
153                         .add(railsButton)
154                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
155                         .add(installedLabel))
156                     .add(org.jdesktop.layout.GroupLayout.TRAILING, jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
157                     .add(jrubyLabel)
158                     .add(proxyButton)
159                     .add(jrubySslLabel)
160                     .add(sslButton))
161                 .addContainerGap())
162         );
163         layout.setVerticalGroup(
164             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
165             .add(layout.createSequentialGroup()
166                 .addContainerGap()
167                 .add(descLabel)
168                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
169                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
170                     .add(railsButton)
171                     .add(installedLabel))
172                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
173                 .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
174                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
175                 .add(jrubyLabel)
176                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
177                 .add(jrubySslLabel)
178                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
179                 .add(sslButton)
180                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 74, Short.MAX_VALUE)
181                 .add(proxyButton)
182                 .addContainerGap())
183         );
184     }// </editor-fold>//GEN-END:initComponents
185

186 private void proxyButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_proxyButtonActionPerformed
187
OptionsDisplayer.getDefault().open("General"); // NOI18N
188
}//GEN-LAST:event_proxyButtonActionPerformed
189

190 private void sslButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_sslButtonActionPerformed
191
Runnable JavaDoc asyncCompletionTask = new InstallationComplete();
192     Gem gem = new Gem("jruby-openssl", null, null); // NOI18N
193
new GemManager().install(new Gem[] { gem }, this, null, false, false, null, true, true, asyncCompletionTask);
194     
195 }//GEN-LAST:event_sslButtonActionPerformed
196

197 private class InstallationComplete implements Runnable JavaDoc {
198     public void run() {
199         RailsInstallationPanel.this.updateLabel();
200         RailsInstallationPanel.this.firer.fireChangeEvent();
201         RubyInstallation.getInstance().recomputeRoots();
202     }
203 }
204
205 private void railsButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_railsButtonActionPerformed
206
Runnable JavaDoc asyncCompletionTask = new InstallationComplete();
207     Gem rails = new Gem("rails", null, null); // NOI18N
208
Gem jdbc = new Gem("ActiveRecord-JDBC", null, null); // NOI18N
209
Gem[] gems = new Gem[] { rails, jdbc };
210     if (RubyInstallation.getInstance().isValidRails((false))) {
211         // Already installed: update
212
new GemManager().update(gems, this, null, false, false, true, asyncCompletionTask);
213     } else {
214         new GemManager().install(gems, this, null, false, false, null, true, true, asyncCompletionTask);
215     }
216 }//GEN-LAST:event_railsButtonActionPerformed
217

218     
219     // Variables declaration - do not modify//GEN-BEGIN:variables
220
private javax.swing.JLabel JavaDoc descLabel;
221     private javax.swing.JLabel JavaDoc installedLabel;
222     private javax.swing.JSeparator JavaDoc jSeparator1;
223     private javax.swing.JLabel JavaDoc jrubyLabel;
224     private javax.swing.JLabel JavaDoc jrubySslLabel;
225     private javax.swing.JButton JavaDoc proxyButton;
226     private javax.swing.JButton JavaDoc railsButton;
227     private javax.swing.JButton JavaDoc sslButton;
228     // End of variables declaration//GEN-END:variables
229

230     static class Panel implements WizardDescriptor.ValidatingPanel {
231         
232         private ArrayList JavaDoc listeners;
233         private RailsInstallationPanel component;
234         private WizardDescriptor settings;
235         
236         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
237             if (this.listeners == null) {
238                 return;
239             }
240             this.listeners.remove(l);
241         }
242
243         public void addChangeListener(ChangeListener JavaDoc l) {
244             if (this.listeners == null) {
245                 this.listeners = new ArrayList JavaDoc ();
246             }
247             this.listeners.add (l);
248         }
249
250         public void readSettings(Object JavaDoc settings) {
251             // Nothing to store/restore here
252
this.settings = (WizardDescriptor) settings;
253             this.component.read(this.settings);
254         }
255
256         public void storeSettings(Object JavaDoc settings) {
257             // Nothing to store/restore here
258
}
259         
260         public void validate() throws WizardValidationException {
261             this.component.validate(this.settings);
262         }
263                 
264         public boolean isValid() {
265             return this.component.valid (this.settings);
266         }
267
268         public synchronized java.awt.Component JavaDoc getComponent() {
269             if (this.component == null) {
270                 this.component = new RailsInstallationPanel(this);
271             }
272             return this.component;
273         }
274
275         public HelpCtx getHelp() {
276             return new HelpCtx (RailsInstallationPanel.class);
277         }
278         
279         private void fireChangeEvent () {
280            Iterator JavaDoc it = null;
281            synchronized (this) {
282                if (this.listeners == null) {
283                    return;
284                }
285                it = ((ArrayList JavaDoc)this.listeners.clone()).iterator();
286            }
287            ChangeEvent JavaDoc event = new ChangeEvent JavaDoc (this);
288            while (it.hasNext()) {
289                ((ChangeListener JavaDoc)it.next()).stateChanged(event);
290            }
291         }
292     }
293 }
294
Popular Tags