KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import java.util.List JavaDoc;
29 import org.netbeans.api.db.explorer.DatabaseException;
30
31 import org.openide.DialogDescriptor;
32 import org.openide.DialogDisplayer;
33 import org.openide.NotifyDescriptor;
34 import org.openide.nodes.Node;
35
36 import org.netbeans.modules.db.explorer.dlg.AddDriverDialog;
37 import org.netbeans.api.db.explorer.JDBCDriver;
38 import org.netbeans.api.db.explorer.JDBCDriverManager;
39 import org.netbeans.modules.db.explorer.infos.DriverListNodeInfo;
40 import org.netbeans.modules.db.explorer.infos.DriverNodeInfo;
41
42 public class CustomizeDriverAction extends DatabaseAction {
43     static final long serialVersionUID =-109193000951395612L;
44     
45     private Dialog JavaDoc dialog;
46     
47     protected boolean enable(Node[] activatedNodes) {
48         if (activatedNodes == null || activatedNodes.length != 1)
49             return false;
50         
51         DriverNodeInfo info = (DriverNodeInfo) activatedNodes[0].getCookie(DriverNodeInfo.class);
52         if (info != null && info.getURL().equals("sun.jdbc.odbc.JdbcOdbcDriver")) //NOI18N
53
return false;
54         
55         return true;
56     }
57     
58     public void performAction(Node[] activatedNodes) {
59         final Node[] n = activatedNodes;
60         
61         int drvIndex = 0;
62         final DriverNodeInfo info = (DriverNodeInfo) n[0].getCookie(DriverNodeInfo.class);
63         if (info == null)
64             return; //should not happen
65
JDBCDriver drv = info.getJDBCDriver();
66         if (drv == null) {
67             return;
68         }
69         final AddDriverDialog dlgPanel = new AddDriverDialog(drv);
70         
71         
72         ActionListener JavaDoc actionListener = new ActionListener JavaDoc() {
73             public void actionPerformed(ActionEvent JavaDoc event) {
74                 if (event.getSource() == DialogDescriptor.OK_OPTION) {
75                     String JavaDoc displayName = dlgPanel.getDisplayName();
76                     List JavaDoc drvLoc = dlgPanel.getDriverLocation();
77                     String JavaDoc drvClass = dlgPanel.getDriverClass();
78                     
79                     StringBuffer JavaDoc err = new StringBuffer JavaDoc();
80                     if (drvLoc.size() < 1)
81                         err.append(bundle().getString("AddDriverDialog_MissingFile")); //NOI18N
82
if (drvClass == null || drvClass.equals("")) {
83                         if (err.length() > 0)
84                             err.append(", "); //NOI18N
85

86                         err.append(bundle().getString("AddDriverDialog_MissingClass")); //NOI18N
87
}
88                     if (err.length() > 0) {
89                         String JavaDoc message = MessageFormat.format(bundle().getString("AddDriverDialog_ErrorMessage"), new String JavaDoc[] {err.toString()}); //NOI18N
90
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.INFORMATION_MESSAGE));
91                         
92                         return;
93                     }
94                     
95                     closeDialog();
96                     
97                     //create driver instance and save it in the XML format
98
if (displayName == null || displayName.equals(""))
99                         displayName = drvClass;
100                     
101                     try {
102                         String JavaDoc oldName = info.getJDBCDriver().getName();
103                         info.delete();
104                         JDBCDriverManager.getDefault().addDriver(JDBCDriver.create(oldName, displayName, drvClass, (URL JavaDoc[]) drvLoc.toArray(new URL JavaDoc[drvLoc.size()])));
105                     } catch (IOException JavaDoc exc) {
106                         //PENDING
107
} catch (DatabaseException exc) {
108                         //PENDING
109
}
110                     
111                     javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
112                         public void run() {
113                             try {
114                                 DriverListNodeInfo info = (DriverListNodeInfo) n[0].getCookie(DriverListNodeInfo.class);
115                                 if (info != null)
116                                     info.refreshChildren();
117                             } catch (Exception JavaDoc exc) {
118 // exc.printStackTrace();
119
}
120                         }
121                     });
122
123                 }
124             }
125         };
126
127         DialogDescriptor descriptor = new DialogDescriptor(dlgPanel, bundle().getString("AddDriverDialogTitle"), true, actionListener); //NOI18N
128
Object JavaDoc [] closingOptions = {DialogDescriptor.CANCEL_OPTION};
129         descriptor.setClosingOptions(closingOptions);
130         dialog = DialogDisplayer.getDefault().createDialog(descriptor);
131         dialog.setVisible(true);
132     }
133     
134     private void closeDialog() {
135         if (dialog != null)
136             dialog.dispose();
137     }
138 }
139
Popular Tags