KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tools > mapping > reversedb2 > gui > JDlgDBConnection


1 package org.apache.ojb.tools.mapping.reversedb2.gui;
2
3
4 import java.util.Vector JavaDoc;
5 import org.apache.ojb.tools.mapping.reversedb2.Main;
6 import javax.swing.MutableComboBoxModel JavaDoc;
7 /* Copyright 2002-2005 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22
23
24
25
26 /**
27  * This dialog collects JDBC connection data and opens the connection. The JDBC
28  * driver class and URL are stored in the properties of Main, so the user only
29  * has to enter this data once.
30  *
31  * The method showAndReturnConnection() performs a show(), blocks until the
32  * dialog is disposed and returns the connection. If no valid connection is
33  * established, null is returned.
34  *
35  * @author <a HREF="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
36  * @version $Id: JDlgDBConnection.java,v 1.1.2.1 2005/12/21 22:32:41 tomdz Exp $
37  */

38 public class JDlgDBConnection extends javax.swing.JDialog JavaDoc
39 {
40     
41     private Vector JavaDoc vJDBCDrivers = new Vector JavaDoc();
42     private Vector JavaDoc vJDBCUrls = new Vector JavaDoc();
43     
44     private java.sql.Connection JavaDoc theConnection = null;
45     
46     private boolean isDisposed = false;
47     /** Creates new form JDlgDBConnection.
48      * @param parent parent frame as specified by javax.swing.JDialog
49      * @param isModal
50      */

51     public JDlgDBConnection(java.awt.Frame JavaDoc parent, boolean isModal)
52     {
53         super(parent, isModal);
54         initComponents();
55         
56         synchronized (Main.getProperties())
57         {
58             // Read all stored JDBCDrivers from properties
59
int i = 0;
60             String JavaDoc s = null;
61             do
62             {
63                 s = Main.getProperties().getProperty(Main.PROPERTY_JDBCDRIVER + i);
64                 if (s != null) vJDBCDrivers.add(s);
65                 i++;
66             } while (s != null);
67
68
69             // Read all stored JDBCUrls from properties
70
i = 0;
71             s = null;
72             do
73             {
74                 s = Main.getProperties().getProperty(Main.PROPERTY_JDBCURL + i);
75                 if (s != null) vJDBCUrls.add(s);
76                 i++;
77             } while (s != null);
78
79
80             // Read all stored JDBC usernames from properties
81
s = Main.getProperties().getProperty("JDBCUser" + i);
82
83
84
85             this.cmbJDBCDriver.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(vJDBCDrivers));
86             this.cmbJDBCURL.setModel(new javax.swing.DefaultComboBoxModel JavaDoc(vJDBCUrls));
87
88
89             this.cmbJDBCDriver.setEditable(true);
90             this.cmbJDBCURL.setEditable(true);
91
92
93             this.tfUsername.setText(Main.getProperties().getProperty(Main.PROPERTY_JDBCUSER, ""));
94             this.tfPassword.setText("");
95
96
97             this.lblResult.setText("");
98             pack();
99         }
100         if (this.cmbJDBCDriver.getSelectedItem() != null &&
101             this.cmbJDBCURL.getSelectedItem() != null)
102         {
103             this.pbTest.setEnabled(true);
104             this.pbOpen.setEnabled(true);
105         }
106         else
107         {
108             this.pbTest.setEnabled(false);
109             this.pbOpen.setEnabled(false);
110         }
111     }
112     
113     /** Performs a show(), blocks until the
114      * dialog is disposed and returns the connection. If no valid connection is
115      * established, null is returned.
116      */

117     public java.sql.Connection JavaDoc showAndReturnConnection()
118     {
119         show();
120         while (!this.isDisposed)
121         {
122             try
123             {
124                 synchronized (this)
125                 {
126                     wait();
127                 }
128             }
129             catch (Throwable JavaDoc t)
130             {
131             }
132         }
133         return theConnection;
134     }
135     
136     /** dispose() of javax.swing.JDialog overridden to be able to wait until the dialog is
137      * disposed even if it is not modal in showAndReturnConnection().
138      *
139      * 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.
140      */

141     public void dispose()
142     {
143         super.dispose();
144         this.isDisposed = true;
145         synchronized(this)
146         {
147             this.notifyAll();
148         }
149     }
150     
151     /** This method is called from within the constructor to
152      * initialize the form.
153      * WARNING: Do NOT modify this code. The content of this method is
154      * always regenerated by the Form Editor.
155      */

156     private void initComponents()//GEN-BEGIN:initComponents
157
{
158         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
159
160         lblJDBCDriver = new javax.swing.JLabel JavaDoc();
161         cmbJDBCDriver = new javax.swing.JComboBox JavaDoc();
162         lblJDBCURL = new javax.swing.JLabel JavaDoc();
163         cmbJDBCURL = new javax.swing.JComboBox JavaDoc();
164         lblUsername = new javax.swing.JLabel JavaDoc();
165         tfUsername = new javax.swing.JTextField JavaDoc();
166         lblPassword = new javax.swing.JLabel JavaDoc();
167         tfPassword = new javax.swing.JPasswordField JavaDoc();
168         lblResult = new javax.swing.JTextField JavaDoc();
169         jPanel1 = new javax.swing.JPanel JavaDoc();
170         pbCancel = new javax.swing.JButton JavaDoc();
171         pbTest = new javax.swing.JButton JavaDoc();
172         pbOpen = new javax.swing.JButton JavaDoc();
173
174         getContentPane().setLayout(new java.awt.GridBagLayout JavaDoc());
175
176         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
177         addWindowListener(new java.awt.event.WindowAdapter JavaDoc()
178         {
179             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt)
180             {
181                 closeDialog(evt);
182             }
183         });
184
185         lblJDBCDriver.setText("JDBC Driver Class:");
186         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
187         gridBagConstraints.gridx = 0;
188         gridBagConstraints.gridy = 0;
189         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
190         getContentPane().add(lblJDBCDriver, gridBagConstraints);
191
192         cmbJDBCDriver.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12));
193         cmbJDBCDriver.setMinimumSize(new java.awt.Dimension JavaDoc(31, 20));
194         cmbJDBCDriver.setPreferredSize(new java.awt.Dimension JavaDoc(31, 20));
195         cmbJDBCDriver.addItemListener(new java.awt.event.ItemListener JavaDoc()
196         {
197             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt)
198             {
199                 cmbJDBCDriverItemStateChanged(evt);
200             }
201         });
202
203         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
204         gridBagConstraints.gridx = 1;
205         gridBagConstraints.gridy = 0;
206         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
207         getContentPane().add(cmbJDBCDriver, gridBagConstraints);
208
209         lblJDBCURL.setText("JDBC URL:");
210         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
211         gridBagConstraints.gridx = 0;
212         gridBagConstraints.gridy = 1;
213         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
214         getContentPane().add(lblJDBCURL, gridBagConstraints);
215
216         cmbJDBCURL.setFont(new java.awt.Font JavaDoc("Dialog", 0, 12));
217         cmbJDBCURL.setMinimumSize(new java.awt.Dimension JavaDoc(31, 20));
218         cmbJDBCURL.setPreferredSize(new java.awt.Dimension JavaDoc(31, 20));
219         cmbJDBCURL.addItemListener(new java.awt.event.ItemListener JavaDoc()
220         {
221             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt)
222             {
223                 cmbJDBCURLItemStateChanged(evt);
224             }
225         });
226
227         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
228         gridBagConstraints.gridx = 1;
229         gridBagConstraints.gridy = 1;
230         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
231         getContentPane().add(cmbJDBCURL, gridBagConstraints);
232
233         lblUsername.setText("Username:");
234         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
235         gridBagConstraints.gridx = 0;
236         gridBagConstraints.gridy = 2;
237         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
238         getContentPane().add(lblUsername, gridBagConstraints);
239
240         tfUsername.setColumns(30);
241         tfUsername.setText("jTextField3");
242         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
243         gridBagConstraints.gridx = 1;
244         gridBagConstraints.gridy = 2;
245         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
246         gridBagConstraints.weightx = 1.0;
247         getContentPane().add(tfUsername, gridBagConstraints);
248
249         lblPassword.setText("Password:");
250         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
251         gridBagConstraints.gridx = 0;
252         gridBagConstraints.gridy = 3;
253         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
254         getContentPane().add(lblPassword, gridBagConstraints);
255
256         tfPassword.setColumns(30);
257         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
258         gridBagConstraints.gridx = 1;
259         gridBagConstraints.gridy = 3;
260         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
261         gridBagConstraints.weightx = 1.0;
262         getContentPane().add(tfPassword, gridBagConstraints);
263
264         lblResult.setBackground((java.awt.Color JavaDoc) javax.swing.UIManager.getDefaults().get("Label.background"));
265         lblResult.setEditable(false);
266         lblResult.setText("jTextField1");
267         lblResult.setBorder(null);
268         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
269         gridBagConstraints.gridx = 0;
270         gridBagConstraints.gridy = 4;
271         gridBagConstraints.gridwidth = 2;
272         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
273         getContentPane().add(lblResult, gridBagConstraints);
274
275         pbCancel.setMnemonic('c');
276         pbCancel.setText("Cancel");
277         pbCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
278         {
279             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
280             {
281                 pbCancelActionPerformed(evt);
282             }
283         });
284
285         jPanel1.add(pbCancel);
286
287         pbTest.setMnemonic('t');
288         pbTest.setText("Test");
289         pbTest.setEnabled(false);
290         pbTest.addActionListener(new java.awt.event.ActionListener JavaDoc()
291         {
292             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
293             {
294                 pbTestActionPerformed(evt);
295             }
296         });
297
298         jPanel1.add(pbTest);
299
300         pbOpen.setMnemonic('o');
301         pbOpen.setText("Open");
302         pbOpen.setEnabled(false);
303         pbOpen.addActionListener(new java.awt.event.ActionListener JavaDoc()
304         {
305             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
306             {
307                 pbOpenActionPerformed(evt);
308             }
309         });
310
311         jPanel1.add(pbOpen);
312
313         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
314         gridBagConstraints.gridx = 1;
315         gridBagConstraints.gridy = 5;
316         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
317         getContentPane().add(jPanel1, gridBagConstraints);
318
319         pack();
320     }//GEN-END:initComponents
321

322     private void pbOpenActionPerformed (java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_pbOpenActionPerformed
323
{//GEN-HEADEREND:event_pbOpenActionPerformed
324
// Add your handling code here:
325
pbTestActionPerformed(evt);
326       if (theConnection != null)
327       {
328           synchronized(Main.getProperties())
329           {
330               int i = 0;
331               while (Main.getProperties().getProperty(Main.PROPERTY_JDBCDRIVER + i) != null)
332               {
333                   Main.getProperties().remove(Main.PROPERTY_JDBCDRIVER + i);
334                   i++;
335               }
336               while (Main.getProperties().getProperty(Main.PROPERTY_JDBCURL + i) != null)
337               {
338                   Main.getProperties().remove(Main.PROPERTY_JDBCURL + i);
339                   i++;
340               }
341               for (i = 0; i < cmbJDBCDriver.getModel().getSize(); i++)
342                   Main.getProperties().setProperty(Main.PROPERTY_JDBCDRIVER + i, cmbJDBCDriver.getModel().getElementAt(i).toString());
343               for (i = 0; i < cmbJDBCURL.getModel().getSize(); i++)
344                   Main.getProperties().setProperty(Main.PROPERTY_JDBCURL + i, cmbJDBCURL.getModel().getElementAt(i).toString());
345               Main.getProperties().setProperty(Main.PROPERTY_JDBCUSER, tfUsername.getText());
346               Main.getProperties().storeProperties("");
347           }
348           dispose();
349       }
350     }//GEN-LAST:event_pbOpenActionPerformed
351

352
353     private void cmbJDBCURLItemStateChanged(java.awt.event.ItemEvent JavaDoc evt)//GEN-FIRST:event_cmbJDBCURLItemStateChanged
354
{//GEN-HEADEREND:event_cmbJDBCURLItemStateChanged
355
if (this.cmbJDBCDriver.getSelectedItem() != null &&
356             this.cmbJDBCURL.getSelectedItem() != null)
357         {
358             this.pbTest.setEnabled(true);
359             this.pbOpen.setEnabled(true);
360         }
361         else
362         {
363             this.pbTest.setEnabled(false);
364             this.pbOpen.setEnabled(false);
365         }
366     }//GEN-LAST:event_cmbJDBCURLItemStateChanged
367

368
369     private void cmbJDBCDriverItemStateChanged(java.awt.event.ItemEvent JavaDoc evt)//GEN-FIRST:event_cmbJDBCDriverItemStateChanged
370
{//GEN-HEADEREND:event_cmbJDBCDriverItemStateChanged
371
// Add your handling code here:
372
if (this.cmbJDBCDriver.getSelectedItem() != null &&
373             this.cmbJDBCURL.getSelectedItem() != null)
374         {
375             this.pbTest.setEnabled(true);
376             this.pbOpen.setEnabled(true);
377         }
378         else
379         {
380             this.pbTest.setEnabled(false);
381             this.pbOpen.setEnabled(false);
382         }
383     }//GEN-LAST:event_cmbJDBCDriverItemStateChanged
384

385   private void pbCancelActionPerformed (java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_pbCancelActionPerformed
386
{//GEN-HEADEREND:event_pbCancelActionPerformed
387
// Add your handling code here:
388
theConnection = null;
389       dispose();
390   }//GEN-LAST:event_pbCancelActionPerformed
391

392   private void pbTestActionPerformed (java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_pbTestActionPerformed
393
{//GEN-HEADEREND:event_pbTestActionPerformed
394
// Add your handling code here:
395
theConnection = null;
396         try
397         {
398             theConnection = this.connectToDB(cmbJDBCDriver.getSelectedItem().toString(),
399                                  cmbJDBCURL.getSelectedItem().toString(),
400                                  tfUsername.getText(),
401                                  new String JavaDoc(tfPassword.getPassword()));
402             this.lblResult.setText("Connection successful");
403             this.lblResult.setForeground(java.awt.Color.green);
404             if (this.cmbJDBCDriver.getModel() instanceof MutableComboBoxModel JavaDoc)
405             {
406                 Object JavaDoc selectedItem = cmbJDBCDriver.getSelectedItem();
407                 ((MutableComboBoxModel JavaDoc)this.cmbJDBCDriver.getModel()).removeElement(selectedItem);
408                 ((MutableComboBoxModel JavaDoc)this.cmbJDBCDriver.getModel()).insertElementAt(selectedItem,0);
409                 cmbJDBCDriver.setSelectedItem(selectedItem);
410             }
411             if (this.cmbJDBCURL.getModel() instanceof MutableComboBoxModel JavaDoc)
412             {
413                 Object JavaDoc selectedItem = cmbJDBCURL.getSelectedItem();
414                 ((MutableComboBoxModel JavaDoc)this.cmbJDBCURL.getModel()).removeElement(selectedItem);
415                 ((MutableComboBoxModel JavaDoc)this.cmbJDBCURL.getModel()).insertElementAt(selectedItem, 0);
416                 cmbJDBCURL.setSelectedItem(selectedItem);
417             }
418         }
419         catch (java.sql.SQLException JavaDoc sqlEx)
420         {
421             StringBuffer JavaDoc strBufMessages = new StringBuffer JavaDoc();
422             java.sql.SQLException JavaDoc currentSqlEx = sqlEx;
423             do
424             {
425                 strBufMessages.append("\n" + sqlEx.getErrorCode() + ":" + sqlEx.getMessage());
426                 currentSqlEx = currentSqlEx.getNextException();
427             } while (currentSqlEx != null);
428             javax.swing.JOptionPane.showMessageDialog(this,
429                 "Error connecting to database:" + strBufMessages.toString(),
430                 "Error connecting to database",
431                 javax.swing.JOptionPane.ERROR_MESSAGE);
432             this.lblResult.setText("Connection failed: JDBC Error");
433             this.lblResult.setForeground(java.awt.Color.red);
434         }
435         catch (java.lang.ClassNotFoundException JavaDoc clsNFEx)
436         {
437             javax.swing.JOptionPane.showMessageDialog(this,
438                 "Could not load driver because the specified class could not be found",
439                 "Error connecting to database",
440                 javax.swing.JOptionPane.ERROR_MESSAGE);
441             this.lblResult.setText("Connection failed: Driver class not found");
442             this.lblResult.setForeground(java.awt.Color.red);
443         }
444   }//GEN-LAST:event_pbTestActionPerformed
445

446     /** Closes the dialog */
447     private void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_closeDialog
448
setVisible(false);
449         dispose();
450     }//GEN-LAST:event_closeDialog
451

452     private java.sql.Connection JavaDoc connectToDB(String JavaDoc strJDBCDriver, String JavaDoc strJDBCURL,
453         String JavaDoc strUsername, String JavaDoc strPassword)
454         throws java.sql.SQLException JavaDoc, java.lang.ClassNotFoundException JavaDoc
455     {
456         Class.forName(strJDBCDriver);
457         java.sql.Connection JavaDoc conn =
458         java.sql.DriverManager.getConnection(strJDBCURL,
459                 strUsername, strPassword);
460         return conn;
461     }
462     
463     /**
464      * @param args the command line arguments
465      */

466     public static void main(String JavaDoc args[])
467     {
468         System.out.println(new JDlgDBConnection(new javax.swing.JFrame JavaDoc(), true).showAndReturnConnection());
469         System.out.println(new JDlgDBConnection(new javax.swing.JFrame JavaDoc(), false).showAndReturnConnection());
470     }
471     
472     
473     // Variables declaration - do not modify//GEN-BEGIN:variables
474
private javax.swing.JLabel JavaDoc lblJDBCURL;
475     private javax.swing.JComboBox JavaDoc cmbJDBCURL;
476     private javax.swing.JComboBox JavaDoc cmbJDBCDriver;
477     private javax.swing.JLabel JavaDoc lblJDBCDriver;
478     private javax.swing.JPanel JavaDoc jPanel1;
479     private javax.swing.JTextField JavaDoc lblResult;
480     private javax.swing.JLabel JavaDoc lblPassword;
481     private javax.swing.JPasswordField JavaDoc tfPassword;
482     private javax.swing.JLabel JavaDoc lblUsername;
483     private javax.swing.JTextField JavaDoc tfUsername;
484     private javax.swing.JButton JavaDoc pbTest;
485     private javax.swing.JButton JavaDoc pbOpen;
486     private javax.swing.JButton JavaDoc pbCancel;
487     // End of variables declaration//GEN-END:variables
488

489 }
490
491
492 /***************************** Changelog *****************************
493  * // $Log: JDlgDBConnection.java,v $
494  * // Revision 1.1.2.1 2005/12/21 22:32:41 tomdz
495  * // Updated license
496  * //
497  * // Revision 1.1 2004/05/05 16:45:09 arminw
498  * // fix fault
499  * // wrong package structure used:
500  * // org.apache.ojb.tools.reversdb
501  * // org.apache.ojb.tools.reversdb2
502  * //
503  * // instead of
504  * // org.apache.ojb.tools.mapping.reversdb
505  * // org.apache.ojb.tools.mapping.reversdb2
506  * //
507  * // Revision 1.1 2004/05/04 13:45:04 arminw
508  * // move reverseDB stuff
509  * //
510  * // Revision 1.3 2004/04/04 23:53:43 brianm
511  * // Fixed initial copyright dates to match cvs repository
512  * //
513  * // Revision 1.2 2004/03/11 18:16:23 brianm
514  * // ASL 2.0
515  * //
516  * // Revision 1.1 2002/06/18 12:37:46 florianbruckner
517  * // adding a complete redesign of a reverse engineering tool. This is still work in progress.
518  * //
519  * // Revision 1.4 2002/06/10 21:30:53 Administrator
520  * // more intuitive handling
521  * //
522  * // Revision 1.3 2002/06/08 00:51:30 Administrator
523  * // no message
524  * //
525  * // Revision 1.2 2002/06/07 10:11:25 Administrator
526  * // *** empty log message ***
527  * //
528  * // Revision 1.1.1.1 2002/06/06 10:54:41 Administrator
529  * // initial import
530  * //
531  * // Revision 1.2 2002/05/16 11:47:09 florianbruckner
532  * // fix CR/LF issue, change license to ASL
533  * //
534  * // Revision 1.1 2002/04/18 11:44:16 mpoeschl
535  * //
536  * // move files to new location
537  * //
538  * // Revision 1.3 2002/04/07 09:05:17 thma
539  * // *** empty log message ***
540  * //
541  * // Revision 1.2 2002/03/11 17:36:26 florianbruckner
542  * // fix line break issue for these files that were previously checked in with -kb
543  * //
544  * // Revision 1.1 2002/03/04 17:19:32 thma
545  * // initial checking for Florians Reverse engineering tool
546  * //
547  * // Revision 1.1.1.1 2002/02/20 13:35:25 Administrator
548  * // initial import
549  * //
550  * /***************************** Changelog *****************************/

551
Popular Tags