KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > common > DatasourceCustomizer


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.common;
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.awt.event.ItemEvent JavaDoc;
27 import java.awt.event.ItemListener JavaDoc;
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import javax.swing.UIManager JavaDoc;
34 import javax.swing.event.DocumentEvent JavaDoc;
35 import javax.swing.event.DocumentListener JavaDoc;
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 /**
47  *
48  * @author Libor Kotouc
49  */

50 class DatasourceCustomizer extends javax.swing.JPanel JavaDoc {
51     
52     private final Color JavaDoc nbErrorForeground;
53     
54     private Dialog JavaDoc dialog = null;
55     private DialogDescriptor descriptor = null;
56     private boolean dialogOK = false;
57     
58     private HashMap JavaDoc<String JavaDoc, Datasource> datasources;
59     
60     private String JavaDoc jndiName;
61     private String JavaDoc url;
62     private String JavaDoc username;
63     private String JavaDoc password;
64     private String JavaDoc driverClassName;
65
66     public DatasourceCustomizer(List JavaDoc<Datasource> datasources) {
67         if (datasources != null) { // transform Set to Map for faster searching
68
this.datasources = new HashMap JavaDoc<String JavaDoc, Datasource>();
69             for (Iterator JavaDoc 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 JavaDoc() {
80             public void actionPerformed(ActionEvent JavaDoc e) {
81                 verify();
82             }
83         });
84         
85         jndiNameField.getDocument().addDocumentListener(new DocumentListener JavaDoc() {
86             public void changedUpdate(DocumentEvent JavaDoc e) {
87                 verify();
88             }
89             public void insertUpdate(DocumentEvent JavaDoc e) {
90                 verify();
91             }
92             public void removeUpdate(DocumentEvent JavaDoc e) {
93                 verify();
94             }
95         });
96         
97         Color JavaDoc errorColor = UIManager.getColor("nb.errorForeground"); //NOI18N
98
if (errorColor == null)
99             errorColor = new Color JavaDoc(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 JavaDoc() {
113                         public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc[] { 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             //user did not provide the password
150
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 JavaDoc jndiName = jndiNameField.getText().trim();
175         if (jndiName.length() == 0) {
176             errorLabel.setText(NbBundle.getMessage(DatasourceCustomizer.class, "ERR_JNDI_NAME_EMPTY")); // NOI18N
177
valid = false;
178         }
179         else
180         if (datasourceAlreadyExists(jndiName)) {
181             errorLabel.setText(NbBundle.getMessage(DatasourceCustomizer.class, "ERR_DS_EXISTS")); // NOI18N
182
valid = false;
183         }
184         else {
185             errorLabel.setText(""); // NOI18N
186
}
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")); // NOI18N
199
valid = false;
200         }
201         else {
202             errorLabel.setText(""); // NOI18N
203
}
204
205         descriptor.setValid(valid);
206         
207         return valid;
208     }
209     
210     private boolean datasourceAlreadyExists(String JavaDoc jndiName) {
211         return datasources.containsKey(jndiName);
212     }
213     
214     String JavaDoc getJndiName() {
215         return jndiName;
216     }
217
218     String JavaDoc getUrl() {
219         return url;
220     }
221
222     String JavaDoc getUsername() {
223         return username;
224     }
225
226     String JavaDoc getPassword() {
227         return password;
228     }
229
230     String JavaDoc getDriverClassName() {
231         return driverClassName;
232     }
233
234     /** This method is called from within the constructor to
235      * initialize the form.
236      * WARNING: Do NOT modify this code. The content of this method is
237      * always regenerated by the Form Editor.
238      */

239     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
240
private void initComponents() {
241         jLabel1 = new javax.swing.JLabel JavaDoc();
242         jLabel2 = new javax.swing.JLabel JavaDoc();
243         jndiNameField = new javax.swing.JTextField JavaDoc();
244         errorLabel = new javax.swing.JLabel JavaDoc();
245         connCombo = new javax.swing.JComboBox JavaDoc();
246
247         setForeground(new java.awt.Color JavaDoc(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     }// </editor-fold>//GEN-END:initComponents
291

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

301 }
302
Popular Tags