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