KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.Vector JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28 import org.netbeans.api.progress.ProgressHandle;
29 import org.netbeans.api.progress.ProgressHandleFactory;
30
31 import org.openide.util.NbBundle;
32
33 import org.netbeans.modules.db.explorer.DatabaseConnection;
34 import org.openide.util.RequestProcessor;
35
36 public class SchemaPanel extends javax.swing.JPanel JavaDoc {
37
38     private ConnectionDialogMediator mediator;
39     private DatabaseConnection dbcon;
40     private ProgressHandle progressHandle;
41     private JComponent JavaDoc progressComponent;
42
43     /**
44      * Creates a new form SchemaPanel
45      *
46      * @param dbcon instance of DatabaseConnection object
47      */

48     public SchemaPanel(ConnectionDialogMediator mediator, DatabaseConnection dbcon) {
49         this.mediator = mediator;
50         this.dbcon = dbcon;
51         initComponents();
52         initAccessibility();
53
54         ConnectionProgressListener progressListener = new ConnectionProgressListener() {
55             public void connectionStarted() {
56                 startProgress();
57             }
58             
59             public void connectionStep(String JavaDoc step) {
60                 setProgressMessage(step);
61             }
62
63             public void connectionFinished() {
64                 stopProgress(true);
65             }
66
67             public void connectionFailed() {
68                 stopProgress(false);
69             }
70         };
71         mediator.addConnectionProgressListener(progressListener);
72     }
73
74     private void initAccessibility() {
75         schemaLabel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_SchemaDialogTextA11yDesc")); //NOI18N
76
schemaComboBox.getAccessibleContext().setAccessibleName(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_SchemaDialogTextComboBoxA11yName")); //NOI18N
77
commentTextArea.getAccessibleContext().setAccessibleName(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_SchemaPanelCommentA11yName")); //NOI18N
78
commentTextArea.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_SchemaPanelCommentA11yDesc")); //NOI18N
79
connectProgressPanel.getAccessibleContext().setAccessibleName(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectionProgressBarA11yName")); //NOI18N
80
connectProgressPanel.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectionProgressBarA11yDesc")); //NOI18N
81
schemaButton.getAccessibleContext().setAccessibleName(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_GetSchemasButtonA11yName")); //NOI18N
82
schemaButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_GetSchemasButtonA11yDesc")); //NOI18N
83
this.getAccessibleContext().setAccessibleName(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_GetSchemasPanelA11yName")); //NOI18N
84
this.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_GetSchemasPanelA11yDesc")); //NOI18N
85
}
86
87     /** This method is called from within the constructor to
88      * initialize the form.
89      * WARNING: Do NOT modify this code. The content of this method is
90      * always regenerated by the Form Editor.
91      */

92     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
93
private void initComponents() {
94         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
95
96         commentTextArea = new javax.swing.JTextArea JavaDoc();
97         schemaLabel = new javax.swing.JLabel JavaDoc();
98         schemaComboBox = new javax.swing.JComboBox JavaDoc();
99         schemaButton = new javax.swing.JButton JavaDoc();
100         connectProgressPanel = new javax.swing.JPanel JavaDoc();
101         progressMessageLabel = new javax.swing.JLabel JavaDoc();
102         progressContainerPanel = new javax.swing.JPanel JavaDoc();
103
104         FormListener formListener = new FormListener();
105
106         setLayout(new java.awt.GridBagLayout JavaDoc());
107
108         commentTextArea.setEditable(false);
109         commentTextArea.setFont(javax.swing.UIManager.getFont("Label.font"));
110         commentTextArea.setLineWrap(true);
111         commentTextArea.setText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("MSG_SchemaPanelComment")); // NOI18N
112
commentTextArea.setWrapStyleWord(true);
113         commentTextArea.setDisabledTextColor(javax.swing.UIManager.getColor("Label.foreground"));
114         commentTextArea.setEnabled(false);
115         commentTextArea.setOpaque(false);
116         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
117         gridBagConstraints.gridwidth = 3;
118         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
119         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 11);
120         add(commentTextArea, gridBagConstraints);
121
122         schemaLabel.setLabelFor(schemaComboBox);
123         org.openide.awt.Mnemonics.setLocalizedText(schemaLabel, NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("SchemaDialogText")); // NOI18N
124
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
125         gridBagConstraints.gridx = 0;
126         gridBagConstraints.gridy = 1;
127         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
128         gridBagConstraints.weighty = 1.0;
129         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 0);
130         add(schemaLabel, gridBagConstraints);
131
132         schemaComboBox.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_SchemaDialogTextComboBoxA11yDesc")); // NOI18N
133
schemaComboBox.setEnabled(false);
134         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
135         gridBagConstraints.gridx = 1;
136         gridBagConstraints.gridy = 1;
137         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
138         gridBagConstraints.weightx = 1.0;
139         gridBagConstraints.weighty = 1.0;
140         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 0, 0);
141         add(schemaComboBox, gridBagConstraints);
142
143         org.openide.awt.Mnemonics.setLocalizedText(schemaButton, NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("SchemaDialogGetButton")); // NOI18N
144
schemaButton.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_GetSchemasButtonA11yDesc")); // NOI18N
145
schemaButton.addActionListener(formListener);
146         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
147         gridBagConstraints.gridx = 2;
148         gridBagConstraints.gridy = 1;
149         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
150         gridBagConstraints.weighty = 1.0;
151         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 5, 0, 11);
152         add(schemaButton, gridBagConstraints);
153
154         connectProgressPanel.setToolTipText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ACS_ConnectionProgressBarA11yDesc")); // NOI18N
155
connectProgressPanel.setLayout(new java.awt.BorderLayout JavaDoc(0, 5));
156
157         org.openide.awt.Mnemonics.setLocalizedText(progressMessageLabel, " ");
158         connectProgressPanel.add(progressMessageLabel, java.awt.BorderLayout.NORTH);
159
160         progressContainerPanel.setMinimumSize(new java.awt.Dimension JavaDoc(20, 20));
161         progressContainerPanel.setPreferredSize(new java.awt.Dimension JavaDoc(20, 20));
162         progressContainerPanel.setLayout(new java.awt.BorderLayout JavaDoc());
163         connectProgressPanel.add(progressContainerPanel, java.awt.BorderLayout.CENTER);
164
165         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
166         gridBagConstraints.gridx = 0;
167         gridBagConstraints.gridy = 2;
168         gridBagConstraints.gridwidth = 3;
169         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
170         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
171         gridBagConstraints.weightx = 1.0;
172         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 11, 11);
173         add(connectProgressPanel, gridBagConstraints);
174     }
175
176     // Code for dispatching events from components to event handlers.
177

178     private class FormListener implements java.awt.event.ActionListener JavaDoc {
179         FormListener() {}
180         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
181             if (evt.getSource() == schemaButton) {
182                 SchemaPanel.this.schemaButtonActionPerformed(evt);
183             }
184         }
185     }// </editor-fold>//GEN-END:initComponents
186

187     private void schemaButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_schemaButtonActionPerformed
188
schemaComboBox.setEnabled(false);
189         schemaComboBox.removeAllItems();
190
191         Connection JavaDoc con = dbcon.getConnection();
192         try {
193             if (con == null || con.isClosed())
194                 dbcon.connect();
195             else {
196                 RequestProcessor.getDefault().post(new Runnable JavaDoc() {
197                     public void run() {
198                         mediator.fireConnectionStarted();
199                         mediator.retrieveSchemas(SchemaPanel.this, dbcon, dbcon.getUser());
200                         mediator.fireConnectionFinished();
201                     }
202                 });
203             }
204         } catch (SQLException JavaDoc exc) {
205             //isClosed() method failed, try to connect
206
dbcon.connect();
207         }
208     }//GEN-LAST:event_schemaButtonActionPerformed
209

210
211     // Variables declaration - do not modify//GEN-BEGIN:variables
212
private javax.swing.JTextArea JavaDoc commentTextArea;
213     private javax.swing.JPanel JavaDoc connectProgressPanel;
214     private javax.swing.JPanel JavaDoc progressContainerPanel;
215     private javax.swing.JLabel JavaDoc progressMessageLabel;
216     private javax.swing.JButton JavaDoc schemaButton;
217     private javax.swing.JComboBox JavaDoc schemaComboBox;
218     private javax.swing.JLabel JavaDoc schemaLabel;
219     // End of variables declaration//GEN-END:variables
220

221     public String JavaDoc getSchema() {
222         Object JavaDoc schema = schemaComboBox.getSelectedItem();
223         if (schema != null && !schema.toString().equals(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("TXT_NoSchema"))) //NOI18N
224
return schema.toString();
225         else
226             return null;
227     }
228
229     public boolean setSchemas(Vector JavaDoc items, String JavaDoc schema) {
230         schemaComboBox.removeAllItems();
231         for (int i = 0; i < items.size(); i++)
232             schemaComboBox.addItem(items.elementAt(i));
233
234         if (items.size() == 0) {
235             schemaComboBox.addItem(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("TXT_NoSchema")); //NOI18N
236
schemaComboBox.setEnabled(false);
237         } else
238             schemaComboBox.setEnabled(true);
239
240         if (items.size() == 1)
241             //no or only one schema in the items
242
return true;
243
244         int idx = items.indexOf(schema);
245         if (idx == -1)
246             idx = items.indexOf(schema.toLowerCase());
247         if (idx == -1)
248             idx = items.indexOf(schema.toUpperCase());
249         if (idx != -1) {
250             schemaComboBox.setSelectedIndex(idx);
251             // schema has been found in the items
252
return true;
253         }
254
255         // schema has not been found in the items; index is set to the first item
256
return false;
257     }
258
259     public void setComment(String JavaDoc msg) {
260         commentTextArea.setText(msg);
261     }
262     
263     private void startProgress() {
264         SwingUtilities.invokeLater(new Runnable JavaDoc() {
265             public void run() {
266                 progressHandle = ProgressHandleFactory.createHandle(null);
267                 progressComponent = ProgressHandleFactory.createProgressComponent(progressHandle);
268                 progressContainerPanel.add(progressComponent, BorderLayout.CENTER);
269                 progressHandle.start();
270                 progressMessageLabel.setText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ConnectionProgress_Connecting"));
271                 schemaButton.setEnabled(false);
272             }
273         });
274     }
275     
276     private void setProgressMessage(final String JavaDoc message) {
277         SwingUtilities.invokeLater(new Runnable JavaDoc() {
278             public void run() {
279                 progressMessageLabel.setText(message);
280                 schemaButton.setEnabled(false);
281             }
282         });
283     }
284
285     private void stopProgress(final boolean connected) {
286         SwingUtilities.invokeLater(new Runnable JavaDoc() {
287             public void run() {
288                 progressHandle.finish();
289                 progressContainerPanel.remove(progressComponent);
290                 // without this, the removed progress component remains painted on its parent... why?
291
progressContainerPanel.repaint();
292                 if (connected) {
293                     progressMessageLabel.setText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ConnectionProgress_Established"));
294                 } else {
295                     progressMessageLabel.setText(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ConnectionProgress_Failed"));
296                 }
297                 schemaButton.setEnabled(true);
298             }
299         });
300     }
301     
302     public void resetProgress() {
303         progressMessageLabel.setText(" "); // NOI18N
304
}
305 }
306
Popular Tags