001: package org.apache.ojb.tools.mapping.reversedb2.gui;
002:
003: import java.util.Vector;
004: import org.apache.ojb.tools.mapping.reversedb2.Main;
005: import javax.swing.MutableComboBoxModel;
006:
007: /* Copyright 2002-2005 The Apache Software Foundation
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: /**
023: * This dialog collects JDBC connection data and opens the connection. The JDBC
024: * driver class and URL are stored in the properties of Main, so the user only
025: * has to enter this data once.
026: *
027: * The method showAndReturnConnection() performs a show(), blocks until the
028: * dialog is disposed and returns the connection. If no valid connection is
029: * established, null is returned.
030: *
031: * @author <a href="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
032: * @version $Id: JDlgDBConnection.java,v 1.1.2.1 2005/12/21 22:32:41 tomdz Exp $
033: */
034: public class JDlgDBConnection extends javax.swing.JDialog {
035:
036: private Vector vJDBCDrivers = new Vector();
037: private Vector vJDBCUrls = new Vector();
038:
039: private java.sql.Connection theConnection = null;
040:
041: private boolean isDisposed = false;
042:
043: /** Creates new form JDlgDBConnection.
044: * @param parent parent frame as specified by javax.swing.JDialog
045: * @param isModal
046: */
047: public JDlgDBConnection(java.awt.Frame parent, boolean isModal) {
048: super (parent, isModal);
049: initComponents();
050:
051: synchronized (Main.getProperties()) {
052: // Read all stored JDBCDrivers from properties
053: int i = 0;
054: String s = null;
055: do {
056: s = Main.getProperties().getProperty(
057: Main.PROPERTY_JDBCDRIVER + i);
058: if (s != null)
059: vJDBCDrivers.add(s);
060: i++;
061: } while (s != null);
062:
063: // Read all stored JDBCUrls from properties
064: i = 0;
065: s = null;
066: do {
067: s = Main.getProperties().getProperty(
068: Main.PROPERTY_JDBCURL + i);
069: if (s != null)
070: vJDBCUrls.add(s);
071: i++;
072: } while (s != null);
073:
074: // Read all stored JDBC usernames from properties
075: s = Main.getProperties().getProperty("JDBCUser" + i);
076:
077: this .cmbJDBCDriver
078: .setModel(new javax.swing.DefaultComboBoxModel(
079: vJDBCDrivers));
080: this .cmbJDBCURL
081: .setModel(new javax.swing.DefaultComboBoxModel(
082: vJDBCUrls));
083:
084: this .cmbJDBCDriver.setEditable(true);
085: this .cmbJDBCURL.setEditable(true);
086:
087: this .tfUsername.setText(Main.getProperties().getProperty(
088: Main.PROPERTY_JDBCUSER, ""));
089: this .tfPassword.setText("");
090:
091: this .lblResult.setText("");
092: pack();
093: }
094: if (this .cmbJDBCDriver.getSelectedItem() != null
095: && this .cmbJDBCURL.getSelectedItem() != null) {
096: this .pbTest.setEnabled(true);
097: this .pbOpen.setEnabled(true);
098: } else {
099: this .pbTest.setEnabled(false);
100: this .pbOpen.setEnabled(false);
101: }
102: }
103:
104: /** Performs a show(), blocks until the
105: * dialog is disposed and returns the connection. If no valid connection is
106: * established, null is returned.
107: */
108: public java.sql.Connection showAndReturnConnection() {
109: show();
110: while (!this .isDisposed) {
111: try {
112: synchronized (this ) {
113: wait();
114: }
115: } catch (Throwable t) {
116: }
117: }
118: return theConnection;
119: }
120:
121: /** dispose() of javax.swing.JDialog overridden to be able to wait until the dialog is
122: * disposed even if it is not modal in showAndReturnConnection().
123: *
124: * Warning: This method should not be called from the Eventdispatch-Thread if the dialog is non-modal, because no events can be dispatched while this method blocks.
125: */
126: public void dispose() {
127: super .dispose();
128: this .isDisposed = true;
129: synchronized (this ) {
130: this .notifyAll();
131: }
132: }
133:
134: /** This method is called from within the constructor to
135: * initialize the form.
136: * WARNING: Do NOT modify this code. The content of this method is
137: * always regenerated by the Form Editor.
138: */
139: private void initComponents()//GEN-BEGIN:initComponents
140: {
141: java.awt.GridBagConstraints gridBagConstraints;
142:
143: lblJDBCDriver = new javax.swing.JLabel();
144: cmbJDBCDriver = new javax.swing.JComboBox();
145: lblJDBCURL = new javax.swing.JLabel();
146: cmbJDBCURL = new javax.swing.JComboBox();
147: lblUsername = new javax.swing.JLabel();
148: tfUsername = new javax.swing.JTextField();
149: lblPassword = new javax.swing.JLabel();
150: tfPassword = new javax.swing.JPasswordField();
151: lblResult = new javax.swing.JTextField();
152: jPanel1 = new javax.swing.JPanel();
153: pbCancel = new javax.swing.JButton();
154: pbTest = new javax.swing.JButton();
155: pbOpen = new javax.swing.JButton();
156:
157: getContentPane().setLayout(new java.awt.GridBagLayout());
158:
159: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
160: addWindowListener(new java.awt.event.WindowAdapter() {
161: public void windowClosing(java.awt.event.WindowEvent evt) {
162: closeDialog(evt);
163: }
164: });
165:
166: lblJDBCDriver.setText("JDBC Driver Class:");
167: gridBagConstraints = new java.awt.GridBagConstraints();
168: gridBagConstraints.gridx = 0;
169: gridBagConstraints.gridy = 0;
170: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
171: getContentPane().add(lblJDBCDriver, gridBagConstraints);
172:
173: cmbJDBCDriver.setFont(new java.awt.Font("Dialog", 0, 12));
174: cmbJDBCDriver.setMinimumSize(new java.awt.Dimension(31, 20));
175: cmbJDBCDriver.setPreferredSize(new java.awt.Dimension(31, 20));
176: cmbJDBCDriver
177: .addItemListener(new java.awt.event.ItemListener() {
178: public void itemStateChanged(
179: java.awt.event.ItemEvent evt) {
180: cmbJDBCDriverItemStateChanged(evt);
181: }
182: });
183:
184: gridBagConstraints = new java.awt.GridBagConstraints();
185: gridBagConstraints.gridx = 1;
186: gridBagConstraints.gridy = 0;
187: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
188: getContentPane().add(cmbJDBCDriver, gridBagConstraints);
189:
190: lblJDBCURL.setText("JDBC URL:");
191: gridBagConstraints = new java.awt.GridBagConstraints();
192: gridBagConstraints.gridx = 0;
193: gridBagConstraints.gridy = 1;
194: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
195: getContentPane().add(lblJDBCURL, gridBagConstraints);
196:
197: cmbJDBCURL.setFont(new java.awt.Font("Dialog", 0, 12));
198: cmbJDBCURL.setMinimumSize(new java.awt.Dimension(31, 20));
199: cmbJDBCURL.setPreferredSize(new java.awt.Dimension(31, 20));
200: cmbJDBCURL.addItemListener(new java.awt.event.ItemListener() {
201: public void itemStateChanged(java.awt.event.ItemEvent evt) {
202: cmbJDBCURLItemStateChanged(evt);
203: }
204: });
205:
206: gridBagConstraints = new java.awt.GridBagConstraints();
207: gridBagConstraints.gridx = 1;
208: gridBagConstraints.gridy = 1;
209: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
210: getContentPane().add(cmbJDBCURL, gridBagConstraints);
211:
212: lblUsername.setText("Username:");
213: gridBagConstraints = new java.awt.GridBagConstraints();
214: gridBagConstraints.gridx = 0;
215: gridBagConstraints.gridy = 2;
216: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
217: getContentPane().add(lblUsername, gridBagConstraints);
218:
219: tfUsername.setColumns(30);
220: tfUsername.setText("jTextField3");
221: gridBagConstraints = new java.awt.GridBagConstraints();
222: gridBagConstraints.gridx = 1;
223: gridBagConstraints.gridy = 2;
224: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
225: gridBagConstraints.weightx = 1.0;
226: getContentPane().add(tfUsername, gridBagConstraints);
227:
228: lblPassword.setText("Password:");
229: gridBagConstraints = new java.awt.GridBagConstraints();
230: gridBagConstraints.gridx = 0;
231: gridBagConstraints.gridy = 3;
232: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
233: getContentPane().add(lblPassword, gridBagConstraints);
234:
235: tfPassword.setColumns(30);
236: gridBagConstraints = new java.awt.GridBagConstraints();
237: gridBagConstraints.gridx = 1;
238: gridBagConstraints.gridy = 3;
239: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
240: gridBagConstraints.weightx = 1.0;
241: getContentPane().add(tfPassword, gridBagConstraints);
242:
243: lblResult.setBackground((java.awt.Color) javax.swing.UIManager
244: .getDefaults().get("Label.background"));
245: lblResult.setEditable(false);
246: lblResult.setText("jTextField1");
247: lblResult.setBorder(null);
248: gridBagConstraints = new java.awt.GridBagConstraints();
249: gridBagConstraints.gridx = 0;
250: gridBagConstraints.gridy = 4;
251: gridBagConstraints.gridwidth = 2;
252: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
253: getContentPane().add(lblResult, gridBagConstraints);
254:
255: pbCancel.setMnemonic('c');
256: pbCancel.setText("Cancel");
257: pbCancel.addActionListener(new java.awt.event.ActionListener() {
258: public void actionPerformed(java.awt.event.ActionEvent evt) {
259: pbCancelActionPerformed(evt);
260: }
261: });
262:
263: jPanel1.add(pbCancel);
264:
265: pbTest.setMnemonic('t');
266: pbTest.setText("Test");
267: pbTest.setEnabled(false);
268: pbTest.addActionListener(new java.awt.event.ActionListener() {
269: public void actionPerformed(java.awt.event.ActionEvent evt) {
270: pbTestActionPerformed(evt);
271: }
272: });
273:
274: jPanel1.add(pbTest);
275:
276: pbOpen.setMnemonic('o');
277: pbOpen.setText("Open");
278: pbOpen.setEnabled(false);
279: pbOpen.addActionListener(new java.awt.event.ActionListener() {
280: public void actionPerformed(java.awt.event.ActionEvent evt) {
281: pbOpenActionPerformed(evt);
282: }
283: });
284:
285: jPanel1.add(pbOpen);
286:
287: gridBagConstraints = new java.awt.GridBagConstraints();
288: gridBagConstraints.gridx = 1;
289: gridBagConstraints.gridy = 5;
290: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
291: getContentPane().add(jPanel1, gridBagConstraints);
292:
293: pack();
294: }//GEN-END:initComponents
295:
296: private void pbOpenActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_pbOpenActionPerformed
297: {//GEN-HEADEREND:event_pbOpenActionPerformed
298: // Add your handling code here:
299: pbTestActionPerformed(evt);
300: if (theConnection != null) {
301: synchronized (Main.getProperties()) {
302: int i = 0;
303: while (Main.getProperties().getProperty(
304: Main.PROPERTY_JDBCDRIVER + i) != null) {
305: Main.getProperties().remove(
306: Main.PROPERTY_JDBCDRIVER + i);
307: i++;
308: }
309: while (Main.getProperties().getProperty(
310: Main.PROPERTY_JDBCURL + i) != null) {
311: Main.getProperties().remove(
312: Main.PROPERTY_JDBCURL + i);
313: i++;
314: }
315: for (i = 0; i < cmbJDBCDriver.getModel().getSize(); i++)
316: Main.getProperties().setProperty(
317: Main.PROPERTY_JDBCDRIVER + i,
318: cmbJDBCDriver.getModel().getElementAt(i)
319: .toString());
320: for (i = 0; i < cmbJDBCURL.getModel().getSize(); i++)
321: Main.getProperties().setProperty(
322: Main.PROPERTY_JDBCURL + i,
323: cmbJDBCURL.getModel().getElementAt(i)
324: .toString());
325: Main.getProperties().setProperty(
326: Main.PROPERTY_JDBCUSER, tfUsername.getText());
327: Main.getProperties().storeProperties("");
328: }
329: dispose();
330: }
331: }//GEN-LAST:event_pbOpenActionPerformed
332:
333: private void cmbJDBCURLItemStateChanged(java.awt.event.ItemEvent evt)//GEN-FIRST:event_cmbJDBCURLItemStateChanged
334: {//GEN-HEADEREND:event_cmbJDBCURLItemStateChanged
335: if (this .cmbJDBCDriver.getSelectedItem() != null
336: && this .cmbJDBCURL.getSelectedItem() != null) {
337: this .pbTest.setEnabled(true);
338: this .pbOpen.setEnabled(true);
339: } else {
340: this .pbTest.setEnabled(false);
341: this .pbOpen.setEnabled(false);
342: }
343: }//GEN-LAST:event_cmbJDBCURLItemStateChanged
344:
345: private void cmbJDBCDriverItemStateChanged(
346: java.awt.event.ItemEvent evt)//GEN-FIRST:event_cmbJDBCDriverItemStateChanged
347: {//GEN-HEADEREND:event_cmbJDBCDriverItemStateChanged
348: // Add your handling code here:
349: if (this .cmbJDBCDriver.getSelectedItem() != null
350: && this .cmbJDBCURL.getSelectedItem() != null) {
351: this .pbTest.setEnabled(true);
352: this .pbOpen.setEnabled(true);
353: } else {
354: this .pbTest.setEnabled(false);
355: this .pbOpen.setEnabled(false);
356: }
357: }//GEN-LAST:event_cmbJDBCDriverItemStateChanged
358:
359: private void pbCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_pbCancelActionPerformed
360: {//GEN-HEADEREND:event_pbCancelActionPerformed
361: // Add your handling code here:
362: theConnection = null;
363: dispose();
364: }//GEN-LAST:event_pbCancelActionPerformed
365:
366: private void pbTestActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_pbTestActionPerformed
367: {//GEN-HEADEREND:event_pbTestActionPerformed
368: // Add your handling code here:
369: theConnection = null;
370: try {
371: theConnection = this .connectToDB(cmbJDBCDriver
372: .getSelectedItem().toString(), cmbJDBCURL
373: .getSelectedItem().toString(),
374: tfUsername.getText(), new String(tfPassword
375: .getPassword()));
376: this .lblResult.setText("Connection successful");
377: this .lblResult.setForeground(java.awt.Color.green);
378: if (this .cmbJDBCDriver.getModel() instanceof MutableComboBoxModel) {
379: Object selectedItem = cmbJDBCDriver.getSelectedItem();
380: ((MutableComboBoxModel) this .cmbJDBCDriver.getModel())
381: .removeElement(selectedItem);
382: ((MutableComboBoxModel) this .cmbJDBCDriver.getModel())
383: .insertElementAt(selectedItem, 0);
384: cmbJDBCDriver.setSelectedItem(selectedItem);
385: }
386: if (this .cmbJDBCURL.getModel() instanceof MutableComboBoxModel) {
387: Object selectedItem = cmbJDBCURL.getSelectedItem();
388: ((MutableComboBoxModel) this .cmbJDBCURL.getModel())
389: .removeElement(selectedItem);
390: ((MutableComboBoxModel) this .cmbJDBCURL.getModel())
391: .insertElementAt(selectedItem, 0);
392: cmbJDBCURL.setSelectedItem(selectedItem);
393: }
394: } catch (java.sql.SQLException sqlEx) {
395: StringBuffer strBufMessages = new StringBuffer();
396: java.sql.SQLException currentSqlEx = sqlEx;
397: do {
398: strBufMessages.append("\n" + sqlEx.getErrorCode() + ":"
399: + sqlEx.getMessage());
400: currentSqlEx = currentSqlEx.getNextException();
401: } while (currentSqlEx != null);
402: javax.swing.JOptionPane.showMessageDialog(this ,
403: "Error connecting to database:"
404: + strBufMessages.toString(),
405: "Error connecting to database",
406: javax.swing.JOptionPane.ERROR_MESSAGE);
407: this .lblResult.setText("Connection failed: JDBC Error");
408: this .lblResult.setForeground(java.awt.Color.red);
409: } catch (java.lang.ClassNotFoundException clsNFEx) {
410: javax.swing.JOptionPane
411: .showMessageDialog(
412: this ,
413: "Could not load driver because the specified class could not be found",
414: "Error connecting to database",
415: javax.swing.JOptionPane.ERROR_MESSAGE);
416: this .lblResult
417: .setText("Connection failed: Driver class not found");
418: this .lblResult.setForeground(java.awt.Color.red);
419: }
420: }//GEN-LAST:event_pbTestActionPerformed
421:
422: /** Closes the dialog */
423: private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
424: setVisible(false);
425: dispose();
426: }//GEN-LAST:event_closeDialog
427:
428: private java.sql.Connection connectToDB(String strJDBCDriver,
429: String strJDBCURL, String strUsername, String strPassword)
430: throws java.sql.SQLException,
431: java.lang.ClassNotFoundException {
432: Class.forName(strJDBCDriver);
433: java.sql.Connection conn = java.sql.DriverManager
434: .getConnection(strJDBCURL, strUsername, strPassword);
435: return conn;
436: }
437:
438: /**
439: * @param args the command line arguments
440: */
441: public static void main(String args[]) {
442: System.out.println(new JDlgDBConnection(
443: new javax.swing.JFrame(), true)
444: .showAndReturnConnection());
445: System.out.println(new JDlgDBConnection(
446: new javax.swing.JFrame(), false)
447: .showAndReturnConnection());
448: }
449:
450: // Variables declaration - do not modify//GEN-BEGIN:variables
451: private javax.swing.JLabel lblJDBCURL;
452: private javax.swing.JComboBox cmbJDBCURL;
453: private javax.swing.JComboBox cmbJDBCDriver;
454: private javax.swing.JLabel lblJDBCDriver;
455: private javax.swing.JPanel jPanel1;
456: private javax.swing.JTextField lblResult;
457: private javax.swing.JLabel lblPassword;
458: private javax.swing.JPasswordField tfPassword;
459: private javax.swing.JLabel lblUsername;
460: private javax.swing.JTextField tfUsername;
461: private javax.swing.JButton pbTest;
462: private javax.swing.JButton pbOpen;
463: private javax.swing.JButton pbCancel;
464: // End of variables declaration//GEN-END:variables
465:
466: }
467:
468: /***************************** Changelog *****************************
469: * // $Log: JDlgDBConnection.java,v $
470: * // Revision 1.1.2.1 2005/12/21 22:32:41 tomdz
471: * // Updated license
472: * //
473: * // Revision 1.1 2004/05/05 16:45:09 arminw
474: * // fix fault
475: * // wrong package structure used:
476: * // org.apache.ojb.tools.reversdb
477: * // org.apache.ojb.tools.reversdb2
478: * //
479: * // instead of
480: * // org.apache.ojb.tools.mapping.reversdb
481: * // org.apache.ojb.tools.mapping.reversdb2
482: * //
483: * // Revision 1.1 2004/05/04 13:45:04 arminw
484: * // move reverseDB stuff
485: * //
486: * // Revision 1.3 2004/04/04 23:53:43 brianm
487: * // Fixed initial copyright dates to match cvs repository
488: * //
489: * // Revision 1.2 2004/03/11 18:16:23 brianm
490: * // ASL 2.0
491: * //
492: * // Revision 1.1 2002/06/18 12:37:46 florianbruckner
493: * // adding a complete redesign of a reverse engineering tool. This is still work in progress.
494: * //
495: * // Revision 1.4 2002/06/10 21:30:53 Administrator
496: * // more intuitive handling
497: * //
498: * // Revision 1.3 2002/06/08 00:51:30 Administrator
499: * // no message
500: * //
501: * // Revision 1.2 2002/06/07 10:11:25 Administrator
502: * // *** empty log message ***
503: * //
504: * // Revision 1.1.1.1 2002/06/06 10:54:41 Administrator
505: * // initial import
506: * //
507: * // Revision 1.2 2002/05/16 11:47:09 florianbruckner
508: * // fix CR/LF issue, change license to ASL
509: * //
510: * // Revision 1.1 2002/04/18 11:44:16 mpoeschl
511: * //
512: * // move files to new location
513: * //
514: * // Revision 1.3 2002/04/07 09:05:17 thma
515: * // *** empty log message ***
516: * //
517: * // Revision 1.2 2002/03/11 17:36:26 florianbruckner
518: * // fix line break issue for these files that were previously checked in with -kb
519: * //
520: * // Revision 1.1 2002/03/04 17:19:32 thma
521: * // initial checking for Florians Reverse engineering tool
522: * //
523: * // Revision 1.1.1.1 2002/02/20 13:35:25 Administrator
524: * // initial import
525: * //
526: * /***************************** Changelog *****************************/
|