001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.util;
032:
033: import java.sql.Connection;
034: import java.sql.DriverManager;
035: import java.util.Enumeration;
036: import java.util.Hashtable;
037: import java.awt.BorderLayout;
038: import java.awt.Button;
039: import java.awt.Choice;
040: import java.awt.Component;
041: import java.awt.Dialog;
042: import java.awt.Dimension;
043: import java.awt.Frame;
044: import java.awt.GridLayout;
045: import java.awt.Label;
046: import java.awt.Panel;
047: import java.awt.SystemColor;
048: import java.awt.TextField;
049: import java.awt.Toolkit;
050: import java.awt.event.ActionEvent;
051: import java.awt.event.ActionListener;
052: import java.awt.event.ItemEvent;
053: import java.awt.event.ItemListener;
054:
055: // sqlbob@users 20020325 - patch 1.7.0 - enhancements
056: // sqlbob@users 20020407 - patch 1.7.0 - reengineering
057: // fredt@users - 20040508 - modified patch by lonbinder@users for saving settings
058:
059: /**
060: * Opens a connection to a database
061: *
062: * @author Thomas Mueller (Hypersonic SQL Group)
063: * @version 1.7.2
064: * @since Hypersonic SQL
065: */
066: class ConnectionDialog extends Dialog implements ActionListener,
067: ItemListener {
068:
069: protected Connection mConnection;
070: protected TextField mName, mDriver, mURL, mUser, mPassword;
071: protected Label mError;
072: private String[][] connTypes;
073: private Hashtable settings;
074: private Choice types, recent;
075:
076: /**
077: * Method declaration
078: *
079: *
080: * @param driver
081: * @param url
082: * @param user
083: * @param password
084: *
085: * @return
086: *
087: * @throws Exception
088: */
089: public static Connection createConnection(String driver,
090: String url, String user, String password) throws Exception {
091:
092: Class.forName(driver).newInstance();
093:
094: return DriverManager.getConnection(url, user, password);
095: }
096:
097: /**
098: * Constructor declaration
099: *
100: *
101: * @param owner
102: * @param title
103: */
104: ConnectionDialog(Frame owner, String title) {
105: super (owner, title, true);
106: }
107:
108: /**
109: * Method declaration
110: *
111: */
112: private void create() {
113:
114: Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
115:
116: setLayout(new BorderLayout());
117:
118: Panel p = new Panel(new BorderLayout());
119: Panel pLabel;
120: Panel pText;
121: Panel pButton;
122: Panel pClearButton;
123:
124: // (ulrivo): full size on screen with less than 640 width
125: if (d.width >= 640) {
126: pLabel = new Panel(new GridLayout(8, 1, 10, 10));
127: pText = new Panel(new GridLayout(8, 1, 10, 10));
128: pButton = new Panel(new GridLayout(1, 2, 10, 10));
129: pClearButton = new Panel(new GridLayout(8, 1, 10, 10));
130: } else {
131: pLabel = new Panel(new GridLayout(8, 1));
132: pText = new Panel(new GridLayout(8, 1));
133: pButton = new Panel(new GridLayout(1, 2));
134: pClearButton = new Panel(new GridLayout(8, 1));
135: }
136:
137: p.add("West", pLabel);
138: p.add("Center", pText);
139: p.add("South", pButton);
140: p.add("North", createLabel(""));
141: p.add("East", pClearButton);
142: p.setBackground(SystemColor.control);
143: pText.setBackground(SystemColor.control);
144: pLabel.setBackground(SystemColor.control);
145: pButton.setBackground(SystemColor.control);
146: pLabel.add(createLabel("Recent:"));
147:
148: recent = new Choice();
149:
150: try {
151: settings = ConnectionDialogCommon
152: .loadRecentConnectionSettings();
153: } catch (java.io.IOException ioe) {
154: ioe.printStackTrace();
155: }
156:
157: recent.add(ConnectionDialogCommon.emptySettingName);
158:
159: Enumeration en = settings.elements();
160:
161: while (en.hasMoreElements()) {
162: recent
163: .add(((ConnectionSetting) en.nextElement())
164: .getName());
165: }
166:
167: recent.addItemListener(new ItemListener() {
168:
169: public void itemStateChanged(ItemEvent e) {
170:
171: String s = (String) e.getItem();
172: ConnectionSetting setting = (ConnectionSetting) settings
173: .get(s);
174:
175: if (setting != null) {
176: mName.setText(setting.getName());
177: mDriver.setText(setting.getDriver());
178: mURL.setText(setting.getUrl());
179: mUser.setText(setting.getUser());
180: mPassword.setText(setting.getPassword());
181: }
182: }
183: });
184: pText.add(recent);
185:
186: Button b;
187:
188: b = new Button("Clr");
189:
190: b.setActionCommand("Clear");
191: b.addActionListener(new ActionListener() {
192:
193: public void actionPerformed(ActionEvent e) {
194:
195: ConnectionDialogCommon.deleteRecentConnectionSettings();
196:
197: settings = new Hashtable();
198:
199: recent.removeAll();
200: recent.add(ConnectionDialogCommon.emptySettingName);
201: mName.setText(null);
202: }
203: });
204: pClearButton.add(b);
205: pLabel.add(createLabel("Setting Name:"));
206:
207: mName = new TextField("");
208:
209: pText.add(mName);
210: pLabel.add(createLabel("Type:"));
211:
212: types = new Choice();
213: connTypes = ConnectionDialogCommon.getTypes();
214:
215: for (int i = 0; i < connTypes.length; i++) {
216: types.add(connTypes[i][0]);
217: }
218:
219: types.addItemListener(this );
220: pText.add(types);
221: pLabel.add(createLabel("Driver:"));
222:
223: mDriver = new TextField(connTypes[0][1]);
224:
225: pText.add(mDriver);
226: pLabel.add(createLabel("URL:"));
227:
228: mURL = new TextField(connTypes[0][2]);
229:
230: mURL.addActionListener(this );
231: pText.add(mURL);
232: pLabel.add(createLabel("User:"));
233:
234: mUser = new TextField("sa");
235:
236: mUser.addActionListener(this );
237: pText.add(mUser);
238: pLabel.add(createLabel("Password:"));
239:
240: mPassword = new TextField("");
241:
242: mPassword.addActionListener(this );
243: mPassword.setEchoChar('*');
244: pText.add(mPassword);
245:
246: b = new Button("Ok");
247:
248: b.setActionCommand("ConnectOk");
249: b.addActionListener(this );
250: pButton.add(b);
251:
252: b = new Button("Cancel");
253:
254: b.setActionCommand("ConnectCancel");
255: b.addActionListener(this );
256: pButton.add(b);
257: add("East", createLabel(""));
258: add("West", createLabel(""));
259:
260: mError = new Label("");
261:
262: Panel pMessage = createBorderPanel(mError);
263:
264: add("South", pMessage);
265: add("North", createLabel(""));
266: add("Center", p);
267: doLayout();
268: pack();
269:
270: Dimension size = getSize();
271:
272: // (ulrivo): full size on screen with less than 640 width
273: if (d.width >= 640) {
274: setLocation((d.width - size.width) / 2,
275: (d.height - size.height) / 2);
276: } else {
277: setLocation(0, 0);
278: setSize(d);
279: }
280:
281: show();
282: }
283:
284: /**
285: * Method declaration
286: *
287: *
288: * @param owner
289: * @param title
290: *
291: * @return
292: */
293: public static Connection createConnection(Frame owner, String title) {
294:
295: ConnectionDialog dialog = new ConnectionDialog(owner, title);
296:
297: dialog.create();
298:
299: return dialog.mConnection;
300: }
301:
302: /**
303: * Method declaration
304: *
305: *
306: * @param s
307: *
308: * @return
309: */
310: protected static Label createLabel(String s) {
311:
312: Label l = new Label(s);
313:
314: l.setBackground(SystemColor.control);
315:
316: return l;
317: }
318:
319: /**
320: * Method declaration
321: *
322: *
323: * @param center
324: *
325: * @return
326: */
327: protected static Panel createBorderPanel(Component center) {
328:
329: Panel p = new Panel();
330:
331: p.setBackground(SystemColor.control);
332: p.setLayout(new BorderLayout());
333: p.add("Center", center);
334: p.add("North", createLabel(""));
335: p.add("South", createLabel(""));
336: p.add("East", createLabel(""));
337: p.add("West", createLabel(""));
338: p.setBackground(SystemColor.control);
339:
340: return p;
341: }
342:
343: /**
344: * Method declaration
345: *
346: *
347: * @param ev
348: */
349: public void actionPerformed(ActionEvent ev) {
350:
351: String s = ev.getActionCommand();
352:
353: if (s.equals("ConnectOk")
354: || (ev.getSource() instanceof TextField)) {
355: try {
356: if (mURL.getText().indexOf('\u00AB') >= 0) {
357: throw new Exception("please specify db path");
358: }
359:
360: mConnection = createConnection(mDriver.getText(), mURL
361: .getText(), mUser.getText(), mPassword
362: .getText());
363:
364: if (mName.getText() != null
365: && mName.getText().trim().length() != 0) {
366: ConnectionSetting newSetting = new ConnectionSetting(
367: mName.getText(), mDriver.getText(), mURL
368: .getText(), mUser.getText(),
369: mPassword.getText());
370:
371: ConnectionDialogCommon
372: .addToRecentConnectionSettings(settings,
373: newSetting);
374: }
375:
376: dispose();
377: } catch (java.io.IOException ioe) {
378: dispose();
379: } catch (Exception e) {
380: e.printStackTrace();
381: mError.setText(e.toString());
382: }
383: } else if (s.equals("ConnectCancel")) {
384: dispose();
385: }
386: }
387:
388: /**
389: * Method declaration
390: *
391: *
392: * @param e
393: */
394: public void itemStateChanged(ItemEvent e) {
395:
396: String s = (String) e.getItem();
397:
398: for (int i = 0; i < connTypes.length; i++) {
399: if (s.equals(connTypes[i][0])) {
400: mDriver.setText(connTypes[i][1]);
401: mURL.setText(connTypes[i][2]);
402: }
403: }
404: }
405: }
|