001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2002-2005 Julian Hyde
008// Copyright (C) 2005-2009 Pentaho and others
009// Copyright (C) 2006-2007 Cincom Systems, Inc.
010// Copyright (C) 2006-2007 JasperSoft
011// All Rights Reserved.
012*/
013package mondrian.gui;
014
015import java.awt.event.*;
016import java.util.*;
017import javax.swing.*;
018
019
020/**
021 * @author sean
022 */
023public class PreferencesDialog extends javax.swing.JDialog {
024    boolean accepted = false;
025
026    /**
027     * Creates new form PreferencesDialog
028     */
029    public PreferencesDialog(java.awt.Frame parent, boolean modal) {
030        super(parent, modal);
031        initComponents();
032        pack();
033        setLocationRelativeTo(parent);
034    }
035
036    public boolean accepted() {
037        return accepted;
038    }
039
040    public void setJDBCConnectionUrl(String s) {
041        this.urlTextField.setText(s);
042    }
043
044    public String getJdbcConnectionUrl() {
045        return urlTextField.getText();
046    }
047
048    public void setJDBCUsername(String s) {
049        this.usernameTextField.setText(s);
050    }
051
052    public String getJdbcUsername() {
053        return usernameTextField.getText();
054    }
055
056    public void setJDBCPassword(String s) {
057        this.passwordTextField.setText(s);
058    }
059
060    public String getJdbcPassword() {
061        return passwordTextField.getText();
062    }
063
064    public void setJDBCDriverClassName(String s) {
065        this.driverClassTextField.setText(s);
066    }
067
068    public String getJdbcDriverClassName() {
069        return driverClassTextField.getText();
070    }
071
072    public void setDatabaseSchema(String schema) {
073        this.schemaTextField.setText(schema);
074    }
075
076    public String getDatabaseSchema() {
077        return schemaTextField.getText();
078    }
079
080    public void setRequireSchema(boolean requireSchema) {
081        requireSchemaButton.setSelected(requireSchema);
082    }
083
084    public boolean getRequireSchema() {
085        return requireSchemaButton.isSelected();
086    }
087
088    /**
089     * @return the workbench i18n converter
090     */
091    public I18n getResourceConverter() {
092        if (getParent() == null && getParent().getClass() != Workbench.class) {
093            return Workbench.getGlobalResourceConverter();
094        }
095        return ((Workbench) getParent()).getResourceConverter();
096    }
097
098    /**
099     * This method is called from within the constructor to
100     * initialize the form.
101     * WARNING: Do NOT modify this code. The content of this method is
102     * always regenerated by the Form Editor.
103     */
104    private void initComponents() {//GEN-BEGIN:initComponents
105        java.awt.GridBagConstraints gridBagConstraints;
106
107        jTabbedPane1 = new javax.swing.JTabbedPane();
108        jPanel1 = new javax.swing.JPanel();
109        jLabel1 = new javax.swing.JLabel();
110        jLabel2 = new javax.swing.JLabel();
111        jLabel3 = new javax.swing.JLabel();
112        jLabel4 = new javax.swing.JLabel();
113        jLabel5 = new javax.swing.JLabel();
114        jLabel6 = new javax.swing.JLabel();
115
116        // Set the url text field to 50 to drive the width of the dialog
117        urlTextField = new javax.swing.JTextField(50);
118
119        usernameTextField = new javax.swing.JTextField();
120        passwordTextField = new javax.swing.JTextField();
121        driverClassTextField = new javax.swing.JTextField();
122        schemaTextField = new javax.swing.JTextField();
123        requireSchemaButton = new javax.swing.JCheckBox();
124        testButton = new javax.swing.JButton();
125        acceptButton = new javax.swing.JButton();
126        cancelButton = new javax.swing.JButton();
127        selectSchemasButton = new javax.swing.JButton();
128
129        getContentPane().setLayout(new java.awt.GridBagLayout());
130
131        setTitle(
132            getResourceConverter().getString(
133                "preferences.pane.title", "Database Connection"));
134        addWindowListener(
135            new WindowAdapter() {
136                public void windowClosing(WindowEvent evt) {
137                    closeDialog(evt);
138                }
139            });
140
141        jPanel1.setLayout(new java.awt.GridBagLayout());
142
143        jLabel1.setText(
144            getResourceConverter().getString(
145                "preferences.driverClassName.title", "Driver Class Name"));
146        gridBagConstraints = new java.awt.GridBagConstraints();
147        gridBagConstraints.gridx = 0;
148        gridBagConstraints.gridy = 0;
149        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
150        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
151        jPanel1.add(jLabel1, gridBagConstraints);
152
153        jLabel2.setText(
154            getResourceConverter().getString(
155                "preferences.connectionURL.title", "Connection URL"));
156        gridBagConstraints = new java.awt.GridBagConstraints();
157        gridBagConstraints.gridx = 0;
158        gridBagConstraints.gridy = 1;
159        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
160        gridBagConstraints.weighty = 1.0;
161        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
162        jPanel1.add(jLabel2, gridBagConstraints);
163
164        jLabel3.setText(
165            getResourceConverter().getString(
166                "preferences.userName.title", "User name"));
167        gridBagConstraints = new java.awt.GridBagConstraints();
168        gridBagConstraints.gridx = 0;
169        gridBagConstraints.gridy = 2;
170        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
171        gridBagConstraints.weighty = 1.0;
172        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
173        jPanel1.add(jLabel3, gridBagConstraints);
174
175        jLabel4.setText(
176            getResourceConverter().getString(
177                "preferences.password.title", "Password"));
178        gridBagConstraints = new java.awt.GridBagConstraints();
179        gridBagConstraints.gridx = 0;
180        gridBagConstraints.gridy = 3;
181        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
182        gridBagConstraints.weighty = 1.0;
183        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
184        jPanel1.add(jLabel4, gridBagConstraints);
185
186        gridBagConstraints = new java.awt.GridBagConstraints();
187        gridBagConstraints.gridx = 1;
188        gridBagConstraints.gridy = 1;
189        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
190        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
191        gridBagConstraints.weightx = 1.0;
192        gridBagConstraints.weighty = 1.0;
193        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
194        jPanel1.add(urlTextField, gridBagConstraints);
195
196        jLabel5.setText(
197            getResourceConverter().getString(
198                "preferences.schema.title",
199                "Schemas (Optional, comma separated)"));
200        gridBagConstraints = new java.awt.GridBagConstraints();
201        gridBagConstraints.gridx = 0;
202        gridBagConstraints.gridy = 4;
203        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
204        gridBagConstraints.weighty = 1.0;
205        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
206        jPanel1.add(jLabel5, gridBagConstraints);
207
208        jLabel6.setText(
209            getResourceConverter().getString(
210                "preferences.requireschema.title",
211                "Require Schema Attributes"));
212        gridBagConstraints = new java.awt.GridBagConstraints();
213        gridBagConstraints.gridx = 0;
214        gridBagConstraints.gridy = 5;
215        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
216        gridBagConstraints.weighty = 1.0;
217        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
218        jPanel1.add(jLabel6, gridBagConstraints);
219
220        usernameTextField.setText("");
221        gridBagConstraints = new java.awt.GridBagConstraints();
222        gridBagConstraints.gridx = 1;
223        gridBagConstraints.gridy = 2;
224        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
225        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
226        gridBagConstraints.weightx = 1.0;
227        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
228        jPanel1.add(usernameTextField, gridBagConstraints);
229
230        passwordTextField.setText("");
231        gridBagConstraints = new java.awt.GridBagConstraints();
232        gridBagConstraints.gridx = 1;
233        gridBagConstraints.gridy = 3;
234        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
235        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
236        gridBagConstraints.weightx = 1.0;
237        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
238        jPanel1.add(passwordTextField, gridBagConstraints);
239
240        schemaTextField.setText("");
241        gridBagConstraints = new java.awt.GridBagConstraints();
242        gridBagConstraints.gridx = 1;
243        gridBagConstraints.gridy = 4;
244        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
245        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
246        gridBagConstraints.weightx = 1.0;
247        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
248        jPanel1.add(schemaTextField, gridBagConstraints);
249
250        selectSchemasButton.setText(
251            getResourceConverter().getString(
252                "preferences.selectSchemasButton.title",
253                "Select Schemas"));
254        selectSchemasButton.addActionListener(
255            new ActionListener() {
256                public void actionPerformed(ActionEvent evt) {
257                    selectSchemasButtonActionPerformed(evt);
258                }
259            });
260        gridBagConstraints = new java.awt.GridBagConstraints();
261        gridBagConstraints.gridx = 2;
262        gridBagConstraints.gridy = 4;
263        gridBagConstraints.weightx = 1.0;
264        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
265        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
266        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 180);
267        jPanel1.add(selectSchemasButton, gridBagConstraints);
268
269        // Disable the select schemas button for now
270        selectSchemasButton.setVisible(false);
271
272        requireSchemaButton.setSelected(false);
273        gridBagConstraints = new java.awt.GridBagConstraints();
274        gridBagConstraints.gridx = 1;
275        gridBagConstraints.gridy = 5;
276        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
277        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
278        gridBagConstraints.weightx = 1.0;
279        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
280        jPanel1.add(requireSchemaButton, gridBagConstraints);
281
282        driverClassTextField.setText("org.gjt.mm.mysql.Driver");
283        gridBagConstraints = new java.awt.GridBagConstraints();
284        gridBagConstraints.gridx = 1;
285        gridBagConstraints.gridy = 0;
286        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
287        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
288        gridBagConstraints.weightx = 1.0;
289        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
290        jPanel1.add(driverClassTextField, gridBagConstraints);
291
292        jTabbedPane1.addTab(
293            getResourceConverter().getString(
294                "preferences.jdbcPanel.title", "JDBC"), jPanel1);
295
296        gridBagConstraints = new java.awt.GridBagConstraints();
297        gridBagConstraints.gridwidth = 2;
298        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
299        gridBagConstraints.weightx = 1.0;
300        gridBagConstraints.weighty = 1.0;
301        getContentPane().add(jTabbedPane1, gridBagConstraints);
302
303        testButton.setText(
304            getResourceConverter().getString(
305                "preferences.testButton.title", "Test Connection"));
306        testButton.addActionListener(
307            new ActionListener() {
308                public void actionPerformed(ActionEvent evt) {
309                    testButtonActionPerformed(evt);
310                }
311            });
312        gridBagConstraints = new java.awt.GridBagConstraints();
313        gridBagConstraints.gridx = 1;
314        gridBagConstraints.gridy = 1;
315        gridBagConstraints.weightx = 1.0;
316        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
317        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 180);
318        getContentPane().add(testButton, gridBagConstraints);
319
320        cancelButton.setText(
321            getResourceConverter().getString(
322                "preferences.cancelButton.title",
323                "Cancel"));
324        cancelButton.addActionListener(
325            new ActionListener() {
326                public void actionPerformed(ActionEvent evt) {
327                    cancelButtonActionPerformed(evt);
328                }
329            });
330
331        gridBagConstraints = new java.awt.GridBagConstraints();
332        gridBagConstraints.gridx = 1;
333        gridBagConstraints.gridy = 1;
334        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
335        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
336        getContentPane().add(cancelButton, gridBagConstraints);
337
338        acceptButton.setText(
339            getResourceConverter().getString(
340                "preferences.acceptButton.title",
341                "Accept"));
342        acceptButton.addActionListener(
343            new ActionListener() {
344                public void actionPerformed(ActionEvent evt) {
345                    acceptButtonActionPerformed(evt);
346                }
347            });
348
349        gridBagConstraints = new java.awt.GridBagConstraints();
350        gridBagConstraints.gridx = 1;
351        gridBagConstraints.gridy = 1;
352        gridBagConstraints.weightx = 1.0;
353        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
354        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 80);
355        getContentPane().add(acceptButton, gridBagConstraints);
356
357        pack();
358    } //GEN-END:initComponents
359
360    private void selectSchemasButtonActionPerformed(
361        ActionEvent evt)
362    {
363        JdbcMetaData jdbcMetaData = getTestingJdbcMetadata();
364        if (jdbcMetaData.getErrMsg() != null) {
365            JOptionPane.showMessageDialog(
366                this,
367                getResourceConverter().getFormattedString(
368                    "preferences.unsuccessfulTestConnection.alert",
369                    "Database connection could not be done.\n{0}",
370                    jdbcMetaData.getErrMsg()),
371                "",
372                JOptionPane.WARNING_MESSAGE);
373            return;
374        }
375
376        List<String> allSchemaNames = jdbcMetaData.listAllSchemas();
377        if (allSchemaNames == null || allSchemaNames.size() == 0) {
378            JOptionPane.showMessageDialog(
379                this, getResourceConverter().getString(
380                    "preferences.noSchemas.cantSetSchemas.alert",
381                    "No schemas available for this database connection"),
382                "",
383                JOptionPane.WARNING_MESSAGE);
384            return;
385        }
386
387        PreferencesSchemasDialog psd = new PreferencesSchemasDialog(
388            this, jdbcMetaData);
389
390        setVisible(false);
391        setModal(false);
392        psd.setVisible(true);
393
394        if (psd.isAccepted()) {
395            // update list of schemas
396        }
397
398        setModal(true);
399        setVisible(true);
400    }
401
402    public JdbcMetaData getTestingJdbcMetadata() {
403        return new JdbcMetaData(
404            getJdbcDriverClassName(),
405            getJdbcConnectionUrl(),
406            getJdbcUsername(),
407            getJdbcPassword());
408    }
409
410    private void testButtonActionPerformed(ActionEvent evt) {
411        //GEN-FIRST:event_testButtonActionPerformed
412        JdbcMetaData jdbcMetaData = getTestingJdbcMetadata();
413        if (jdbcMetaData.getErrMsg() != null) {
414            JOptionPane.showMessageDialog(
415                this,
416                getResourceConverter().getFormattedString(
417                    "preferences.unsuccessfulTestConnection.alert",
418                    "Database connection could not be done.\n{0}",
419                    jdbcMetaData.getErrMsg()),
420                "",
421                JOptionPane.WARNING_MESSAGE);
422            accepted = false;
423            return;
424        }
425
426        List<String> allSchemaNames = jdbcMetaData.listAllSchemas();
427
428        String currentSchemas = schemaTextField.getText();
429
430        // If (schemas entered  or schemas required) and
431        // DB does not support schemas
432        // -> error
433        if (allSchemaNames == null || allSchemaNames.size() == 0) {
434            if (currentSchemas != null && currentSchemas.trim().length() > 0) {
435                JOptionPane.showMessageDialog(
436                    this, getResourceConverter().getString(
437                        "preferences.noSchemas.butSchemasEntered.alert",
438                        "No schemas available for this database connection, but schemas entered"),
439                    "",
440                    JOptionPane.WARNING_MESSAGE);
441                accepted = false;
442                return;
443            }
444
445            if (requireSchemaButton.isSelected()) {
446                JOptionPane.showMessageDialog(
447                    this, getResourceConverter().getString(
448                        "preferences.noSchemas.butSchemasRequired.alert",
449                        "No schemas available for this database connection, but schemas are required"),
450                    "",
451                    JOptionPane.WARNING_MESSAGE);
452                accepted = false;
453                return;
454            }
455        }
456
457        // Validate entered schemas
458        if (currentSchemas != null && currentSchemas.trim().length() > 0) {
459            String schemasArray[] = currentSchemas.trim().split("[,;]");
460
461            Set<String> validSchemas = new TreeSet<String>();
462            StringBuffer schemasInError = new StringBuffer();
463
464            for (int i = 0; i < schemasArray.length; i++) {
465                // trim the names, removing empties
466                String enteredSchemaName = schemasArray[i].trim();
467
468                if (enteredSchemaName.length() > 0) {
469                    for (String actualSchemaName : allSchemaNames) {
470                        if (actualSchemaName
471                            .equalsIgnoreCase(enteredSchemaName))
472                        {
473                            validSchemas.add(enteredSchemaName);
474                            break;
475                        }
476                    }
477                }
478
479                if (!validSchemas.contains(enteredSchemaName)) {
480                    schemasInError.append(" ").append(enteredSchemaName);
481                }
482            }
483
484            if (schemasInError.length() > 0) {
485                JOptionPane.showMessageDialog(
486                    this,
487                    getResourceConverter().getFormattedString(
488                        "preferences.invalidSchemas.alert",
489                        "The following entered schemas are invalid.\n{0}",
490                        schemasInError.toString()),
491                    "",
492                    JOptionPane.WARNING_MESSAGE);
493                accepted = false;
494                return;
495            }
496        }
497
498        JOptionPane.showMessageDialog(
499            this, getResourceConverter().getString(
500                "preferences.successfulTestConnection.alert",
501                "Database connection successful"),
502            "",
503            JOptionPane.INFORMATION_MESSAGE);
504        accepted = true;
505    } //GEN-LAST:event_testButtonActionPerformed
506
507    private void acceptButtonActionPerformed(ActionEvent evt) {
508        //GEN-FIRST:event_acceptButtonActionPerformed
509        accepted = true;
510        setVisible(false);
511    } //GEN-LAST:event_acceptButtonActionPerformed
512
513    private void cancelButtonActionPerformed(ActionEvent evt) {
514        //GEN-FIRST:event_cancelButtonActionPerformed
515        accepted = false;
516        setVisible(false);
517    } //GEN-LAST:event_cancelButtonActionPerformed
518
519    /**
520     * Closes the dialog
521     */
522    private void closeDialog(WindowEvent evt) {
523        //GEN-FIRST:event_closeDialog
524        setVisible(false);
525        dispose();
526    } //GEN-LAST:event_closeDialog
527
528    // Variables declaration - do not modify//GEN-BEGIN:variables
529    private javax.swing.JTextField driverClassTextField;
530    private javax.swing.JPanel jPanel1;
531    private javax.swing.JTextField urlTextField;
532    private javax.swing.JTextField usernameTextField;
533    private javax.swing.JTextField passwordTextField;
534    private javax.swing.JTextField schemaTextField;
535    private javax.swing.JCheckBox requireSchemaButton;
536    private javax.swing.JButton acceptButton;
537    private javax.swing.JButton testButton;
538    private javax.swing.JTabbedPane jTabbedPane1;
539    private javax.swing.JButton cancelButton;
540    private javax.swing.JButton selectSchemasButton;
541    private javax.swing.JLabel jLabel6;
542    private javax.swing.JLabel jLabel5;
543    private javax.swing.JLabel jLabel4;
544    private javax.swing.JLabel jLabel3;
545    private javax.swing.JLabel jLabel2;
546    private javax.swing.JLabel jLabel1;
547    // End of variables declaration//GEN-END:variables
548
549}
550
551// End PreferencesDialog.java