001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017: package com.finalist.jaggenerator;
018:
019: import javax.swing.*;
020: import java.sql.Connection;
021: import java.sql.SQLException;
022:
023: /**
024: *
025: * @author hillie
026: */
027: public class ConnectDialog extends javax.swing.JDialog {
028:
029: private JagGenerator parent;
030: private static final String DRIVER = "driver";
031: private String[] showTableTypes = null;
032: private boolean offLineMode = false;
033:
034: /** Creates new form ConnectDialog */
035: public ConnectDialog(JagGenerator parent) {
036: super (parent, true);
037: this .parent = parent;
038: initComponents();
039: this .setTitle("Connect to database");
040: this .setLocation(50, 150);
041: String url = parent.root.datasource.getJdbcUrl().toString();
042: String user = parent.root.datasource.userNameText.getText();
043: String password = parent.root.datasource.passwordText.getText();
044:
045: if (url == null)
046: urlTextField
047: .setText("jdbc:oracle:thin:@localhost:1521:orcl");
048: else
049: urlTextField.setText(url);
050: if (user == null)
051: user = "";
052: usernameTextField.setText(user);
053: if (password == null)
054: password = "";
055: passwordField.setText(password);
056: }
057:
058: /** This method is called from within the constructor to
059: * initialize the form.
060: * WARNING: Do NOT modify this code. The content of this method is
061: * always regenerated by the Form Editor.
062: */
063: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
064: private void initComponents() {
065: urlLabel = new javax.swing.JLabel();
066: usernameLabel = new javax.swing.JLabel();
067: passwordLabel = new javax.swing.JLabel();
068: urlTextField = new javax.swing.JTextField();
069: usernameTextField = new javax.swing.JTextField();
070: connectButton = new javax.swing.JButton();
071: passwordField = new javax.swing.JPasswordField();
072: passwordLabel1 = new javax.swing.JLabel();
073: showTablesRadioButton = new javax.swing.JCheckBox();
074: showViewsRadioButton = new javax.swing.JCheckBox();
075: showSynonymsRadioButton = new javax.swing.JCheckBox();
076: connectButton1 = new javax.swing.JButton();
077:
078: getContentPane().setLayout(
079: new org.netbeans.lib.awtextra.AbsoluteLayout());
080:
081: addWindowListener(new java.awt.event.WindowAdapter() {
082: public void windowClosing(java.awt.event.WindowEvent evt) {
083: closeDialog(evt);
084: }
085: });
086:
087: urlLabel.setText("Database URL");
088: getContentPane().add(
089: urlLabel,
090: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
091: 20, 120, -1));
092:
093: usernameLabel.setText("Username");
094: getContentPane().add(
095: usernameLabel,
096: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
097: 40, 100, -1));
098:
099: passwordLabel.setText("Password");
100: getContentPane().add(
101: passwordLabel,
102: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
103: 60, -1, -1));
104:
105: getContentPane().add(
106: urlTextField,
107: new org.netbeans.lib.awtextra.AbsoluteConstraints(110,
108: 20, 370, -1));
109:
110: getContentPane().add(
111: usernameTextField,
112: new org.netbeans.lib.awtextra.AbsoluteConstraints(110,
113: 40, 170, -1));
114:
115: connectButton.setText("Connect");
116: connectButton.setToolTipText("Connect to the database");
117: connectButton
118: .addActionListener(new java.awt.event.ActionListener() {
119: public void actionPerformed(
120: java.awt.event.ActionEvent evt) {
121: connectButtonActionPerformed(evt);
122: }
123: });
124:
125: getContentPane().add(
126: connectButton,
127: new org.netbeans.lib.awtextra.AbsoluteConstraints(170,
128: 130, -1, -1));
129:
130: getContentPane().add(
131: passwordField,
132: new org.netbeans.lib.awtextra.AbsoluteConstraints(110,
133: 60, 170, -1));
134:
135: passwordLabel1.setText("Show");
136: getContentPane().add(
137: passwordLabel1,
138: new org.netbeans.lib.awtextra.AbsoluteConstraints(20,
139: 90, -1, -1));
140:
141: showTablesRadioButton.setSelected(true);
142: showTablesRadioButton.setLabel("tables");
143: showTablesRadioButton
144: .addActionListener(new java.awt.event.ActionListener() {
145: public void actionPerformed(
146: java.awt.event.ActionEvent evt) {
147: showTablesRadioButtonActionPerformed(evt);
148: }
149: });
150:
151: getContentPane().add(
152: showTablesRadioButton,
153: new org.netbeans.lib.awtextra.AbsoluteConstraints(110,
154: 90, -1, -1));
155:
156: showViewsRadioButton.setText("views");
157: getContentPane().add(
158: showViewsRadioButton,
159: new org.netbeans.lib.awtextra.AbsoluteConstraints(190,
160: 90, -1, -1));
161:
162: showSynonymsRadioButton.setText("synonyms");
163: getContentPane().add(
164: showSynonymsRadioButton,
165: new org.netbeans.lib.awtextra.AbsoluteConstraints(270,
166: 90, -1, -1));
167:
168: connectButton1.setText("Offline");
169: connectButton1.setToolTipText("Work in offline mode");
170: connectButton1
171: .addActionListener(new java.awt.event.ActionListener() {
172: public void actionPerformed(
173: java.awt.event.ActionEvent evt) {
174: offlineButtonActionPerformed(evt);
175: }
176: });
177:
178: getContentPane().add(
179: connectButton1,
180: new org.netbeans.lib.awtextra.AbsoluteConstraints(260,
181: 130, -1, -1));
182:
183: pack();
184: }
185:
186: // </editor-fold>//GEN-END:initComponents
187:
188: private void offlineButtonActionPerformed(
189: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_offlineButtonActionPerformed
190: // TODO add your handling code here:
191: parent.setOfflineMode(true);
192: this .dispose();
193: }//GEN-LAST:event_offlineButtonActionPerformed
194:
195: private void showTablesRadioButtonActionPerformed(
196: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showTablesRadioButtonActionPerformed
197: // TODO add your handling code here:
198: }//GEN-LAST:event_showTablesRadioButtonActionPerformed
199:
200: private void connectButtonActionPerformed(
201: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed
202: parent.setOfflineMode(false);
203: String url = urlTextField.getText();
204: String username = usernameTextField.getText();
205: String password = new String(passwordField.getPassword());
206: java.util.ArrayList theTypes = new java.util.ArrayList();
207: if (this .showTablesRadioButton.isSelected()) {
208: theTypes.add("TABLE");
209: }
210: if (this .showSynonymsRadioButton.isSelected()) {
211: theTypes.add("SYNONYM");
212: }
213: if (this .showViewsRadioButton.isSelected()) {
214: theTypes.add("VIEW");
215: }
216: showTableTypes = new String[theTypes.size()];
217: for (int i = 0; i < theTypes.size(); i++) {
218: showTableTypes[i] = (String) theTypes.get(i);
219: }
220: Connection con = null;
221:
222: String databaseType = parent.root.datasource.getDatabase()
223: .getDbName();
224: parent.setConManager(new GenericJdbcManager(url, username,
225: password, parent.root.datasource.getDatabase()
226: .getDriverClass(), showTableTypes));
227:
228: // Assume the connection will fail.
229: String connectLabel = "Database Connection: failed";
230: String connectToolTip = "Database Connection: failed to "
231: + databaseType + " using: url=" + url + " username="
232: + username + " pasword=" + password;
233: try {
234: con = parent.getConManager().connect();
235: JagGenerator.logToConsole("Connected to " + databaseType
236: + " at " + url);
237: connectLabel = "Database Connection: connected";
238: connectToolTip = "Database Connection: connected to "
239: + databaseType + " using: url=" + url
240: + " username=" + username + " pasword=" + password;
241: parent.databaseConnectionLabel.setText(connectLabel);
242: parent.databaseConnectionLabel
243: .setToolTipText(connectToolTip);
244:
245: } catch (ClassNotFoundException e) {
246: requiresRestartWarning(e.getMessage(), databaseType,
247: connectLabel, connectToolTip);
248:
249: } catch (SQLException e) {
250: e.printStackTrace();
251: if (e.getMessage().indexOf(DRIVER) != -1) {
252: requiresRestartWarning(e.getMessage(), databaseType,
253: connectLabel, connectToolTip);
254: } else {
255: genericConnectError(e, connectLabel, connectToolTip);
256: }
257: } catch (Exception e) {
258: genericConnectError(e, connectLabel, connectToolTip);
259:
260: } finally {
261: if (con != null) {
262: try {
263: con.close();
264: } catch (Exception e) {
265: JagGenerator
266: .logToConsole("Connection couldn't be closed");
267: }
268: }
269: }
270: this .dispose();
271: }//GEN-LAST:event_connectButtonActionPerformed
272:
273: private void genericConnectError(Exception e, String connectLabel,
274: String connectToolTip) {
275: e.printStackTrace();
276: JagGenerator.logToConsole("Error while connecting! - " + e);
277: parent.databaseConnectionLabel.setText(connectLabel);
278: parent.databaseConnectionLabel.setToolTipText(connectToolTip);
279: parent.setConManager(null);
280: }
281:
282: private void requiresRestartWarning(String message,
283: String databaseType, String connectLabel,
284: String connectToolTip) {
285: JagGenerator
286: .logToConsole("Database driver problem! Driver class: "
287: + message);
288: String msg = "The driver you are trying to use to connect to the database is not working.\n"
289: + "Please check that you have chosen the correct 'Database Type' in the Datasource configuration screen.\n\n"
290: + "NOTE: If you added the driver for "
291: + databaseType
292: + " databases during this session,\n"
293: + "you'll need to restart JAG first to make the necessary driver available. Sorry!";
294: JagGenerator.logToConsole("\n" + msg + "\n");
295: JOptionPane.showMessageDialog(JagGenerator.jagGenerator, msg,
296: "Driver problems!", JOptionPane.INFORMATION_MESSAGE);
297:
298: parent.databaseConnectionLabel.setText(connectLabel);
299: parent.databaseConnectionLabel.setToolTipText(connectToolTip);
300: parent.setConManager(null);
301: }
302:
303: /** Closes the dialog */
304: private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
305:
306: }//GEN-LAST:event_closeDialog
307:
308: // Variables declaration - do not modify//GEN-BEGIN:variables
309: private javax.swing.JButton connectButton;
310: private javax.swing.JButton connectButton1;
311: private javax.swing.JPasswordField passwordField;
312: private javax.swing.JLabel passwordLabel;
313: private javax.swing.JLabel passwordLabel1;
314: private javax.swing.JCheckBox showSynonymsRadioButton;
315: private javax.swing.JCheckBox showTablesRadioButton;
316: private javax.swing.JCheckBox showViewsRadioButton;
317: private javax.swing.JLabel urlLabel;
318: private javax.swing.JTextField urlTextField;
319: private javax.swing.JLabel usernameLabel;
320: private javax.swing.JTextField usernameTextField;
321: // End of variables declaration//GEN-END:variables
322:
323: }
|