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.sql.SQLException;
036: import java.util.Hashtable;
037: import java.util.Iterator;
038: import java.util.Vector;
039: import java.awt.Dimension;
040: import java.awt.Toolkit;
041: import java.awt.event.ActionEvent;
042: import java.awt.event.ActionListener;
043: import java.awt.event.ItemEvent;
044: import java.awt.event.ItemListener;
045:
046: import javax.swing.Box;
047: import javax.swing.JButton;
048: import javax.swing.JComboBox;
049: import javax.swing.JDialog;
050: import javax.swing.JFrame;
051: import javax.swing.JLabel;
052: import javax.swing.JPanel;
053: import javax.swing.JPasswordField;
054: import javax.swing.JTextField;
055: import javax.swing.SwingUtilities;
056: import javax.swing.border.EmptyBorder;
057:
058: // sqlbob@users 20020325 - patch 1.7.0 - enhancements
059: // sqlbob@users 20020407 - patch 1.7.0 - reengineering
060: // weconsultants@users 20041109 - patch 1.8.0 - enhancements:
061: // Added CommonSwing.errorMessage() to handle error messages
062: // for errors so eliminated the mError JLable field and Status HorizontalBox.
063: // Changed dispose on cancel to exit. If "Dup", "Restore" or Transer" needed ust
064: // Press <OK> Conform toprogramming standards
065: // Added spaces to "OK" button to make same size buttons
066: // Added ":" to all labels as in databaseManager.java
067: // Added: Added code from DatabaseManager to store connection settings
068:
069: /**
070: * Opens a connection to a database
071: *
072: * New class based on Hypersonic original
073: *
074: * @author dmarshall@users
075: * @version 1.7.2
076: * @since 1.7.0
077: */
078: class ConnectionDialogSwing extends JDialog implements ActionListener,
079: ItemListener {
080:
081: /**
082: * Comment for <code>serialVersionUID</code>
083: */
084: private static final long serialVersionUID = 1L;
085: private Connection mConnection;
086: private JTextField mName, mDriver, mURL, mUser;
087: private JPasswordField mPassword;
088: private String[][] connTypes;
089: private Hashtable settings;
090: private JButton okCancel, clear;
091: private JComboBox mSettingName = new JComboBox(
092: loadRecentConnectionSettings());
093: private static ConnectionSetting currentConnectionSetting = null;
094:
095: public static void setConnectionSetting(
096: ConnectionSetting connectionSetting) {
097: currentConnectionSetting = connectionSetting;
098: }
099:
100: public static Connection createConnection(String driver,
101: String url, String user, String password) throws Exception {
102:
103: Class.forName(driver).newInstance();
104:
105: return DriverManager.getConnection(url, user, password);
106: }
107:
108: ConnectionDialogSwing(JFrame owner, String title) {
109: super (owner, title, true);
110: }
111:
112: private void create() {
113:
114: Box main = Box.createHorizontalBox();
115: Box labels = Box.createVerticalBox();
116: Box controls = Box.createVerticalBox();
117: Box buttons = Box.createHorizontalBox();
118: Box whole = Box.createVerticalBox();
119:
120: // (weconsultants@users) New code
121: Box extra = Box.createHorizontalBox();
122:
123: main.add(Box.createHorizontalStrut(10));
124: main.add(Box.createHorizontalGlue());
125: main.add(labels);
126: main.add(Box.createHorizontalStrut(10));
127: main.add(Box.createHorizontalGlue());
128: main.add(controls);
129: main.add(Box.createHorizontalStrut(10));
130: main.add(Box.createVerticalGlue());
131: main.add(extra);
132: main.add(Box.createVerticalGlue());
133: whole.add(Box.createVerticalGlue());
134: whole.add(Box.createVerticalStrut(10));
135: whole.add(main);
136: whole.add(Box.createVerticalGlue());
137: whole.add(Box.createVerticalStrut(10));
138: whole.add(buttons);
139: whole.add(Box.createVerticalGlue());
140: whole.add(Box.createVerticalStrut(10));
141: whole.add(Box.createVerticalGlue());
142: labels.add(createLabel("Recent Setting:"));
143: labels.add(Box.createVerticalGlue());
144: labels.add(createLabel("Setting Name:"));
145: labels.add(Box.createVerticalGlue());
146: labels.add(createLabel("Type:"));
147: labels.add(Box.createVerticalGlue());
148: labels.add(createLabel("Driver:"));
149: labels.add(Box.createVerticalGlue());
150: labels.add(createLabel("URL:"));
151: labels.add(Box.createVerticalGlue());
152: labels.add(createLabel("User:"));
153: labels.add(Box.createVerticalGlue());
154: labels.add(createLabel("Password:"));
155: labels.add(Box.createVerticalGlue());
156: labels.add(Box.createVerticalStrut(10));
157: controls.add(Box.createVerticalGlue());
158:
159: // (weconsultants@users) New code
160: mSettingName.setActionCommand("Select Setting");
161: mSettingName.addActionListener(this );
162: controls.add(mSettingName);
163: controls.add(Box.createHorizontalGlue());
164:
165: // (weconsultants@users) New code
166: mName = new JTextField();
167:
168: mName.addActionListener(this );
169: controls.add(mName);
170:
171: // (weconsultants@users) New code
172: clear = new JButton("Clear Names");
173:
174: clear.setActionCommand("Clear");
175: clear.addActionListener(this );
176: buttons.add(clear);
177: buttons.add(Box.createHorizontalGlue());
178: buttons.add(Box.createHorizontalStrut(10));
179:
180: JComboBox types = new JComboBox();
181:
182: connTypes = ConnectionDialogCommon.getTypes();
183:
184: for (int i = 0; i < connTypes.length; i++) {
185: types.addItem(connTypes[i][0]);
186: }
187:
188: types.addItemListener(this );
189: controls.add(types);
190: controls.add(Box.createVerticalGlue());
191:
192: mDriver = new JTextField(connTypes[0][1]);
193:
194: mDriver.addActionListener(this );
195: controls.add(mDriver);
196:
197: mURL = new JTextField(connTypes[0][2]);
198:
199: mURL.addActionListener(this );
200: controls.add(mURL);
201: controls.add(Box.createVerticalGlue());
202:
203: mUser = new JTextField("sa");
204:
205: mUser.addActionListener(this );
206: controls.add(mUser);
207: controls.add(Box.createVerticalGlue());
208:
209: mPassword = new JPasswordField("");
210:
211: mPassword.addActionListener(this );
212: controls.add(mPassword);
213: controls.add(Box.createVerticalGlue());
214: controls.add(Box.createVerticalStrut(10));
215:
216: // The button bar
217: buttons.add(Box.createHorizontalGlue());
218: buttons.add(Box.createHorizontalStrut(10));
219:
220: okCancel = new JButton(" Ok ");
221:
222: okCancel.setActionCommand("ConnectOk");
223: okCancel.addActionListener(this );
224: buttons.add(okCancel);
225: getRootPane().setDefaultButton(okCancel);
226: buttons.add(Box.createHorizontalGlue());
227: buttons.add(Box.createHorizontalStrut(20));
228:
229: okCancel = new JButton(" Cancel ");
230:
231: okCancel.setActionCommand("ConnectCancel");
232: okCancel.addActionListener(this );
233: buttons.add(okCancel);
234: buttons.add(Box.createHorizontalGlue());
235: buttons.add(Box.createHorizontalStrut(10));
236:
237: JPanel jPanel = new JPanel();
238:
239: jPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
240: jPanel.add("Center", whole);
241: getContentPane().add("Center", jPanel);
242: doLayout();
243: pack();
244:
245: Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
246: Dimension size = getSize();
247:
248: if (currentConnectionSetting != null) {
249: mName.setText(currentConnectionSetting.getName());
250: mDriver.setText(currentConnectionSetting.getDriver());
251: mURL.setText(currentConnectionSetting.getUrl());
252: mUser.setText(currentConnectionSetting.getUser());
253: mPassword.setText(currentConnectionSetting.getPassword());
254: }
255:
256: // (ulrivo): full size on screen with less than 640 width
257: if (d.width >= 640) {
258: setLocation((d.width - size.width) / 2,
259: (d.height - size.height) / 2);
260: } else {
261: setLocation(0, 0);
262: setSize(d);
263: }
264:
265: setVisible(true);
266: }
267:
268: public static Connection createConnection(JFrame owner, String title) {
269:
270: ConnectionDialogSwing dialog = new ConnectionDialogSwing(owner,
271: title);
272:
273: // Added: (weconsultants@users) Default LAF of Native
274: try {
275:
276: // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
277: SwingUtilities.updateComponentTreeUI(dialog);
278: } catch (Exception e) {
279: CommonSwing.errorMessage(e);
280: }
281:
282: dialog.create();
283:
284: return dialog.mConnection;
285: }
286:
287: private static JLabel createLabel(String s) {
288:
289: JLabel l = new JLabel(s);
290:
291: return l;
292: }
293:
294: // (weconsultants@users) New code
295: public Vector loadRecentConnectionSettings() {
296:
297: Vector passSettings = new Vector();
298:
299: settings = new Hashtable();
300:
301: try {
302: settings = ConnectionDialogCommon
303: .loadRecentConnectionSettings();
304:
305: Iterator it = settings.values().iterator();
306:
307: passSettings.add(ConnectionDialogCommon.emptySettingName);
308:
309: while (it.hasNext()) {
310: passSettings.add(((ConnectionSetting) it.next())
311: .getName());
312: }
313: } catch (java.io.IOException ioe) {
314: CommonSwing.errorMessage(ioe);
315: }
316:
317: return (passSettings);
318: }
319:
320: public void actionPerformed(ActionEvent ev) {
321:
322: String s = ev.getActionCommand();
323:
324: if (s.equals("ConnectOk")
325: || (ev.getSource() instanceof JTextField)) {
326: try {
327: if (mURL.getText().indexOf('\u00AB') >= 0) {
328: throw new Exception("please specify db path");
329: }
330:
331: mConnection = createConnection(mDriver.getText(), mURL
332: .getText(), mUser.getText(), new String(
333: mPassword.getPassword()));
334:
335: // (weconsultants@users) New code
336: if (mName.getText() != null
337: && mName.getText().trim().length() != 0) {
338: ConnectionSetting newSetting = new ConnectionSetting(
339: mName.getText(), mDriver.getText(), mURL
340: .getText(), mUser.getText(),
341: new String(mPassword.getPassword()));
342:
343: ConnectionDialogCommon
344: .addToRecentConnectionSettings(settings,
345: newSetting);
346: }
347:
348: dispose();
349: } catch (SQLException e) {
350: mConnection = null;
351:
352: CommonSwing.errorMessage(e, true);
353: } catch (Exception e) {
354:
355: // Added: (weconsultants@users)
356: CommonSwing.errorMessage(e);
357: }
358:
359: // (weconsultants@users) New code
360: } else if (s.equals("Select Setting")) {
361: String s2 = (String) mSettingName.getSelectedItem();
362: ConnectionSetting setting = (ConnectionSetting) settings
363: .get(s2);
364:
365: if (setting != null) {
366: mName.setText(setting.getName());
367: mDriver.setText(setting.getDriver());
368: mURL.setText(setting.getUrl());
369: mUser.setText(setting.getUser());
370: mPassword.setText(setting.getPassword());
371: }
372: } else if (s.equals("ConnectCancel")) {
373: dispose();
374:
375: // (weconsultants@users) New code
376: } else if (s.equals("Clear")) {
377: ConnectionDialogCommon.deleteRecentConnectionSettings();
378:
379: settings = new Hashtable();
380:
381: mSettingName.removeAllItems();
382: mSettingName
383: .addItem(ConnectionDialogCommon.emptySettingName);
384: mName.setText(null);
385: }
386: }
387:
388: public void itemStateChanged(ItemEvent e) {
389:
390: String s = (String) e.getItem();
391:
392: for (int i = 0; i < connTypes.length; i++) {
393: if (s.equals(connTypes[i][0])) {
394: mDriver.setText(connTypes[i][1]);
395: mURL.setText(connTypes[i][2]);
396: }
397: }
398: }
399: }
|