View Javadoc

1   /*   Copyright (C) 2003 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  package com.finalist.jaggenerator;
18  
19  import javax.swing.*;
20  import java.sql.Connection;
21  import java.sql.SQLException;
22  
23  /***
24   *
25   * @author  hillie
26   */
27  public class ConnectDialog extends javax.swing.JDialog {
28  
29     private JagGenerator parent;
30     private static final String DRIVER = "driver";
31     private String[] showTableTypes = null;
32  
33  
34     /*** Creates new form ConnectDialog */
35     public ConnectDialog(JagGenerator parent) {
36        super(parent, true);
37        this.parent = parent;
38        initComponents();
39        this.setTitle("Connect to database");
40        this.setLocation(50, 150);
41        String url = parent.root.datasource.getJdbcUrl().toString();
42        String user = parent.root.datasource.userNameText.getText();
43        String password = parent.root.datasource.passwordText.getText();
44  
45        if (url == null)
46           urlTextField.setText("jdbc:oracle:thin:@localhost:1521:orcl");
47        else
48           urlTextField.setText(url);
49        if (user == null)
50           user = "";
51        usernameTextField.setText(user);
52        if (password == null)
53           password = "";
54        passwordField.setText(password);
55     }
56  
57  
58     /*** This method is called from within the constructor to
59      * initialize the form.
60      * WARNING: Do NOT modify this code. The content of this method is
61      * always regenerated by the Form Editor.
62      */
63     private void initComponents() {//GEN-BEGIN:initComponents
64        urlLabel = new javax.swing.JLabel();
65        usernameLabel = new javax.swing.JLabel();
66        passwordLabel = new javax.swing.JLabel();
67        urlTextField = new javax.swing.JTextField();
68        usernameTextField = new javax.swing.JTextField();
69        connectButton = new javax.swing.JButton();
70        passwordField = new javax.swing.JPasswordField();
71        passwordLabel1 = new javax.swing.JLabel();
72        showTablesRadioButton = new javax.swing.JCheckBox();
73        showViewsRadioButton = new javax.swing.JCheckBox();
74        showSynonymsRadioButton = new javax.swing.JCheckBox();
75  
76        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
77  
78        addWindowListener(new java.awt.event.WindowAdapter() {
79           public void windowClosing(java.awt.event.WindowEvent evt) {
80              closeDialog(evt);
81           }
82        });
83  
84        urlLabel.setText("Database URL");
85        getContentPane().add(urlLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 120, -1));
86  
87        usernameLabel.setText("Username");
88        getContentPane().add(usernameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 100, -1));
89  
90        passwordLabel.setText("Password");
91        getContentPane().add(passwordLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, -1, -1));
92  
93        getContentPane().add(urlTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 20, 370, -1));
94  
95        getContentPane().add(usernameTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 40, 170, -1));
96  
97        connectButton.setText("Connect");
98        connectButton.addActionListener(new java.awt.event.ActionListener() {
99           public void actionPerformed(java.awt.event.ActionEvent evt) {
100             connectButtonActionPerformed(evt);
101          }
102       });
103 
104       getContentPane().add(connectButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 130, -1, -1));
105 
106       getContentPane().add(passwordField, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 60, 170, -1));
107 
108       passwordLabel1.setText("Show");
109       getContentPane().add(passwordLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 90, -1, -1));
110 
111       showTablesRadioButton.setSelected(true);
112       showTablesRadioButton.setText("tables");
113       showTablesRadioButton.addActionListener(new java.awt.event.ActionListener() {
114          public void actionPerformed(java.awt.event.ActionEvent evt) {
115             showTablesRadioButtonActionPerformed(evt);
116          }
117       });
118 
119       getContentPane().add(showTablesRadioButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 90, -1, -1));
120 
121       showViewsRadioButton.setText("views");
122       getContentPane().add(showViewsRadioButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 90, -1, -1));
123 
124       showSynonymsRadioButton.setText("synonyms");
125       getContentPane().add(showSynonymsRadioButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 90, -1, -1));
126 
127       pack();
128    }//GEN-END:initComponents
129 
130    private void showTablesRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showTablesRadioButtonActionPerformed
131       // TODO add your handling code here:
132    }//GEN-LAST:event_showTablesRadioButtonActionPerformed
133 
134 
135    private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed
136       String url = urlTextField.getText();
137       String username = usernameTextField.getText();
138       String password = new String(passwordField.getPassword());
139       java.util.ArrayList theTypes = new java.util.ArrayList();
140       if (this.showTablesRadioButton.isSelected()) {
141          theTypes.add("TABLE");
142       }
143       if (this.showSynonymsRadioButton.isSelected()) {
144          theTypes.add("SYNONYM");
145       }
146       if (this.showViewsRadioButton.isSelected()) {
147          theTypes.add("VIEW");
148       }
149       showTableTypes = new String[theTypes.size()];
150       for (int i = 0; i < theTypes.size(); i++) {
151          showTableTypes[i] = (String) theTypes.get(i);
152       }
153       Connection con = null;
154 
155       String databaseType = parent.root.datasource.getDatabase().getDbName();
156       parent.setConManager(
157             new GenericJdbcManager(url, username, password, parent.root.datasource.getDatabase().getDriverClass(), showTableTypes));
158 
159       // Assume the connection will fail.
160       String connectLabel = "Database Connection: failed";
161       String connectToolTip = "Database Connection: failed to " + databaseType + " using: url=" + url + " username=" + username + " pasword=" + password;
162       try {
163          con = parent.getConManager().connect();
164          JagGenerator.logToConsole("Connected to " + databaseType + " at " + url);
165          connectLabel = "Database Connection: connected";
166          connectToolTip = "Database Connection: connected to " + databaseType + " using: url=" + url + " username=" + username + " pasword=" + password;
167          parent.databaseConnectionLabel.setText(connectLabel);
168          parent.databaseConnectionLabel.setToolTipText(connectToolTip);
169 
170       } catch (ClassNotFoundException e) {
171          requiresRestartWarning(e.getMessage(), databaseType, connectLabel, connectToolTip);
172 
173       } catch (SQLException e) {
174          e.printStackTrace();
175          if (e.getMessage().indexOf(DRIVER) != -1) {
176             requiresRestartWarning(e.getMessage(), databaseType, connectLabel, connectToolTip);
177          } else {
178             genericConnectError(e, connectLabel, connectToolTip);
179          }
180       } catch (Exception e) {
181          genericConnectError(e, connectLabel, connectToolTip);
182 
183       } finally {
184          if (con != null) {
185             try {
186                con.close();
187             } catch (Exception e) {
188                JagGenerator.logToConsole("Connection couldn't be closed");
189             }
190          }
191       }
192       this.dispose();
193    }//GEN-LAST:event_connectButtonActionPerformed
194 
195    private void genericConnectError(Exception e, String connectLabel, String connectToolTip) {
196       e.printStackTrace();
197       JagGenerator.logToConsole("Error while connecting! - " + e);
198       parent.databaseConnectionLabel.setText(connectLabel);
199       parent.databaseConnectionLabel.setToolTipText(connectToolTip);
200       parent.setConManager(null);
201    }
202 
203    private void requiresRestartWarning(String message, String databaseType, String connectLabel, String connectToolTip) {
204       JagGenerator.logToConsole("Database driver problem!  Driver class: " + message);
205       String msg = "The driver you are trying to use to connect to the database is not working.\n" +
206             "Please check that you have chosen the correct 'Database Type' in the Datasource configuration screen.\n\n" +
207             "NOTE: If you added the driver for " + databaseType + " databases during this session,\n" +
208             "you'll  need to restart JAG first to make the necessary driver available.  Sorry!";
209       JagGenerator.logToConsole("\n" + msg + "\n");
210       JOptionPane.showMessageDialog(JagGenerator.jagGenerator,
211             msg, "Driver problems!", JOptionPane.INFORMATION_MESSAGE);
212 
213       parent.databaseConnectionLabel.setText(connectLabel);
214       parent.databaseConnectionLabel.setToolTipText(connectToolTip);
215       parent.setConManager(null);
216    }
217 
218 
219    /*** Closes the dialog */
220    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
221 
222    }//GEN-LAST:event_closeDialog
223 
224 
225    // Variables declaration - do not modify//GEN-BEGIN:variables
226    private javax.swing.JButton connectButton;
227    private javax.swing.JPasswordField passwordField;
228    private javax.swing.JLabel passwordLabel;
229    private javax.swing.JLabel passwordLabel1;
230    private javax.swing.JCheckBox showSynonymsRadioButton;
231    private javax.swing.JCheckBox showTablesRadioButton;
232    private javax.swing.JCheckBox showViewsRadioButton;
233    private javax.swing.JLabel urlLabel;
234    private javax.swing.JTextField urlTextField;
235    private javax.swing.JLabel usernameLabel;
236    private javax.swing.JTextField usernameTextField;
237    // End of variables declaration//GEN-END:variables
238 
239 }