KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > dlg > ConnectPanel


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.db.explorer.dlg;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27
28 import javax.swing.event.DocumentListener JavaDoc;
29 import javax.swing.event.ListDataListener JavaDoc;
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 JavaDoc, ListDataListener JavaDoc {
38
39     private ConnectionDialogMediator mediator;
40     private DatabaseConnection connection;
41     private ProgressHandle progressHandle;
42     private JComponent JavaDoc progressComponent;
43
44     /**
45      * Creates new form ConnectPanel
46      *
47      * @param connection instance of DatabaseConnection object
48      */

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 JavaDoc 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")); //NOI18N
82
userTextField.getAccessibleContext().setAccessibleName(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogUserNameTextFieldA11yName")); //NOI18N
83
passwordLabel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogPasswordA11yDesc")); //NOI18N
84
passwordField.getAccessibleContext().setAccessibleName(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogPasswordTextFieldA11yName")); //NOI18N
85
connectProgressPanel.getAccessibleContext().setAccessibleName(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectionProgressBarA11yName")); //NOI18N
86
connectProgressPanel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectionProgressBarA11yDesc")); //NOI18N
87
}
88
89     public void initializeFocus() {
90         getInitiallyFocusedComponent().requestFocusInWindow();
91     }
92
93     private JComponent JavaDoc getInitiallyFocusedComponent() {
94         if (userTextField.getText().length() == 0) {
95             return userTextField;
96         }
97         if (passwordField.getPassword().length == 0) {
98             return passwordField;
99         }
100         // fall back to the user field
101
return userTextField;
102     }
103
104     /** This method is called from within the constructor to
105      * initialize the form.
106      * WARNING: Do NOT modify this code. The content of this method is
107      * always regenerated by the Form Editor.
108      */

109     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
110
private void initComponents() {
111         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
112
113         userLabel = new javax.swing.JLabel JavaDoc();
114         passwordLabel = new javax.swing.JLabel JavaDoc();
115         passwordCheckBox = new javax.swing.JCheckBox JavaDoc();
116         passwordField = new javax.swing.JPasswordField JavaDoc();
117         userTextField = new javax.swing.JTextField JavaDoc();
118         connectProgressPanel = new javax.swing.JPanel JavaDoc();
119         progressMessageLabel = new javax.swing.JLabel JavaDoc();
120         progressContainerPanel = new javax.swing.JPanel JavaDoc();
121
122         setLayout(new java.awt.GridBagLayout JavaDoc());
123
124         userLabel.setLabelFor(userTextField);
125         org.openide.awt.Mnemonics.setLocalizedText(userLabel, NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ConnectDialogUserName")); // NOI18N
126
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
127         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
128         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
129         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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")); // NOI18N
134
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(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")); // NOI18N
143
passwordCheckBox.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogRememberPasswordA11yDesc")); // NOI18N
144
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(5, 5, 0, 11);
150         add(passwordCheckBox, gridBagConstraints);
151
152         passwordField.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogPasswordTextFieldA11yDesc")); // NOI18N
153
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
154         gridBagConstraints.gridx = 1;
155         gridBagConstraints.gridy = 1;
156         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
157         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 0, 11);
158         add(passwordField, gridBagConstraints);
159
160         userTextField.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectDialogUserNameTextFieldA11yDesc")); // NOI18N
161
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
162         gridBagConstraints.gridx = 1;
163         gridBagConstraints.gridy = 0;
164         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
165         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 5, 0, 11);
166         add(userTextField, gridBagConstraints);
167
168         connectProgressPanel.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectionProgressBarA11yDesc")); // NOI18N
169
connectProgressPanel.setLayout(new java.awt.BorderLayout JavaDoc(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 JavaDoc(20, 20));
175         progressContainerPanel.setPreferredSize(new java.awt.Dimension JavaDoc(20, 20));
176         progressContainerPanel.setLayout(new java.awt.BorderLayout JavaDoc());
177         connectProgressPanel.add(progressContainerPanel, java.awt.BorderLayout.CENTER);
178
179         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(12, 12, 11, 11);
188         add(connectProgressPanel, gridBagConstraints);
189     }// </editor-fold>//GEN-END:initComponents
190

191
192     // Variables declaration - do not modify//GEN-BEGIN:variables
193
private javax.swing.JPanel JavaDoc connectProgressPanel;
194     private javax.swing.JCheckBox JavaDoc passwordCheckBox;
195     private javax.swing.JPasswordField JavaDoc passwordField;
196     private javax.swing.JLabel JavaDoc passwordLabel;
197     private javax.swing.JPanel JavaDoc progressContainerPanel;
198     private javax.swing.JLabel JavaDoc progressMessageLabel;
199     private javax.swing.JLabel JavaDoc userLabel;
200     private javax.swing.JTextField JavaDoc userTextField;
201     // End of variables declaration//GEN-END:variables
202

203     public String JavaDoc getUser() {
204         return userTextField.getText();
205     }
206
207     public String JavaDoc 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 JavaDoc getTitle() {
220         return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ConnectDialogTitle"); // NOI18N
221
}
222
223     private void startProgress() {
224         SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc message) {
236         SwingUtilities.invokeLater(new Runnable JavaDoc() {
237             public void run() {
238                 progressMessageLabel.setText(message);
239             }
240         });
241     }
242
243     private void stopProgress(final boolean connected) {
244         SwingUtilities.invokeLater(new Runnable JavaDoc() {
245             public void run() {
246                 progressHandle.finish();
247                 progressContainerPanel.remove(progressComponent);
248                 // without this, the removed progress component remains painted on its parent... why?
249
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(""); // NOI18N
261
}
262
263     public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
264         fireChange();
265     }
266
267     public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
268         fireChange();
269     }
270
271     public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
272         fireChange();
273     }
274
275     public void contentsChanged(javax.swing.event.ListDataEvent JavaDoc e) {
276         fireChange();
277     }
278
279     public void intervalAdded(javax.swing.event.ListDataEvent JavaDoc e) {
280         fireChange();
281     }
282
283     public void intervalRemoved(javax.swing.event.ListDataEvent JavaDoc e) {
284         fireChange();
285     }
286
287     private void fireChange() {
288         firePropertyChange("argumentChanged", null, null);
289         resetProgress();
290     }
291 }
292
Popular Tags