Back to index

wims  3.65+svn20090927
ConnectDialog.java
Go to the documentation of this file.
00001 /*
00002  * @ (#)ConnectDialog.java
00003  *
00004  * $Id: ConnectDialog.java,v 1.11 2002/08/08 05:15:02 huaz Exp $
00005  *
00006  * Created on May 19, 2001, 09:10:28 PM
00007  *
00008  * Changelogs:
00009  *
00010  * first version - Shiraz Kanga
00011  * various fixes and enhancements - Hua Zhong
00012  */
00013 package SharpTools;
00014 import javax.swing.*;
00015 import java.awt.*;
00016 import javax.swing.BorderFactory;
00017 import javax.swing.border.Border;
00018 import java.awt.event.*;
00019 import java.sql.*;
00020 import java.util.Vector;
00021 
00031 public class ConnectDialog extends SharpDialog {
00032     
00033     private JFrame frame;
00034     private JOptionPane optionPane;
00035     private JCheckBox lockTableBox;
00036     private JCheckBox verifySaveBox;
00037     final ImageIcon removeIcon=new ImageIcon(getClass().getResource("/images/no.gif"));
00038     //final private static ImageIcon removeIcon = SharpTools.getImageIcon ("no.gif");
00039     final ImageIcon databaseIcon=new ImageIcon(getClass().getResource("/images/database32.gif"));
00040     //final private static ImageIcon databaseIcon = SharpTools.getImageIcon ("database32.gif");
00041     final private static ImageIcon dbErrorIcon = null;
00042     
00043     private Connection dbConnection = null;
00044     
00045     final private JTextField connectnameField = new JTextField ();
00046     final private JTextField usernameField = new JTextField ();
00047     final private JPasswordField passwordField = new JPasswordField ();
00048     final private JTextField driverField = new JTextField ();
00049     final private JTextField urlField = new JTextField ();
00050     //    final private JCheckBox save = new JCheckBox("Save connection", true);
00051     final private JCheckBox saveConnection = new JCheckBox("Save Connection", true);
00052     final private JCheckBox savePassword = new JCheckBox("Save Password", false);
00053 
00054     private int maxConn = 0;
00055     
00056     final private JButton removeButton = new JButton("Remove", removeIcon);
00057     private JComboBox box;
00058     
00059     public ConnectDialog (JFrame aFrame) {
00060        super (aFrame, "Connect to Database", true);
00061        
00062        frame = aFrame;
00063 
00064        //various properties of the dialog labels and text fields      
00065        final String msgString0 = "Connection Name:";
00066        final String msgString1 = "Username:";
00067        final String msgString2 = "Password:";
00068        final String msgString3 = "Driver:";
00069        final String msgString4 = "URL:";
00070 
00071        final Config config = SharpTools.getConfig();
00072        maxConn = config.getInt ("NUMCONNECTIONS");
00073 
00074        final Vector possibleValues = new Vector();
00075        possibleValues.add("Previous connections");
00076        
00077        // use Vector so that we won't enter null pointers in - huaz
00078        for (int i=1; i<=maxConn; i++) {
00079            String entry = config.get ("CONNECTION." + i + ".NAME");
00080            if (entry != null && entry.length() > 0)
00081               possibleValues.add(entry);
00082            else
00083               break;    
00084        }
00085 
00086        box = new JComboBox(possibleValues);
00087        box.addItemListener(new ItemListener() {
00088               public void itemStateChanged(ItemEvent e) {
00089                   if (e.getStateChange() == ItemEvent.SELECTED) {
00090                          
00091                      //                   Config config = SharpTools.getConfig();
00092                      int index = box.getSelectedIndex();
00093                      if (index <= 0)
00094                          return;
00095                      String key = "CONNECTION."+index;
00096                      connectnameField.setText(config.get(key+".NAME"));
00097                      usernameField.setText(config.get(key+".USERNAME"));
00098                      passwordField.setText(config.get(key+".PASSWORD"));
00099                      urlField.setText(config.get(key+".URL"));
00100                      driverField.setText(config.get(key+".DRIVER"));
00101                   }
00102               }
00103            });
00104 
00105        removeButton.setToolTipText("Remove the selected connection");
00106        removeButton.setMnemonic(KeyEvent.VK_R);
00107        removeButton.addActionListener(new ActionListener() {
00108               public void actionPerformed(ActionEvent e) {
00109                   int index = box.getSelectedIndex();
00110                   if (index <= 0)
00111                      return;
00112                   //            Config config = SharpTools.getConfig();
00113 
00114                   // we need to move the following connections upward
00115                   for (int i = index; i < maxConn; i++) {
00116                      config.set("CONNECTION."+i+".NAME",
00117                                config.get("CONNECTION."+(i+1)+".NAME"));
00118                      config.set("CONNECTION."+i+".USERNAME",
00119                                config.get("CONNECTION."+(i+1)+".USERNAME"));
00120                      config.set("CONNECTION."+i+".PASSWORD",
00121                                config.get("CONNECTION."+(i+1)+".PASSWORD"));
00122                      config.set("CONNECTION."+i+".URL",
00123                                config.get("CONNECTION."+(i+1)+".URL"));
00124                      config.set("CONNECTION."+i+".DRIVER",
00125                                config.get("CONNECTION."+(i+1)+".DRIVER"));
00126                   }
00127 
00128                   String maxkey = "CONNECTION."+maxConn;
00129                   config.set(maxkey+".NAME", "");
00130 
00131                   // remove from item
00132                   box.setSelectedIndex(0);
00133                   box.removeItemAt(index);
00134               }
00135            });
00136 
00137        JPanel panel = new JPanel(new FlowLayout());
00138        panel.add(box);
00139        panel.add(removeButton);
00140 
00141        JPanel cbPanel = new JPanel(new FlowLayout());
00142        cbPanel.add(saveConnection);
00143        cbPanel.add(savePassword);
00144        
00145        Object[] input = {
00146            panel,
00147            msgString0, connectnameField, msgString1, usernameField,
00148            msgString2, passwordField, msgString3, driverField,
00149            msgString4, urlField, cbPanel
00150        };
00151        
00152        setOptionPane (input,
00153                      JOptionPane.PLAIN_MESSAGE,
00154                      JOptionPane.OK_CANCEL_OPTION,
00155                      databaseIcon);
00156        
00157     }
00158 
00159     public Connection getConnection() {
00160        return dbConnection;
00161     }
00162 
00163     protected boolean onOK() {
00164         String connectName = connectnameField.getText ().trim();
00165         String dbUsername = usernameField.getText ().trim();
00166         String dbPassword = String.copyValueOf(passwordField.getPassword ());
00167         String dbDriver = driverField.getText ().trim();
00168        String dbUrl = urlField.getText ().trim();       
00169        
00170        // validation moved from Database.java - huaz
00171 
00172        if ((dbUsername == null || dbUsername.length () == 0) &&
00173            (dbUrl == null || dbUrl.length () == 0) &&
00174            (dbDriver == null || dbDriver.length () == 0)) {
00175            SharpOptionPane.showMessageDialog(this,
00176                                          "You must provide values for Username, Url and Driver.\n",
00177                                          "Connect",
00178                                          JOptionPane.INFORMATION_MESSAGE,
00179                                          databaseIcon);
00180            return false;
00181        }
00182 
00183        try {
00184            Class.forName (dbDriver);
00185        }
00186        catch (ClassNotFoundException e) {
00187            SharpOptionPane.showMessageDialog (this, "Unable to load class " + dbDriver + ". Please ensure that it is in your classpath.\n" + e.toString (),
00188                                           "Class Not Found", JOptionPane.ERROR_MESSAGE, dbErrorIcon);
00189            return false;
00190        }
00191        
00192        try {
00193            dbConnection =  DriverManager.getConnection (dbUrl, dbUsername, dbPassword);
00194        }
00195        catch (SQLException e) {
00196            SharpOptionPane.showMessageDialog (this, "Unable to connect to the database at " + dbUrl + ".\nPlease ensure that the URL, Username and Password are correct.\n\n" + e.toString (),
00197                                           "Connect", JOptionPane.ERROR_MESSAGE, dbErrorIcon);
00198            return false;
00199        }      
00200        
00201        // now we could try to save the connection - huaz
00202        
00203        // if saveConnection is not checked we just return
00204        if (! saveConnection.isSelected())
00205            return true;
00206        
00207        // if no name provided we don't save
00208        if (connectName.length() == 0) {
00209            SharpOptionPane.showMessageDialog(this, "You did not enter a connection name.\n\nThis connection succeeded but will not be saved.\n", "Save Connection", JOptionPane.WARNING_MESSAGE);
00210            return true;
00211        }
00212            
00213 
00214        // first check whether the connectName is already saved
00215        int index;
00216        
00217        for (index = 1; index < box.getItemCount(); index++)
00218            if (connectName.equals(box.getItemAt(index).toString()))
00219               break;
00220 
00221        if ((index != box.getItemCount() || box.getItemCount() <= maxConn)) {
00222            String key = "CONNECTION."+index;
00223            Debug.println("Saving "+key);
00224        
00225            Config config = SharpTools.getConfig();
00226            config.set(key+".NAME", connectName);
00227            config.set(key+".USERNAME", dbUsername);
00228            if (savePassword.isSelected())
00229               config.set(key+".PASSWORD", dbPassword);
00230            config.set(key+".URL", dbUrl);
00231            config.set(key+".DRIVER", dbDriver);
00232        }
00233        else
00234            SharpOptionPane.showMessageDialog(this, "You have defined "+maxConn+" connections.\nYour current connection setting cannot be saved.\n\nRefer to the manual for advanced configuration.\n", "Save Connection", JOptionPane.WARNING_MESSAGE);
00235            
00236         return true;
00237     }
00238 
00239     protected void onOpen() {
00240         // set the initial focus to textField
00241         connectnameField.requestFocus ();
00242     }
00243 }
00244 
00245