Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hsqldb/util/ZaurusConnectionDialog.java


1   /* Copyright (c) 2001-2002, 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.awt.*;
35  import java.awt.event.*;
36  import java.awt.image.*;
37  import java.applet.*;
38  import java.sql.*;
39  import java.net.*;
40  import java.io.*;
41  import java.util.*;
42  
43  /**
44   * Class declaration
45   *
46   *
47   * @author ulrivo@users
48   * @version 1.0.0.1
49   */
50  public class ZaurusConnectionDialog extends ConnectionDialog
51  implements ActionListener, ItemListener, KeyListener {
52  
53      final static String sJDBCTypes[][] = {
54          {
55              "HSQL In-Memory", "org.hsqldb.jdbcDriver", "jdbc:hsqldb:."
56          }, {
57              "HSQL Standalone", "org.hsqldb.jdbcDriver", "jdbc:hsqldb:test"
58          }, {
59              "MM.MySQL", "org.gjt.mm.mysql.Driver", "jdbc:mysql://localhost/"
60          }, {
61              "JDBC-ODBC Brigde from Sun", "sun.jdbc.odbc.JdbcOdbcDriver",
62              "jdbc:odbc:test"
63          }, {
64              "Oracle", "oracle.jdbc.driver.OracleDriver", "jdbc:oracle:oci8:@"
65          }, {
66              "IBM DB2", "COM.ibm.db2.jdbc.app.DB2Driver", "jdbc:db2:test"
67          }, {
68              "Cloudscape RMI", "RmiJdbc.RJDriver",
69              "jdbc:rmi://localhost:1099/jdbc:cloudscape:test;create=true"
70          }, {
71              "InstantDb", "jdbc.idbDriver", "jdbc:idb:sample.prp"
72          },
73          {
74              "PointBase", "com.pointbase.jdbc.jdbcUniversalDriver",
75              "jdbc:pointbase://localhost/sample"
76          },    // PUBLIC / public
77      };
78  
79      /**
80       * Constructor declaration
81       *
82       *
83       * @param owner
84       * @param title
85       */
86      ZaurusConnectionDialog(Frame owner, String title) {
87  
88          super(owner, title);
89  
90          addKeyListener(this);
91      }
92  
93      /**
94       * Method declaration
95       *
96       */
97      void create(Insets defInsets) {
98  
99          setLayout(new BorderLayout());
100         addKeyListener(this);
101 
102         Panel p = new Panel(new GridLayout(6, 2, 10, 10));
103 
104         p.addKeyListener(this);
105         p.setBackground(SystemColor.control);
106         p.add(createLabel("Type:"));
107 
108         Choice types = new Choice();
109 
110         types.addItemListener(this);
111         types.addKeyListener(this);
112 
113         for (int i = 0; i < sJDBCTypes.length; i++) {
114             types.add(sJDBCTypes[i][0]);
115         }
116 
117         p.add(types);
118         p.add(createLabel("Driver:"));
119 
120         mDriver = new TextField("org.hsqldb.jdbcDriver");
121 
122         mDriver.addKeyListener(this);
123         p.add(mDriver);
124         p.add(createLabel("URL:"));
125 
126         mURL = new TextField("jdbc:hsqldb:.");
127 
128         mURL.addKeyListener(this);
129         p.add(mURL);
130         p.add(createLabel("User:"));
131 
132         mUser = new TextField("sa");
133 
134         mUser.addKeyListener(this);
135         p.add(mUser);
136         p.add(createLabel("Password:"));
137 
138         mPassword = new TextField("");
139 
140         mPassword.addKeyListener(this);
141         mPassword.setEchoChar('*');
142         p.add(mPassword);
143 
144         Button b;
145 
146         b = new Button("Cancel");
147 
148         b.setActionCommand("ConnectCancel");
149         b.addActionListener(this);
150         b.addKeyListener(this);
151         p.add(b);
152 
153         b = new Button("Ok");
154 
155         b.setActionCommand("ConnectOk");
156         b.addActionListener(this);
157         b.addKeyListener(this);
158         p.add(b);
159         setLayout(new BorderLayout());
160         add("East", createLabel(" "));
161         add("West", createLabel(" "));
162 
163         mError = new Label("");
164 
165         Panel pMessage = createBorderPanel(mError);
166 
167         pMessage.addKeyListener(this);
168         add("South", pMessage);
169         add("North", createLabel(""));
170         add("Center", p);
171         doLayout();
172         pack();
173 
174         Dimension d    = Toolkit.getDefaultToolkit().getScreenSize();
175         Dimension size = getSize();
176 
177         if (d.width > 640) {
178             setLocation((d.width - size.width) / 2,
179                         (d.height - size.height) / 2);
180         } else if (defInsets.top > 0 && defInsets.left > 0) {
181             setLocation(defInsets.bottom, defInsets.right);
182             setSize(defInsets.top, defInsets.left);
183 
184             // full size on screen with less than 640 width
185         } else {
186             setLocation(0, 0);
187             setSize(d);
188         }
189 
190         show();
191     }
192 
193     /**
194      * Method declaration
195      *
196      *
197      * @param ev
198      */
199     public void actionPerformed(ActionEvent ev) {
200 
201         String s = ev.getActionCommand();
202 
203         //  System.out.println("Action performed " + s);
204         if (s.equals("ConnectOk")) {
205             finishCreate();
206         } else if (s.equals("ConnectCancel")) {
207             dispose();
208         }
209     }
210 
211     //    public boolean isFocusTraversable() { return true; }
212     public void keyPressed(KeyEvent k) {
213 
214         //  System.out.println("Key pressed: " + k.getKeyCode());
215         if (k.getKeyCode() == KeyEvent.VK_ENTER) {
216             finishCreate();
217         } else if (k.getKeyCode() == KeyEvent.VK_ESCAPE) {
218             dispose();
219         }
220     }
221 
222     public void keyTyped(KeyEvent k) {}
223 
224     public void keyReleased(KeyEvent k) {}
225 
226     /**
227      * Method declaration
228      *
229      *
230      * @param ev
231      */
232     public void windowClosing(WindowEvent ev) {
233 
234         //  System.out.println("windowClosing");
235     }
236 
237     /**
238      * Method declaration
239      *
240      *
241      */
242     protected void finishCreate() {
243 
244         try {
245             mConnection = createConnection(mDriver.getText(), mURL.getText(),
246                                            mUser.getText(),
247                                            mPassword.getText());
248 
249             dispose();
250         } catch (Exception e) {
251             e.printStackTrace();
252             mError.setText(e.toString());
253         }
254     }
255 
256     /**
257      * Method declaration
258      *
259      *
260      * @param owner
261      * @param title
262      *
263      * @return
264      */
265     public static Connection createConnection(Frame owner, String title,
266             Insets defInsets) {
267 
268         ZaurusConnectionDialog dialog = new ZaurusConnectionDialog(owner,
269             title);
270 
271         dialog.create(defInsets);
272 
273         return dialog.mConnection;
274     }
275 
276     /**
277      * Method declaration
278      *
279      *
280      * @param e
281      */
282     public void itemStateChanged(ItemEvent e) {
283 
284         String s = (String) e.getItem();
285 
286         for (int i = 0; i < sJDBCTypes.length; i++) {
287             if (s.equals(sJDBCTypes[i][0])) {
288                 mDriver.setText(sJDBCTypes[i][1]);
289                 mURL.setText(sJDBCTypes[i][2]);
290             }
291         }
292     }
293 }