KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > dlg > AddDriverDialog


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.db.explorer.dlg;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URI JavaDoc;
28 import java.net.URISyntaxException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.ResourceBundle JavaDoc;
35 import java.util.jar.JarEntry JavaDoc;
36 import java.util.jar.JarFile JavaDoc;
37 import javax.swing.DefaultComboBoxModel JavaDoc;
38
39 import javax.swing.DefaultListModel JavaDoc;
40 import javax.swing.JComponent JavaDoc;
41 import javax.swing.JFileChooser JavaDoc;
42 import javax.swing.ListSelectionModel JavaDoc;
43 import javax.swing.SwingUtilities JavaDoc;
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 JavaDoc {
60     
61     private DefaultListModel JavaDoc dlm;
62     private List JavaDoc drvs;
63     private boolean customizer;
64     private ProgressHandle progressHandle;
65     private JComponent JavaDoc progressComponent;
66     
67     private static final String JavaDoc BUNDLE = "org.netbeans.modules.db.resources.Bundle"; //NOI18N
68

69     /** Creates new form AddDriverDialog1 */
70     public AddDriverDialog() {
71         customizer = false;
72         initComponents();
73         // hack to force the progressContainerPanel honor its preferred height
74
// without it, the preferred height is sometimes ignored during resize
75
// progressContainerPanel.add(Box.createVerticalStrut(progressContainerPanel.getPreferredSize().height), BorderLayout.EAST);
76
initAccessibility();
77         dlm = (DefaultListModel JavaDoc) drvList.getModel();
78         drvs = new LinkedList JavaDoc();
79     }
80     
81     public AddDriverDialog(JDBCDriver drv) {
82         this();
83         customizer = true;
84         
85         String JavaDoc fileName;
86         URL JavaDoc[] 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 JavaDoc(new URI JavaDoc(urls[i].toExternalForm())).getAbsolutePath();
92                 } catch (URISyntaxException JavaDoc 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 JavaDoc b = NbBundle.getBundle(BUNDLE);
111         this.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverDialogA11yDesc")); //NOI18N
112
drvListLabel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverDriverFileA11yDesc")); //NOI18N
113
drvList.getAccessibleContext().setAccessibleName(b.getString("ACS_AddDriverDriverFileListA11yName")); //NOI18N
114
drvClassLabel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverDriverDriverClassA11yDesc")); //NOI18N
115
drvClassComboBox.getAccessibleContext().setAccessibleName(b.getString("ACS_AddDriverDriverDriverClassComboBoxA11yName")); //NOI18N
116
nameLabel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverDriverNameA11yDesc")); //NOI18N
117
nameTextField.getAccessibleContext().setAccessibleName(b.getString("ACS_AddDriverDriverNameTextFieldA11yName")); //NOI18N
118
browseButton.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverAddButtonA11yDesc")); //NOI18N
119
findButton.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverRemoveButtonA11yDesc")); //NOI18N
120
removeButton.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverFindButtonA11yDesc")); //NOI18N
121
progressContainerPanel.getAccessibleContext().setAccessibleName(b.getString("ACS_AddDriverProgressBarA11yName")); //NOI18N
122
progressContainerPanel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_AddDriverProgressBarA11yDesc")); //NOI18N
123
}
124
125     /** This method is called from within the constructor to
126      * initialize the form.
127      * WARNING: Do NOT modify this code. The content of this method is
128      * always regenerated by the Form Editor.
129      */

130     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
131
private void initComponents() {
132         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
133
134         drvListLabel = new javax.swing.JLabel JavaDoc();
135         drvListScrollPane = new javax.swing.JScrollPane JavaDoc();
136         drvList = new javax.swing.JList JavaDoc();
137         browseButton = new javax.swing.JButton JavaDoc();
138         removeButton = new javax.swing.JButton JavaDoc();
139         drvClassLabel = new javax.swing.JLabel JavaDoc();
140         drvClassComboBox = new javax.swing.JComboBox JavaDoc();
141         findButton = new javax.swing.JButton JavaDoc();
142         nameLabel = new javax.swing.JLabel JavaDoc();
143         nameTextField = new javax.swing.JTextField JavaDoc();
144         progressMessageLabel = new javax.swing.JLabel JavaDoc();
145         progressContainerPanel = new javax.swing.JPanel JavaDoc();
146
147         FormListener formListener = new FormListener();
148
149         setLayout(new java.awt.GridBagLayout JavaDoc());
150
151         drvListLabel.setLabelFor(drvList);
152         org.openide.awt.Mnemonics.setLocalizedText(drvListLabel, NbBundle.getBundle(BUNDLE).getString("AddDriverDriverFile")); // NOI18N
153
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(12, 12, 0, 12);
159         add(drvListLabel, gridBagConstraints);
160
161         drvList.setModel(new DefaultListModel JavaDoc());
162         drvListScrollPane.setViewportView(drvList);
163
164         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(11, 0, 0, 11);
172         add(drvListScrollPane, gridBagConstraints);
173
174         org.openide.awt.Mnemonics.setLocalizedText(browseButton, NbBundle.getBundle(BUNDLE).getString("AddDriverDriverAdd")); // NOI18N
175
browseButton.addActionListener(formListener);
176         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(11, 0, 0, 11);
182         add(browseButton, gridBagConstraints);
183
184         org.openide.awt.Mnemonics.setLocalizedText(removeButton, NbBundle.getBundle(BUNDLE).getString("AddDriverDriverRemove")); // NOI18N
185
removeButton.addActionListener(formListener);
186         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(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")); // NOI18N
196
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
197         gridBagConstraints.gridx = 0;
198         gridBagConstraints.gridy = 2;
199         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
200         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 12, 0, 12);
201         add(drvClassLabel, gridBagConstraints);
202
203         drvClassComboBox.setEditable(true);
204         drvClassComboBox.addActionListener(formListener);
205         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
206         gridBagConstraints.gridx = 1;
207         gridBagConstraints.gridy = 2;
208         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
209         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 0, 0, 12);
210         add(drvClassComboBox, gridBagConstraints);
211
212         org.openide.awt.Mnemonics.setLocalizedText(findButton, NbBundle.getBundle(BUNDLE).getString("AddDriverDriverFind")); // NOI18N
213
findButton.addActionListener(formListener);
214         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
215         gridBagConstraints.gridx = 2;
216         gridBagConstraints.gridy = 2;
217         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
218         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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")); // NOI18N
223
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
224         gridBagConstraints.gridx = 0;
225         gridBagConstraints.gridy = 3;
226         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
227         gridBagConstraints.insets = new java.awt.Insets JavaDoc(11, 12, 0, 12);
228         add(nameLabel, gridBagConstraints);
229         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(11, 0, 0, 12);
235         add(nameTextField, gridBagConstraints);
236
237         org.openide.awt.Mnemonics.setLocalizedText(progressMessageLabel, " ");
238         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(11, 12, 0, 12);
245         add(progressMessageLabel, gridBagConstraints);
246
247         progressContainerPanel.setMinimumSize(new java.awt.Dimension JavaDoc(20, 20));
248         progressContainerPanel.setPreferredSize(new java.awt.Dimension JavaDoc(20, 20));
249         progressContainerPanel.setLayout(new java.awt.BorderLayout JavaDoc());
250         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(5, 12, 0, 12);
257         add(progressContainerPanel, gridBagConstraints);
258     }
259
260     // Code for dispatching events from components to event handlers.
261

262     private class FormListener implements java.awt.event.ActionListener JavaDoc {
263         FormListener() {}
264         public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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     }// </editor-fold>//GEN-END:initComponents
279

280     private void drvClassComboBoxActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_drvClassComboBoxActionPerformed
281
if (!customizer)
282             nameTextField.setText(DriverListUtil.findFreeName(DriverListUtil.getName((String JavaDoc) drvClassComboBox.getSelectedItem())));
283     }//GEN-LAST:event_drvClassComboBoxActionPerformed
284

285     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
286
stopProgress();
287         
288         ListSelectionModel JavaDoc 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     }//GEN-LAST:event_removeButtonActionPerformed
307

308     private void findButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_findButtonActionPerformed
309

310         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
311             public void run() {
312                 startProgress();
313                 
314                 for (int i = 0; i < drvs.size(); i++) {
315                     try {
316                         URL JavaDoc url = (URL JavaDoc)drvs.get(i);
317                         File JavaDoc file = new File JavaDoc(new URI JavaDoc(url.toExternalForm()));
318                         JarFile JavaDoc jf = new JarFile JavaDoc(file);
319                         try {
320                             Enumeration JavaDoc entries = jf.entries();
321                             while (entries.hasMoreElements()) {
322                                 JarEntry JavaDoc entry = (JarEntry JavaDoc)entries.nextElement();
323                                 String JavaDoc className = entry.getName();
324                                 if (className.endsWith(".class")) { // NOI18N
325
className = className.replace('/', '.');
326                                     className = className.substring(0, className.length() - 6);
327                                     InputStream JavaDoc stream = jf.getInputStream(entry);
328                                     try {
329                                         ClassFile classFile = new ClassFile(stream, false);
330                                         for (Iterator JavaDoc iter = classFile.getInterfaces().iterator(); iter.hasNext();) {
331                                             ClassName interfaceName = (ClassName)iter.next();
332                                             if ("java.sql.Driver".equals(interfaceName.getExternalName())) { // NOI18N
333
addDriverClass(className);
334                                             }
335                                         }
336                                     } finally {
337                                         stream.close();
338                                     }
339                                 }
340                             }
341                         } finally {
342                             jf.close();
343                         }
344                     } catch (IOException JavaDoc exc) {
345                         //PENDING
346
} catch (URISyntaxException JavaDoc use) {
347                         //PENDING
348
}
349                 }
350                 
351                 stopProgress();
352             }
353         }, 0);
354     }//GEN-LAST:event_findButtonActionPerformed
355

356     private void browseButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseButtonActionPerformed
357
stopProgress();
358         
359         JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
360         FileUtil.preventFileChooserSymlinkTraversal(fc, null);
361         fc.setDialogTitle(NbBundle.getBundle(BUNDLE).getString("AddDriver_Chooser_Title")); //NOI18N
362
fc.setMultiSelectionEnabled(true);
363         fc.setAcceptAllFileFilterUsed(false);
364         
365         //.jar and .zip file filter
366
fc.setFileFilter(new javax.swing.filechooser.FileFilter JavaDoc() {
367             public boolean accept(File JavaDoc f) {
368                 return (f.isDirectory() || f.getName().endsWith(".jar") || f.getName().endsWith(".zip")); //NOI18N
369
}
370             
371             public String JavaDoc getDescription() {
372                 return NbBundle.getBundle(BUNDLE).getString("AddDriver_Chooser_Filter"); //NOI18N
373
}
374         });
375         
376         if (fc.showOpenDialog(WindowManager.getDefault().getMainWindow()) == JFileChooser.APPROVE_OPTION) { //NOI18N
377
File JavaDoc[] 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 JavaDoc exc) {
384                         //PENDING
385
}
386                 }
387             
388             findDriverClass();
389         }
390     }//GEN-LAST:event_browseButtonActionPerformed
391

392     
393     // Variables declaration - do not modify//GEN-BEGIN:variables
394
private javax.swing.JButton JavaDoc browseButton;
395     private javax.swing.JComboBox JavaDoc drvClassComboBox;
396     private javax.swing.JLabel JavaDoc drvClassLabel;
397     private javax.swing.JList JavaDoc drvList;
398     private javax.swing.JLabel JavaDoc drvListLabel;
399     private javax.swing.JScrollPane JavaDoc drvListScrollPane;
400     private javax.swing.JButton JavaDoc findButton;
401     private javax.swing.JLabel JavaDoc nameLabel;
402     private javax.swing.JTextField JavaDoc nameTextField;
403     private javax.swing.JPanel JavaDoc progressContainerPanel;
404     private javax.swing.JLabel JavaDoc progressMessageLabel;
405     private javax.swing.JButton JavaDoc removeButton;
406     // End of variables declaration//GEN-END:variables
407

408     public String JavaDoc getDisplayName() {
409         return nameTextField.getText();
410     }
411     
412     public List JavaDoc getDriverLocation() {
413         return drvs;
414     }
415     
416     public String JavaDoc getDriverClass() {
417         return (String JavaDoc) drvClassComboBox.getSelectedItem();
418     }
419     
420     private void findDriverClass() {
421         JarFile JavaDoc jf;
422         String JavaDoc[] drivers = (String JavaDoc[]) DriverListUtil.getDrivers().toArray(new String JavaDoc[DriverListUtil.getDrivers().size()]);
423         
424         drvClassComboBox.removeAllItems();
425         for (int i = 0; i < drvs.size(); i++) {
426             try {
427                 URL JavaDoc url = (URL JavaDoc)drvs.get(i);
428                 File JavaDoc file = new File JavaDoc(new URI JavaDoc(url.toExternalForm()));
429                 jf = new JarFile JavaDoc(file);
430                 for (int j = 0; j < drivers.length; j++)
431                     if (jf.getEntry(drivers[j].replace('.', '/') + ".class") != null) //NOI18N
432
addDriverClass(drivers[j]);
433                 jf.close();
434             } catch (IOException JavaDoc exc) {
435                 //PENDING
436
} catch (URISyntaxException JavaDoc e) {
437                 //PENDING
438
}
439         }
440     }
441     
442     private void addDriverClass(String JavaDoc drv) {
443         if (((DefaultComboBoxModel JavaDoc) drvClassComboBox.getModel()).getIndexOf(drv) < 0)
444             drvClassComboBox.addItem(drv);
445     }
446     
447     private void startProgress() {
448         SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc() {
461             public void run() {
462                 if (progressHandle != null) {
463                     progressHandle.finish();
464                     progressHandle = null;
465                     progressMessageLabel.setText(" "); // NOI18N
466
progressContainerPanel.remove(progressComponent);
467                     // without this, the removed progress component remains painted on its parent... why?
468
repaint();
469                 }
470             }
471         });
472     }
473 }
474
Popular Tags