KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > util > ConnectionDialog


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (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  */

30
31
32 package org.hsqldb.util;
33
34 import java.sql.Connection JavaDoc;
35 import java.sql.DriverManager JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.Hashtable JavaDoc;
38 import java.awt.BorderLayout JavaDoc;
39 import java.awt.Button JavaDoc;
40 import java.awt.Choice JavaDoc;
41 import java.awt.Component JavaDoc;
42 import java.awt.Dialog JavaDoc;
43 import java.awt.Dimension JavaDoc;
44 import java.awt.Frame JavaDoc;
45 import java.awt.GridLayout JavaDoc;
46 import java.awt.Label JavaDoc;
47 import java.awt.Panel JavaDoc;
48 import java.awt.SystemColor JavaDoc;
49 import java.awt.TextField JavaDoc;
50 import java.awt.Toolkit JavaDoc;
51 import java.awt.event.ActionEvent JavaDoc;
52 import java.awt.event.ActionListener JavaDoc;
53 import java.awt.event.ItemEvent JavaDoc;
54 import java.awt.event.ItemListener JavaDoc;
55
56 // sqlbob@users 20020325 - patch 1.7.0 - enhancements
57
// sqlbob@users 20020407 - patch 1.7.0 - reengineering
58
// fredt@users - 20040508 - modified patch by lonbinder@users for saving settings
59

60 /**
61  * Opens a connection to a database
62  *
63  * @author Thomas Mueller (Hypersonic SQL Group)
64  * @version 1.7.2
65  * @since Hypersonic SQL
66  */

67 class ConnectionDialog extends Dialog JavaDoc
68 implements ActionListener JavaDoc, ItemListener JavaDoc {
69
70     protected Connection JavaDoc mConnection;
71     protected TextField JavaDoc mName, mDriver, mURL, mUser, mPassword;
72     protected Label JavaDoc mError;
73     private String JavaDoc[][] connTypes;
74     private Hashtable JavaDoc settings;
75     private Choice JavaDoc types, recent;
76
77     /**
78      * Method declaration
79      *
80      *
81      * @param driver
82      * @param url
83      * @param user
84      * @param password
85      *
86      * @return
87      *
88      * @throws Exception
89      */

90     public static Connection JavaDoc createConnection(String JavaDoc driver, String JavaDoc url,
91             String JavaDoc user, String JavaDoc password) throws Exception JavaDoc {
92
93         Class.forName(driver).newInstance();
94
95         return DriverManager.getConnection(url, user, password);
96     }
97
98     /**
99      * Constructor declaration
100      *
101      *
102      * @param owner
103      * @param title
104      */

105     ConnectionDialog(Frame JavaDoc owner, String JavaDoc title) {
106         super(owner, title, true);
107     }
108
109     /**
110      * Method declaration
111      *
112      */

113     private void create() {
114
115         Dimension JavaDoc d = Toolkit.getDefaultToolkit().getScreenSize();
116
117         setLayout(new BorderLayout JavaDoc());
118
119         Panel JavaDoc p = new Panel JavaDoc(new BorderLayout JavaDoc());
120         Panel JavaDoc pLabel;
121         Panel JavaDoc pText;
122         Panel JavaDoc pButton;
123         Panel JavaDoc pClearButton;
124
125         // (ulrivo): full size on screen with less than 640 width
126
if (d.width >= 640) {
127             pLabel = new Panel JavaDoc(new GridLayout JavaDoc(8, 1, 10, 10));
128             pText = new Panel JavaDoc(new GridLayout JavaDoc(8, 1, 10, 10));
129             pButton = new Panel JavaDoc(new GridLayout JavaDoc(1, 2, 10, 10));
130             pClearButton = new Panel JavaDoc(new GridLayout JavaDoc(8, 1, 10, 10));
131         } else {
132             pLabel = new Panel JavaDoc(new GridLayout JavaDoc(8, 1));
133             pText = new Panel JavaDoc(new GridLayout JavaDoc(8, 1));
134             pButton = new Panel JavaDoc(new GridLayout JavaDoc(1, 2));
135             pClearButton = new Panel JavaDoc(new GridLayout JavaDoc(8, 1));
136         }
137
138         p.add("West", pLabel);
139         p.add("Center", pText);
140         p.add("South", pButton);
141         p.add("North", createLabel(""));
142         p.add("East", pClearButton);
143         p.setBackground(SystemColor.control);
144         pText.setBackground(SystemColor.control);
145         pLabel.setBackground(SystemColor.control);
146         pButton.setBackground(SystemColor.control);
147         pLabel.add(createLabel("Recent:"));
148
149         recent = new Choice JavaDoc();
150
151         try {
152             settings = ConnectionDialogCommon.loadRecentConnectionSettings();
153         } catch (java.io.IOException JavaDoc ioe) {
154             ioe.printStackTrace();
155         }
156
157         recent.add(ConnectionDialogCommon.emptySettingName);
158
159         Enumeration JavaDoc en = settings.elements();
160
161         while (en.hasMoreElements()) {
162             recent.add(((ConnectionSetting) en.nextElement()).getName());
163         }
164
165         recent.addItemListener(new ItemListener JavaDoc() {
166
167             public void itemStateChanged(ItemEvent JavaDoc e) {
168
169                 String JavaDoc s = (String JavaDoc) e.getItem();
170                 ConnectionSetting setting =
171                     (ConnectionSetting) settings.get(s);
172
173                 if (setting != null) {
174                     mName.setText(setting.getName());
175                     mDriver.setText(setting.getDriver());
176                     mURL.setText(setting.getUrl());
177                     mUser.setText(setting.getUser());
178                     mPassword.setText(setting.getPassword());
179                 }
180             }
181         });
182         pText.add(recent);
183
184         Button JavaDoc b;
185
186         b = new Button JavaDoc("Clr");
187
188         b.setActionCommand("Clear");
189         b.addActionListener(new ActionListener JavaDoc() {
190
191             public void actionPerformed(ActionEvent JavaDoc e) {
192
193                 ConnectionDialogCommon.deleteRecentConnectionSettings();
194
195                 settings = new Hashtable JavaDoc();
196
197                 recent.removeAll();
198                 recent.add(ConnectionDialogCommon.emptySettingName);
199                 mName.setText(null);
200             }
201         });
202         pClearButton.add(b);
203         pLabel.add(createLabel("Setting Name:"));
204
205         mName = new TextField JavaDoc("");
206
207         pText.add(mName);
208         pLabel.add(createLabel("Type:"));
209
210         types = new Choice JavaDoc();
211         connTypes = ConnectionDialogCommon.getTypes();
212
213         for (int i = 0; i < connTypes.length; i++) {
214             types.add(connTypes[i][0]);
215         }
216
217         types.addItemListener(this);
218         pText.add(types);
219         pLabel.add(createLabel("Driver:"));
220
221         mDriver = new TextField JavaDoc(connTypes[0][1]);
222
223         pText.add(mDriver);
224         pLabel.add(createLabel("URL:"));
225
226         mURL = new TextField JavaDoc(connTypes[0][2]);
227
228         mURL.addActionListener(this);
229         pText.add(mURL);
230         pLabel.add(createLabel("User:"));
231
232         mUser = new TextField JavaDoc("sa");
233
234         mUser.addActionListener(this);
235         pText.add(mUser);
236         pLabel.add(createLabel("Password:"));
237
238         mPassword = new TextField JavaDoc("");
239
240         mPassword.addActionListener(this);
241         mPassword.setEchoChar('*');
242         pText.add(mPassword);
243
244         b = new Button JavaDoc("Ok");
245
246         b.setActionCommand("ConnectOk");
247         b.addActionListener(this);
248         pButton.add(b);
249
250         b = new Button JavaDoc("Cancel");
251
252         b.setActionCommand("ConnectCancel");
253         b.addActionListener(this);
254         pButton.add(b);
255         add("East", createLabel(""));
256         add("West", createLabel(""));
257
258         mError = new Label JavaDoc("");
259
260         Panel JavaDoc pMessage = createBorderPanel(mError);
261
262         add("South", pMessage);
263         add("North", createLabel(""));
264         add("Center", p);
265         doLayout();
266         pack();
267
268         Dimension JavaDoc size = getSize();
269
270         // (ulrivo): full size on screen with less than 640 width
271
if (d.width >= 640) {
272             setLocation((d.width - size.width) / 2,
273                         (d.height - size.height) / 2);
274         } else {
275             setLocation(0, 0);
276             setSize(d);
277         }
278
279         show();
280     }
281
282     /**
283      * Method declaration
284      *
285      *
286      * @param owner
287      * @param title
288      *
289      * @return
290      */

291     public static Connection JavaDoc createConnection(Frame JavaDoc owner, String JavaDoc title) {
292
293         ConnectionDialog dialog = new ConnectionDialog(owner, title);
294
295         dialog.create();
296
297         return dialog.mConnection;
298     }
299
300     /**
301      * Method declaration
302      *
303      *
304      * @param s
305      *
306      * @return
307      */

308     protected static Label JavaDoc createLabel(String JavaDoc s) {
309
310         Label JavaDoc l = new Label JavaDoc(s);
311
312         l.setBackground(SystemColor.control);
313
314         return l;
315     }
316
317     /**
318      * Method declaration
319      *
320      *
321      * @param center
322      *
323      * @return
324      */

325     protected static Panel JavaDoc createBorderPanel(Component JavaDoc center) {
326
327         Panel JavaDoc p = new Panel JavaDoc();
328
329         p.setBackground(SystemColor.control);
330         p.setLayout(new BorderLayout JavaDoc());
331         p.add("Center", center);
332         p.add("North", createLabel(""));
333         p.add("South", createLabel(""));
334         p.add("East", createLabel(""));
335         p.add("West", createLabel(""));
336         p.setBackground(SystemColor.control);
337
338         return p;
339     }
340
341     /**
342      * Method declaration
343      *
344      *
345      * @param ev
346      */

347     public void actionPerformed(ActionEvent JavaDoc ev) {
348
349         String JavaDoc s = ev.getActionCommand();
350
351         if (s.equals("ConnectOk") || (ev.getSource() instanceof TextField JavaDoc)) {
352             try {
353                 if (mURL.getText().indexOf('\u00AB') >= 0) {
354                     throw new Exception JavaDoc("please specify db path");
355                 }
356
357                 mConnection = createConnection(mDriver.getText(),
358                                                mURL.getText(),
359                                                mUser.getText(),
360                                                mPassword.getText());
361
362                 if (mName.getText() != null
363                         && mName.getText().trim().length() != 0) {
364                     ConnectionSetting newSetting =
365                         new ConnectionSetting(mName.getText(),
366                                               mDriver.getText(),
367                                               mURL.getText(),
368                                               mUser.getText(),
369                                               mPassword.getText());
370
371                     ConnectionDialogCommon.addToRecentConnectionSettings(
372                         settings, newSetting);
373                 }
374
375                 dispose();
376             } catch (java.io.IOException JavaDoc ioe) {
377                 dispose();
378             } catch (Exception JavaDoc e) {
379                 e.printStackTrace();
380                 mError.setText(e.toString());
381             }
382         } else if (s.equals("ConnectCancel")) {
383             dispose();
384         }
385     }
386
387     /**
388      * Method declaration
389      *
390      *
391      * @param e
392      */

393     public void itemStateChanged(ItemEvent JavaDoc e) {
394
395         String JavaDoc s = (String JavaDoc) e.getItem();
396
397         for (int i = 0; i < connTypes.length; i++) {
398             if (s.equals(connTypes[i][0])) {
399                 mDriver.setText(connTypes[i][1]);
400                 mURL.setText(connTypes[i][2]);
401             }
402         }
403     }
404 }
405
Popular Tags