001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer.dlg;
043:
044: import java.awt.BorderLayout;
045: import javax.swing.JComponent;
046: import javax.swing.SwingUtilities;
047:
048: import javax.swing.event.DocumentListener;
049: import javax.swing.event.ListDataListener;
050: import org.netbeans.api.progress.ProgressHandle;
051: import org.netbeans.api.progress.ProgressHandleFactory;
052:
053: import org.openide.util.NbBundle;
054:
055: import org.netbeans.modules.db.explorer.DatabaseConnection;
056:
057: public class ConnectPanel extends ConnectionDialog.FocusablePanel
058: implements DocumentListener, ListDataListener {
059:
060: private ConnectionDialogMediator mediator;
061: private DatabaseConnection connection;
062: private ProgressHandle progressHandle;
063: private JComponent progressComponent;
064:
065: /**
066: * Creates new form ConnectPanel
067: *
068: * @param connection instance of DatabaseConnection object
069: */
070: public ConnectPanel(ConnectionDialogMediator mediator,
071: DatabaseConnection connection) {
072: this .mediator = mediator;
073: this .connection = connection;
074: initComponents();
075: initAccessibility();
076:
077: ConnectionProgressListener progressListener = new ConnectionProgressListener() {
078: public void connectionStarted() {
079: startProgress();
080: }
081:
082: public void connectionStep(String step) {
083: setProgressMessage(step);
084: }
085:
086: public void connectionFinished() {
087: stopProgress(true);
088: }
089:
090: public void connectionFailed() {
091: stopProgress(false);
092: }
093: };
094: mediator.addConnectionProgressListener(progressListener);
095:
096: userTextField.setText(connection.getUser());
097: if (connection.rememberPassword()) {
098: passwordField.setText(connection.getPassword());
099: passwordCheckBox.setSelected(true);
100: }
101:
102: userTextField.getDocument().addDocumentListener(this );
103: passwordField.getDocument().addDocumentListener(this );
104: }
105:
106: private void initAccessibility() {
107: userLabel
108: .getAccessibleContext()
109: .setAccessibleDescription(
110: NbBundle
111: .getBundle(
112: "org.netbeans.modules.db.resources.Bundle")
113: .getString(
114: "ACS_ConnectDialogUserNameA11yDesc")); //NOI18N
115: userTextField
116: .getAccessibleContext()
117: .setAccessibleName(
118: NbBundle
119: .getBundle(
120: "org.netbeans.modules.db.resources.Bundle")
121: .getString(
122: "ACS_ConnectDialogUserNameTextFieldA11yName")); //NOI18N
123: passwordLabel
124: .getAccessibleContext()
125: .setAccessibleDescription(
126: NbBundle
127: .getBundle(
128: "org.netbeans.modules.db.resources.Bundle")
129: .getString(
130: "ACS_ConnectDialogPasswordA11yDesc")); //NOI18N
131: passwordField
132: .getAccessibleContext()
133: .setAccessibleName(
134: NbBundle
135: .getBundle(
136: "org.netbeans.modules.db.resources.Bundle")
137: .getString(
138: "ACS_ConnectDialogPasswordTextFieldA11yName")); //NOI18N
139: connectProgressPanel
140: .getAccessibleContext()
141: .setAccessibleName(
142: NbBundle
143: .getBundle(
144: "org.netbeans.modules.db.resources.Bundle")
145: .getString(
146: "ACS_ConnectionProgressBarA11yName")); //NOI18N
147: connectProgressPanel
148: .getAccessibleContext()
149: .setAccessibleDescription(
150: NbBundle
151: .getBundle(
152: "org.netbeans.modules.db.resources.Bundle")
153: .getString(
154: "ACS_ConnectionProgressBarA11yDesc")); //NOI18N
155: }
156:
157: public void initializeFocus() {
158: getInitiallyFocusedComponent().requestFocusInWindow();
159: }
160:
161: private JComponent getInitiallyFocusedComponent() {
162: if (userTextField.getText().length() == 0) {
163: return userTextField;
164: }
165: if (passwordField.getPassword().length == 0) {
166: return passwordField;
167: }
168: // fall back to the user field
169: return userTextField;
170: }
171:
172: /** This method is called from within the constructor to
173: * initialize the form.
174: * WARNING: Do NOT modify this code. The content of this method is
175: * always regenerated by the Form Editor.
176: */
177: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
178: private void initComponents() {
179: java.awt.GridBagConstraints gridBagConstraints;
180:
181: userLabel = new javax.swing.JLabel();
182: passwordLabel = new javax.swing.JLabel();
183: passwordCheckBox = new javax.swing.JCheckBox();
184: passwordField = new javax.swing.JPasswordField();
185: userTextField = new javax.swing.JTextField();
186: connectProgressPanel = new javax.swing.JPanel();
187: progressMessageLabel = new javax.swing.JLabel();
188: progressContainerPanel = new javax.swing.JPanel();
189:
190: setLayout(new java.awt.GridBagLayout());
191:
192: userLabel.setLabelFor(userTextField);
193: org.openide.awt.Mnemonics.setLocalizedText(userLabel, NbBundle
194: .getBundle("org.netbeans.modules.db.resources.Bundle")
195: .getString("ConnectDialogUserName")); // NOI18N
196: gridBagConstraints = new java.awt.GridBagConstraints();
197: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
198: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
199: gridBagConstraints.insets = new java.awt.Insets(12, 12, 0, 0);
200: add(userLabel, gridBagConstraints);
201:
202: passwordLabel.setLabelFor(passwordField);
203: org.openide.awt.Mnemonics.setLocalizedText(passwordLabel,
204: NbBundle.getBundle(
205: "org.netbeans.modules.db.resources.Bundle")
206: .getString("ConnectDialogPassword")); // NOI18N
207: gridBagConstraints = new java.awt.GridBagConstraints();
208: gridBagConstraints.gridx = 0;
209: gridBagConstraints.gridy = 1;
210: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
211: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
212: gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 0);
213: add(passwordLabel, gridBagConstraints);
214:
215: org.openide.awt.Mnemonics.setLocalizedText(passwordCheckBox,
216: NbBundle.getBundle(
217: "org.netbeans.modules.db.resources.Bundle")
218: .getString("ConnectDialogRememberPassword")); // NOI18N
219: passwordCheckBox.setToolTipText(NbBundle.getBundle(
220: "org.netbeans.modules.db.resources.Bundle").getString(
221: "ACS_ConnectDialogRememberPasswordA11yDesc")); // NOI18N
222: passwordCheckBox
223: .setVerticalTextPosition(javax.swing.SwingConstants.TOP);
224: gridBagConstraints = new java.awt.GridBagConstraints();
225: gridBagConstraints.gridx = 1;
226: gridBagConstraints.gridy = 2;
227: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
228: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
229: gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 11);
230: add(passwordCheckBox, gridBagConstraints);
231:
232: passwordField.setToolTipText(NbBundle.getBundle(
233: "org.netbeans.modules.db.resources.Bundle").getString(
234: "ACS_ConnectDialogPasswordTextFieldA11yDesc")); // NOI18N
235: gridBagConstraints = new java.awt.GridBagConstraints();
236: gridBagConstraints.gridx = 1;
237: gridBagConstraints.gridy = 1;
238: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
239: gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 11);
240: add(passwordField, gridBagConstraints);
241:
242: userTextField.setToolTipText(NbBundle.getBundle(
243: "org.netbeans.modules.db.resources.Bundle").getString(
244: "ACS_ConnectDialogUserNameTextFieldA11yDesc")); // NOI18N
245: gridBagConstraints = new java.awt.GridBagConstraints();
246: gridBagConstraints.gridx = 1;
247: gridBagConstraints.gridy = 0;
248: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
249: gridBagConstraints.insets = new java.awt.Insets(12, 5, 0, 11);
250: add(userTextField, gridBagConstraints);
251:
252: connectProgressPanel.setToolTipText(NbBundle.getBundle(
253: "org.netbeans.modules.db.resources.Bundle").getString(
254: "ACS_ConnectionProgressBarA11yDesc")); // NOI18N
255: connectProgressPanel.setLayout(new java.awt.BorderLayout(0, 5));
256:
257: org.openide.awt.Mnemonics.setLocalizedText(
258: progressMessageLabel, " ");
259: connectProgressPanel.add(progressMessageLabel,
260: java.awt.BorderLayout.NORTH);
261:
262: progressContainerPanel.setMinimumSize(new java.awt.Dimension(
263: 20, 20));
264: progressContainerPanel.setPreferredSize(new java.awt.Dimension(
265: 20, 20));
266: progressContainerPanel.setLayout(new java.awt.BorderLayout());
267: connectProgressPanel.add(progressContainerPanel,
268: java.awt.BorderLayout.CENTER);
269:
270: gridBagConstraints = new java.awt.GridBagConstraints();
271: gridBagConstraints.gridx = 0;
272: gridBagConstraints.gridy = 3;
273: gridBagConstraints.gridwidth = 2;
274: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
275: gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
276: gridBagConstraints.weightx = 1.0;
277: gridBagConstraints.weighty = 1.0;
278: gridBagConstraints.insets = new java.awt.Insets(12, 12, 11, 11);
279: add(connectProgressPanel, gridBagConstraints);
280: }// </editor-fold>//GEN-END:initComponents
281:
282: // Variables declaration - do not modify//GEN-BEGIN:variables
283: private javax.swing.JPanel connectProgressPanel;
284: private javax.swing.JCheckBox passwordCheckBox;
285: private javax.swing.JPasswordField passwordField;
286: private javax.swing.JLabel passwordLabel;
287: private javax.swing.JPanel progressContainerPanel;
288: private javax.swing.JLabel progressMessageLabel;
289: private javax.swing.JLabel userLabel;
290: private javax.swing.JTextField userTextField;
291:
292: // End of variables declaration//GEN-END:variables
293:
294: public String getUser() {
295: return userTextField.getText();
296: }
297:
298: public String getPassword() {
299: return String.valueOf(passwordField.getPassword());
300: }
301:
302: public void showRememberPasswordOption(boolean flag) {
303: passwordCheckBox.setVisible(flag);
304: }
305:
306: public boolean rememberPassword() {
307: return passwordCheckBox.isSelected();
308: }
309:
310: public String getTitle() {
311: return NbBundle.getBundle(
312: "org.netbeans.modules.db.resources.Bundle").getString(
313: "ConnectDialogTitle"); // NOI18N
314: }
315:
316: private void startProgress() {
317: SwingUtilities.invokeLater(new Runnable() {
318: public void run() {
319: progressHandle = ProgressHandleFactory
320: .createHandle(null);
321: progressComponent = ProgressHandleFactory
322: .createProgressComponent(progressHandle);
323: progressContainerPanel.add(progressComponent,
324: BorderLayout.CENTER);
325: progressHandle.start();
326: progressMessageLabel.setText(NbBundle.getBundle(
327: "org.netbeans.modules.db.resources.Bundle")
328: .getString("ConnectionProgress_Connecting"));
329: }
330: });
331: }
332:
333: private void setProgressMessage(final String message) {
334: SwingUtilities.invokeLater(new Runnable() {
335: public void run() {
336: progressMessageLabel.setText(message);
337: }
338: });
339: }
340:
341: private void stopProgress(final boolean connected) {
342: SwingUtilities.invokeLater(new Runnable() {
343: public void run() {
344: progressHandle.finish();
345: progressContainerPanel.remove(progressComponent);
346: // without this, the removed progress component remains painted on its parent... why?
347: progressContainerPanel.repaint();
348: if (connected) {
349: progressMessageLabel
350: .setText(NbBundle
351: .getBundle(
352: "org.netbeans.modules.db.resources.Bundle")
353: .getString(
354: "ConnectionProgress_Established"));
355: } else {
356: progressMessageLabel.setText(NbBundle.getBundle(
357: "org.netbeans.modules.db.resources.Bundle")
358: .getString("ConnectionProgress_Failed"));
359: }
360: }
361: });
362: }
363:
364: private void resetProgress() {
365: progressMessageLabel.setText(""); // NOI18N
366: }
367:
368: public void changedUpdate(javax.swing.event.DocumentEvent e) {
369: fireChange();
370: }
371:
372: public void insertUpdate(javax.swing.event.DocumentEvent e) {
373: fireChange();
374: }
375:
376: public void removeUpdate(javax.swing.event.DocumentEvent e) {
377: fireChange();
378: }
379:
380: public void contentsChanged(javax.swing.event.ListDataEvent e) {
381: fireChange();
382: }
383:
384: public void intervalAdded(javax.swing.event.ListDataEvent e) {
385: fireChange();
386: }
387:
388: public void intervalRemoved(javax.swing.event.ListDataEvent e) {
389: fireChange();
390: }
391:
392: private void fireChange() {
393: firePropertyChange("argumentChanged", null, null);
394: resetProgress();
395: }
396: }
|