KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > actions > AddDriverAction


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.actions;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.api.db.explorer.DatabaseException;
29
30 import org.openide.DialogDescriptor;
31 import org.openide.DialogDisplayer;
32 import org.openide.NotifyDescriptor;
33 import org.openide.nodes.Node;
34
35 import org.netbeans.modules.db.explorer.dlg.AddDriverDialog;
36 import org.netbeans.api.db.explorer.JDBCDriver;
37 import org.netbeans.api.db.explorer.JDBCDriverManager;
38
39 public class AddDriverAction extends DatabaseAction {
40     static final long serialVersionUID =-109193000951395612L;
41     
42     public void performAction(Node[] activatedNodes) {
43         new AddDriverDialogDisplayer().showDialog();
44     }
45     
46     public static final class AddDriverDialogDisplayer {
47         
48         private Dialog JavaDoc dialog;
49         private JDBCDriver driver;
50         
51         public void showDialog() {
52             final AddDriverDialog dlgPanel = new AddDriverDialog();
53
54             ActionListener JavaDoc actionListener = new ActionListener JavaDoc() {
55                 public void actionPerformed(ActionEvent JavaDoc event) {
56                     if (event.getSource() == DialogDescriptor.OK_OPTION) {
57                         String JavaDoc name = dlgPanel.getDisplayName();
58                         List JavaDoc drvLoc = dlgPanel.getDriverLocation();
59                         String JavaDoc drvClass = dlgPanel.getDriverClass();
60
61                         StringBuffer JavaDoc err = new StringBuffer JavaDoc();
62                         if (drvLoc.size() < 1)
63                             err.append(bundle().getString("AddDriverDialog_MissingFile")); //NOI18N
64
if (drvClass == null || drvClass.equals("")) {
65                             if (err.length() > 0)
66                                 err.append(", "); //NOI18N
67

68                             err.append(bundle().getString("AddDriverDialog_MissingClass")); //NOI18N
69
}
70                         if (err.length() > 0) {
71                             String JavaDoc message = MessageFormat.format(bundle().getString("AddDriverDialog_ErrorMessage"), new String JavaDoc[] {err.toString()}); //NOI18N
72
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.INFORMATION_MESSAGE));
73
74                             return;
75                         }
76
77                         if (dialog != null)
78                             dialog.dispose();
79
80                         //create driver instance and save it in the XML format
81
if (name == null || name.equals("")) // NOI18N
82
name = drvClass;
83
84                         try {
85                             driver = JDBCDriver.create(name, name, drvClass, (URL JavaDoc[]) drvLoc.toArray(new URL JavaDoc[drvLoc.size()]));
86                             JDBCDriverManager.getDefault().addDriver(driver);
87                         } catch (DatabaseException exc) {
88                             //PENDING
89
}
90                     }
91                 }
92             };
93
94             DialogDescriptor descriptor = new DialogDescriptor(dlgPanel, bundle().getString("AddDriverDialogTitle"), true, actionListener); //NOI18N
95
Object JavaDoc [] closingOptions = {DialogDescriptor.CANCEL_OPTION};
96             descriptor.setClosingOptions(closingOptions);
97             dialog = DialogDisplayer.getDefault().createDialog(descriptor);
98             dialog.setVisible(true);
99         }
100     }
101 }
102
Popular Tags