1 19 20 package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres; 21 22 import java.awt.Color ; 23 import java.awt.Dialog ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Set ; 29 import javax.swing.UIManager ; 30 import javax.swing.event.DocumentEvent ; 31 import javax.swing.event.DocumentListener ; 32 import org.netbeans.api.db.explorer.ConnectionManager; 33 import org.netbeans.api.db.explorer.DatabaseConnection; 34 import org.netbeans.api.db.explorer.support.DatabaseExplorerUIs; 35 import org.netbeans.modules.j2ee.deployment.common.api.Datasource; 36 import org.openide.DialogDescriptor; 37 import org.openide.DialogDisplayer; 38 import org.openide.util.HelpCtx; 39 import org.openide.util.NbBundle; 40 41 45 final class DatasourceComboBoxCustomizer extends javax.swing.JPanel { 46 47 private final Color nbErrorForeground; 48 49 private Dialog dialog = null; 50 private DialogDescriptor descriptor = null; 51 private boolean dialogOK = false; 52 53 private final HashMap <String , Datasource> datasources; 54 55 private String jndiName; 56 private String url; 57 private String username; 58 private String password; 59 private String driverClassName; 60 61 public DatasourceComboBoxCustomizer(Set <Datasource> datasources) { 62 this.datasources = new HashMap <String , Datasource>(); 63 if (datasources != null) { for (Iterator it = datasources.iterator(); it.hasNext();) { 65 Datasource datasource = (Datasource) it.next(); 66 if (datasource.getJndiName() != null) 67 this.datasources.put(datasource.getJndiName(), datasource); 68 } 69 } 70 initComponents(); 71 72 DatabaseExplorerUIs.connect(connCombo, ConnectionManager.getDefault()); 73 74 connCombo.addActionListener(new ActionListener () { 75 public void actionPerformed(ActionEvent actionEvent) { 76 verify(); 77 } 78 }); 79 80 jndiNameField.getDocument().addDocumentListener(new DocumentListener () { 81 public void changedUpdate(DocumentEvent documentEvent) { 82 verify(); 83 } 84 public void insertUpdate(DocumentEvent documentEvent) { 85 verify(); 86 } 87 public void removeUpdate(DocumentEvent documentEvent) { 88 verify(); 89 } 90 }); 91 92 Color errorColor = UIManager.getColor("nb.errorForeground"); if (errorColor == null) 94 errorColor = new Color (255, 0, 0); 95 nbErrorForeground = errorColor; 96 97 errorLabel.setForeground(nbErrorForeground); 98 } 99 100 public boolean showDialog() { 101 102 descriptor = new DialogDescriptor 103 (this, NbBundle.getMessage(DatasourceComboBoxCustomizer.class, "LBL_DatasourceCustomizer"), true, 104 DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, 105 DialogDescriptor.DEFAULT_ALIGN, 106 new HelpCtx("DatasourceUIHelper_DatasourceCustomizer"), new ActionListener () { 108 public void actionPerformed(ActionEvent actionEvent) { 109 boolean close = true; 110 if (descriptor.getValue().equals(DialogDescriptor.OK_OPTION)) { 111 boolean valid = handleConfirmation(); 112 close = valid; 113 dialogOK = valid; 114 } 115 116 if (close) { 117 dialog.dispose(); 118 } 119 } 120 }); 121 122 descriptor.setClosingOptions(new Object [] { DialogDescriptor.CANCEL_OPTION }); 123 124 verify(); 125 126 dialog = DialogDisplayer.getDefault().createDialog(descriptor); 127 dialog.setVisible(true); 128 repaint(); 129 130 return dialogOK; 131 } 132 133 private boolean handleConfirmation() { 134 135 jndiName = jndiNameField.getText().trim(); 136 137 DatabaseConnection conn = (DatabaseConnection)connCombo.getSelectedItem(); 138 139 if (conn.getPassword() == null) { 140 ConnectionManager.getDefault().showConnectionDialog(conn); 141 } 142 if (conn.getPassword() == null) { 143 errorLabel.setText(NbBundle.getMessage(DatasourceComboBoxCustomizer.class, "ERR_NoPassword")); 145 return false; 146 } 147 url = conn.getDatabaseURL(); 148 username = conn.getUser(); 149 password = conn.getPassword(); 150 driverClassName = conn.getDriverClass(); 151 152 return true; 153 } 154 155 private boolean verify() { 156 157 boolean isValid = verifyJndiName(); 158 if (isValid) 159 isValid = verifyConnection(); 160 161 return isValid; 162 } 163 164 private boolean verifyJndiName() { 165 166 boolean valid = true; 167 168 String jndiName = jndiNameField.getText().trim(); 169 if (jndiName.length() == 0) { 170 errorLabel.setText(NbBundle.getMessage(DatasourceComboBoxCustomizer.class, "ERR_JNDI_NAME_EMPTY")); 171 valid = false; 172 } 173 else 174 if (datasourceAlreadyExists(jndiName)) { 175 errorLabel.setText(NbBundle.getMessage(DatasourceComboBoxCustomizer.class, "ERR_DS_EXISTS")); 176 valid = false; 177 } 178 else { 179 errorLabel.setText(""); } 181 182 descriptor.setValid(valid); 183 184 return valid; 185 } 186 187 private boolean verifyConnection() { 188 189 boolean valid = true; 190 191 if (!(connCombo.getSelectedItem() instanceof DatabaseConnection)) { 192 errorLabel.setText(NbBundle.getMessage(DatasourceComboBoxCustomizer.class, "ERR_NO_CONN_SELECTED")); 193 valid = false; 194 } 195 else { 196 errorLabel.setText(""); } 198 199 descriptor.setValid(valid); 200 201 return valid; 202 } 203 204 private boolean datasourceAlreadyExists(String jndiName) { 205 return datasources.containsKey(jndiName); 206 } 207 208 String getJndiName() { 209 return jndiName; 210 } 211 212 String getUrl() { 213 return url; 214 } 215 216 String getUsername() { 217 return username; 218 } 219 220 String getPassword() { 221 return password; 222 } 223 224 String getDriverClassName() { 225 return driverClassName; 226 } 227 228 233 private void initComponents() { 235 jLabel1 = new javax.swing.JLabel (); 236 jLabel2 = new javax.swing.JLabel (); 237 jndiNameField = new javax.swing.JTextField (); 238 errorLabel = new javax.swing.JLabel (); 239 connCombo = new javax.swing.JComboBox (); 240 warningLabel = new javax.swing.JLabel (); 241 242 setForeground(new java.awt.Color (255, 0, 0)); 243 jLabel1.setLabelFor(jndiNameField); 244 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(DatasourceComboBoxCustomizer.class, "LBL_DSC_JndiName")); 245 246 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(DatasourceComboBoxCustomizer.class, "LBL_DSC_DbConn")); 247 248 org.openide.awt.Mnemonics.setLocalizedText(warningLabel, org.openide.util.NbBundle.getMessage(DatasourceComboBoxCustomizer.class, "LBL_DSC_Warning")); 249 warningLabel.setEnabled(false); 250 warningLabel.setFocusable(false); 251 252 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 253 this.setLayout(layout); 254 layout.setHorizontalGroup( 255 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 256 .add(layout.createSequentialGroup() 257 .addContainerGap() 258 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 259 .add(layout.createSequentialGroup() 260 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 261 .add(jLabel1) 262 .add(jLabel2)) 263 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 264 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 265 .add(layout.createSequentialGroup() 266 .add(jndiNameField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE) 267 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)) 268 .add(connCombo, 0, 359, Short.MAX_VALUE))) 269 .add(errorLabel) 270 .add(warningLabel)) 271 .addContainerGap()) 272 ); 273 layout.setVerticalGroup( 274 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 275 .add(layout.createSequentialGroup() 276 .addContainerGap() 277 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 278 .add(jLabel1) 279 .add(jndiNameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 280 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 281 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 282 .add(layout.createSequentialGroup() 283 .add(jLabel2) 284 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 33, Short.MAX_VALUE) 285 .add(warningLabel) 286 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 287 .add(errorLabel)) 288 .add(layout.createSequentialGroup() 289 .add(connCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 290 .addContainerGap()))) 291 ); 292 } 294 295 private javax.swing.JComboBox connCombo; 297 private javax.swing.JLabel errorLabel; 298 private javax.swing.JLabel jLabel1; 299 private javax.swing.JLabel jLabel2; 300 private javax.swing.JTextField jndiNameField; 301 private javax.swing.JLabel warningLabel; 302 304 } 305 | Popular Tags |