1 30 31 32 package org.hsqldb.util; 33 34 import java.sql.Connection ; 35 import java.sql.DriverManager ; 36 import java.sql.SQLException ; 37 import java.util.Hashtable ; 38 import java.util.Iterator ; 39 import java.util.Vector ; 40 import java.awt.Dimension ; 41 import java.awt.Toolkit ; 42 import java.awt.event.ActionEvent ; 43 import java.awt.event.ActionListener ; 44 import java.awt.event.ItemEvent ; 45 import java.awt.event.ItemListener ; 46 47 import javax.swing.Box ; 48 import javax.swing.JButton ; 49 import javax.swing.JComboBox ; 50 import javax.swing.JDialog ; 51 import javax.swing.JFrame ; 52 import javax.swing.JLabel ; 53 import javax.swing.JPanel ; 54 import javax.swing.JPasswordField ; 55 import javax.swing.JTextField ; 56 import javax.swing.SwingUtilities ; 57 import javax.swing.border.EmptyBorder ; 58 59 70 79 class ConnectionDialogSwing extends JDialog 80 implements ActionListener , ItemListener { 81 82 85 private static final long serialVersionUID = 1L; 86 private Connection mConnection; 87 private JTextField mName, mDriver, mURL, mUser; 88 private JPasswordField mPassword; 89 private String [][] connTypes; 90 private Hashtable settings; 91 private JButton okCancel, clear; 92 private JComboBox mSettingName = 93 new JComboBox (loadRecentConnectionSettings()); 94 private static ConnectionSetting currentConnectionSetting = null; 95 96 public static void setConnectionSetting( 97 ConnectionSetting connectionSetting) { 98 currentConnectionSetting = connectionSetting; 99 } 100 101 public static Connection createConnection(String driver, String url, 102 String user, String password) throws Exception { 103 104 Class.forName(driver).newInstance(); 105 106 return DriverManager.getConnection(url, user, password); 107 } 108 109 ConnectionDialogSwing(JFrame owner, String title) { 110 super(owner, title, true); 111 } 112 113 private void create() { 114 115 Box main = Box.createHorizontalBox(); 116 Box labels = Box.createVerticalBox(); 117 Box controls = Box.createVerticalBox(); 118 Box buttons = Box.createHorizontalBox(); 119 Box whole = Box.createVerticalBox(); 120 121 Box extra = Box.createHorizontalBox(); 123 124 main.add(Box.createHorizontalStrut(10)); 125 main.add(Box.createHorizontalGlue()); 126 main.add(labels); 127 main.add(Box.createHorizontalStrut(10)); 128 main.add(Box.createHorizontalGlue()); 129 main.add(controls); 130 main.add(Box.createHorizontalStrut(10)); 131 main.add(Box.createVerticalGlue()); 132 main.add(extra); 133 main.add(Box.createVerticalGlue()); 134 whole.add(Box.createVerticalGlue()); 135 whole.add(Box.createVerticalStrut(10)); 136 whole.add(main); 137 whole.add(Box.createVerticalGlue()); 138 whole.add(Box.createVerticalStrut(10)); 139 whole.add(buttons); 140 whole.add(Box.createVerticalGlue()); 141 whole.add(Box.createVerticalStrut(10)); 142 whole.add(Box.createVerticalGlue()); 143 labels.add(createLabel("Recent Setting:")); 144 labels.add(Box.createVerticalGlue()); 145 labels.add(createLabel("Setting Name:")); 146 labels.add(Box.createVerticalGlue()); 147 labels.add(createLabel("Type:")); 148 labels.add(Box.createVerticalGlue()); 149 labels.add(createLabel("Driver:")); 150 labels.add(Box.createVerticalGlue()); 151 labels.add(createLabel("URL:")); 152 labels.add(Box.createVerticalGlue()); 153 labels.add(createLabel("User:")); 154 labels.add(Box.createVerticalGlue()); 155 labels.add(createLabel("Password:")); 156 labels.add(Box.createVerticalGlue()); 157 labels.add(Box.createVerticalStrut(10)); 158 controls.add(Box.createVerticalGlue()); 159 160 mSettingName.setActionCommand("Select Setting"); 162 mSettingName.addActionListener(this); 163 controls.add(mSettingName); 164 controls.add(Box.createHorizontalGlue()); 165 166 mName = new JTextField (); 168 169 mName.addActionListener(this); 170 controls.add(mName); 171 172 clear = new JButton ("Clear Names"); 174 175 clear.setActionCommand("Clear"); 176 clear.addActionListener(this); 177 buttons.add(clear); 178 buttons.add(Box.createHorizontalGlue()); 179 buttons.add(Box.createHorizontalStrut(10)); 180 181 JComboBox types = new JComboBox (); 182 183 connTypes = ConnectionDialogCommon.getTypes(); 184 185 for (int i = 0; i < connTypes.length; i++) { 186 types.addItem(connTypes[i][0]); 187 } 188 189 types.addItemListener(this); 190 controls.add(types); 191 controls.add(Box.createVerticalGlue()); 192 193 mDriver = new JTextField (connTypes[0][1]); 194 195 mDriver.addActionListener(this); 196 controls.add(mDriver); 197 198 mURL = new JTextField (connTypes[0][2]); 199 200 mURL.addActionListener(this); 201 controls.add(mURL); 202 controls.add(Box.createVerticalGlue()); 203 204 mUser = new JTextField ("sa"); 205 206 mUser.addActionListener(this); 207 controls.add(mUser); 208 controls.add(Box.createVerticalGlue()); 209 210 mPassword = new JPasswordField (""); 211 212 mPassword.addActionListener(this); 213 controls.add(mPassword); 214 controls.add(Box.createVerticalGlue()); 215 controls.add(Box.createVerticalStrut(10)); 216 217 buttons.add(Box.createHorizontalGlue()); 219 buttons.add(Box.createHorizontalStrut(10)); 220 221 okCancel = new JButton (" Ok "); 222 223 okCancel.setActionCommand("ConnectOk"); 224 okCancel.addActionListener(this); 225 buttons.add(okCancel); 226 getRootPane().setDefaultButton(okCancel); 227 buttons.add(Box.createHorizontalGlue()); 228 buttons.add(Box.createHorizontalStrut(20)); 229 230 okCancel = new JButton (" Cancel "); 231 232 okCancel.setActionCommand("ConnectCancel"); 233 okCancel.addActionListener(this); 234 buttons.add(okCancel); 235 buttons.add(Box.createHorizontalGlue()); 236 buttons.add(Box.createHorizontalStrut(10)); 237 238 JPanel jPanel = new JPanel (); 239 240 jPanel.setBorder(new EmptyBorder (10, 10, 10, 10)); 241 jPanel.add("Center", whole); 242 getContentPane().add("Center", jPanel); 243 doLayout(); 244 pack(); 245 246 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 247 Dimension size = getSize(); 248 249 if (currentConnectionSetting != null) { 250 mName.setText(currentConnectionSetting.getName()); 251 mDriver.setText(currentConnectionSetting.getDriver()); 252 mURL.setText(currentConnectionSetting.getUrl()); 253 mUser.setText(currentConnectionSetting.getUser()); 254 mPassword.setText(currentConnectionSetting.getPassword()); 255 } 256 257 if (d.width >= 640) { 259 setLocation((d.width - size.width) / 2, 260 (d.height - size.height) / 2); 261 } else { 262 setLocation(0, 0); 263 setSize(d); 264 } 265 266 setVisible(true); 267 } 268 269 public static Connection createConnection(JFrame owner, String title) { 270 271 ConnectionDialogSwing dialog = new ConnectionDialogSwing(owner, 272 title); 273 274 try { 276 277 SwingUtilities.updateComponentTreeUI(dialog); 279 } catch (Exception e) { 280 CommonSwing.errorMessage(e); 281 } 282 283 dialog.create(); 284 285 return dialog.mConnection; 286 } 287 288 private static JLabel createLabel(String s) { 289 290 JLabel l = new JLabel (s); 291 292 return l; 293 } 294 295 public Vector loadRecentConnectionSettings() { 297 298 Vector passSettings = new Vector (); 299 300 settings = new Hashtable (); 301 302 try { 303 settings = ConnectionDialogCommon.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()).getName()); 311 } 312 } catch (java.io.IOException ioe) { 313 CommonSwing.errorMessage(ioe); 314 } 315 316 return (passSettings); 317 } 318 319 public void actionPerformed(ActionEvent ev) { 320 321 String s = ev.getActionCommand(); 322 323 if (s.equals("ConnectOk") || (ev.getSource() instanceof JTextField )) { 324 try { 325 if (mURL.getText().indexOf('\u00AB') >= 0) { 326 throw new Exception ("please specify db path"); 327 } 328 329 mConnection = 330 createConnection(mDriver.getText(), mURL.getText(), 331 mUser.getText(), 332 new String (mPassword.getPassword())); 333 334 if (mName.getText() != null 336 && mName.getText().trim().length() != 0) { 337 ConnectionSetting newSetting = new ConnectionSetting( 338 mName.getText(), mDriver.getText(), mURL.getText(), 339 mUser.getText(), new String (mPassword.getPassword())); 340 341 ConnectionDialogCommon.addToRecentConnectionSettings( 342 settings, newSetting); 343 } 344 345 dispose(); 346 } catch (SQLException e) { 347 mConnection = null; 348 349 CommonSwing.errorMessage(e, true); 350 } catch (Exception e) { 351 352 CommonSwing.errorMessage(e); 354 } 355 356 } else if (s.equals("Select Setting")) { 358 String s2 = (String ) mSettingName.getSelectedItem(); 359 ConnectionSetting setting = (ConnectionSetting) settings.get(s2); 360 361 if (setting != null) { 362 mName.setText(setting.getName()); 363 mDriver.setText(setting.getDriver()); 364 mURL.setText(setting.getUrl()); 365 mUser.setText(setting.getUser()); 366 mPassword.setText(setting.getPassword()); 367 } 368 } else if (s.equals("ConnectCancel")) { 369 dispose(); 370 371 } else if (s.equals("Clear")) { 373 ConnectionDialogCommon.deleteRecentConnectionSettings(); 374 375 settings = new Hashtable (); 376 377 mSettingName.removeAllItems(); 378 mSettingName.addItem(ConnectionDialogCommon.emptySettingName); 379 mName.setText(null); 380 } 381 } 382 383 public void itemStateChanged(ItemEvent e) { 384 385 String s = (String ) e.getItem(); 386 387 for (int i = 0; i < connTypes.length; i++) { 388 if (s.equals(connTypes[i][0])) { 389 mDriver.setText(connTypes[i][1]); 390 mURL.setText(connTypes[i][2]); 391 } 392 } 393 } 394 } 395 | Popular Tags |