KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25 import java.util.regex.PatternSyntaxException JavaDoc;
26 import javax.swing.DefaultListModel 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.gems.GemManager.Gem;
32 import org.openide.util.NbBundle;
33 import org.openide.util.RequestProcessor;
34
35 /**
36  *
37  * @author Tor Norbye
38  */

39 public class InstallNewPanel extends javax.swing.JPanel JavaDoc {
40     private GemManager gemManager;
41     private List JavaDoc<Gem> gems;
42     
43     /** Creates new form InstallNewPanel */
44     public InstallNewPanel(GemManager gemManager) {
45         initComponents();
46         this.gemManager = gemManager;
47         gemList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
48         gemList.getSelectionModel().addListSelectionListener(new ListSelectionListener JavaDoc() {
49             public void valueChanged(ListSelectionEvent JavaDoc ev) {
50                 if (ev.getValueIsAdjusting()) {
51                     return;
52                 }
53                 int index = gemList.getSelectedIndex();
54                 if (index != -1) {
55                     Object JavaDoc o = gemList.getModel().getElementAt(index);
56                     if (o instanceof Gem) { // Could be "Please Wait..." String
57
Gem gem = (Gem)o;
58                         versionText.setText(gem.getVersions());
59                         descArea.setText(gem.getDescription());
60                         return;
61                     }
62                 }
63                 versionText.setText("");
64                 descArea.setText("");
65             }
66         });
67         refreshInstalledList(true);
68     }
69     
70     public Gem getChosenGem() {
71         int index = gemList.getSelectedIndex();
72         if (index != -1) {
73             Object JavaDoc o = gemList.getModel().getElementAt(index);
74             if (o instanceof Gem) { // Could be "Please Wait..." String
75
Gem gem = (Gem)o;
76                 return gem;
77             }
78         }
79         
80         return null;
81     }
82     
83     /** This method is called from within the constructor to
84      * initialize the form.
85      * WARNING: Do NOT modify this code. The content of this method is
86      * always regenerated by the Form Editor.
87      */

88     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
89
private void initComponents() {
90
91         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
92         gemList = new javax.swing.JList JavaDoc();
93         gemsLabel = new javax.swing.JLabel JavaDoc();
94         jLabel4 = new javax.swing.JLabel JavaDoc();
95         jLabel6 = new javax.swing.JLabel JavaDoc();
96         jScrollPane2 = new javax.swing.JScrollPane JavaDoc();
97         descArea = new javax.swing.JTextArea JavaDoc();
98         filterLabel = new javax.swing.JLabel JavaDoc();
99         filterField = new javax.swing.JTextField JavaDoc();
100         filterButton = new javax.swing.JButton JavaDoc();
101         jButton1 = new javax.swing.JButton JavaDoc();
102         versionText = new javax.swing.JTextField JavaDoc();
103         refreshButton = new javax.swing.JButton JavaDoc();
104
105         jScrollPane1.setViewportView(gemList);
106
107         gemsLabel.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.gemsLabel.text")); // NOI18N
108

109         jLabel4.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.jLabel4.text")); // NOI18N
110

111         jLabel6.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.jLabel6.text")); // NOI18N
112

113         descArea.setColumns(20);
114         descArea.setEditable(false);
115         descArea.setLineWrap(true);
116         descArea.setRows(4);
117         descArea.setWrapStyleWord(true);
118         jScrollPane2.setViewportView(descArea);
119
120         filterLabel.setLabelFor(filterField);
121         filterLabel.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.filterLabel.text")); // NOI18N
122

123         filterField.setColumns(20);
124         filterField.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.filterField.text")); // NOI18N
125
filterField.addActionListener(new java.awt.event.ActionListener JavaDoc() {
126             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
127                 filterFieldActionPerformed(evt);
128             }
129         });
130
131         filterButton.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.filterButton.text")); // NOI18N
132
filterButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
133             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
134                 filterButtonActionPerformed(evt);
135             }
136         });
137
138         jButton1.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.jButton1.text")); // NOI18N
139
jButton1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
140             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
141                 jButton1ActionPerformed(evt);
142             }
143         });
144
145         versionText.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.versionText.text")); // NOI18N
146

147         refreshButton.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.refreshButton.text")); // NOI18N
148
refreshButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
149             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
150                 refreshButtonActionPerformed(evt);
151             }
152         });
153
154         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
155         this.setLayout(layout);
156         layout.setHorizontalGroup(
157             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
158             .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
159                 .addContainerGap()
160                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
161                     .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 801, Short.MAX_VALUE)
162                     .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 801, Short.MAX_VALUE)
163                     .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel6)
164                     .add(layout.createSequentialGroup()
165                         .add(gemsLabel)
166                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 490, Short.MAX_VALUE)
167                         .add(jButton1)
168                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
169                         .add(refreshButton))
170                     .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
171                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
172                             .add(jLabel4)
173                             .add(filterLabel))
174                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
175                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
176                             .add(versionText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
177                             .add(filterField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE))
178                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
179                         .add(filterButton)))
180                 .addContainerGap())
181         );
182         layout.setVerticalGroup(
183             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
184             .add(layout.createSequentialGroup()
185                 .addContainerGap()
186                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
187                     .add(gemsLabel)
188                     .add(refreshButton)
189                     .add(jButton1))
190                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
191                 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 225, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
192                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
193                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
194                     .add(filterLabel)
195                     .add(filterButton)
196                     .add(filterField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
197                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
198                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
199                     .add(jLabel4)
200                     .add(versionText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
201                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
202                 .add(jLabel6)
203                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
204                 .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
205                 .addContainerGap(29, Short.MAX_VALUE))
206         );
207     }// </editor-fold>//GEN-END:initComponents
208

209 private void refreshButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_refreshButtonActionPerformed
210
refreshInstalledList(true);
211 }//GEN-LAST:event_refreshButtonActionPerformed
212

213 private void jButton1ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButton1ActionPerformed
214
OptionsDisplayer.getDefault().open("General"); // NOI18N
215
}//GEN-LAST:event_jButton1ActionPerformed
216

217 private void filterFieldActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_filterFieldActionPerformed
218
refreshInstalledList(gems != null && gems.size() > 0);
219 }//GEN-LAST:event_filterFieldActionPerformed
220

221 private void filterButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_filterButtonActionPerformed
222
filterFieldActionPerformed(evt);
223 }//GEN-LAST:event_filterButtonActionPerformed
224

225     
226     // Variables declaration - do not modify//GEN-BEGIN:variables
227
private javax.swing.JTextArea JavaDoc descArea;
228     private javax.swing.JButton JavaDoc filterButton;
229     private javax.swing.JTextField JavaDoc filterField;
230     private javax.swing.JLabel JavaDoc filterLabel;
231     private javax.swing.JList JavaDoc gemList;
232     private javax.swing.JLabel JavaDoc gemsLabel;
233     private javax.swing.JButton JavaDoc jButton1;
234     private javax.swing.JLabel JavaDoc jLabel4;
235     private javax.swing.JLabel JavaDoc jLabel6;
236     private javax.swing.JScrollPane JavaDoc jScrollPane1;
237     private javax.swing.JScrollPane JavaDoc jScrollPane2;
238     private javax.swing.JButton JavaDoc refreshButton;
239     private javax.swing.JTextField JavaDoc versionText;
240     // End of variables declaration//GEN-END:variables
241

242     /** Refresh the list of displayed gems. If refresh is true, refresh the list from the gem manager, otherwise just refilter list */
243     private void refreshInstalledList(final boolean refresh) {
244         Runnable JavaDoc runner = new Runnable JavaDoc() {
245             public void run() {
246                 DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
247                 List JavaDoc<String JavaDoc> lines = new ArrayList JavaDoc<String JavaDoc>();
248                 if (refresh) {
249                     gems = gemManager.getAvailableGems(lines);
250                 }
251                 String JavaDoc filter = filterField.getText().trim();
252                 Pattern JavaDoc pattern = null;
253                 if (filter.indexOf('*') != -1 || filter.indexOf('^') != -1 || filter.indexOf('$') != -1) {
254                     try {
255                         pattern = Pattern.compile(filter);
256                     } catch (PatternSyntaxException JavaDoc pse) {
257                         // Don't treat the filter as a regexp
258
}
259                 }
260                 for (Gem gem : gems) {
261                     if (filter.length() == 0) {
262                         model.addElement(gem);
263                     } else if (pattern == null) {
264                        if ((gem.getName().indexOf(filter) != -1) ||
265                                (gem.getDescription() != null && gem.getDescription().indexOf(filter) != -1)) {
266                             model.addElement(gem);
267                        }
268                     } else if (pattern.matcher(gem.getName()).find() ||
269                             (gem.getDescription() != null && pattern.matcher(gem.getDescription()).find())) {
270                         model.addElement(gem);
271                     }
272                 }
273                 if (refresh && gems.size() == 0) {
274                     model.addElement(NbBundle.getMessage(InstallNewPanel.class, "NoNetwork"));
275                     for (String JavaDoc line : lines) {
276                         descArea.append(line + "\n");
277                     }
278                 }
279                 gemList.setModel(model);
280                 if (gems.size() > 0) {
281                     gemList.setSelectedIndex(0);
282                 }
283             }
284         };
285         if (refresh || (gemList == null)) {
286             DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
287             model.addElement(NbBundle.getMessage(InstallNewPanel.class, "FetchingRemote"));
288             versionText.setText("");
289             descArea.setText("");
290             gemList.setModel(model);
291             RequestProcessor.getDefault().post(runner, 50);
292         } else {
293             // Do immediate
294
runner.run();
295         }
296     }
297 }
298
Popular Tags