1 19 20 package org.netbeans.modules.ruby.rubyproject.gems; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.regex.Pattern ; 25 import java.util.regex.PatternSyntaxException ; 26 import javax.swing.DefaultListModel ; 27 import javax.swing.ListSelectionModel ; 28 import javax.swing.event.ListSelectionEvent ; 29 import javax.swing.event.ListSelectionListener ; 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 39 public class InstallNewPanel extends javax.swing.JPanel { 40 private GemManager gemManager; 41 private List <Gem> gems; 42 43 44 public InstallNewPanel(GemManager gemManager) { 45 initComponents(); 46 this.gemManager = gemManager; 47 gemList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 48 gemList.getSelectionModel().addListSelectionListener(new ListSelectionListener () { 49 public void valueChanged(ListSelectionEvent ev) { 50 if (ev.getValueIsAdjusting()) { 51 return; 52 } 53 int index = gemList.getSelectedIndex(); 54 if (index != -1) { 55 Object o = gemList.getModel().getElementAt(index); 56 if (o instanceof Gem) { 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 o = gemList.getModel().getElementAt(index); 74 if (o instanceof Gem) { Gem gem = (Gem)o; 76 return gem; 77 } 78 } 79 80 return null; 81 } 82 83 88 private void initComponents() { 90 91 jScrollPane1 = new javax.swing.JScrollPane (); 92 gemList = new javax.swing.JList (); 93 gemsLabel = new javax.swing.JLabel (); 94 jLabel4 = new javax.swing.JLabel (); 95 jLabel6 = new javax.swing.JLabel (); 96 jScrollPane2 = new javax.swing.JScrollPane (); 97 descArea = new javax.swing.JTextArea (); 98 filterLabel = new javax.swing.JLabel (); 99 filterField = new javax.swing.JTextField (); 100 filterButton = new javax.swing.JButton (); 101 jButton1 = new javax.swing.JButton (); 102 versionText = new javax.swing.JTextField (); 103 refreshButton = new javax.swing.JButton (); 104 105 jScrollPane1.setViewportView(gemList); 106 107 gemsLabel.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.gemsLabel.text")); 109 jLabel4.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.jLabel4.text")); 111 jLabel6.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.jLabel6.text")); 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")); 123 filterField.setColumns(20); 124 filterField.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.filterField.text")); filterField.addActionListener(new java.awt.event.ActionListener () { 126 public void actionPerformed(java.awt.event.ActionEvent evt) { 127 filterFieldActionPerformed(evt); 128 } 129 }); 130 131 filterButton.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.filterButton.text")); filterButton.addActionListener(new java.awt.event.ActionListener () { 133 public void actionPerformed(java.awt.event.ActionEvent evt) { 134 filterButtonActionPerformed(evt); 135 } 136 }); 137 138 jButton1.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.jButton1.text")); jButton1.addActionListener(new java.awt.event.ActionListener () { 140 public void actionPerformed(java.awt.event.ActionEvent evt) { 141 jButton1ActionPerformed(evt); 142 } 143 }); 144 145 versionText.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.versionText.text")); 147 refreshButton.setText(org.openide.util.NbBundle.getMessage(InstallNewPanel.class, "InstallNewPanel.refreshButton.text")); refreshButton.addActionListener(new java.awt.event.ActionListener () { 149 public void actionPerformed(java.awt.event.ActionEvent 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 } 209 private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) { refreshInstalledList(true); 211 } 213 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { OptionsDisplayer.getDefault().open("General"); } 217 private void filterFieldActionPerformed(java.awt.event.ActionEvent evt) { refreshInstalledList(gems != null && gems.size() > 0); 219 } 221 private void filterButtonActionPerformed(java.awt.event.ActionEvent evt) { filterFieldActionPerformed(evt); 223 } 225 226 private javax.swing.JTextArea descArea; 228 private javax.swing.JButton filterButton; 229 private javax.swing.JTextField filterField; 230 private javax.swing.JLabel filterLabel; 231 private javax.swing.JList gemList; 232 private javax.swing.JLabel gemsLabel; 233 private javax.swing.JButton jButton1; 234 private javax.swing.JLabel jLabel4; 235 private javax.swing.JLabel jLabel6; 236 private javax.swing.JScrollPane jScrollPane1; 237 private javax.swing.JScrollPane jScrollPane2; 238 private javax.swing.JButton refreshButton; 239 private javax.swing.JTextField versionText; 240 242 243 private void refreshInstalledList(final boolean refresh) { 244 Runnable runner = new Runnable () { 245 public void run() { 246 DefaultListModel model = new DefaultListModel (); 247 List <String > lines = new ArrayList <String >(); 248 if (refresh) { 249 gems = gemManager.getAvailableGems(lines); 250 } 251 String filter = filterField.getText().trim(); 252 Pattern pattern = null; 253 if (filter.indexOf('*') != -1 || filter.indexOf('^') != -1 || filter.indexOf('$') != -1) { 254 try { 255 pattern = Pattern.compile(filter); 256 } catch (PatternSyntaxException pse) { 257 } 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 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 model = new DefaultListModel (); 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 runner.run(); 295 } 296 } 297 } 298 | Popular Tags |