View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/devgui/DbPanel.java,v 1.9 2005/11/30 20:31:17 hkollmann Exp $
3    * $Revision: 1.9 $
4    * $Date: 2005/11/30 20:31:17 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  /*
25   * DbPanel.java
26   *
27   * Created on 26. April 2001, 15:42
28   */
29  package org.dbforms.devgui;
30  
31  import java.awt.*;
32  import java.awt.event.*;
33  
34  import java.sql.*;
35  
36  import javax.swing.*;
37  
38  
39  
40  /***
41   * DOCUMENT ME!
42   *
43   * @author Joachim Peer
44   * @version
45   */
46  public class DbPanel extends AbstractPropertyPanel implements ActionListener {
47     private JButton b_testConnection;
48  
49     // Variables declaration - do not modify//GEN-BEGIN:variables
50     private JLabel     jLabel1;
51     private JLabel     jLabel5;
52     private JLabel     jLabel6;
53     private JLabel     jLabel7;
54     private JTextField tf_jdbcDriver;
55     private JTextField tf_jdbcURL;
56     private JTextField tf_password;
57     private JTextField tf_username;
58  
59     // End of variables declaration//GEN-END:variables
60  
61     /***
62      * Creates new form DbPanel
63      *
64      * @param parent DOCUMENT ME!
65      */
66     public DbPanel(DevGui parent) {
67        super(parent.getProjectData());
68        initComponents();
69        doLayout();
70     }
71  
72     /***
73      * DOCUMENT ME!
74      *
75      * @param projectData DOCUMENT ME!
76      */
77     public void setNewProjectData(ProjectData projectData) {
78        this.projectData = projectData;
79  
80        tf_jdbcDriver.setText(projectData.getProperty("jdbcDriver"));
81        tf_jdbcURL.setText(projectData.getProperty("jdbcURL"));
82        tf_username.setText(projectData.getProperty("username"));
83        tf_password.setText(projectData.getProperty("password"));
84     }
85  
86  
87     /***
88      * DOCUMENT ME!
89      *
90      * @param e DOCUMENT ME!
91      */
92     public void actionPerformed(ActionEvent e) {
93        if (e.getSource() == b_testConnection) {
94           testConnection();
95        }
96     }
97  
98  
99     /***
100     * This method is called from within the constructor to initialize the form.
101     * WARNING: Do NOT modify this code. The content of this method is always
102     * regenerated by the FormEditor.
103     */
104    private void initComponents() { //GEN-BEGIN:initComponents
105       jLabel1       = new javax.swing.JLabel();
106       tf_jdbcDriver = new javax.swing.JTextField();
107       jLabel5       = new javax.swing.JLabel();
108       tf_jdbcURL    = new javax.swing.JTextField();
109       jLabel6       = new javax.swing.JLabel();
110       tf_username   = new javax.swing.JTextField();
111       jLabel7       = new javax.swing.JLabel();
112       tf_password   = new javax.swing.JTextField();
113 
114       setLayout(new BorderLayout());
115 
116       JPanel panel_top = new JPanel();
117       panel_top.setLayout(new GridLayout(5, 2));
118       add(BorderLayout.NORTH, panel_top);
119 
120       // property jdbc driver
121       jLabel1.setText("JDBC Driver Class");
122       panel_top.add(jLabel1);
123       addAFocusListener(tf_jdbcDriver, "jdbcDriver");
124       panel_top.add(tf_jdbcDriver);
125 
126       // property jdbcuUrl
127       jLabel5.setText("JDBC URL");
128       panel_top.add(jLabel5);
129       addAFocusListener(tf_jdbcURL, "jdbcURL");
130       panel_top.add(tf_jdbcURL);
131 
132       // property userName
133       jLabel6.setText("Username");
134       panel_top.add(jLabel6);
135       addAFocusListener(tf_username, "username");
136       panel_top.add(tf_username);
137 
138       // property userName
139       jLabel7.setText("Password");
140       panel_top.add(jLabel7);
141       addAFocusListener(tf_password, "password");
142       panel_top.add(tf_password);
143 
144       b_testConnection = new JButton("Test connection");
145       b_testConnection.addActionListener(this);
146       b_testConnection.setToolTipText("Check if the configuration provided in this dialog is correct.");
147       panel_top.add(b_testConnection);
148    }
149 
150 
151    //GEN-END:initComponents
152    private void testConnection() {
153       String     jdbcDriver = projectData.getProperty("jdbcDriver");
154       String     jdbcURL  = projectData.getProperty("jdbcURL");
155       String     username = projectData.getProperty("username");
156       String     password = projectData.getProperty("password");
157 
158       Connection con = null;
159 
160       try {
161          con = XMLConfigGenerator.createConnection(jdbcDriver, jdbcURL,
162                                                    username, password);
163 
164          if (con == null) {
165             JOptionPane.showMessageDialog(this, "No connection", "db failure",
166                                           JOptionPane.ERROR_MESSAGE);
167          } else {
168             JOptionPane.showMessageDialog(this,
169                                           "Database connection successfully installed.",
170                                           "Success", JOptionPane.PLAIN_MESSAGE);
171          }
172       } catch (Exception e) {
173          if (con == null) {
174             JOptionPane.showMessageDialog(this, "Error:" + e.toString(),
175                                           "db failure",
176                                           JOptionPane.ERROR_MESSAGE);
177          }
178 
179          e.printStackTrace();
180       } finally {
181          try {
182             if (con != null) {
183                con.close();
184             }
185          } catch (SQLException sqle) {
186             ;
187          }
188       }
189    }
190 }