1 19 20 package org.netbeans.modules.db.explorer.actions; 21 22 import java.awt.Dialog ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.io.IOException ; 26 import java.net.URL ; 27 import java.text.MessageFormat ; 28 import java.util.List ; 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 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")) 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; JDBCDriver drv = info.getJDBCDriver(); 66 if (drv == null) { 67 return; 68 } 69 final AddDriverDialog dlgPanel = new AddDriverDialog(drv); 70 71 72 ActionListener actionListener = new ActionListener () { 73 public void actionPerformed(ActionEvent event) { 74 if (event.getSource() == DialogDescriptor.OK_OPTION) { 75 String displayName = dlgPanel.getDisplayName(); 76 List drvLoc = dlgPanel.getDriverLocation(); 77 String drvClass = dlgPanel.getDriverClass(); 78 79 StringBuffer err = new StringBuffer (); 80 if (drvLoc.size() < 1) 81 err.append(bundle().getString("AddDriverDialog_MissingFile")); if (drvClass == null || drvClass.equals("")) { 83 if (err.length() > 0) 84 err.append(", "); 86 err.append(bundle().getString("AddDriverDialog_MissingClass")); } 88 if (err.length() > 0) { 89 String message = MessageFormat.format(bundle().getString("AddDriverDialog_ErrorMessage"), new String [] {err.toString()}); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.INFORMATION_MESSAGE)); 91 92 return; 93 } 94 95 closeDialog(); 96 97 if (displayName == null || displayName.equals("")) 99 displayName = drvClass; 100 101 try { 102 String oldName = info.getJDBCDriver().getName(); 103 info.delete(); 104 JDBCDriverManager.getDefault().addDriver(JDBCDriver.create(oldName, displayName, drvClass, (URL []) drvLoc.toArray(new URL [drvLoc.size()]))); 105 } catch (IOException exc) { 106 } catch (DatabaseException exc) { 108 } 110 111 javax.swing.SwingUtilities.invokeLater(new Runnable () { 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 exc) { 118 } 120 } 121 }); 122 123 } 124 } 125 }; 126 127 DialogDescriptor descriptor = new DialogDescriptor(dlgPanel, bundle().getString("AddDriverDialogTitle"), true, actionListener); Object [] 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 |