KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > entres > DatasourceComboBoxCustomizer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.UIManager JavaDoc;
30 import javax.swing.event.DocumentEvent JavaDoc;
31 import javax.swing.event.DocumentListener JavaDoc;
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 /**
42  *
43  * @author Libor Kotouc
44  */

45 final class DatasourceComboBoxCustomizer extends javax.swing.JPanel JavaDoc {
46     
47     private final Color JavaDoc nbErrorForeground;
48     
49     private Dialog JavaDoc dialog = null;
50     private DialogDescriptor descriptor = null;
51     private boolean dialogOK = false;
52     
53     private final HashMap JavaDoc<String JavaDoc, Datasource> datasources;
54     
55     private String JavaDoc jndiName;
56     private String JavaDoc url;
57     private String JavaDoc username;
58     private String JavaDoc password;
59     private String JavaDoc driverClassName;
60
61     public DatasourceComboBoxCustomizer(Set JavaDoc<Datasource> datasources) {
62         this.datasources = new HashMap JavaDoc<String JavaDoc, Datasource>();
63         if (datasources != null) { // transform Set to Map for faster searching
64
for (Iterator JavaDoc 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 JavaDoc() {
75             public void actionPerformed(ActionEvent JavaDoc actionEvent) {
76                 verify();
77             }
78         });
79         
80         jndiNameField.getDocument().addDocumentListener(new DocumentListener JavaDoc() {
81             public void changedUpdate(DocumentEvent JavaDoc documentEvent) {
82                 verify();
83             }
84             public void insertUpdate(DocumentEvent JavaDoc documentEvent) {
85                 verify();
86             }
87             public void removeUpdate(DocumentEvent JavaDoc documentEvent) {
88                 verify();
89             }
90         });
91         
92         Color JavaDoc errorColor = UIManager.getColor("nb.errorForeground"); //NOI18N
93
if (errorColor == null)
94             errorColor = new Color JavaDoc(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"), // NOI18N
107
new ActionListener JavaDoc() {
108                 public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc[] { 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             //user did not provide the password
144
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 JavaDoc 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(""); // NOI18N
180
}
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(""); // NOI18N
197
}
198
199         descriptor.setValid(valid);
200         
201         return valid;
202     }
203     
204     private boolean datasourceAlreadyExists(String JavaDoc jndiName) {
205         return datasources.containsKey(jndiName);
206     }
207     
208     String JavaDoc getJndiName() {
209         return jndiName;
210     }
211
212     String JavaDoc getUrl() {
213         return url;
214     }
215
216     String JavaDoc getUsername() {
217         return username;
218     }
219
220     String JavaDoc getPassword() {
221         return password;
222     }
223
224     String JavaDoc getDriverClassName() {
225         return driverClassName;
226     }
227
228     /** This method is called from within the constructor to
229      * initialize the form.
230      * WARNING: Do NOT modify this code. The content of this method is
231      * always regenerated by the Form Editor.
232      */

233     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
234
private void initComponents() {
235         jLabel1 = new javax.swing.JLabel JavaDoc();
236         jLabel2 = new javax.swing.JLabel JavaDoc();
237         jndiNameField = new javax.swing.JTextField JavaDoc();
238         errorLabel = new javax.swing.JLabel JavaDoc();
239         connCombo = new javax.swing.JComboBox JavaDoc();
240         warningLabel = new javax.swing.JLabel JavaDoc();
241
242         setForeground(new java.awt.Color JavaDoc(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     }// </editor-fold>//GEN-END:initComponents
293

294     
295     // Variables declaration - do not modify//GEN-BEGIN:variables
296
private javax.swing.JComboBox JavaDoc connCombo;
297     private javax.swing.JLabel JavaDoc errorLabel;
298     private javax.swing.JLabel JavaDoc jLabel1;
299     private javax.swing.JLabel JavaDoc jLabel2;
300     private javax.swing.JTextField JavaDoc jndiNameField;
301     private javax.swing.JLabel JavaDoc warningLabel;
302     // End of variables declaration//GEN-END:variables
303

304 }
305
Popular Tags