KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > gems > InstalledGemsPanel


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.rubyproject.gems;
21
22 import java.awt.Dialog JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.DefaultListModel JavaDoc;
26 import javax.swing.JButton JavaDoc;
27 import javax.swing.ListSelectionModel JavaDoc;
28 import javax.swing.event.ListSelectionEvent JavaDoc;
29 import javax.swing.event.ListSelectionListener JavaDoc;
30 import org.netbeans.api.options.OptionsDisplayer;
31 import org.netbeans.modules.ruby.rubyproject.api.RubyInstallation;
32 import org.netbeans.modules.ruby.rubyproject.gems.GemManager.Gem;
33 import org.openide.DialogDescriptor;
34 import org.openide.DialogDisplayer;
35 import org.openide.NotifyDescriptor;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.NbBundle;
38 import org.openide.util.RequestProcessor;
39
40 /**
41  *
42  * @author Tor Norbye
43  */

44 public class InstalledGemsPanel extends javax.swing.JPanel JavaDoc {
45     private GemManager gemManager;
46     
47     private boolean modified;
48     
49     /** Creates new form GemManager */
50     public InstalledGemsPanel() {
51         initComponents();
52         gemManager = new GemManager();
53         if (!gemManager.haveGem()) {
54             installButton.setEnabled(false);
55             updateAllButton.setEnabled(false);
56             DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
57             String JavaDoc msg = NbBundle.getMessage(InstalledGemsPanel.class, "GemMissing");
58             model.addElement(msg);
59             gemList.setModel(model);
60             return;
61         }
62         gemList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
63         gemList.getSelectionModel().addListSelectionListener(new ListSelectionListener JavaDoc() {
64             public void valueChanged(ListSelectionEvent JavaDoc ev) {
65                 if (ev.getValueIsAdjusting()) {
66                     return;
67                 }
68                 int index = gemList.getSelectedIndex();
69                 if (index != -1) {
70                     Object JavaDoc o = gemList.getModel().getElementAt(index);
71                     if (o instanceof Gem) { // Could be "Please Wait..." String
72
Gem gem = (Gem)o;
73                         
74                         upgradeButton.setEnabled(true);
75                         uninstallButton.setEnabled(true);
76                         return;
77                     }
78                 }
79                 upgradeButton.setEnabled(false);
80                 uninstallButton.setEnabled(false);
81             }
82         });
83         refreshInstalledList(false);
84     }
85     
86     public boolean isModified() {
87         return modified;
88     }
89     
90     /** This method is called from within the constructor to
91      * initialize the form.
92      * WARNING: Do NOT modify this code. The content of this method is
93      * always regenerated by the Form Editor.
94      */

95     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
96
private void initComponents() {
97
98         jButton3 = new javax.swing.JButton JavaDoc();
99         gemListLabel = new javax.swing.JLabel JavaDoc();
100         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
101         gemList = new javax.swing.JList JavaDoc();
102         installButton = new javax.swing.JButton JavaDoc();
103         upgradeButton = new javax.swing.JButton JavaDoc();
104         uninstallButton = new javax.swing.JButton JavaDoc();
105         updateAllButton = new javax.swing.JButton JavaDoc();
106         proxyButton = new javax.swing.JButton JavaDoc();
107
108         jButton3.setText("jButton3");
109
110         gemListLabel.setText(org.openide.util.NbBundle.getMessage(InstalledGemsPanel.class, "GemManager.gemListLabel.text")); // NOI18N
111

112         jScrollPane1.setViewportView(gemList);
113
114         installButton.setText(org.openide.util.NbBundle.getMessage(InstalledGemsPanel.class, "GemManager.installButton.text")); // NOI18N
115
installButton.setPreferredSize(null);
116         installButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
117             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
118                 installGem(evt);
119             }
120         });
121
122         upgradeButton.setText(org.openide.util.NbBundle.getMessage(InstalledGemsPanel.class, "GemManager.upgradeButton.text")); // NOI18N
123
upgradeButton.setEnabled(false);
124         upgradeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
125             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
126                 upgradeButtonActionPerformed(evt);
127             }
128         });
129
130         uninstallButton.setText(org.openide.util.NbBundle.getMessage(InstalledGemsPanel.class, "GemManager.uninstallButton.text")); // NOI18N
131
uninstallButton.setEnabled(false);
132         uninstallButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
133             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
134                 uninstallButtonActionPerformed(evt);
135             }
136         });
137
138         updateAllButton.setText(org.openide.util.NbBundle.getMessage(InstalledGemsPanel.class, "InstalledGemsPanel.updateAllButton.text")); // NOI18N
139
updateAllButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
140             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
141                 updateAllButtonActionPerformed(evt);
142             }
143         });
144
145         proxyButton.setText(org.openide.util.NbBundle.getMessage(InstalledGemsPanel.class, "InstalledGemsPanel.proxyButton.text")); // NOI18N
146
proxyButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
147             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
148                 proxyButtonActionPerformed(evt);
149             }
150         });
151
152         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
153         this.setLayout(layout);
154         layout.setHorizontalGroup(
155             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
156             .add(layout.createSequentialGroup()
157                 .addContainerGap()
158                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
159                     .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
160                         .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE)
161                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
162                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
163                             .add(proxyButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
164                             .add(uninstallButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
165                             .add(upgradeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
166                             .add(updateAllButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
167                             .add(installButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
168                     .add(gemListLabel))
169                 .addContainerGap())
170         );
171         layout.setVerticalGroup(
172             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
173             .add(layout.createSequentialGroup()
174                 .addContainerGap()
175                 .add(gemListLabel)
176                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
177                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
178                     .add(layout.createSequentialGroup()
179                         .add(installButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
180                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
181                         .add(updateAllButton)
182                         .add(36, 36, 36)
183                         .add(upgradeButton)
184                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
185                         .add(uninstallButton)
186                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 178, Short.MAX_VALUE)
187                         .add(proxyButton))
188                     .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE))
189                 .addContainerGap())
190         );
191     }// </editor-fold>//GEN-END:initComponents
192

193 private void proxyButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_proxyButtonActionPerformed
194
OptionsDisplayer.getDefault().open("General"); // NOI18N
195
}//GEN-LAST:event_proxyButtonActionPerformed
196

197 private void uninstallButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_uninstallButtonActionPerformed
198
int[] indices = gemList.getSelectedIndices();
199     List JavaDoc<Gem> gems = new ArrayList JavaDoc<Gem>();
200     if (indices != null) {
201         for (int index : indices) {
202             assert index >= 0;
203             Object JavaDoc o = gemList.getModel().getElementAt(index);
204             if (o instanceof Gem) { // Could be "Please Wait..." String
205
Gem gem = (Gem)o;
206                 gems.add(gem);
207             }
208         }
209     }
210     if (gems.size() > 0) {
211         Runnable JavaDoc completionTask = new ListRefresher();
212         gemManager.uninstall(gems.toArray(new Gem[gems.size()]), this, null, true, completionTask);
213         modified = true;
214     }
215 }//GEN-LAST:event_uninstallButtonActionPerformed
216

217 private void upgradeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_upgradeButtonActionPerformed
218
int[] indices = gemList.getSelectedIndices();
219     List JavaDoc<Gem> gems = new ArrayList JavaDoc<Gem>();
220     if (indices != null) {
221         for (int index : indices) {
222             assert index >= 0;
223             Object JavaDoc o = gemList.getModel().getElementAt(index);
224             if (o instanceof Gem) { // Could be "Please Wait..." String
225
Gem gem = (Gem)o;
226                 gems.add(gem);
227             }
228         }
229     }
230     if (gems.size() > 0) {
231         Runnable JavaDoc completionTask = new ListRefresher();
232         gemManager.update(gems.toArray(new Gem[gems.size()]), this, null, false, false, true, completionTask);
233         modified = true;
234     }
235 }//GEN-LAST:event_upgradeButtonActionPerformed
236

237 private void updateAllButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_updateAllButtonActionPerformed
238
Runnable JavaDoc completionTask = new ListRefresher();
239     gemManager.update(null, this, null, false, false, true, completionTask);
240     modified = true;
241 }//GEN-LAST:event_updateAllButtonActionPerformed
242

243 private void installGem(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_installGem
244
InstallNewPanel customizer = new InstallNewPanel(gemManager);
245         javax.swing.JButton JavaDoc closeButton = new javax.swing.JButton JavaDoc(NbBundle.getMessage(InstalledGemsPanel.class,"CTL_Close"));
246         closeButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(InstalledGemsPanel.class,"AD_Close"));
247         final JButton JavaDoc installButton = new JButton JavaDoc (NbBundle.getMessage (InstalledGemsPanel.class, "Install"));
248         Object JavaDoc[] options = new Object JavaDoc[] {
249             installButton,
250             closeButton
251         };
252         DialogDescriptor descriptor = new DialogDescriptor (customizer, NbBundle.getMessage(InstalledGemsPanel.class,
253                 "InstallNew"), true, options, installButton,
254                 DialogDescriptor.DEFAULT_ALIGN, new HelpCtx (InstalledGemsPanel.class),null); // NOI18N
255
Dialog JavaDoc dlg = null;
256         //descriptor.setModal(true);
257
try {
258             dlg = DialogDisplayer.getDefault().createDialog (descriptor);
259             dlg.setVisible(true);
260         } finally {
261             if (dlg != null)
262                 dlg.dispose();
263         }
264
265         if (descriptor.getValue() == installButton) {
266             Gem chosen = customizer.getChosenGem();
267             if (chosen != null) {
268                 // Get some information about the chosen gem
269
InstallationSettingsPanel panel = new InstallationSettingsPanel(chosen);
270                 DialogDescriptor dd = new DialogDescriptor(panel, NbBundle.getMessage(InstalledGemsPanel.class, "ChooseGemSettings"));
271                 dd.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION);
272                 dd.setModal(true);
273                 dd.setHelpCtx(new HelpCtx(InstalledGemsPanel.class));
274                 Object JavaDoc result = DialogDisplayer.getDefault().notify(dd);
275                 if (result.equals(NotifyDescriptor.OK_OPTION)) {
276                     Runnable JavaDoc completionTask = new ListRefresher();
277                     Gem gem = new Gem(panel.getName(), null, null);
278                     gemManager.install(new Gem[] { gem }, this, null, false, false, panel.getVersion(),
279                             panel.getIncludeDepencies(), true, completionTask);
280                     modified = true;
281                 }
282             }
283         }
284 }//GEN-LAST:event_installGem
285

286     
287     // Variables declaration - do not modify//GEN-BEGIN:variables
288
private javax.swing.JList JavaDoc gemList;
289     private javax.swing.JLabel JavaDoc gemListLabel;
290     private javax.swing.JButton JavaDoc installButton;
291     private javax.swing.JButton JavaDoc jButton3;
292     private javax.swing.JScrollPane JavaDoc jScrollPane1;
293     private javax.swing.JButton JavaDoc proxyButton;
294     private javax.swing.JButton JavaDoc uninstallButton;
295     private javax.swing.JButton JavaDoc updateAllButton;
296     private javax.swing.JButton JavaDoc upgradeButton;
297     // End of variables declaration//GEN-END:variables
298

299     private void refreshInstalledList(final boolean refresh) {
300         DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
301         model.addElement(NbBundle.getMessage(InstalledGemsPanel.class, "PleaseWait"));
302         gemList.setModel(model);
303
304         Runnable JavaDoc runner = new Runnable JavaDoc() {
305             public void run() {
306                 DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
307                 List JavaDoc<String JavaDoc> lines = new ArrayList JavaDoc<String JavaDoc>();
308                 List JavaDoc<Gem> gems = gemManager.getInstalledGems(refresh, lines);
309                 for (Gem gem : gems) {
310                     model.addElement(gem);
311                 }
312                 gemList.setModel(model);
313             }
314         };
315         RequestProcessor.getDefault().post(runner, 50);
316     }
317
318     private class ListRefresher implements Runnable JavaDoc {
319         public void run() {
320             refreshInstalledList(true);
321         }
322     }
323 }
324
Popular Tags