1 19 20 package org.netbeans.modules.db.explorer.dlg; 21 22 import java.awt.BorderLayout ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import javax.swing.JComponent ; 26 import javax.swing.SwingUtilities ; 27 28 import javax.swing.event.DocumentListener ; 29 import javax.swing.event.ListDataListener ; 30 import org.netbeans.api.progress.ProgressHandle; 31 import org.netbeans.api.progress.ProgressHandleFactory; 32 33 import org.openide.util.NbBundle; 34 35 import org.netbeans.modules.db.explorer.DatabaseConnection; 36 37 public class ConnectPanel extends ConnectionDialog.FocusablePanel implements DocumentListener , ListDataListener { 38 39 private ConnectionDialogMediator mediator; 40 private DatabaseConnection connection; 41 private ProgressHandle progressHandle; 42 private JComponent progressComponent; 43 44 49 public ConnectPanel(ConnectionDialogMediator mediator, DatabaseConnection connection) { 50 this.mediator = mediator; 51 this.connection = connection; 52 initComponents(); 53 initAccessibility(); 54 55 ConnectionProgressListener progressListener = new ConnectionProgressListener() { 56 public void connectionStarted() { 57 startProgress(); 58 } 59 60 public void connectionStep(String step) { 61 setProgressMessage(step); 62 } 63 64 public void connectionFinished() { 65 stopProgress(true); 66 } 67 68 public void connectionFailed() { 69 stopProgress(false); 70 } 71 }; 72 mediator.addConnectionProgressListener(progressListener); 73 74 userTextField.setText(connection.getUser()); 75 76 userTextField.getDocument().addDocumentListener(this); 77 passwordField.getDocument().addDocumentListener(this); 78 } 79 80 private void initAccessibility() { 81 userLabel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogUserNameA11yDesc")); userTextField.getAccessibleContext().setAccessibleName(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogUserNameTextFieldA11yName")); passwordLabel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogPasswordA11yDesc")); passwordField.getAccessibleContext().setAccessibleName(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogPasswordTextFieldA11yName")); connectProgressPanel.getAccessibleContext().setAccessibleName(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectionProgressBarA11yName")); connectProgressPanel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectionProgressBarA11yDesc")); } 88 89 public void initializeFocus() { 90 getInitiallyFocusedComponent().requestFocusInWindow(); 91 } 92 93 private JComponent getInitiallyFocusedComponent() { 94 if (userTextField.getText().length() == 0) { 95 return userTextField; 96 } 97 if (passwordField.getPassword().length == 0) { 98 return passwordField; 99 } 100 return userTextField; 102 } 103 104 109 private void initComponents() { 111 java.awt.GridBagConstraints gridBagConstraints; 112 113 userLabel = new javax.swing.JLabel (); 114 passwordLabel = new javax.swing.JLabel (); 115 passwordCheckBox = new javax.swing.JCheckBox (); 116 passwordField = new javax.swing.JPasswordField (); 117 userTextField = new javax.swing.JTextField (); 118 connectProgressPanel = new javax.swing.JPanel (); 119 progressMessageLabel = new javax.swing.JLabel (); 120 progressContainerPanel = new javax.swing.JPanel (); 121 122 setLayout(new java.awt.GridBagLayout ()); 123 124 userLabel.setLabelFor(userTextField); 125 org.openide.awt.Mnemonics.setLocalizedText(userLabel, NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ConnectDialogUserName")); gridBagConstraints = new java.awt.GridBagConstraints (); 127 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 128 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 129 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 0); 130 add(userLabel, gridBagConstraints); 131 132 passwordLabel.setLabelFor(passwordField); 133 org.openide.awt.Mnemonics.setLocalizedText(passwordLabel, NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ConnectDialogPassword")); gridBagConstraints = new java.awt.GridBagConstraints (); 135 gridBagConstraints.gridx = 0; 136 gridBagConstraints.gridy = 1; 137 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 138 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 139 gridBagConstraints.insets = new java.awt.Insets (5, 12, 0, 0); 140 add(passwordLabel, gridBagConstraints); 141 142 org.openide.awt.Mnemonics.setLocalizedText(passwordCheckBox, NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ConnectDialogRememberPassword")); passwordCheckBox.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogRememberPasswordA11yDesc")); gridBagConstraints = new java.awt.GridBagConstraints (); 145 gridBagConstraints.gridx = 1; 146 gridBagConstraints.gridy = 2; 147 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 148 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 149 gridBagConstraints.insets = new java.awt.Insets (5, 5, 0, 11); 150 add(passwordCheckBox, gridBagConstraints); 151 152 passwordField.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogPasswordTextFieldA11yDesc")); gridBagConstraints = new java.awt.GridBagConstraints (); 154 gridBagConstraints.gridx = 1; 155 gridBagConstraints.gridy = 1; 156 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 157 gridBagConstraints.insets = new java.awt.Insets (5, 5, 0, 11); 158 add(passwordField, gridBagConstraints); 159 160 userTextField.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogUserNameTextFieldA11yDesc")); gridBagConstraints = new java.awt.GridBagConstraints (); 162 gridBagConstraints.gridx = 1; 163 gridBagConstraints.gridy = 0; 164 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 165 gridBagConstraints.insets = new java.awt.Insets (12, 5, 0, 11); 166 add(userTextField, gridBagConstraints); 167 168 connectProgressPanel.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectionProgressBarA11yDesc")); connectProgressPanel.setLayout(new java.awt.BorderLayout (0, 5)); 170 171 org.openide.awt.Mnemonics.setLocalizedText(progressMessageLabel, " "); 172 connectProgressPanel.add(progressMessageLabel, java.awt.BorderLayout.NORTH); 173 174 progressContainerPanel.setMinimumSize(new java.awt.Dimension (20, 20)); 175 progressContainerPanel.setPreferredSize(new java.awt.Dimension (20, 20)); 176 progressContainerPanel.setLayout(new java.awt.BorderLayout ()); 177 connectProgressPanel.add(progressContainerPanel, java.awt.BorderLayout.CENTER); 178 179 gridBagConstraints = new java.awt.GridBagConstraints (); 180 gridBagConstraints.gridx = 0; 181 gridBagConstraints.gridy = 3; 182 gridBagConstraints.gridwidth = 2; 183 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 184 gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH; 185 gridBagConstraints.weightx = 1.0; 186 gridBagConstraints.weighty = 1.0; 187 gridBagConstraints.insets = new java.awt.Insets (12, 12, 11, 11); 188 add(connectProgressPanel, gridBagConstraints); 189 } 191 192 private javax.swing.JPanel connectProgressPanel; 194 private javax.swing.JCheckBox passwordCheckBox; 195 private javax.swing.JPasswordField passwordField; 196 private javax.swing.JLabel passwordLabel; 197 private javax.swing.JPanel progressContainerPanel; 198 private javax.swing.JLabel progressMessageLabel; 199 private javax.swing.JLabel userLabel; 200 private javax.swing.JTextField userTextField; 201 203 public String getUser() { 204 return userTextField.getText(); 205 } 206 207 public String getPassword() { 208 return String.valueOf(passwordField.getPassword()); 209 } 210 211 public void showRememberPasswordOption(boolean flag) { 212 passwordCheckBox.setVisible(flag); 213 } 214 215 public boolean rememberPassword() { 216 return passwordCheckBox.isSelected(); 217 } 218 219 public String getTitle() { 220 return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ConnectDialogTitle"); } 222 223 private void startProgress() { 224 SwingUtilities.invokeLater(new Runnable () { 225 public void run() { 226 progressHandle = ProgressHandleFactory.createHandle(null); 227 progressComponent = ProgressHandleFactory.createProgressComponent(progressHandle); 228 progressContainerPanel.add(progressComponent, BorderLayout.CENTER); 229 progressHandle.start(); 230 progressMessageLabel.setText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ConnectionProgress_Connecting")); 231 } 232 }); 233 } 234 235 private void setProgressMessage(final String message) { 236 SwingUtilities.invokeLater(new Runnable () { 237 public void run() { 238 progressMessageLabel.setText(message); 239 } 240 }); 241 } 242 243 private void stopProgress(final boolean connected) { 244 SwingUtilities.invokeLater(new Runnable () { 245 public void run() { 246 progressHandle.finish(); 247 progressContainerPanel.remove(progressComponent); 248 progressContainerPanel.repaint(); 250 if (connected) { 251 progressMessageLabel.setText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ConnectionProgress_Established")); 252 } else { 253 progressMessageLabel.setText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ConnectionProgress_Failed")); 254 } 255 } 256 }); 257 } 258 259 private void resetProgress() { 260 progressMessageLabel.setText(""); } 262 263 public void changedUpdate(javax.swing.event.DocumentEvent e) { 264 fireChange(); 265 } 266 267 public void insertUpdate(javax.swing.event.DocumentEvent e) { 268 fireChange(); 269 } 270 271 public void removeUpdate(javax.swing.event.DocumentEvent e) { 272 fireChange(); 273 } 274 275 public void contentsChanged(javax.swing.event.ListDataEvent e) { 276 fireChange(); 277 } 278 279 public void intervalAdded(javax.swing.event.ListDataEvent e) { 280 fireChange(); 281 } 282 283 public void intervalRemoved(javax.swing.event.ListDataEvent e) { 284 fireChange(); 285 } 286 287 private void fireChange() { 288 firePropertyChange("argumentChanged", null, null); 289 resetProgress(); 290 } 291 } 292 | Popular Tags |