1 19 20 package org.netbeans.modules.ruby.rubyproject.gems; 21 22 import java.awt.Dialog ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import javax.swing.DefaultListModel ; 26 import javax.swing.JButton ; 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.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 44 public class InstalledGemsPanel extends javax.swing.JPanel { 45 private GemManager gemManager; 46 47 private boolean modified; 48 49 50 public InstalledGemsPanel() { 51 initComponents(); 52 gemManager = new GemManager(); 53 if (!gemManager.haveGem()) { 54 installButton.setEnabled(false); 55 updateAllButton.setEnabled(false); 56 DefaultListModel model = new DefaultListModel (); 57 String 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 () { 64 public void valueChanged(ListSelectionEvent ev) { 65 if (ev.getValueIsAdjusting()) { 66 return; 67 } 68 int index = gemList.getSelectedIndex(); 69 if (index != -1) { 70 Object o = gemList.getModel().getElementAt(index); 71 if (o instanceof Gem) { 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 95 private void initComponents() { 97 98 jButton3 = new javax.swing.JButton (); 99 gemListLabel = new javax.swing.JLabel (); 100 jScrollPane1 = new javax.swing.JScrollPane (); 101 gemList = new javax.swing.JList (); 102 installButton = new javax.swing.JButton (); 103 upgradeButton = new javax.swing.JButton (); 104 uninstallButton = new javax.swing.JButton (); 105 updateAllButton = new javax.swing.JButton (); 106 proxyButton = new javax.swing.JButton (); 107 108 jButton3.setText("jButton3"); 109 110 gemListLabel.setText(org.openide.util.NbBundle.getMessage(InstalledGemsPanel.class, "GemManager.gemListLabel.text")); 112 jScrollPane1.setViewportView(gemList); 113 114 installButton.setText(org.openide.util.NbBundle.getMessage(InstalledGemsPanel.class, "GemManager.installButton.text")); installButton.setPreferredSize(null); 116 installButton.addActionListener(new java.awt.event.ActionListener () { 117 public void actionPerformed(java.awt.event.ActionEvent evt) { 118 installGem(evt); 119 } 120 }); 121 122 upgradeButton.setText(org.openide.util.NbBundle.getMessage(InstalledGemsPanel.class, "GemManager.upgradeButton.text")); upgradeButton.setEnabled(false); 124 upgradeButton.addActionListener(new java.awt.event.ActionListener () { 125 public void actionPerformed(java.awt.event.ActionEvent evt) { 126 upgradeButtonActionPerformed(evt); 127 } 128 }); 129 130 uninstallButton.setText(org.openide.util.NbBundle.getMessage(InstalledGemsPanel.class, "GemManager.uninstallButton.text")); uninstallButton.setEnabled(false); 132 uninstallButton.addActionListener(new java.awt.event.ActionListener () { 133 public void actionPerformed(java.awt.event.ActionEvent evt) { 134 uninstallButtonActionPerformed(evt); 135 } 136 }); 137 138 updateAllButton.setText(org.openide.util.NbBundle.getMessage(InstalledGemsPanel.class, "InstalledGemsPanel.updateAllButton.text")); updateAllButton.addActionListener(new java.awt.event.ActionListener () { 140 public void actionPerformed(java.awt.event.ActionEvent evt) { 141 updateAllButtonActionPerformed(evt); 142 } 143 }); 144 145 proxyButton.setText(org.openide.util.NbBundle.getMessage(InstalledGemsPanel.class, "InstalledGemsPanel.proxyButton.text")); proxyButton.addActionListener(new java.awt.event.ActionListener () { 147 public void actionPerformed(java.awt.event.ActionEvent 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 } 193 private void proxyButtonActionPerformed(java.awt.event.ActionEvent evt) { OptionsDisplayer.getDefault().open("General"); } 197 private void uninstallButtonActionPerformed(java.awt.event.ActionEvent evt) { int[] indices = gemList.getSelectedIndices(); 199 List <Gem> gems = new ArrayList <Gem>(); 200 if (indices != null) { 201 for (int index : indices) { 202 assert index >= 0; 203 Object o = gemList.getModel().getElementAt(index); 204 if (o instanceof Gem) { Gem gem = (Gem)o; 206 gems.add(gem); 207 } 208 } 209 } 210 if (gems.size() > 0) { 211 Runnable completionTask = new ListRefresher(); 212 gemManager.uninstall(gems.toArray(new Gem[gems.size()]), this, null, true, completionTask); 213 modified = true; 214 } 215 } 217 private void upgradeButtonActionPerformed(java.awt.event.ActionEvent evt) { int[] indices = gemList.getSelectedIndices(); 219 List <Gem> gems = new ArrayList <Gem>(); 220 if (indices != null) { 221 for (int index : indices) { 222 assert index >= 0; 223 Object o = gemList.getModel().getElementAt(index); 224 if (o instanceof Gem) { Gem gem = (Gem)o; 226 gems.add(gem); 227 } 228 } 229 } 230 if (gems.size() > 0) { 231 Runnable completionTask = new ListRefresher(); 232 gemManager.update(gems.toArray(new Gem[gems.size()]), this, null, false, false, true, completionTask); 233 modified = true; 234 } 235 } 237 private void updateAllButtonActionPerformed(java.awt.event.ActionEvent evt) { Runnable completionTask = new ListRefresher(); 239 gemManager.update(null, this, null, false, false, true, completionTask); 240 modified = true; 241 } 243 private void installGem(java.awt.event.ActionEvent evt) { InstallNewPanel customizer = new InstallNewPanel(gemManager); 245 javax.swing.JButton closeButton = new javax.swing.JButton (NbBundle.getMessage(InstalledGemsPanel.class,"CTL_Close")); 246 closeButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(InstalledGemsPanel.class,"AD_Close")); 247 final JButton installButton = new JButton (NbBundle.getMessage (InstalledGemsPanel.class, "Install")); 248 Object [] options = new Object [] { 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); Dialog dlg = null; 256 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 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 result = DialogDisplayer.getDefault().notify(dd); 275 if (result.equals(NotifyDescriptor.OK_OPTION)) { 276 Runnable 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 } 286 287 private javax.swing.JList gemList; 289 private javax.swing.JLabel gemListLabel; 290 private javax.swing.JButton installButton; 291 private javax.swing.JButton jButton3; 292 private javax.swing.JScrollPane jScrollPane1; 293 private javax.swing.JButton proxyButton; 294 private javax.swing.JButton uninstallButton; 295 private javax.swing.JButton updateAllButton; 296 private javax.swing.JButton upgradeButton; 297 299 private void refreshInstalledList(final boolean refresh) { 300 DefaultListModel model = new DefaultListModel (); 301 model.addElement(NbBundle.getMessage(InstalledGemsPanel.class, "PleaseWait")); 302 gemList.setModel(model); 303 304 Runnable runner = new Runnable () { 305 public void run() { 306 DefaultListModel model = new DefaultListModel (); 307 List <String > lines = new ArrayList <String >(); 308 List <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 { 319 public void run() { 320 refreshInstalledList(true); 321 } 322 } 323 } 324 | Popular Tags |