Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hsqldb/util/ConnectionDialogSwing.java


1   /* Copyrights and Licenses
2    *
3    * This product includes Hypersonic SQL.
4    * Originally developed by Thomas Mueller and the Hypersonic SQL Group. 
5    *
6    * Copyright (c) 1995-2000 by the Hypersonic SQL Group. All rights reserved. 
7    * Redistribution and use in source and binary forms, with or without modification, are permitted
8    * provided that the following conditions are met: 
9    *     -  Redistributions of source code must retain the above copyright notice, this list of conditions
10   *         and the following disclaimer. 
11   *     -  Redistributions in binary form must reproduce the above copyright notice, this list of
12   *         conditions and the following disclaimer in the documentation and/or other materials
13   *         provided with the distribution. 
14   *     -  All advertising materials mentioning features or use of this software must display the
15   *        following acknowledgment: "This product includes Hypersonic SQL." 
16   *     -  Products derived from this software may not be called "Hypersonic SQL" nor may
17   *        "Hypersonic SQL" appear in their names without prior written permission of the
18   *         Hypersonic SQL Group. 
19   *     -  Redistributions of any form whatsoever must retain the following acknowledgment: "This
20   *          product includes Hypersonic SQL." 
21   * This software is provided "as is" and any expressed or implied warranties, including, but
22   * not limited to, the implied warranties of merchantability and fitness for a particular purpose are
23   * disclaimed. In no event shall the Hypersonic SQL Group or its contributors be liable for any
24   * direct, indirect, incidental, special, exemplary, or consequential damages (including, but
25   * not limited to, procurement of substitute goods or services; loss of use, data, or profits;
26   * or business interruption). However caused any on any theory of liability, whether in contract,
27   * strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this
28   * software, even if advised of the possibility of such damage. 
29   * This software consists of voluntary contributions made by many individuals on behalf of the
30   * Hypersonic SQL Group.
31   *
32   *
33   * For work added by the HSQL Development Group:
34   *
35   * Copyright (c) 2001-2002, The HSQL Development Group
36   * All rights reserved.
37   *
38   * Redistribution and use in source and binary forms, with or without
39   * modification, are permitted provided that the following conditions are met:
40   *
41   * Redistributions of source code must retain the above copyright notice, this
42   * list of conditions and the following disclaimer, including earlier
43   * license statements (above) and comply with all above license conditions.
44   *
45   * Redistributions in binary form must reproduce the above copyright notice,
46   * this list of conditions and the following disclaimer in the documentation
47   * and/or other materials provided with the distribution, including earlier
48   * license statements (above) and comply with all above license conditions.
49   *
50   * Neither the name of the HSQL Development Group nor the names of its
51   * contributors may be used to endorse or promote products derived from this
52   * software without specific prior written permission.
53   *
54   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
55   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57   * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, 
58   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
59   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
60   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
61   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
62   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65   */
66  
67  
68  package org.hsqldb.util;
69  
70  import java.awt.*;
71  import java.awt.event.*;
72  import java.sql.*;
73  import javax.swing.*;
74  import javax.swing.border.*;
75  
76  // sqlbob@users 20020325 - patch 1.7.0 - enhancements
77  // sqlbob@users 20020407 - patch 1.7.0 - reengineering
78  
79  /**
80   * Opens a connection to a database
81   *
82   * @version 1.7.0
83   */
84  class ConnectionDialogSwing extends JDialog
85  implements ActionListener, ItemListener {
86  
87      private Connection     mConnection;
88      private JTextField     mDriver, mURL, mUser, mError;
89      private JPasswordField mPassword;
90      private String         connTypes[][];
91  
92      public static Connection createConnection(String driver, String url,
93              String user, String password) throws Exception {
94  
95          Class.forName(driver).newInstance();
96  
97          return DriverManager.getConnection(url, user, password);
98      }
99  
100     ConnectionDialogSwing(JFrame owner, String title) {
101         super(owner, title, true);
102     }
103 
104     private void create() {
105 
106         CommonSwing.setDefaultColor();
107 
108         JButton b;
109         Box     main     = Box.createHorizontalBox();
110         Box     labels   = Box.createVerticalBox();
111         Box     controls = Box.createVerticalBox();
112         Box     buttons  = Box.createHorizontalBox();
113         Box     whole    = Box.createVerticalBox();
114         Box     status   = Box.createHorizontalBox();
115 
116         main.add(Box.createHorizontalGlue());
117         main.add(labels);
118         main.add(Box.createHorizontalStrut(10));
119         main.add(Box.createHorizontalGlue());
120         main.add(controls);
121         main.add(Box.createHorizontalGlue());
122         whole.add(Box.createVerticalGlue());
123         whole.add(Box.createVerticalStrut(10));
124         whole.add(main);
125         whole.add(Box.createVerticalGlue());
126         whole.add(Box.createVerticalStrut(10));
127         whole.add(buttons);
128         whole.add(Box.createVerticalGlue());
129         whole.add(Box.createVerticalStrut(10));
130         whole.add(status);
131         whole.add(Box.createVerticalStrut(10));
132         whole.add(Box.createVerticalGlue());
133         labels.add(Box.createVerticalGlue());
134         labels.add(createLabel("Type:"));
135         labels.add(Box.createVerticalGlue());
136         labels.add(createLabel("Driver"));
137         labels.add(Box.createVerticalGlue());
138         labels.add(createLabel("URL"));
139         labels.add(Box.createVerticalGlue());
140         labels.add(createLabel("User:"));
141         labels.add(Box.createVerticalGlue());
142         labels.add(createLabel("Password:"));
143         labels.add(Box.createVerticalGlue());
144         labels.add(Box.createVerticalStrut(10));
145 
146         // Now the 2nd column which is the controls box:
147         controls.add(Box.createVerticalGlue());
148 
149         JComboBox types = new JComboBox();
150 
151         connTypes = ConnectionDialogCommon.getTypes();
152 
153         for (int i = 0; i < connTypes.length; i++) {
154             types.addItem(connTypes[i][0]);
155         }
156 
157         types.addItemListener(this);
158         controls.add(types);
159         controls.add(Box.createVerticalGlue());
160 
161         mDriver = new JTextField(connTypes[0][1]);
162 
163         mDriver.addActionListener(this);
164         controls.add(mDriver);
165 
166         mURL = new JTextField(connTypes[0][2]);
167 
168         mURL.addActionListener(this);
169         controls.add(mURL);
170         controls.add(Box.createVerticalGlue());
171 
172         mUser = new JTextField("sa");
173 
174         mUser.addActionListener(this);
175         controls.add(mUser);
176         controls.add(Box.createVerticalGlue());
177 
178         mPassword = new JPasswordField("");
179 
180         mPassword.addActionListener(this);
181         controls.add(mPassword);
182         controls.add(Box.createVerticalGlue());
183         controls.add(Box.createVerticalStrut(10));
184 
185         // The button bar
186         buttons.add(Box.createHorizontalGlue());
187         buttons.add(Box.createHorizontalStrut(10));
188 
189         b = new JButton("Ok");
190 
191         b.setActionCommand("ConnectOk");
192         b.addActionListener(this);
193         buttons.add(b);
194         getRootPane().setDefaultButton(b);
195         buttons.add(Box.createHorizontalGlue());
196         buttons.add(Box.createHorizontalStrut(20));
197 
198         b = new JButton("Cancel");
199 
200         b.setActionCommand("ConnectCancel");
201         b.addActionListener(this);
202         buttons.add(b);
203         buttons.add(Box.createHorizontalGlue());
204         buttons.add(Box.createHorizontalStrut(10));
205 
206         // Now the status line
207         mError = new JTextField("");
208 
209         mError.setEditable(false);
210         status.add(Box.createHorizontalGlue());
211         status.add(mError);
212         status.add(Box.createHorizontalGlue());
213 
214         JPanel jp = new JPanel();
215 
216         jp.setBorder(new EmptyBorder(10, 10, 10, 10));
217         jp.add("Center", whole);
218         getContentPane().add("Center", jp);
219         doLayout();
220         pack();
221 
222         Dimension d    = Toolkit.getDefaultToolkit().getScreenSize();
223         Dimension size = getSize();
224 
225         // (ulrivo): full size on screen with less than 640 width
226         if (d.width >= 640) {
227             setLocation((d.width - size.width) / 2,
228                         (d.height - size.height) / 2);
229         } else {
230             setLocation(0, 0);
231             setSize(d);
232         }
233 
234         show();
235     }
236 
237     public static Connection createConnection(JFrame owner, String title) {
238 
239         ConnectionDialogSwing dialog = new ConnectionDialogSwing(owner,
240             title);
241 
242         dialog.create();
243 
244         return dialog.mConnection;
245     }
246 
247     private static JLabel createLabel(String s) {
248 
249         JLabel l = new JLabel(s);
250 
251         return l;
252     }
253 
254     public void actionPerformed(ActionEvent ev) {
255 
256         String s = ev.getActionCommand();
257 
258         if (s.equals("ConnectOk") || (ev.getSource() instanceof JTextField)) {
259             try {
260                 mConnection =
261                     createConnection(mDriver.getText(), mURL.getText(),
262                                      mUser.getText(),
263                                      new String(mPassword.getPassword()));
264 
265                 dispose();
266             } catch (Exception e) {
267                 e.printStackTrace();
268                 mError.setText(e.toString());
269             }
270         } else if (s.equals("ConnectCancel")) {
271             dispose();
272         }
273     }
274 
275     public void itemStateChanged(ItemEvent e) {
276 
277         String s = (String) e.getItem();
278 
279         for (int i = 0; i < connTypes.length; i++) {
280             if (s.equals(connTypes[i][0])) {
281                 mDriver.setText(connTypes[i][1]);
282                 mURL.setText(connTypes[i][2]);
283             }
284         }
285     }
286 }