1 19 20 package org.netbeans.modules.db.explorer.dlg; 21 import java.awt.BorderLayout ; 22 import java.util.List ; 23 import java.util.ResourceBundle ; 24 import javax.swing.JComponent ; 25 import javax.swing.SwingUtilities ; 26 27 import javax.swing.event.DocumentListener ; 28 import javax.swing.event.ListDataListener ; 29 30 import org.netbeans.modules.db.explorer.DatabaseConnection; 31 import org.netbeans.api.db.explorer.JDBCDriver; 32 import org.netbeans.api.db.explorer.JDBCDriverManager; 33 import org.netbeans.api.progress.ProgressHandle; 34 import org.netbeans.api.progress.ProgressHandleFactory; 35 import org.netbeans.modules.db.util.DatabaseExplorerInternalUIs; 36 import org.netbeans.modules.db.util.DriverListUtil; 37 38 import org.openide.util.NbBundle; 39 40 public class NewConnectionPanel extends ConnectionDialog.FocusablePanel implements DocumentListener , ListDataListener { 41 42 private ConnectionDialogMediator mediator; 43 private DatabaseConnection connection; 45 private ProgressHandle progressHandle; 46 private JComponent progressComponent; 47 48 private static final String BUNDLE = "org.netbeans.modules.db.resources.Bundle"; 50 public NewConnectionPanel(ConnectionDialogMediator mediator, String driverClass, DatabaseConnection connection) { 51 this.mediator = mediator; 52 this.connection = connection; 53 initComponents(); 54 initAccessibility(); 55 56 DatabaseExplorerInternalUIs.connect(templateComboBox, JDBCDriverManager.getDefault(), driverClass); 57 58 ConnectionProgressListener progressListener = new ConnectionProgressListener() { 59 public void connectionStarted() { 60 startProgress(); 61 } 62 63 public void connectionStep(String step) { 64 setProgressMessage(step); 65 } 66 67 public void connectionFinished() { 68 stopProgress(true); 69 } 70 71 public void connectionFailed() { 72 stopProgress(false); 73 } 74 }; 75 mediator.addConnectionProgressListener(progressListener); 76 77 driverTextField.setText(connection.getDriver()); 78 urlComboBox.setSelectedItem(connection.getDatabase()); 79 userTextField.setText(connection.getUser()); 80 passwordField.setText(connection.getPassword()); 81 82 String driver = connection.getDriver(); 83 String driverName = connection.getDriverName(); 84 if (driver != null && driverName != null) { 85 for (int i = 0; i < templateComboBox.getItemCount(); i++) { 86 Object item = templateComboBox.getItemAt(i); 87 if (item instanceof JDBCDriver) { 88 JDBCDriver dbDriver = (JDBCDriver)item; 89 if (dbDriver.getClassName().equals(driver) && dbDriver.getName().equals(driverName)) { 90 templateComboBox.setSelectedIndex(i); 91 break; 92 } 93 } 94 } 95 } 96 97 driverTextField.getDocument().addDocumentListener(this); 98 userTextField.getDocument().addDocumentListener(this); 99 passwordField.getDocument().addDocumentListener(this); 100 templateComboBox.getModel().addListDataListener(this); 101 urlComboBox.getModel().addListDataListener(this); 102 103 checkValid(); 104 } 105 106 private void initAccessibility() { 107 ResourceBundle b = NbBundle.getBundle(BUNDLE); 108 templateLabel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_NewConnectionDriverNameA11yDesc")); templateComboBox.getAccessibleContext().setAccessibleName(b.getString("ACS_NewConnectionDriverNameComboBoxA11yName")); driverLabel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_NewConnectionDriverClassA11yDesc")); driverTextField.getAccessibleContext().setAccessibleName(b.getString("ACS_NewConnectionDriverClassComboBoxA11yName")); urlLabel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_NewConnectionDatabaseURLA11yDesc")); urlComboBox.getAccessibleContext().setAccessibleName(b.getString("ACS_NewConnectionDatabaseURLTextFieldA11yName")); userLabel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_NewConnectionUserNameA11yDesc")); userTextField.getAccessibleContext().setAccessibleName(b.getString("ACS_NewConnectionUserNameTextFieldA11yName")); passwordLabel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_NewConnectionPasswordA11yDesc")); passwordField.getAccessibleContext().setAccessibleName(b.getString("ACS_NewConnectionPasswordTextFieldA11yName")); connectProgressPanel.getAccessibleContext().setAccessibleName(b.getString("ACS_ConnectionProgressBarA11yName")); connectProgressPanel.getAccessibleContext().setAccessibleDescription(b.getString("ACS_ConnectionProgressBarA11yDesc")); } 121 122 public void initializeFocus() { 123 getInitiallyFocusedComponent().requestFocusInWindow(); 124 } 125 126 private JComponent getInitiallyFocusedComponent() { 127 if (templateComboBox.getItemCount() <= 1) { return templateComboBox; 129 } 130 if (connection.getDatabase().length() == 0) { 131 return urlComboBox; 132 } 133 if (userTextField.getText().length() == 0) { 134 return userTextField; 135 } 136 if (passwordField.getPassword().length == 0) { 137 return passwordField; 138 } 139 return urlComboBox; 141 } 142 143 148 private void initComponents() { 150 java.awt.GridBagConstraints gridBagConstraints; 151 152 templateLabel = new javax.swing.JLabel (); 153 templateComboBox = new javax.swing.JComboBox (); 154 driverLabel = new javax.swing.JLabel (); 155 driverTextField = new javax.swing.JTextField (); 156 urlLabel = new javax.swing.JLabel (); 157 urlComboBox = new javax.swing.JComboBox (); 158 userLabel = new javax.swing.JLabel (); 159 userTextField = new javax.swing.JTextField (); 160 passwordLabel = new javax.swing.JLabel (); 161 passwordField = new javax.swing.JPasswordField (); 162 passwordCheckBox = new javax.swing.JCheckBox (); 163 jPanel1 = new javax.swing.JPanel (); 164 connectProgressPanel = new javax.swing.JPanel (); 165 progressMessageLabel = new javax.swing.JLabel (); 166 progressContainerPanel = new javax.swing.JPanel (); 167 168 FormListener formListener = new FormListener(); 169 170 setLayout(new java.awt.GridBagLayout ()); 171 172 templateLabel.setLabelFor(templateComboBox); 173 org.openide.awt.Mnemonics.setLocalizedText(templateLabel, NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("NewConnectionDriverName")); gridBagConstraints = new java.awt.GridBagConstraints (); 175 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 176 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 177 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 0); 178 add(templateLabel, gridBagConstraints); 179 180 templateComboBox.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_NewConnectionDriverNameComboBoxA11yDesc")); templateComboBox.addItemListener(formListener); 182 templateComboBox.addActionListener(formListener); 183 gridBagConstraints = new java.awt.GridBagConstraints (); 184 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 185 gridBagConstraints.insets = new java.awt.Insets (12, 5, 0, 11); 186 add(templateComboBox, gridBagConstraints); 187 188 driverLabel.setLabelFor(driverTextField); 189 org.openide.awt.Mnemonics.setLocalizedText(driverLabel, NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("NewConnectionDriverClass")); gridBagConstraints = new java.awt.GridBagConstraints (); 191 gridBagConstraints.gridx = 0; 192 gridBagConstraints.gridy = 1; 193 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 194 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 195 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 0); 196 add(driverLabel, gridBagConstraints); 197 198 driverTextField.setColumns(50); 199 driverTextField.setEditable(false); 200 driverTextField.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_NewConnectionDriverClassComboBoxA11yDesc")); gridBagConstraints = new java.awt.GridBagConstraints (); 202 gridBagConstraints.gridx = 1; 203 gridBagConstraints.gridy = 1; 204 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 205 gridBagConstraints.insets = new java.awt.Insets (5, 5, 0, 11); 206 add(driverTextField, gridBagConstraints); 207 208 urlLabel.setLabelFor(urlComboBox); 209 org.openide.awt.Mnemonics.setLocalizedText(urlLabel, NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("NewConnectionDatabaseURL")); gridBagConstraints = new java.awt.GridBagConstraints (); 211 gridBagConstraints.gridx = 0; 212 gridBagConstraints.gridy = 2; 213 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 214 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 215 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 0); 216 add(urlLabel, gridBagConstraints); 217 218 urlComboBox.setEditable(true); 219 gridBagConstraints = new java.awt.GridBagConstraints (); 220 gridBagConstraints.gridx = 1; 221 gridBagConstraints.gridy = 2; 222 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 223 gridBagConstraints.insets = new java.awt.Insets (5, 5, 0, 11); 224 add(urlComboBox, gridBagConstraints); 225 226 userLabel.setLabelFor(userTextField); 227 org.openide.awt.Mnemonics.setLocalizedText(userLabel, NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("NewConnectionUserName")); gridBagConstraints = new java.awt.GridBagConstraints (); 229 gridBagConstraints.gridx = 0; 230 gridBagConstraints.gridy = 3; 231 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 232 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 233 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 0); 234 add(userLabel, gridBagConstraints); 235 236 userTextField.setColumns(50); 237 userTextField.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_NewConnectionUserNameTextFieldA11yDesc")); gridBagConstraints = new java.awt.GridBagConstraints (); 239 gridBagConstraints.gridx = 1; 240 gridBagConstraints.gridy = 3; 241 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 242 gridBagConstraints.insets = new java.awt.Insets (5, 5, 0, 11); 243 add(userTextField, gridBagConstraints); 244 245 passwordLabel.setLabelFor(passwordField); 246 org.openide.awt.Mnemonics.setLocalizedText(passwordLabel, NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("NewConnectionPassword")); gridBagConstraints = new java.awt.GridBagConstraints (); 248 gridBagConstraints.gridx = 0; 249 gridBagConstraints.gridy = 4; 250 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 251 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 252 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 0); 253 add(passwordLabel, gridBagConstraints); 254 255 passwordField.setColumns(50); 256 passwordField.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_NewConnectionPasswordTextFieldA11yDesc")); gridBagConstraints = new java.awt.GridBagConstraints (); 258 gridBagConstraints.gridx = 1; 259 gridBagConstraints.gridy = 4; 260 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 261 gridBagConstraints.insets = new java.awt.Insets (5, 5, 0, 11); 262 add(passwordField, gridBagConstraints); 263 264 org.openide.awt.Mnemonics.setLocalizedText(passwordCheckBox, NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("NewConnectionRememberPassword")); passwordCheckBox.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_NewConnectionRememberPasswordA11yDesc")); gridBagConstraints = new java.awt.GridBagConstraints (); 267 gridBagConstraints.gridx = 1; 268 gridBagConstraints.gridy = 5; 269 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 270 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 271 gridBagConstraints.insets = new java.awt.Insets (5, 5, 0, 11); 272 add(passwordCheckBox, gridBagConstraints); 273 gridBagConstraints = new java.awt.GridBagConstraints (); 274 gridBagConstraints.gridy = 6; 275 gridBagConstraints.gridwidth = 2; 276 gridBagConstraints.weightx = 1.0; 277 gridBagConstraints.weighty = 1.0; 278 add(jPanel1, gridBagConstraints); 279 280 connectProgressPanel.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectionProgressBarA11yDesc")); connectProgressPanel.setLayout(new java.awt.BorderLayout (0, 5)); 282 283 org.openide.awt.Mnemonics.setLocalizedText(progressMessageLabel, " "); 284 connectProgressPanel.add(progressMessageLabel, java.awt.BorderLayout.NORTH); 285 286 progressContainerPanel.setMinimumSize(new java.awt.Dimension (20, 20)); 287 progressContainerPanel.setPreferredSize(new java.awt.Dimension (20, 20)); 288 progressContainerPanel.setLayout(new java.awt.BorderLayout ()); 289 connectProgressPanel.add(progressContainerPanel, java.awt.BorderLayout.CENTER); 290 291 gridBagConstraints = new java.awt.GridBagConstraints (); 292 gridBagConstraints.gridx = 0; 293 gridBagConstraints.gridy = 7; 294 gridBagConstraints.gridwidth = 2; 295 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 296 gridBagConstraints.insets = new java.awt.Insets (12, 12, 11, 11); 297 add(connectProgressPanel, gridBagConstraints); 298 } 299 300 302 private class FormListener implements java.awt.event.ActionListener , java.awt.event.ItemListener { 303 FormListener() {} 304 public void actionPerformed(java.awt.event.ActionEvent evt) { 305 if (evt.getSource() == templateComboBox) { 306 NewConnectionPanel.this.templateComboBoxActionPerformed(evt); 307 } 308 } 309 310 public void itemStateChanged(java.awt.event.ItemEvent evt) { 311 if (evt.getSource() == templateComboBox) { 312 NewConnectionPanel.this.templateComboBoxItemStateChanged(evt); 313 } 314 } 315 } 317 private void templateComboBoxItemStateChanged(java.awt.event.ItemEvent evt) { checkValid(); 319 } 321 private void templateComboBoxActionPerformed(java.awt.event.ActionEvent evt) { Object item = templateComboBox.getSelectedItem(); 323 if (!(item instanceof JDBCDriver)) { 324 return; 325 } 326 JDBCDriver drv = (JDBCDriver)item; 327 List urls = null; 328 String driver = null; 329 if (drv != null) { 330 driver = drv.getClassName(); 331 urls = DriverListUtil.getURLs(driver); 332 } 333 334 urlComboBox.removeAllItems(); 335 if (!connection.getDatabase().equals("")) urlComboBox.addItem(connection.getDatabase()); 337 else if (urls != null) 338 for (int i = 0; i < urls.size(); i++) 339 urlComboBox.addItem((String ) urls.get(i)); 340 341 if (driver != null) 342 driverTextField.setText(driver); 343 } 345 346 private javax.swing.JPanel connectProgressPanel; 348 private javax.swing.JLabel driverLabel; 349 private javax.swing.JTextField driverTextField; 350 private javax.swing.JPanel jPanel1; 351 private javax.swing.JCheckBox passwordCheckBox; 352 private javax.swing.JPasswordField passwordField; 353 private javax.swing.JLabel passwordLabel; 354 private javax.swing.JPanel progressContainerPanel; 355 private javax.swing.JLabel progressMessageLabel; 356 private javax.swing.JComboBox templateComboBox; 357 private javax.swing.JLabel templateLabel; 358 private javax.swing.JComboBox urlComboBox; 359 private javax.swing.JLabel urlLabel; 360 private javax.swing.JLabel userLabel; 361 private javax.swing.JTextField userTextField; 362 364 private JDBCDriver getSelectedDriver() { 365 Object item = templateComboBox.getSelectedItem(); 366 if (item instanceof JDBCDriver) { 367 return (JDBCDriver)item; 368 } 369 return null; 370 } 371 372 public void setConnectionInfo() { 373 JDBCDriver driver = getSelectedDriver(); 374 if (driver != null) { 375 connection.setDriverName(driver.getName()); 376 connection.setDriver(driver.getClassName()); 377 } 378 connection.setDatabase(urlComboBox.getEditor().getItem().toString()); 381 connection.setUser(userTextField.getText()); 382 connection.setPassword(getPassword()); 383 connection.setRememberPassword(passwordCheckBox.isSelected()); 384 } 385 386 private String getPassword() { 387 String password; 388 String tempPassword = new String (passwordField.getPassword()); 389 if (tempPassword.length() > 0) 390 password = tempPassword; 391 else 392 password = null; 393 394 return password; 395 } 396 397 public String getTitle() { 398 return NbBundle.getBundle(BUNDLE).getString("NewConnectionDialogTitle"); } 400 401 private void startProgress() { 402 SwingUtilities.invokeLater(new Runnable () { 403 public void run() { 404 progressHandle = ProgressHandleFactory.createHandle(null); 405 progressComponent = ProgressHandleFactory.createProgressComponent(progressHandle); 406 progressContainerPanel.add(progressComponent, BorderLayout.CENTER); 407 progressHandle.start(); 408 progressMessageLabel.setText(NbBundle.getBundle(BUNDLE).getString("ConnectionProgress_Connecting")); 409 } 410 }); 411 } 412 413 private void setProgressMessage(final String message) { 414 SwingUtilities.invokeLater(new Runnable () { 415 public void run() { 416 progressMessageLabel.setText(message); 417 } 418 }); 419 } 420 421 private void stopProgress(final boolean connected) { 422 SwingUtilities.invokeLater(new Runnable () { 423 public void run() { 424 progressHandle.finish(); 425 progressContainerPanel.remove(progressComponent); 426 progressContainerPanel.repaint(); 428 if (connected) { 429 progressMessageLabel.setText(NbBundle.getBundle(BUNDLE).getString("ConnectionProgress_Established")); 430 } else { 431 progressMessageLabel.setText(NbBundle.getBundle(BUNDLE).getString("ConnectionProgress_Failed")); 432 } 433 } 434 }); 435 } 436 437 private void resetProgress() { 438 progressMessageLabel.setText(""); } 440 441 public void changedUpdate(javax.swing.event.DocumentEvent e) { 442 fireChange(); 443 } 444 445 public void insertUpdate(javax.swing.event.DocumentEvent e) { 446 fireChange(); 447 } 448 449 public void removeUpdate(javax.swing.event.DocumentEvent e) { 450 fireChange(); 451 } 452 453 public void contentsChanged(javax.swing.event.ListDataEvent e) { 454 fireChange(); 455 } 456 457 public void intervalAdded(javax.swing.event.ListDataEvent e) { 458 fireChange(); 459 } 460 461 public void intervalRemoved(javax.swing.event.ListDataEvent e) { 462 fireChange(); 463 } 464 465 private void fireChange() { 466 firePropertyChange("argumentChanged", null, null); 467 resetProgress(); 468 } 469 470 private void checkValid() { 471 mediator.setValid(getSelectedDriver() != null); 472 } 473 } 474 | Popular Tags |