1 19 20 package org.netbeans.modules.db.explorer.dlg; 21 22 import java.awt.BorderLayout ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.MalformedURLException ; 27 import java.net.URI ; 28 import java.net.URISyntaxException ; 29 import java.net.URL ; 30 import java.util.Enumeration ; 31 import java.util.Iterator ; 32 import java.util.LinkedList ; 33 import java.util.List ; 34 import java.util.ResourceBundle ; 35 import java.util.jar.JarEntry ; 36 import java.util.jar.JarFile ; 37 import javax.swing.DefaultComboBoxModel ; 38 39 import javax.swing.DefaultListModel ; 40 import javax.swing.JComponent ; 41 import javax.swing.JFileChooser ; 42 import javax.swing.ListSelectionModel ; 43 import javax.swing.SwingUtilities ; 44 import org.openide.ErrorManager; 45 import org.openide.filesystems.FileObject; 46 47 import org.openide.filesystems.FileUtil; 48 import org.openide.filesystems.URLMapper; 49 import org.openide.util.NbBundle; 50 import org.openide.util.RequestProcessor; 51 import org.openide.windows.WindowManager; 52 import org.netbeans.api.db.explorer.JDBCDriver; 53 import org.netbeans.api.progress.ProgressHandle; 54 import org.netbeans.api.progress.ProgressHandleFactory; 55 import org.netbeans.modules.classfile.ClassFile; 56 import org.netbeans.modules.classfile.ClassName; 57 import org.netbeans.modules.db.util.DriverListUtil; 58 59 public class AddDriverDialog extends javax.swing.JPanel { 60 61 private DefaultListModel dlm; 62 private List drvs; 63 private boolean customizer; 64 private ProgressHandle progressHandle; 65 private JComponent progressComponent; 66 67 private static final String BUNDLE = "org.netbeans.modules.db.resources.Bundle"; 69 70 public AddDriverDialog() { 71 customizer = false; 72 initComponents(); 73 initAccessibility(); 77 dlm = (DefaultListModel ) drvList.getModel(); 78 drvs = new LinkedList (); 79 } 80 81 public AddDriverDialog(JDBCDriver drv) { 82 this(); 83 customizer = true; 84 85 String fileName; 86 URL [] urls = drv.getURLs(); 87 for (int i = 0; i < urls.length; i++) { 88 FileObject fo = URLMapper.findFileObject(urls[i]); 89 if (fo == null) { 90 try { 91 fileName = new File (new URI (urls[i].toExternalForm())).getAbsolutePath(); 92 } catch (URISyntaxException e) { 93 ErrorManager.getDefault().notify(e); 94 fileName = null; 95 } 96 } else { 97 fileName = FileUtil.toFile(fo).getAbsolutePath(); 98 } 99 if (fileName != null) { 100 dlm.addElement(fileName); 101 drvs.add(urls[i]); 102 } 103 } 104 drvClassComboBox.addItem(drv.getClassName()); 105 drvClassComboBox.setSelectedItem(drv.getClassName()); 106 nameTextField.setText(drv.getDisplayName()); 107 } 108 109 private void initAccessibility() { 110 ResourceBundle b = NbBundle.getBundle(BUNDLE); 111 this.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverDialogA11yDesc")); drvListLabel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverDriverFileA11yDesc")); drvList.getAccessibleContext().setAccessibleName(b.getString("ACS_AddDriverDriverFileListA11yName")); drvClassLabel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverDriverDriverClassA11yDesc")); drvClassComboBox.getAccessibleContext().setAccessibleName(b.getString("ACS_AddDriverDriverDriverClassComboBoxA11yName")); nameLabel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverDriverNameA11yDesc")); nameTextField.getAccessibleContext().setAccessibleName(b.getString("ACS_AddDriverDriverNameTextFieldA11yName")); browseButton.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverAddButtonA11yDesc")); findButton.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverRemoveButtonA11yDesc")); removeButton.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverFindButtonA11yDesc")); progressContainerPanel.getAccessibleContext().setAccessibleName(b.getString("ACS_AddDriverProgressBarA11yName")); progressContainerPanel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverProgressBarA11yDesc")); } 124 125 130 private void initComponents() { 132 java.awt.GridBagConstraints gridBagConstraints; 133 134 drvListLabel = new javax.swing.JLabel (); 135 drvListScrollPane = new javax.swing.JScrollPane (); 136 drvList = new javax.swing.JList (); 137 browseButton = new javax.swing.JButton (); 138 removeButton = new javax.swing.JButton (); 139 drvClassLabel = new javax.swing.JLabel (); 140 drvClassComboBox = new javax.swing.JComboBox (); 141 findButton = new javax.swing.JButton (); 142 nameLabel = new javax.swing.JLabel (); 143 nameTextField = new javax.swing.JTextField (); 144 progressMessageLabel = new javax.swing.JLabel (); 145 progressContainerPanel = new javax.swing.JPanel (); 146 147 FormListener formListener = new FormListener(); 148 149 setLayout(new java.awt.GridBagLayout ()); 150 151 drvListLabel.setLabelFor(drvList); 152 org.openide.awt.Mnemonics.setLocalizedText(drvListLabel, NbBundle.getBundle(BUNDLE).getString("AddDriverDriverFile")); gridBagConstraints = new java.awt.GridBagConstraints (); 154 gridBagConstraints.gridx = 0; 155 gridBagConstraints.gridy = 0; 156 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 157 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 158 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 12); 159 add(drvListLabel, gridBagConstraints); 160 161 drvList.setModel(new DefaultListModel ()); 162 drvListScrollPane.setViewportView(drvList); 163 164 gridBagConstraints = new java.awt.GridBagConstraints (); 165 gridBagConstraints.gridx = 1; 166 gridBagConstraints.gridy = 0; 167 gridBagConstraints.gridheight = 2; 168 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 169 gridBagConstraints.weightx = 1.0; 170 gridBagConstraints.weighty = 1.0; 171 gridBagConstraints.insets = new java.awt.Insets (11, 0, 0, 11); 172 add(drvListScrollPane, gridBagConstraints); 173 174 org.openide.awt.Mnemonics.setLocalizedText(browseButton, NbBundle.getBundle(BUNDLE).getString("AddDriverDriverAdd")); browseButton.addActionListener(formListener); 176 gridBagConstraints = new java.awt.GridBagConstraints (); 177 gridBagConstraints.gridx = 2; 178 gridBagConstraints.gridy = 0; 179 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 180 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 181 gridBagConstraints.insets = new java.awt.Insets (11, 0, 0, 11); 182 add(browseButton, gridBagConstraints); 183 184 org.openide.awt.Mnemonics.setLocalizedText(removeButton, NbBundle.getBundle(BUNDLE).getString("AddDriverDriverRemove")); removeButton.addActionListener(formListener); 186 gridBagConstraints = new java.awt.GridBagConstraints (); 187 gridBagConstraints.gridx = 2; 188 gridBagConstraints.gridy = 1; 189 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 190 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 191 gridBagConstraints.insets = new java.awt.Insets (11, 0, 0, 11); 192 add(removeButton, gridBagConstraints); 193 194 drvClassLabel.setLabelFor(drvClassComboBox); 195 org.openide.awt.Mnemonics.setLocalizedText(drvClassLabel, NbBundle.getBundle(BUNDLE).getString("AddDriverDriverClass")); gridBagConstraints = new java.awt.GridBagConstraints (); 197 gridBagConstraints.gridx = 0; 198 gridBagConstraints.gridy = 2; 199 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 200 gridBagConstraints.insets = new java.awt.Insets (11, 12, 0, 12); 201 add(drvClassLabel, gridBagConstraints); 202 203 drvClassComboBox.setEditable(true); 204 drvClassComboBox.addActionListener(formListener); 205 gridBagConstraints = new java.awt.GridBagConstraints (); 206 gridBagConstraints.gridx = 1; 207 gridBagConstraints.gridy = 2; 208 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 209 gridBagConstraints.insets = new java.awt.Insets (11, 0, 0, 12); 210 add(drvClassComboBox, gridBagConstraints); 211 212 org.openide.awt.Mnemonics.setLocalizedText(findButton, NbBundle.getBundle(BUNDLE).getString("AddDriverDriverFind")); findButton.addActionListener(formListener); 214 gridBagConstraints = new java.awt.GridBagConstraints (); 215 gridBagConstraints.gridx = 2; 216 gridBagConstraints.gridy = 2; 217 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 218 gridBagConstraints.insets = new java.awt.Insets (11, 0, 0, 11); 219 add(findButton, gridBagConstraints); 220 221 nameLabel.setLabelFor(nameTextField); 222 org.openide.awt.Mnemonics.setLocalizedText(nameLabel, NbBundle.getBundle(BUNDLE).getString("AddDriverDriverName")); gridBagConstraints = new java.awt.GridBagConstraints (); 224 gridBagConstraints.gridx = 0; 225 gridBagConstraints.gridy = 3; 226 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 227 gridBagConstraints.insets = new java.awt.Insets (11, 12, 0, 12); 228 add(nameLabel, gridBagConstraints); 229 gridBagConstraints = new java.awt.GridBagConstraints (); 230 gridBagConstraints.gridx = 1; 231 gridBagConstraints.gridy = 3; 232 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 233 gridBagConstraints.weightx = 1.0; 234 gridBagConstraints.insets = new java.awt.Insets (11, 0, 0, 12); 235 add(nameTextField, gridBagConstraints); 236 237 org.openide.awt.Mnemonics.setLocalizedText(progressMessageLabel, " "); 238 gridBagConstraints = new java.awt.GridBagConstraints (); 239 gridBagConstraints.gridx = 0; 240 gridBagConstraints.gridy = 4; 241 gridBagConstraints.gridwidth = 3; 242 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 243 gridBagConstraints.weightx = 1.0; 244 gridBagConstraints.insets = new java.awt.Insets (11, 12, 0, 12); 245 add(progressMessageLabel, gridBagConstraints); 246 247 progressContainerPanel.setMinimumSize(new java.awt.Dimension (20, 20)); 248 progressContainerPanel.setPreferredSize(new java.awt.Dimension (20, 20)); 249 progressContainerPanel.setLayout(new java.awt.BorderLayout ()); 250 gridBagConstraints = new java.awt.GridBagConstraints (); 251 gridBagConstraints.gridx = 0; 252 gridBagConstraints.gridy = 5; 253 gridBagConstraints.gridwidth = 3; 254 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 255 gridBagConstraints.weightx = 1.0; 256 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 12); 257 add(progressContainerPanel, gridBagConstraints); 258 } 259 260 262 private class FormListener implements java.awt.event.ActionListener { 263 FormListener() {} 264 public void actionPerformed(java.awt.event.ActionEvent evt) { 265 if (evt.getSource() == browseButton) { 266 AddDriverDialog.this.browseButtonActionPerformed(evt); 267 } 268 else if (evt.getSource() == removeButton) { 269 AddDriverDialog.this.removeButtonActionPerformed(evt); 270 } 271 else if (evt.getSource() == drvClassComboBox) { 272 AddDriverDialog.this.drvClassComboBoxActionPerformed(evt); 273 } 274 else if (evt.getSource() == findButton) { 275 AddDriverDialog.this.findButtonActionPerformed(evt); 276 } 277 } 278 } 280 private void drvClassComboBoxActionPerformed(java.awt.event.ActionEvent evt) { if (!customizer) 282 nameTextField.setText(DriverListUtil.findFreeName(DriverListUtil.getName((String ) drvClassComboBox.getSelectedItem()))); 283 } 285 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { stopProgress(); 287 288 ListSelectionModel lsm = drvList.getSelectionModel(); 289 int count = dlm.getSize(); 290 int i = 0; 291 292 if (count < 1) 293 return; 294 295 do { 296 if (lsm.isSelectedIndex(i)) { 297 dlm.remove(i); 298 drvs.remove(i); 299 count--; 300 continue; 301 } 302 i++; 303 } while (count != i); 304 305 findDriverClass(); 306 } 308 private void findButtonActionPerformed(java.awt.event.ActionEvent evt) { 310 RequestProcessor.getDefault().post(new Runnable () { 311 public void run() { 312 startProgress(); 313 314 for (int i = 0; i < drvs.size(); i++) { 315 try { 316 URL url = (URL )drvs.get(i); 317 File file = new File (new URI (url.toExternalForm())); 318 JarFile jf = new JarFile (file); 319 try { 320 Enumeration entries = jf.entries(); 321 while (entries.hasMoreElements()) { 322 JarEntry entry = (JarEntry )entries.nextElement(); 323 String className = entry.getName(); 324 if (className.endsWith(".class")) { className = className.replace('/', '.'); 326 className = className.substring(0, className.length() - 6); 327 InputStream stream = jf.getInputStream(entry); 328 try { 329 ClassFile classFile = new ClassFile(stream, false); 330 for (Iterator iter = classFile.getInterfaces().iterator(); iter.hasNext();) { 331 ClassName interfaceName = (ClassName)iter.next(); 332 if ("java.sql.Driver".equals(interfaceName.getExternalName())) { addDriverClass(className); 334 } 335 } 336 } finally { 337 stream.close(); 338 } 339 } 340 } 341 } finally { 342 jf.close(); 343 } 344 } catch (IOException exc) { 345 } catch (URISyntaxException use) { 347 } 349 } 350 351 stopProgress(); 352 } 353 }, 0); 354 } 356 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) { stopProgress(); 358 359 JFileChooser fc = new JFileChooser (); 360 FileUtil.preventFileChooserSymlinkTraversal(fc, null); 361 fc.setDialogTitle(NbBundle.getBundle(BUNDLE).getString("AddDriver_Chooser_Title")); fc.setMultiSelectionEnabled(true); 363 fc.setAcceptAllFileFilterUsed(false); 364 365 fc.setFileFilter(new javax.swing.filechooser.FileFilter () { 367 public boolean accept(File f) { 368 return (f.isDirectory() || f.getName().endsWith(".jar") || f.getName().endsWith(".zip")); } 370 371 public String getDescription() { 372 return NbBundle.getBundle(BUNDLE).getString("AddDriver_Chooser_Filter"); } 374 }); 375 376 if (fc.showOpenDialog(WindowManager.getDefault().getMainWindow()) == JFileChooser.APPROVE_OPTION) { File [] files = fc.getSelectedFiles(); 378 for (int i = 0; i < files.length; i++) 379 if (files[i] != null && files[i].isFile()) { 380 dlm.addElement(files[i].toString()); 381 try { 382 drvs.add(files[i].toURI().toURL()); 383 } catch (MalformedURLException exc) { 384 } 386 } 387 388 findDriverClass(); 389 } 390 } 392 393 private javax.swing.JButton browseButton; 395 private javax.swing.JComboBox drvClassComboBox; 396 private javax.swing.JLabel drvClassLabel; 397 private javax.swing.JList drvList; 398 private javax.swing.JLabel drvListLabel; 399 private javax.swing.JScrollPane drvListScrollPane; 400 private javax.swing.JButton findButton; 401 private javax.swing.JLabel nameLabel; 402 private javax.swing.JTextField nameTextField; 403 private javax.swing.JPanel progressContainerPanel; 404 private javax.swing.JLabel progressMessageLabel; 405 private javax.swing.JButton removeButton; 406 408 public String getDisplayName() { 409 return nameTextField.getText(); 410 } 411 412 public List getDriverLocation() { 413 return drvs; 414 } 415 416 public String getDriverClass() { 417 return (String ) drvClassComboBox.getSelectedItem(); 418 } 419 420 private void findDriverClass() { 421 JarFile jf; 422 String [] drivers = (String []) DriverListUtil.getDrivers().toArray(new String [DriverListUtil.getDrivers().size()]); 423 424 drvClassComboBox.removeAllItems(); 425 for (int i = 0; i < drvs.size(); i++) { 426 try { 427 URL url = (URL )drvs.get(i); 428 File file = new File (new URI (url.toExternalForm())); 429 jf = new JarFile (file); 430 for (int j = 0; j < drivers.length; j++) 431 if (jf.getEntry(drivers[j].replace('.', '/') + ".class") != null) addDriverClass(drivers[j]); 433 jf.close(); 434 } catch (IOException exc) { 435 } catch (URISyntaxException e) { 437 } 439 } 440 } 441 442 private void addDriverClass(String drv) { 443 if (((DefaultComboBoxModel ) drvClassComboBox.getModel()).getIndexOf(drv) < 0) 444 drvClassComboBox.addItem(drv); 445 } 446 447 private void startProgress() { 448 SwingUtilities.invokeLater(new Runnable () { 449 public void run() { 450 progressHandle = ProgressHandleFactory.createHandle(null); 451 progressComponent = ProgressHandleFactory.createProgressComponent(progressHandle); 452 progressContainerPanel.add(progressComponent, BorderLayout.CENTER); 453 progressHandle.start(); 454 progressMessageLabel.setText(NbBundle.getBundle(BUNDLE).getString("AddDriverProgressStart")); 455 } 456 }); 457 } 458 459 private void stopProgress() { 460 SwingUtilities.invokeLater(new Runnable () { 461 public void run() { 462 if (progressHandle != null) { 463 progressHandle.finish(); 464 progressHandle = null; 465 progressMessageLabel.setText(" "); progressContainerPanel.remove(progressComponent); 467 repaint(); 469 } 470 } 471 }); 472 } 473 } 474 | Popular Tags |