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.Dialog;
045: import java.awt.event.ActionListener;
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import java.util.ResourceBundle;
049:
050: import javax.swing.JPanel;
051: import javax.swing.JTabbedPane;
052:
053: import javax.swing.event.ChangeListener;
054:
055: import org.openide.DialogDescriptor;
056: import org.openide.DialogDisplayer;
057: import org.openide.NotifyDescriptor;
058: import org.openide.util.HelpCtx;
059: import org.openide.util.NbBundle;
060:
061: public class ConnectionDialog {
062:
063: private transient ConnectionDialogMediator mediator;
064: private transient JTabbedPane tabs;
065: private transient Exception storedExp;
066:
067: ResourceBundle bundle = NbBundle
068: .getBundle("org.netbeans.modules.db.resources.Bundle"); //NOI18N
069:
070: final DialogDescriptor descriptor;
071: final Dialog dialog;
072:
073: public ConnectionDialog(ConnectionDialogMediator mediator,
074: FocusablePanel basePane, JPanel extendPane,
075: String dlgTitle, HelpCtx helpCtx,
076: ActionListener actionListener, ChangeListener tabListener) {
077: if (basePane.equals(extendPane)) {
078: throw new IllegalArgumentException(
079: "The basePane and extendPane must not equal!"); // NOI18N
080: }
081:
082: this .mediator = mediator;
083: ConnectionProgressListener progressListener = new ConnectionProgressListener() {
084: public void connectionStarted() {
085: descriptor.setValid(false);
086: }
087:
088: public void connectionStep(String step) {
089: }
090:
091: public void connectionFinished() {
092: descriptor.setValid(true);
093: }
094:
095: public void connectionFailed() {
096: descriptor.setValid(true);
097: }
098: };
099: mediator.addConnectionProgressListener(progressListener);
100:
101: PropertyChangeListener propChangeListener = new PropertyChangeListener() {
102: public void propertyChange(PropertyChangeEvent evt) {
103: String propertyName = evt.getPropertyName();
104: if (propertyName == null
105: || propertyName
106: .equals(ConnectionDialogMediator.PROP_VALID)) {
107: updateValid();
108: }
109: }
110: };
111: mediator.addPropertyChangeListener(propChangeListener);
112:
113: tabs = new JTabbedPane(JTabbedPane.TOP);
114:
115: tabs.addChangeListener(tabListener);
116:
117: // base panel for set base info for connection
118: tabs.addTab(bundle.getString("BasePanelTitle"), // NOI18N
119: /*icon*/null, basePane, bundle
120: .getString("BasePanelHint")); // NOI18N
121:
122: // extend panel for select schema name
123: tabs.addTab(bundle.getString("ExtendPanelTitle"), // NOI18N
124: /*icon*/null, extendPane, bundle
125: .getString("ExtendPanelHint")); // NOI18N
126:
127: tabs.getAccessibleContext().setAccessibleName(
128: bundle.getString("ACS_ConnectDialogA11yName"));
129: tabs.getAccessibleContext().setAccessibleDescription(
130: bundle.getString("ACS_ConnectDialogA11yDesc"));
131:
132: descriptor = new DialogDescriptor(tabs, dlgTitle, true,
133: DialogDescriptor.OK_CANCEL_OPTION,
134: DialogDescriptor.CANCEL_OPTION,
135: DialogDescriptor.DEFAULT_ALIGN, helpCtx, actionListener);
136: // inbuilt close of the dialog is only after CANCEL button click
137: // after OK button is dialog closed by hand
138: Object[] closingOptions = { DialogDescriptor.CANCEL_OPTION };
139: descriptor.setClosingOptions(closingOptions);
140: updateValid();
141: dialog = DialogDisplayer.getDefault().createDialog(descriptor);
142: // needed for issue 82787, allows the panel to request the focus
143: // to the password text field
144: basePane.initializeFocus();
145: dialog.setVisible(false);
146: }
147:
148: public void close() {
149: // dialog is closed after successfully create connection
150: dialog.setVisible(false);
151: dialog.dispose();
152: }
153:
154: public void setVisible(boolean mode) {
155: dialog.setVisible(mode);
156: }
157:
158: public void setSelectedComponent(JPanel panel) {
159: tabs.setSelectedComponent(panel);
160: }
161:
162: public void setException(Exception e) {
163: storedExp = e;
164: }
165:
166: public boolean isException() {
167: return (storedExp != null);
168: }
169:
170: private void updateValid() {
171: boolean valid = ConnectionDialog.this .mediator.getValid();
172: descriptor.setValid(valid);
173: tabs.setEnabledAt(1, valid);
174: }
175:
176: /**
177: * A {@link JPanel} with an {@link #initializeFocus} method whose implementation
178: * can call {@link JComponent#requestFocusInWindow} on a children component.
179: * Needed because <code>requestFocusInWindow</code> must be called
180: * after a component was <code>pack()</code>-ed, but before it is displayed, and
181: * the <code>JPanel</code>, which is displayed using <code>DialogDescriptor</code>
182: * does not know when this happens.
183: */
184: public static abstract class FocusablePanel extends JPanel {
185:
186: public abstract void initializeFocus();
187: }
188: }
|