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.awt.BorderLayout;
035: import java.awt.Button;
036: import java.awt.Choice;
037: import java.awt.Dimension;
038: import java.awt.Frame;
039: import java.awt.GridLayout;
040: import java.awt.Insets;
041: import java.awt.Label;
042: import java.awt.Panel;
043: import java.awt.SystemColor;
044: import java.awt.TextField;
045: import java.awt.Toolkit;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import java.awt.event.ItemEvent;
049: import java.awt.event.ItemListener;
050: import java.awt.event.KeyEvent;
051: import java.awt.event.KeyListener;
052: import java.awt.event.WindowEvent;
053:
054: /**
055: * Class declaration
056: *
057: *
058: * @author ulrivo@users
059: * @version 1.0.0.1
060: */
061: public class ZaurusConnectionDialog extends ConnectionDialog implements
062: ActionListener, ItemListener, KeyListener {
063:
064: static final String[][] sJDBCTypes = {
065: { "HSQL In-Memory", "org.hsqldb.jdbcDriver",
066: "jdbc:hsqldb:." },
067: { "HSQL Standalone", "org.hsqldb.jdbcDriver",
068: "jdbc:hsqldb:test" },
069: { "MM.MySQL", "org.gjt.mm.mysql.Driver",
070: "jdbc:mysql://localhost/" },
071: { "JDBC-ODBC Brigde from Sun",
072: "sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:test" },
073: { "Oracle", "oracle.jdbc.driver.OracleDriver",
074: "jdbc:oracle:oci8:@" },
075: { "IBM DB2", "COM.ibm.db2.jdbc.app.DB2Driver",
076: "jdbc:db2:test" },
077: { "Cloudscape RMI", "RmiJdbc.RJDriver",
078: "jdbc:rmi://localhost:1099/jdbc:cloudscape:test;create=true" },
079: { "InstantDb", "jdbc.idbDriver", "jdbc:idb:sample.prp" },
080: { "PointBase", "com.pointbase.jdbc.jdbcUniversalDriver",
081: "jdbc:pointbase://localhost/sample" }, // PUBLIC / public
082: };
083:
084: /**
085: * Constructor declaration
086: *
087: *
088: * @param owner
089: * @param title
090: */
091: ZaurusConnectionDialog(Frame owner, String title) {
092:
093: super (owner, title);
094:
095: addKeyListener(this );
096: }
097:
098: /**
099: * Method declaration
100: *
101: */
102: void create(Insets defInsets) {
103:
104: setLayout(new BorderLayout());
105: addKeyListener(this );
106:
107: Panel p = new Panel(new GridLayout(6, 2, 10, 10));
108:
109: p.addKeyListener(this );
110: p.setBackground(SystemColor.control);
111: p.add(createLabel("Type:"));
112:
113: Choice types = new Choice();
114:
115: types.addItemListener(this );
116: types.addKeyListener(this );
117:
118: for (int i = 0; i < sJDBCTypes.length; i++) {
119: types.add(sJDBCTypes[i][0]);
120: }
121:
122: p.add(types);
123: p.add(createLabel("Driver:"));
124:
125: mDriver = new TextField("org.hsqldb.jdbcDriver");
126:
127: mDriver.addKeyListener(this );
128: p.add(mDriver);
129: p.add(createLabel("URL:"));
130:
131: mURL = new TextField("jdbc:hsqldb:.");
132:
133: mURL.addKeyListener(this );
134: p.add(mURL);
135: p.add(createLabel("User:"));
136:
137: mUser = new TextField("sa");
138:
139: mUser.addKeyListener(this );
140: p.add(mUser);
141: p.add(createLabel("Password:"));
142:
143: mPassword = new TextField("");
144:
145: mPassword.addKeyListener(this );
146: mPassword.setEchoChar('*');
147: p.add(mPassword);
148:
149: Button b;
150:
151: b = new Button("Cancel");
152:
153: b.setActionCommand("ConnectCancel");
154: b.addActionListener(this );
155: b.addKeyListener(this );
156: p.add(b);
157:
158: b = new Button("Ok");
159:
160: b.setActionCommand("ConnectOk");
161: b.addActionListener(this );
162: b.addKeyListener(this );
163: p.add(b);
164: setLayout(new BorderLayout());
165: add("East", createLabel(" "));
166: add("West", createLabel(" "));
167:
168: mError = new Label("");
169:
170: Panel pMessage = createBorderPanel(mError);
171:
172: pMessage.addKeyListener(this );
173: add("South", pMessage);
174: add("North", createLabel(""));
175: add("Center", p);
176: doLayout();
177: pack();
178:
179: Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
180: Dimension size = getSize();
181:
182: if (d.width > 640) {
183: setLocation((d.width - size.width) / 2,
184: (d.height - size.height) / 2);
185: } else if (defInsets.top > 0 && defInsets.left > 0) {
186: setLocation(defInsets.bottom, defInsets.right);
187: setSize(defInsets.top, defInsets.left);
188:
189: // full size on screen with less than 640 width
190: } else {
191: setLocation(0, 0);
192: setSize(d);
193: }
194:
195: show();
196: }
197:
198: /**
199: * Method declaration
200: *
201: *
202: * @param ev
203: */
204: public void actionPerformed(ActionEvent ev) {
205:
206: String s = ev.getActionCommand();
207:
208: // System.out.println("Action performed " + s);
209: if (s.equals("ConnectOk")) {
210: finishCreate();
211: } else if (s.equals("ConnectCancel")) {
212: dispose();
213: }
214: }
215:
216: // public boolean isFocusTraversable() { return true; }
217: public void keyPressed(KeyEvent k) {
218:
219: // System.out.println("Key pressed: " + k.getKeyCode());
220: if (k.getKeyCode() == KeyEvent.VK_ENTER) {
221: finishCreate();
222: } else if (k.getKeyCode() == KeyEvent.VK_ESCAPE) {
223: dispose();
224: }
225: }
226:
227: public void keyTyped(KeyEvent k) {
228: }
229:
230: public void keyReleased(KeyEvent k) {
231: }
232:
233: /**
234: * Method declaration
235: *
236: *
237: * @param ev
238: */
239: public void windowClosing(WindowEvent ev) {
240:
241: // System.out.println("windowClosing");
242: }
243:
244: /**
245: * Method declaration
246: *
247: *
248: */
249: protected void finishCreate() {
250:
251: try {
252: mConnection = createConnection(mDriver.getText(), mURL
253: .getText(), mUser.getText(), mPassword.getText());
254:
255: dispose();
256: } catch (Exception e) {
257: e.printStackTrace();
258: mError.setText(e.toString());
259: }
260: }
261:
262: /**
263: * Method declaration
264: *
265: *
266: * @param owner
267: * @param title
268: *
269: * @return
270: */
271: public static Connection createConnection(Frame owner,
272: String title, Insets defInsets) {
273:
274: ZaurusConnectionDialog dialog = new ZaurusConnectionDialog(
275: owner, title);
276:
277: dialog.create(defInsets);
278:
279: return dialog.mConnection;
280: }
281:
282: /**
283: * Method declaration
284: *
285: *
286: * @param e
287: */
288: public void itemStateChanged(ItemEvent e) {
289:
290: String s = (String) e.getItem();
291:
292: for (int i = 0; i < sJDBCTypes.length; i++) {
293: if (s.equals(sJDBCTypes[i][0])) {
294: mDriver.setText(sJDBCTypes[i][1]);
295: mURL.setText(sJDBCTypes[i][2]);
296: }
297: }
298: }
299: }
|