1 30 31 32 package org.hsqldb.util; 33 34 import java.sql.Connection ; 35 import java.sql.DriverManager ; 36 import java.util.Enumeration ; 37 import java.util.Hashtable ; 38 import java.awt.BorderLayout ; 39 import java.awt.Button ; 40 import java.awt.Choice ; 41 import java.awt.Component ; 42 import java.awt.Dialog ; 43 import java.awt.Dimension ; 44 import java.awt.Frame ; 45 import java.awt.GridLayout ; 46 import java.awt.Label ; 47 import java.awt.Panel ; 48 import java.awt.SystemColor ; 49 import java.awt.TextField ; 50 import java.awt.Toolkit ; 51 import java.awt.event.ActionEvent ; 52 import java.awt.event.ActionListener ; 53 import java.awt.event.ItemEvent ; 54 import java.awt.event.ItemListener ; 55 56 60 67 class ConnectionDialog extends Dialog 68 implements ActionListener , ItemListener { 69 70 protected Connection mConnection; 71 protected TextField mName, mDriver, mURL, mUser, mPassword; 72 protected Label mError; 73 private String [][] connTypes; 74 private Hashtable settings; 75 private Choice types, recent; 76 77 90 public static Connection createConnection(String driver, String url, 91 String user, String password) throws Exception { 92 93 Class.forName(driver).newInstance(); 94 95 return DriverManager.getConnection(url, user, password); 96 } 97 98 105 ConnectionDialog(Frame owner, String title) { 106 super(owner, title, true); 107 } 108 109 113 private void create() { 114 115 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 116 117 setLayout(new BorderLayout ()); 118 119 Panel p = new Panel (new BorderLayout ()); 120 Panel pLabel; 121 Panel pText; 122 Panel pButton; 123 Panel pClearButton; 124 125 if (d.width >= 640) { 127 pLabel = new Panel (new GridLayout (8, 1, 10, 10)); 128 pText = new Panel (new GridLayout (8, 1, 10, 10)); 129 pButton = new Panel (new GridLayout (1, 2, 10, 10)); 130 pClearButton = new Panel (new GridLayout (8, 1, 10, 10)); 131 } else { 132 pLabel = new Panel (new GridLayout (8, 1)); 133 pText = new Panel (new GridLayout (8, 1)); 134 pButton = new Panel (new GridLayout (1, 2)); 135 pClearButton = new Panel (new GridLayout (8, 1)); 136 } 137 138 p.add("West", pLabel); 139 p.add("Center", pText); 140 p.add("South", pButton); 141 p.add("North", createLabel("")); 142 p.add("East", pClearButton); 143 p.setBackground(SystemColor.control); 144 pText.setBackground(SystemColor.control); 145 pLabel.setBackground(SystemColor.control); 146 pButton.setBackground(SystemColor.control); 147 pLabel.add(createLabel("Recent:")); 148 149 recent = new Choice (); 150 151 try { 152 settings = ConnectionDialogCommon.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.add(((ConnectionSetting) en.nextElement()).getName()); 163 } 164 165 recent.addItemListener(new ItemListener () { 166 167 public void itemStateChanged(ItemEvent e) { 168 169 String s = (String ) e.getItem(); 170 ConnectionSetting setting = 171 (ConnectionSetting) settings.get(s); 172 173 if (setting != null) { 174 mName.setText(setting.getName()); 175 mDriver.setText(setting.getDriver()); 176 mURL.setText(setting.getUrl()); 177 mUser.setText(setting.getUser()); 178 mPassword.setText(setting.getPassword()); 179 } 180 } 181 }); 182 pText.add(recent); 183 184 Button b; 185 186 b = new Button ("Clr"); 187 188 b.setActionCommand("Clear"); 189 b.addActionListener(new ActionListener () { 190 191 public void actionPerformed(ActionEvent e) { 192 193 ConnectionDialogCommon.deleteRecentConnectionSettings(); 194 195 settings = new Hashtable (); 196 197 recent.removeAll(); 198 recent.add(ConnectionDialogCommon.emptySettingName); 199 mName.setText(null); 200 } 201 }); 202 pClearButton.add(b); 203 pLabel.add(createLabel("Setting Name:")); 204 205 mName = new TextField (""); 206 207 pText.add(mName); 208 pLabel.add(createLabel("Type:")); 209 210 types = new Choice (); 211 connTypes = ConnectionDialogCommon.getTypes(); 212 213 for (int i = 0; i < connTypes.length; i++) { 214 types.add(connTypes[i][0]); 215 } 216 217 types.addItemListener(this); 218 pText.add(types); 219 pLabel.add(createLabel("Driver:")); 220 221 mDriver = new TextField (connTypes[0][1]); 222 223 pText.add(mDriver); 224 pLabel.add(createLabel("URL:")); 225 226 mURL = new TextField (connTypes[0][2]); 227 228 mURL.addActionListener(this); 229 pText.add(mURL); 230 pLabel.add(createLabel("User:")); 231 232 mUser = new TextField ("sa"); 233 234 mUser.addActionListener(this); 235 pText.add(mUser); 236 pLabel.add(createLabel("Password:")); 237 238 mPassword = new TextField (""); 239 240 mPassword.addActionListener(this); 241 mPassword.setEchoChar('*'); 242 pText.add(mPassword); 243 244 b = new Button ("Ok"); 245 246 b.setActionCommand("ConnectOk"); 247 b.addActionListener(this); 248 pButton.add(b); 249 250 b = new Button ("Cancel"); 251 252 b.setActionCommand("ConnectCancel"); 253 b.addActionListener(this); 254 pButton.add(b); 255 add("East", createLabel("")); 256 add("West", createLabel("")); 257 258 mError = new Label (""); 259 260 Panel pMessage = createBorderPanel(mError); 261 262 add("South", pMessage); 263 add("North", createLabel("")); 264 add("Center", p); 265 doLayout(); 266 pack(); 267 268 Dimension size = getSize(); 269 270 if (d.width >= 640) { 272 setLocation((d.width - size.width) / 2, 273 (d.height - size.height) / 2); 274 } else { 275 setLocation(0, 0); 276 setSize(d); 277 } 278 279 show(); 280 } 281 282 291 public static Connection createConnection(Frame owner, String title) { 292 293 ConnectionDialog dialog = new ConnectionDialog(owner, title); 294 295 dialog.create(); 296 297 return dialog.mConnection; 298 } 299 300 308 protected static Label createLabel(String s) { 309 310 Label l = new Label (s); 311 312 l.setBackground(SystemColor.control); 313 314 return l; 315 } 316 317 325 protected static Panel createBorderPanel(Component center) { 326 327 Panel p = new Panel (); 328 329 p.setBackground(SystemColor.control); 330 p.setLayout(new BorderLayout ()); 331 p.add("Center", center); 332 p.add("North", createLabel("")); 333 p.add("South", createLabel("")); 334 p.add("East", createLabel("")); 335 p.add("West", createLabel("")); 336 p.setBackground(SystemColor.control); 337 338 return p; 339 } 340 341 347 public void actionPerformed(ActionEvent ev) { 348 349 String s = ev.getActionCommand(); 350 351 if (s.equals("ConnectOk") || (ev.getSource() instanceof TextField )) { 352 try { 353 if (mURL.getText().indexOf('\u00AB') >= 0) { 354 throw new Exception ("please specify db path"); 355 } 356 357 mConnection = createConnection(mDriver.getText(), 358 mURL.getText(), 359 mUser.getText(), 360 mPassword.getText()); 361 362 if (mName.getText() != null 363 && mName.getText().trim().length() != 0) { 364 ConnectionSetting newSetting = 365 new ConnectionSetting(mName.getText(), 366 mDriver.getText(), 367 mURL.getText(), 368 mUser.getText(), 369 mPassword.getText()); 370 371 ConnectionDialogCommon.addToRecentConnectionSettings( 372 settings, newSetting); 373 } 374 375 dispose(); 376 } catch (java.io.IOException ioe) { 377 dispose(); 378 } catch (Exception e) { 379 e.printStackTrace(); 380 mError.setText(e.toString()); 381 } 382 } else if (s.equals("ConnectCancel")) { 383 dispose(); 384 } 385 } 386 387 393 public void itemStateChanged(ItemEvent e) { 394 395 String s = (String ) e.getItem(); 396 397 for (int i = 0; i < connTypes.length; i++) { 398 if (s.equals(connTypes[i][0])) { 399 mDriver.setText(connTypes[i][1]); 400 mURL.setText(connTypes[i][2]); 401 } 402 } 403 } 404 } 405 | Popular Tags |