KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
37 import java.util.Hashtable JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Vector JavaDoc;
40 import java.awt.Dimension JavaDoc;
41 import java.awt.Toolkit JavaDoc;
42 import java.awt.event.ActionEvent JavaDoc;
43 import java.awt.event.ActionListener JavaDoc;
44 import java.awt.event.ItemEvent JavaDoc;
45 import java.awt.event.ItemListener JavaDoc;
46
47 import javax.swing.Box JavaDoc;
48 import javax.swing.JButton JavaDoc;
49 import javax.swing.JComboBox JavaDoc;
50 import javax.swing.JDialog JavaDoc;
51 import javax.swing.JFrame JavaDoc;
52 import javax.swing.JLabel JavaDoc;
53 import javax.swing.JPanel JavaDoc;
54 import javax.swing.JPasswordField JavaDoc;
55 import javax.swing.JTextField JavaDoc;
56 import javax.swing.SwingUtilities JavaDoc;
57 import javax.swing.border.EmptyBorder JavaDoc;
58
59 // sqlbob@users 20020325 - patch 1.7.0 - enhancements
60
// sqlbob@users 20020407 - patch 1.7.0 - reengineering
61
// weconsultants@users 20041109 - patch 1.8.0 - enhancements:
62
// Added CommonSwing.errorMessage() to handle error messages
63
// for errors so eliminated the mError JLable field and Status HorizontalBox.
64
// Changed dispose on cancel to exit. If "Dup", "Restore" or Transer" needed ust
65
// Press <OK> Conform toprogramming standards
66
// Added spaces to "OK" button to make same size buttons
67
// Added ":" to all labels as in databaseManager.java
68
// Added: Added code from DatabaseManager to store connection settings
69

70 /**
71  * Opens a connection to a database
72  *
73  * New class based on Hypersonic original
74  *
75  * @author dmarshall@users
76  * @version 1.7.2
77  * @since 1.7.0
78  */

79 class ConnectionDialogSwing extends JDialog JavaDoc
80 implements ActionListener JavaDoc, ItemListener JavaDoc {
81
82     /**
83      * Comment for <code>serialVersionUID</code>
84      */

85     private static final long serialVersionUID = 1L;
86     private Connection JavaDoc mConnection;
87     private JTextField JavaDoc mName, mDriver, mURL, mUser;
88     private JPasswordField JavaDoc mPassword;
89     private String JavaDoc[][] connTypes;
90     private Hashtable JavaDoc settings;
91     private JButton JavaDoc okCancel, clear;
92     private JComboBox JavaDoc mSettingName =
93         new JComboBox JavaDoc(loadRecentConnectionSettings());
94     private static ConnectionSetting currentConnectionSetting = null;
95
96     public static void setConnectionSetting(
97             ConnectionSetting connectionSetting) {
98         currentConnectionSetting = connectionSetting;
99     }
100
101     public static Connection JavaDoc createConnection(String JavaDoc driver, String JavaDoc url,
102             String JavaDoc user, String JavaDoc password) throws Exception JavaDoc {
103
104         Class.forName(driver).newInstance();
105
106         return DriverManager.getConnection(url, user, password);
107     }
108
109     ConnectionDialogSwing(JFrame JavaDoc owner, String JavaDoc title) {
110         super(owner, title, true);
111     }
112
113     private void create() {
114
115         Box JavaDoc main = Box.createHorizontalBox();
116         Box JavaDoc labels = Box.createVerticalBox();
117         Box JavaDoc controls = Box.createVerticalBox();
118         Box JavaDoc buttons = Box.createHorizontalBox();
119         Box JavaDoc whole = Box.createVerticalBox();
120
121         // (weconsultants@users) New code
122
Box JavaDoc extra = Box.createHorizontalBox();
123
124         main.add(Box.createHorizontalStrut(10));
125         main.add(Box.createHorizontalGlue());
126         main.add(labels);
127         main.add(Box.createHorizontalStrut(10));
128         main.add(Box.createHorizontalGlue());
129         main.add(controls);
130         main.add(Box.createHorizontalStrut(10));
131         main.add(Box.createVerticalGlue());
132         main.add(extra);
133         main.add(Box.createVerticalGlue());
134         whole.add(Box.createVerticalGlue());
135         whole.add(Box.createVerticalStrut(10));
136         whole.add(main);
137         whole.add(Box.createVerticalGlue());
138         whole.add(Box.createVerticalStrut(10));
139         whole.add(buttons);
140         whole.add(Box.createVerticalGlue());
141         whole.add(Box.createVerticalStrut(10));
142         whole.add(Box.createVerticalGlue());
143         labels.add(createLabel("Recent Setting:"));
144         labels.add(Box.createVerticalGlue());
145         labels.add(createLabel("Setting Name:"));
146         labels.add(Box.createVerticalGlue());
147         labels.add(createLabel("Type:"));
148         labels.add(Box.createVerticalGlue());
149         labels.add(createLabel("Driver:"));
150         labels.add(Box.createVerticalGlue());
151         labels.add(createLabel("URL:"));
152         labels.add(Box.createVerticalGlue());
153         labels.add(createLabel("User:"));
154         labels.add(Box.createVerticalGlue());
155         labels.add(createLabel("Password:"));
156         labels.add(Box.createVerticalGlue());
157         labels.add(Box.createVerticalStrut(10));
158         controls.add(Box.createVerticalGlue());
159
160         // (weconsultants@users) New code
161
mSettingName.setActionCommand("Select Setting");
162         mSettingName.addActionListener(this);
163         controls.add(mSettingName);
164         controls.add(Box.createHorizontalGlue());
165
166         // (weconsultants@users) New code
167
mName = new JTextField JavaDoc();
168
169         mName.addActionListener(this);
170         controls.add(mName);
171
172         // (weconsultants@users) New code
173
clear = new JButton JavaDoc("Clear Names");
174
175         clear.setActionCommand("Clear");
176         clear.addActionListener(this);
177         buttons.add(clear);
178         buttons.add(Box.createHorizontalGlue());
179         buttons.add(Box.createHorizontalStrut(10));
180
181         JComboBox JavaDoc types = new JComboBox JavaDoc();
182
183         connTypes = ConnectionDialogCommon.getTypes();
184
185         for (int i = 0; i < connTypes.length; i++) {
186             types.addItem(connTypes[i][0]);
187         }
188
189         types.addItemListener(this);
190         controls.add(types);
191         controls.add(Box.createVerticalGlue());
192
193         mDriver = new JTextField JavaDoc(connTypes[0][1]);
194
195         mDriver.addActionListener(this);
196         controls.add(mDriver);
197
198         mURL = new JTextField JavaDoc(connTypes[0][2]);
199
200         mURL.addActionListener(this);
201         controls.add(mURL);
202         controls.add(Box.createVerticalGlue());
203
204         mUser = new JTextField JavaDoc("sa");
205
206         mUser.addActionListener(this);
207         controls.add(mUser);
208         controls.add(Box.createVerticalGlue());
209
210         mPassword = new JPasswordField JavaDoc("");
211
212         mPassword.addActionListener(this);
213         controls.add(mPassword);
214         controls.add(Box.createVerticalGlue());
215         controls.add(Box.createVerticalStrut(10));
216
217         // The button bar
218
buttons.add(Box.createHorizontalGlue());
219         buttons.add(Box.createHorizontalStrut(10));
220
221         okCancel = new JButton JavaDoc(" Ok ");
222
223         okCancel.setActionCommand("ConnectOk");
224         okCancel.addActionListener(this);
225         buttons.add(okCancel);
226         getRootPane().setDefaultButton(okCancel);
227         buttons.add(Box.createHorizontalGlue());
228         buttons.add(Box.createHorizontalStrut(20));
229
230         okCancel = new JButton JavaDoc(" Cancel ");
231
232         okCancel.setActionCommand("ConnectCancel");
233         okCancel.addActionListener(this);
234         buttons.add(okCancel);
235         buttons.add(Box.createHorizontalGlue());
236         buttons.add(Box.createHorizontalStrut(10));
237
238         JPanel JavaDoc jPanel = new JPanel JavaDoc();
239
240         jPanel.setBorder(new EmptyBorder JavaDoc(10, 10, 10, 10));
241         jPanel.add("Center", whole);
242         getContentPane().add("Center", jPanel);
243         doLayout();
244         pack();
245
246         Dimension JavaDoc d = Toolkit.getDefaultToolkit().getScreenSize();
247         Dimension JavaDoc size = getSize();
248
249         if (currentConnectionSetting != null) {
250             mName.setText(currentConnectionSetting.getName());
251             mDriver.setText(currentConnectionSetting.getDriver());
252             mURL.setText(currentConnectionSetting.getUrl());
253             mUser.setText(currentConnectionSetting.getUser());
254             mPassword.setText(currentConnectionSetting.getPassword());
255         }
256
257         // (ulrivo): full size on screen with less than 640 width
258
if (d.width >= 640) {
259             setLocation((d.width - size.width) / 2,
260                         (d.height - size.height) / 2);
261         } else {
262             setLocation(0, 0);
263             setSize(d);
264         }
265
266         setVisible(true);
267     }
268
269     public static Connection JavaDoc createConnection(JFrame JavaDoc owner, String JavaDoc title) {
270
271         ConnectionDialogSwing dialog = new ConnectionDialogSwing(owner,
272             title);
273
274 // Added: (weconsultants@users) Default LAF of Native
275
try {
276
277 // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
278
SwingUtilities.updateComponentTreeUI(dialog);
279         } catch (Exception JavaDoc e) {
280             CommonSwing.errorMessage(e);
281         }
282
283         dialog.create();
284
285         return dialog.mConnection;
286     }
287
288     private static JLabel JavaDoc createLabel(String JavaDoc s) {
289
290         JLabel JavaDoc l = new JLabel JavaDoc(s);
291
292         return l;
293     }
294
295     // (weconsultants@users) New code
296
public Vector JavaDoc loadRecentConnectionSettings() {
297
298         Vector JavaDoc passSettings = new Vector JavaDoc();
299
300         settings = new Hashtable JavaDoc();
301
302         try {
303             settings = ConnectionDialogCommon.loadRecentConnectionSettings();
304
305             Iterator JavaDoc it = settings.values().iterator();
306
307             passSettings.add(ConnectionDialogCommon.emptySettingName);
308
309             while (it.hasNext()) {
310                 passSettings.add(((ConnectionSetting) it.next()).getName());
311             }
312         } catch (java.io.IOException JavaDoc ioe) {
313             CommonSwing.errorMessage(ioe);
314         }
315
316         return (passSettings);
317     }
318
319     public void actionPerformed(ActionEvent JavaDoc ev) {
320
321         String JavaDoc s = ev.getActionCommand();
322
323         if (s.equals("ConnectOk") || (ev.getSource() instanceof JTextField JavaDoc)) {
324             try {
325                 if (mURL.getText().indexOf('\u00AB') >= 0) {
326                     throw new Exception JavaDoc("please specify db path");
327                 }
328
329                 mConnection =
330                     createConnection(mDriver.getText(), mURL.getText(),
331                                      mUser.getText(),
332                                      new String JavaDoc(mPassword.getPassword()));
333
334                 // (weconsultants@users) New code
335
if (mName.getText() != null
336                         && mName.getText().trim().length() != 0) {
337                     ConnectionSetting newSetting = new ConnectionSetting(
338                         mName.getText(), mDriver.getText(), mURL.getText(),
339                         mUser.getText(), new String JavaDoc(mPassword.getPassword()));
340
341                     ConnectionDialogCommon.addToRecentConnectionSettings(
342                         settings, newSetting);
343                 }
344
345                 dispose();
346             } catch (SQLException JavaDoc e) {
347                 mConnection = null;
348
349                 CommonSwing.errorMessage(e, true);
350             } catch (Exception JavaDoc e) {
351
352                 // Added: (weconsultants@users)
353
CommonSwing.errorMessage(e);
354             }
355
356             // (weconsultants@users) New code
357
} else if (s.equals("Select Setting")) {
358             String JavaDoc s2 = (String JavaDoc) mSettingName.getSelectedItem();
359             ConnectionSetting setting = (ConnectionSetting) settings.get(s2);
360
361             if (setting != null) {
362                 mName.setText(setting.getName());
363                 mDriver.setText(setting.getDriver());
364                 mURL.setText(setting.getUrl());
365                 mUser.setText(setting.getUser());
366                 mPassword.setText(setting.getPassword());
367             }
368         } else if (s.equals("ConnectCancel")) {
369             dispose();
370
371             // (weconsultants@users) New code
372
} else if (s.equals("Clear")) {
373             ConnectionDialogCommon.deleteRecentConnectionSettings();
374
375             settings = new Hashtable JavaDoc();
376
377             mSettingName.removeAllItems();
378             mSettingName.addItem(ConnectionDialogCommon.emptySettingName);
379             mName.setText(null);
380         }
381     }
382
383     public void itemStateChanged(ItemEvent JavaDoc e) {
384
385         String JavaDoc s = (String JavaDoc) e.getItem();
386
387         for (int i = 0; i < connTypes.length; i++) {
388             if (s.equals(connTypes[i][0])) {
389                 mDriver.setText(connTypes[i][1]);
390                 mURL.setText(connTypes[i][2]);
391             }
392         }
393     }
394 }
395
Popular Tags