KickJava   Java API By Example, From Geeks To Geeks.

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


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.Dialog JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.JTabbedPane JavaDoc;
30
31 import javax.swing.event.ChangeListener JavaDoc;
32
33 import org.openide.DialogDescriptor;
34 import org.openide.DialogDisplayer;
35 import org.openide.util.NbBundle;
36
37 public class ConnectionDialog {
38
39     private transient ConnectionDialogMediator mediator;
40     private transient JTabbedPane JavaDoc tabs;
41     private transient Exception JavaDoc storedExp;
42
43     ResourceBundle JavaDoc bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); //NOI18N
44

45     final DialogDescriptor descriptor;
46     final Dialog JavaDoc dialog;
47     
48     public ConnectionDialog(ConnectionDialogMediator mediator, FocusablePanel basePane, JPanel JavaDoc extendPane, String JavaDoc dlgTitle, ActionListener JavaDoc actionListener, ChangeListener JavaDoc tabListener) {
49         if(basePane.equals(extendPane)) {
50             throw new IllegalArgumentException JavaDoc("The basePane and extendPane must not equal!"); // NOI18N
51
}
52         
53         this.mediator = mediator;
54         ConnectionProgressListener progressListener = new ConnectionProgressListener() {
55             public void connectionStarted() {
56                 descriptor.setValid(false);
57             }
58             
59             public void connectionStep(String JavaDoc step) {
60             }
61
62             public void connectionFinished() {
63                 descriptor.setValid(true);
64             }
65
66             public void connectionFailed() {
67                 descriptor.setValid(true);
68             }
69         };
70         mediator.addConnectionProgressListener(progressListener);
71         
72         PropertyChangeListener JavaDoc propChangeListener = new PropertyChangeListener JavaDoc() {
73             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
74                 String JavaDoc propertyName = evt.getPropertyName();
75                 if (propertyName == null || propertyName.equals(ConnectionDialogMediator.PROP_VALID)) {
76                     updateValid();
77                 }
78             }
79         };
80         mediator.addPropertyChangeListener(propChangeListener);
81
82         tabs = new JTabbedPane JavaDoc(JTabbedPane.TOP);
83         
84         tabs.addChangeListener(tabListener);
85
86         // base panel for set base info for connection
87
tabs.addTab( bundle.getString("BasePanelTitle"), // NOI18N
88
/*icon*/ null, basePane,
89                     bundle.getString("BasePanelHint") ); // NOI18N
90

91         // extend panel for select schema name
92
tabs.addTab( bundle.getString("ExtendPanelTitle"), // NOI18N
93
/*icon*/ null,
94                     extendPane,
95                     bundle.getString("ExtendPanelHint") ); // NOI18N
96

97         tabs.getAccessibleContext().setAccessibleName(bundle.getString("ACS_ConnectDialogA11yName"));
98         tabs.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_ConnectDialogA11yDesc"));
99
100         descriptor = new DialogDescriptor(tabs, dlgTitle, true, actionListener); //NOI18N
101
// inbuilt close of the dialog is only after CANCEL button click
102
// after OK button is dialog closed by hand
103
Object JavaDoc [] closingOptions = {DialogDescriptor.CANCEL_OPTION};
104         descriptor.setClosingOptions(closingOptions);
105         updateValid();
106         dialog = DialogDisplayer.getDefault().createDialog(descriptor);
107         // needed for issue 82787, allows the panel to request the focus
108
// to the password text field
109
basePane.initializeFocus();
110         dialog.setVisible(false);
111     }
112     
113     public void close() {
114         // dialog is closed after successfully create connection
115
dialog.setVisible(false);
116         dialog.dispose();
117     }
118     
119     public void setVisible(boolean mode) {
120         dialog.setVisible(mode);
121     }
122     
123     public void setSelectedComponent(JPanel JavaDoc panel) {
124         tabs.setSelectedComponent(panel);
125     }
126     
127     public void setException(Exception JavaDoc e) {
128         storedExp = e;
129     }
130     
131     public boolean isException() {
132         return (storedExp != null);
133     }
134     
135     private void updateValid() {
136         boolean valid = ConnectionDialog.this.mediator.getValid();
137         descriptor.setValid(valid);
138         tabs.setEnabledAt(1, valid);
139     }
140     
141     /**
142      * A {@link JPanel} with an {@link #initializeFocus} method whose implementation
143      * can call {@link JComponent#requestFocusInWindow} on a children component.
144      * Needed because <code>requestFocusInWindow</code> must be called
145      * after a component was <code>pack()</code>-ed, but before it is displayed, and
146      * the <code>JPanel</code>, which is displayed using <code>DialogDescriptor</code>
147      * does not know when this happens.
148      */

149     public static abstract class FocusablePanel extends JPanel JavaDoc {
150         
151         public abstract void initializeFocus();
152     }
153 }
154
Popular Tags