Back to index

wims  3.65+svn20090927
Public Member Functions | Static Public Attributes | Protected Member Functions | Package Attributes | Private Attributes | Static Private Attributes
SharpTools.ConnectDialog Class Reference

This class provides a database conection dialog. More...

Inheritance diagram for SharpTools.ConnectDialog:
Inheritance graph
[legend]
Collaboration diagram for SharpTools.ConnectDialog:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ConnectDialog (JFrame aFrame)
Connection getConnection ()
int getChoice ()
boolean isCancelled ()

Static Public Attributes

static final int CLOSED_OPTION = JOptionPane.CLOSED_OPTION
static final int YES_OPTION = JOptionPane.YES_OPTION
static final int NO_OPTION = JOptionPane.NO_OPTION
static final int OK_OPTION = JOptionPane.OK_OPTION
static final int CANCEL_OPTION = JOptionPane.CANCEL_OPTION

Protected Member Functions

boolean onOK ()
 This function is called when the OK button is pressed.
void onOpen ()
 I provide several hooks for the sublasses to override.
JRootPane createRootPane ()
 override this protected funciton to register ESCAPE with the root pane
boolean onClose ()
 This function is called when (1) the close button is clocked (2) Escape is pressed.
boolean onCancel ()
 This function is called when the Cancel button is pressed.
boolean onYes ()
 This function is called when the Yes button is pressed.
boolean onNo ()
 This function is called when the No button is pressed.
void setOptionPane (Object message, int messageType, int optionType, Icon icon)
 This is used by subclasses to set the JOptionPane component in the dialog.
void setOptionPane (Object message, int messageType, int optionType, Icon icon, int defaultIndex)
 This is used by subclasses to set the JOptionPane component in the dialog.

Package Attributes

final ImageIcon removeIcon = new ImageIcon(getClass().getResource("/images/no.gif"))
final ImageIcon databaseIcon = new ImageIcon(getClass().getResource("/images/database32.gif"))
final ImageIcon okIcon = new ImageIcon(getClass().getResource("/images/ok.gif"))
final ImageIcon cancelIcon = new ImageIcon(getClass().getResource("/images/cancel.gif"))
final ImageIcon noIcon = new ImageIcon(getClass().getResource("/images/no.gif"))

Private Attributes

JFrame frame
JOptionPane optionPane
JCheckBox lockTableBox
JCheckBox verifySaveBox
Connection dbConnection = null
final JTextField connectnameField = new JTextField ()
final JTextField usernameField = new JTextField ()
final JPasswordField passwordField = new JPasswordField ()
final JTextField driverField = new JTextField ()
final JTextField urlField = new JTextField ()
final JCheckBox saveConnection = new JCheckBox("Save Connection", true)
final JCheckBox savePassword = new JCheckBox("Save Password", false)
int maxConn = 0
final JButton removeButton = new JButton("Remove", removeIcon)
JComboBox box

Static Private Attributes

final static ImageIcon dbErrorIcon = null

Detailed Description

This class provides a database conection dialog.

User is prompted to choose various database options.

Author:
Shiraz Kanga (first version)
Hua Zhong (ported to SharpDialog and various enhancements)
Version:
Revision:
1.11

Definition at line 31 of file ConnectDialog.java.


Constructor & Destructor Documentation

SharpTools.ConnectDialog.ConnectDialog ( JFrame  aFrame) [inline]

Definition at line 59 of file ConnectDialog.java.

                                         {
       super (aFrame, "Connect to Database", true);
       
       frame = aFrame;

       //various properties of the dialog labels and text fields      
       final String msgString0 = "Connection Name:";
       final String msgString1 = "Username:";
       final String msgString2 = "Password:";
       final String msgString3 = "Driver:";
       final String msgString4 = "URL:";

       final Config config = SharpTools.getConfig();
       maxConn = config.getInt ("NUMCONNECTIONS");

       final Vector possibleValues = new Vector();
       possibleValues.add("Previous connections");
       
       // use Vector so that we won't enter null pointers in - huaz
       for (int i=1; i<=maxConn; i++) {
           String entry = config.get ("CONNECTION." + i + ".NAME");
           if (entry != null && entry.length() > 0)
              possibleValues.add(entry);
           else
              break;    
       }

       box = new JComboBox(possibleValues);
       box.addItemListener(new ItemListener() {
              public void itemStateChanged(ItemEvent e) {
                  if (e.getStateChange() == ItemEvent.SELECTED) {
                         
                     //                   Config config = SharpTools.getConfig();
                     int index = box.getSelectedIndex();
                     if (index <= 0)
                         return;
                     String key = "CONNECTION."+index;
                     connectnameField.setText(config.get(key+".NAME"));
                     usernameField.setText(config.get(key+".USERNAME"));
                     passwordField.setText(config.get(key+".PASSWORD"));
                     urlField.setText(config.get(key+".URL"));
                     driverField.setText(config.get(key+".DRIVER"));
                  }
              }
           });

       removeButton.setToolTipText("Remove the selected connection");
       removeButton.setMnemonic(KeyEvent.VK_R);
       removeButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  int index = box.getSelectedIndex();
                  if (index <= 0)
                     return;
                  //            Config config = SharpTools.getConfig();

                  // we need to move the following connections upward
                  for (int i = index; i < maxConn; i++) {
                     config.set("CONNECTION."+i+".NAME",
                               config.get("CONNECTION."+(i+1)+".NAME"));
                     config.set("CONNECTION."+i+".USERNAME",
                               config.get("CONNECTION."+(i+1)+".USERNAME"));
                     config.set("CONNECTION."+i+".PASSWORD",
                               config.get("CONNECTION."+(i+1)+".PASSWORD"));
                     config.set("CONNECTION."+i+".URL",
                               config.get("CONNECTION."+(i+1)+".URL"));
                     config.set("CONNECTION."+i+".DRIVER",
                               config.get("CONNECTION."+(i+1)+".DRIVER"));
                  }

                  String maxkey = "CONNECTION."+maxConn;
                  config.set(maxkey+".NAME", "");

                  // remove from item
                  box.setSelectedIndex(0);
                  box.removeItemAt(index);
              }
           });

       JPanel panel = new JPanel(new FlowLayout());
       panel.add(box);
       panel.add(removeButton);

       JPanel cbPanel = new JPanel(new FlowLayout());
       cbPanel.add(saveConnection);
       cbPanel.add(savePassword);
       
       Object[] input = {
           panel,
           msgString0, connectnameField, msgString1, usernameField,
           msgString2, passwordField, msgString3, driverField,
           msgString4, urlField, cbPanel
       };
       
       setOptionPane (input,
                     JOptionPane.PLAIN_MESSAGE,
                     JOptionPane.OK_CANCEL_OPTION,
                     databaseIcon);
       
    }

Here is the call graph for this function:


Member Function Documentation

JRootPane SharpTools.SharpDialog.createRootPane ( ) [inline, protected, inherited]

override this protected funciton to register ESCAPE with the root pane

Returns:
a customized JRootPane object

Definition at line 126 of file SharpDialog.java.

                                         {
       KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
       JRootPane rootPane = new JRootPane();
       rootPane.registerKeyboardAction(
                                   new ActionListener() {
                                          public void actionPerformed
                                              (ActionEvent ev) {    
                                              if (onClose()) {
                                                 choice = CLOSED_OPTION;
                                                 setVisible(false);
                                              }
                                          }
                                       },
                                   stroke,
                                   JComponent.WHEN_IN_FOCUSED_WINDOW);
       return rootPane;
    }

Here is the call graph for this function:

int SharpTools.SharpDialog.getChoice ( ) [inline, inherited]
Returns:
user choice

Definition at line 344 of file SharpDialog.java.

                           {
       return choice;
    }

Here is the caller graph for this function:

Connection SharpTools.ConnectDialog.getConnection ( ) [inline]

Definition at line 159 of file ConnectDialog.java.

                                      {
       return dbConnection;
    }

Here is the caller graph for this function:

Returns:
whether this operation was cancelled

Definition at line 351 of file SharpDialog.java.

                                 {
       return choice == CANCEL_OPTION || choice == CLOSED_OPTION;
    }

Here is the caller graph for this function:

boolean SharpTools.SharpDialog.onCancel ( ) [inline, protected, inherited]

This function is called when the Cancel button is pressed.

Returns:
whether the dialog should be closed

Reimplemented in SharpTools.SortDialog, SharpTools.NewFileDialog, and SharpTools.PasswordDialog.

Definition at line 187 of file SharpDialog.java.

                                 {
       return true;
    }

Here is the caller graph for this function:

boolean SharpTools.SharpDialog.onClose ( ) [inline, protected, inherited]

This function is called when (1) the close button is clocked (2) Escape is pressed.

By default, we think it's the same as clicking Cancel button

Returns:
whether the dialog should be closed

Reimplemented in SharpTools.NewFileDialog.

Definition at line 167 of file SharpDialog.java.

                                {
       return onCancel();
    }

Here is the call graph for this function:

Here is the caller graph for this function:

boolean SharpTools.SharpDialog.onNo ( ) [inline, protected, inherited]

This function is called when the No button is pressed.

Returns:
whether the dialog should be closed

Definition at line 207 of file SharpDialog.java.

                             {
       return true;
    }

Here is the caller graph for this function:

boolean SharpTools.ConnectDialog.onOK ( ) [inline, protected]

This function is called when the OK button is pressed.

Returns:
whether the dialog should be closed

Reimplemented from SharpTools.SharpDialog.

Definition at line 163 of file ConnectDialog.java.

                             {
        String connectName = connectnameField.getText ().trim();
        String dbUsername = usernameField.getText ().trim();
        String dbPassword = String.copyValueOf(passwordField.getPassword ());
        String dbDriver = driverField.getText ().trim();
       String dbUrl = urlField.getText ().trim();       
       
       // validation moved from Database.java - huaz

       if ((dbUsername == null || dbUsername.length () == 0) &&
           (dbUrl == null || dbUrl.length () == 0) &&
           (dbDriver == null || dbDriver.length () == 0)) {
           SharpOptionPane.showMessageDialog(this,
                                         "You must provide values for Username, Url and Driver.\n",
                                         "Connect",
                                         JOptionPane.INFORMATION_MESSAGE,
                                         databaseIcon);
           return false;
       }

       try {
           Class.forName (dbDriver);
       }
       catch (ClassNotFoundException e) {
           SharpOptionPane.showMessageDialog (this, "Unable to load class " + dbDriver + ". Please ensure that it is in your classpath.\n" + e.toString (),
                                          "Class Not Found", JOptionPane.ERROR_MESSAGE, dbErrorIcon);
           return false;
       }
       
       try {
           dbConnection =  DriverManager.getConnection (dbUrl, dbUsername, dbPassword);
       }
       catch (SQLException e) {
           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 (),
                                          "Connect", JOptionPane.ERROR_MESSAGE, dbErrorIcon);
           return false;
       }      
       
       // now we could try to save the connection - huaz
       
       // if saveConnection is not checked we just return
       if (! saveConnection.isSelected())
           return true;
       
       // if no name provided we don't save
       if (connectName.length() == 0) {
           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);
           return true;
       }
           

       // first check whether the connectName is already saved
       int index;
       
       for (index = 1; index < box.getItemCount(); index++)
           if (connectName.equals(box.getItemAt(index).toString()))
              break;

       if ((index != box.getItemCount() || box.getItemCount() <= maxConn)) {
           String key = "CONNECTION."+index;
           Debug.println("Saving "+key);
       
           Config config = SharpTools.getConfig();
           config.set(key+".NAME", connectName);
           config.set(key+".USERNAME", dbUsername);
           if (savePassword.isSelected())
              config.set(key+".PASSWORD", dbPassword);
           config.set(key+".URL", dbUrl);
           config.set(key+".DRIVER", dbDriver);
       }
       else
           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);
           
        return true;
    }

Here is the call graph for this function:

void SharpTools.ConnectDialog.onOpen ( ) [inline, protected]

I provide several hooks for the sublasses to override.

This function is called when the dialog window is opened.

Reimplemented from SharpTools.SharpDialog.

Definition at line 239 of file ConnectDialog.java.

                            {
        // set the initial focus to textField
        connectnameField.requestFocus ();
    }
boolean SharpTools.SharpDialog.onYes ( ) [inline, protected, inherited]

This function is called when the Yes button is pressed.

Returns:
whether the dialog should be closed

Definition at line 197 of file SharpDialog.java.

                              {
       return true;
    }

Here is the caller graph for this function:

void SharpTools.SharpDialog.setOptionPane ( Object  message,
int  messageType,
int  optionType,
Icon  icon 
) [inline, protected, inherited]

This is used by subclasses to set the JOptionPane component in the dialog.

Parameters:
messagethe message box
messageTypethe message type
optionTypethe option type
iconthe icon to display

Definition at line 220 of file SharpDialog.java.

                                        {
       
       setOptionPane(message, messageType, optionType, icon, 0);
    }

Here is the caller graph for this function:

void SharpTools.SharpDialog.setOptionPane ( Object  message,
int  messageType,
int  optionType,
Icon  icon,
int  defaultIndex 
) [inline, protected, inherited]

This is used by subclasses to set the JOptionPane component in the dialog.

Parameters:
messagethe message box
messageTypethe message type
optionTypethe option type
iconthe icon to display
defaultIndexthe default index

Definition at line 238 of file SharpDialog.java.

                                               {
       
       addWindowListener(new WindowAdapter() {

              public void windowOpened(WindowEvent we) {
                  // set the initial focus to rowField
                  onOpen();
              }
              
                public void windowClosing(WindowEvent we) {
                  if (onClose()) {
                     choice = CLOSED_OPTION;
                     setVisible(false);
                  }
              }
           });
       
       // handle this by onClose()
       setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

       okButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  if (onOK()) {
                     choice = OK_OPTION;
                     setVisible(false);
                  }
              }
           });

       cancelButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  if (onCancel()) {
                     choice = CANCEL_OPTION;
                     setVisible(false);
                  }
              }
           });
       
       yesButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  if (onYes()) {
                     choice = YES_OPTION;
                     setVisible(false);
                  }
              }
           });
       
       noButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent evt) {
                  if (onNo()) {
                     choice = NO_OPTION;
                     setVisible(false);
                  }
              }
           });

       // setup Mnemonic
       okButton.setMnemonic (KeyEvent.VK_O);
       cancelButton.setMnemonic (KeyEvent.VK_C);
       yesButton.setMnemonic (KeyEvent.VK_Y);
       noButton.setMnemonic (KeyEvent.VK_N);
       
       Object[] buttons = setButtons(optionType);
       JOptionPane optionPane = new JOptionPane(message, messageType,
                                           optionType, icon,
                                           buttons,
                                           buttons[defaultIndex]);
       
       setContentPane(optionPane);

       pack();
       setLocationRelativeTo(getParent());
    }

Here is the call graph for this function:


Member Data Documentation

JComboBox SharpTools.ConnectDialog.box [private]

Definition at line 57 of file ConnectDialog.java.

final int SharpTools.SharpDialog.CANCEL_OPTION = JOptionPane.CANCEL_OPTION [static, inherited]

Definition at line 26 of file SharpDialog.java.

final ImageIcon SharpTools.SharpDialog.cancelIcon = new ImageIcon(getClass().getResource("/images/cancel.gif")) [package, inherited]

Definition at line 28 of file SharpDialog.java.

final int SharpTools.SharpDialog.CLOSED_OPTION = JOptionPane.CLOSED_OPTION [static, inherited]

Definition at line 22 of file SharpDialog.java.

final JTextField SharpTools.ConnectDialog.connectnameField = new JTextField () [private]

Definition at line 45 of file ConnectDialog.java.

final ImageIcon SharpTools.ConnectDialog.databaseIcon = new ImageIcon(getClass().getResource("/images/database32.gif")) [package]

Definition at line 39 of file ConnectDialog.java.

Connection SharpTools.ConnectDialog.dbConnection = null [private]

Definition at line 43 of file ConnectDialog.java.

final static ImageIcon SharpTools.ConnectDialog.dbErrorIcon = null [static, private]

Definition at line 41 of file ConnectDialog.java.

final JTextField SharpTools.ConnectDialog.driverField = new JTextField () [private]

Definition at line 48 of file ConnectDialog.java.

Definition at line 33 of file ConnectDialog.java.

Definition at line 35 of file ConnectDialog.java.

Definition at line 54 of file ConnectDialog.java.

final int SharpTools.SharpDialog.NO_OPTION = JOptionPane.NO_OPTION [static, inherited]

Definition at line 24 of file SharpDialog.java.

final ImageIcon SharpTools.SharpDialog.noIcon = new ImageIcon(getClass().getResource("/images/no.gif")) [package, inherited]

Definition at line 29 of file SharpDialog.java.

final int SharpTools.SharpDialog.OK_OPTION = JOptionPane.OK_OPTION [static, inherited]

Definition at line 25 of file SharpDialog.java.

final ImageIcon SharpTools.SharpDialog.okIcon = new ImageIcon(getClass().getResource("/images/ok.gif")) [package, inherited]

Definition at line 27 of file SharpDialog.java.

JOptionPane SharpTools.ConnectDialog.optionPane [private]

Definition at line 34 of file ConnectDialog.java.

final JPasswordField SharpTools.ConnectDialog.passwordField = new JPasswordField () [private]

Definition at line 47 of file ConnectDialog.java.

final JButton SharpTools.ConnectDialog.removeButton = new JButton("Remove", removeIcon) [private]

Definition at line 56 of file ConnectDialog.java.

final ImageIcon SharpTools.ConnectDialog.removeIcon = new ImageIcon(getClass().getResource("/images/no.gif")) [package]

Definition at line 37 of file ConnectDialog.java.

final JCheckBox SharpTools.ConnectDialog.saveConnection = new JCheckBox("Save Connection", true) [private]

Definition at line 51 of file ConnectDialog.java.

final JCheckBox SharpTools.ConnectDialog.savePassword = new JCheckBox("Save Password", false) [private]

Definition at line 52 of file ConnectDialog.java.

final JTextField SharpTools.ConnectDialog.urlField = new JTextField () [private]

Definition at line 49 of file ConnectDialog.java.

final JTextField SharpTools.ConnectDialog.usernameField = new JTextField () [private]

Definition at line 46 of file ConnectDialog.java.

Definition at line 36 of file ConnectDialog.java.

final int SharpTools.SharpDialog.YES_OPTION = JOptionPane.YES_OPTION [static, inherited]

Definition at line 23 of file SharpDialog.java.


The documentation for this class was generated from the following file: