KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.BorderLayout JavaDoc;
36 import java.awt.Button JavaDoc;
37 import java.awt.Choice JavaDoc;
38 import java.awt.Dimension JavaDoc;
39 import java.awt.Frame JavaDoc;
40 import java.awt.GridLayout JavaDoc;
41 import java.awt.Insets JavaDoc;
42 import java.awt.Label JavaDoc;
43 import java.awt.Panel JavaDoc;
44 import java.awt.SystemColor JavaDoc;
45 import java.awt.TextField JavaDoc;
46 import java.awt.Toolkit JavaDoc;
47 import java.awt.event.ActionEvent JavaDoc;
48 import java.awt.event.ActionListener JavaDoc;
49 import java.awt.event.ItemEvent JavaDoc;
50 import java.awt.event.ItemListener JavaDoc;
51 import java.awt.event.KeyEvent JavaDoc;
52 import java.awt.event.KeyListener JavaDoc;
53 import java.awt.event.WindowEvent JavaDoc;
54
55 /**
56  * Class declaration
57  *
58  *
59  * @author ulrivo@users
60  * @version 1.0.0.1
61  */

62 public class ZaurusConnectionDialog extends ConnectionDialog
63 implements ActionListener JavaDoc, ItemListener JavaDoc, KeyListener JavaDoc {
64
65     static final String JavaDoc[][] sJDBCTypes = {
66         {
67             "HSQL In-Memory", "org.hsqldb.jdbcDriver", "jdbc:hsqldb:."
68         }, {
69             "HSQL Standalone", "org.hsqldb.jdbcDriver", "jdbc:hsqldb:test"
70         }, {
71             "MM.MySQL", "org.gjt.mm.mysql.Driver", "jdbc:mysql://localhost/"
72         }, {
73             "JDBC-ODBC Brigde from Sun", "sun.jdbc.odbc.JdbcOdbcDriver",
74             "jdbc:odbc:test"
75         }, {
76             "Oracle", "oracle.jdbc.driver.OracleDriver", "jdbc:oracle:oci8:@"
77         }, {
78             "IBM DB2", "COM.ibm.db2.jdbc.app.DB2Driver", "jdbc:db2:test"
79         }, {
80             "Cloudscape RMI", "RmiJdbc.RJDriver",
81             "jdbc:rmi://localhost:1099/jdbc:cloudscape:test;create=true"
82         }, {
83             "InstantDb", "jdbc.idbDriver", "jdbc:idb:sample.prp"
84         },
85         {
86             "PointBase", "com.pointbase.jdbc.jdbcUniversalDriver",
87             "jdbc:pointbase://localhost/sample"
88         }, // PUBLIC / public
89
};
90
91     /**
92      * Constructor declaration
93      *
94      *
95      * @param owner
96      * @param title
97      */

98     ZaurusConnectionDialog(Frame JavaDoc owner, String JavaDoc title) {
99
100         super(owner, title);
101
102         addKeyListener(this);
103     }
104
105     /**
106      * Method declaration
107      *
108      */

109     void create(Insets JavaDoc defInsets) {
110
111         setLayout(new BorderLayout JavaDoc());
112         addKeyListener(this);
113
114         Panel JavaDoc p = new Panel JavaDoc(new GridLayout JavaDoc(6, 2, 10, 10));
115
116         p.addKeyListener(this);
117         p.setBackground(SystemColor.control);
118         p.add(createLabel("Type:"));
119
120         Choice JavaDoc types = new Choice JavaDoc();
121
122         types.addItemListener(this);
123         types.addKeyListener(this);
124
125         for (int i = 0; i < sJDBCTypes.length; i++) {
126             types.add(sJDBCTypes[i][0]);
127         }
128
129         p.add(types);
130         p.add(createLabel("Driver:"));
131
132         mDriver = new TextField JavaDoc("org.hsqldb.jdbcDriver");
133
134         mDriver.addKeyListener(this);
135         p.add(mDriver);
136         p.add(createLabel("URL:"));
137
138         mURL = new TextField JavaDoc("jdbc:hsqldb:.");
139
140         mURL.addKeyListener(this);
141         p.add(mURL);
142         p.add(createLabel("User:"));
143
144         mUser = new TextField JavaDoc("sa");
145
146         mUser.addKeyListener(this);
147         p.add(mUser);
148         p.add(createLabel("Password:"));
149
150         mPassword = new TextField JavaDoc("");
151
152         mPassword.addKeyListener(this);
153         mPassword.setEchoChar('*');
154         p.add(mPassword);
155
156         Button JavaDoc b;
157
158         b = new Button JavaDoc("Cancel");
159
160         b.setActionCommand("ConnectCancel");
161         b.addActionListener(this);
162         b.addKeyListener(this);
163         p.add(b);
164
165         b = new Button JavaDoc("Ok");
166
167         b.setActionCommand("ConnectOk");
168         b.addActionListener(this);
169         b.addKeyListener(this);
170         p.add(b);
171         setLayout(new BorderLayout JavaDoc());
172         add("East", createLabel(" "));
173         add("West", createLabel(" "));
174
175         mError = new Label JavaDoc("");
176
177         Panel JavaDoc pMessage = createBorderPanel(mError);
178
179         pMessage.addKeyListener(this);
180         add("South", pMessage);
181         add("North", createLabel(""));
182         add("Center", p);
183         doLayout();
184         pack();
185
186         Dimension JavaDoc d = Toolkit.getDefaultToolkit().getScreenSize();
187         Dimension JavaDoc size = getSize();
188
189         if (d.width > 640) {
190             setLocation((d.width - size.width) / 2,
191                         (d.height - size.height) / 2);
192         } else if (defInsets.top > 0 && defInsets.left > 0) {
193             setLocation(defInsets.bottom, defInsets.right);
194             setSize(defInsets.top, defInsets.left);
195
196             // full size on screen with less than 640 width
197
} else {
198             setLocation(0, 0);
199             setSize(d);
200         }
201
202         show();
203     }
204
205     /**
206      * Method declaration
207      *
208      *
209      * @param ev
210      */

211     public void actionPerformed(ActionEvent JavaDoc ev) {
212
213         String JavaDoc s = ev.getActionCommand();
214
215         // System.out.println("Action performed " + s);
216
if (s.equals("ConnectOk")) {
217             finishCreate();
218         } else if (s.equals("ConnectCancel")) {
219             dispose();
220         }
221     }
222
223     // public boolean isFocusTraversable() { return true; }
224
public void keyPressed(KeyEvent JavaDoc k) {
225
226         // System.out.println("Key pressed: " + k.getKeyCode());
227
if (k.getKeyCode() == KeyEvent.VK_ENTER) {
228             finishCreate();
229         } else if (k.getKeyCode() == KeyEvent.VK_ESCAPE) {
230             dispose();
231         }
232     }
233
234     public void keyTyped(KeyEvent JavaDoc k) {}
235
236     public void keyReleased(KeyEvent JavaDoc k) {}
237
238     /**
239      * Method declaration
240      *
241      *
242      * @param ev
243      */

244     public void windowClosing(WindowEvent JavaDoc ev) {
245
246         // System.out.println("windowClosing");
247
}
248
249     /**
250      * Method declaration
251      *
252      *
253      */

254     protected void finishCreate() {
255
256         try {
257             mConnection = createConnection(mDriver.getText(), mURL.getText(),
258                                            mUser.getText(),
259                                            mPassword.getText());
260
261             dispose();
262         } catch (Exception JavaDoc e) {
263             e.printStackTrace();
264             mError.setText(e.toString());
265         }
266     }
267
268     /**
269      * Method declaration
270      *
271      *
272      * @param owner
273      * @param title
274      *
275      * @return
276      */

277     public static Connection JavaDoc createConnection(Frame JavaDoc owner, String JavaDoc title,
278             Insets JavaDoc defInsets) {
279
280         ZaurusConnectionDialog dialog = new ZaurusConnectionDialog(owner,
281             title);
282
283         dialog.create(defInsets);
284
285         return dialog.mConnection;
286     }
287
288     /**
289      * Method declaration
290      *
291      *
292      * @param e
293      */

294     public void itemStateChanged(ItemEvent JavaDoc e) {
295
296         String JavaDoc s = (String JavaDoc) e.getItem();
297
298         for (int i = 0; i < sJDBCTypes.length; i++) {
299             if (s.equals(sJDBCTypes[i][0])) {
300                 mDriver.setText(sJDBCTypes[i][1]);
301                 mURL.setText(sJDBCTypes[i][2]);
302             }
303         }
304     }
305 }
306
Popular Tags