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) 2009-2011 Pentaho and others
008// All Rights Reserved.
009*/
010package mondrian.gui;
011
012import java.awt.*;
013import java.awt.event.*;
014import java.util.Arrays;
015import java.util.List;
016import javax.swing.*;
017import javax.swing.table.TableModel;
018
019/**
020 *
021 * @author swood
022 */
023public class PreferencesSchemasDialog extends JDialog {
024
025    PreferencesDialog preferences = null;
026    JdbcMetaData jdbcMetadata = null;
027    String selectedSchemaString = null;
028    boolean accepted = false;
029
030    public PreferencesSchemasDialog() {
031        initComponents();
032    }
033
034    public PreferencesSchemasDialog(
035        PreferencesDialog preferences,
036        JdbcMetaData jdbcMetadata)
037    {
038        this.preferences = preferences;
039        this.jdbcMetadata = jdbcMetadata;
040        initComponents();
041    }
042
043    private void initComponents() {
044        jScrollPane1 = new javax.swing.JScrollPane();
045        jTable1 = new javax.swing.JTable();
046        cancelButton = new javax.swing.JButton();
047        okButton = new javax.swing.JButton();
048
049        setLayout(new java.awt.GridBagLayout());
050        addWindowListener(
051            new WindowAdapter() {
052                public void windowClosing(WindowEvent evt) {
053                    closeDialog(evt);
054                }
055            });
056
057        jTable1.setModel(getSchemaTableModel());
058        jTable1.setRowSelectionAllowed(false);
059        jScrollPane1.setViewportView(jTable1);
060
061        GridBagConstraints gridBagConstraints = new GridBagConstraints();
062        gridBagConstraints.gridx = 0;
063        gridBagConstraints.gridy = 0;
064        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
065        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
066
067        add(jScrollPane1, gridBagConstraints);
068
069        cancelButton.setText(
070            getResourceConverter().getString(
071                "preferences.cancelButton.title",
072                "Cancel"));
073        cancelButton.addActionListener(
074            new ActionListener() {
075                public void actionPerformed(ActionEvent evt) {
076                    closeDialog(evt);
077                }
078            });
079
080        gridBagConstraints = new java.awt.GridBagConstraints();
081        gridBagConstraints.gridx = 1;
082        gridBagConstraints.gridy = 1;
083        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
084        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
085        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
086        add(cancelButton, gridBagConstraints);
087
088        cancelButton.setText(
089            getResourceConverter().getString(
090                "preferences.okButton.title",
091                "OK"));
092        okButton.addActionListener(
093            new ActionListener() {
094                public void actionPerformed(ActionEvent evt) {
095                    acceptButtonActionPerformed(evt);
096                }
097            });
098
099        gridBagConstraints = new java.awt.GridBagConstraints();
100        gridBagConstraints.gridx = 0;
101        gridBagConstraints.gridy = 1;
102        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
103        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
104        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
105        add(okButton, gridBagConstraints);
106        pack();
107    }
108
109    private void acceptButtonActionPerformed(ActionEvent evt) {
110        TableModel tm = jTable1.getModel();
111
112        StringBuffer sb = new StringBuffer();
113
114        for (int i = 0; i < tm.getRowCount(); i++) {
115            Boolean selected = (Boolean) tm.getValueAt(i, 0);
116
117            if (selected) {
118                if (sb.length() > 0) {
119                    sb.append(",");
120                }
121                sb.append((String) tm.getValueAt(i, 1));
122            }
123        }
124
125        selectedSchemaString = sb.toString();
126
127        accepted = true;
128        setVisible(false);
129        dispose();
130    }
131
132    private void closeDialog(WindowEvent evt) {
133        setVisible(false);
134        dispose();
135    }
136
137    private void closeDialog(ActionEvent evt) {
138        setVisible(false);
139        dispose();
140    }
141
142    public boolean isAccepted() {
143        return accepted;
144    }
145
146    public TableModel getSchemaTableModel() {
147        List<String> allSchemaNames = jdbcMetadata.listAllSchemas();
148
149        String currentSchemas = preferences.getDatabaseSchema();
150
151        boolean selectedSchemas[] = new boolean[allSchemaNames.size()];
152
153        Arrays.fill(selectedSchemas, false);
154
155        // Validate entered schemas
156        if (currentSchemas != null && currentSchemas.trim().length() > 0) {
157            String schemasArray[] = currentSchemas.trim().split("[,;]");
158
159            for (int i = 0; i < schemasArray.length; i++) {
160                // trim the names, removing empties
161                String enteredSchemaName = schemasArray[i].trim();
162
163                if (enteredSchemaName.length() > 0) {
164                    for (int j = 0; j < allSchemaNames.size(); j++) {
165                        String actualSchemaName = allSchemaNames.get(j);
166                        if (actualSchemaName.equalsIgnoreCase(
167                                enteredSchemaName))
168                        {
169                            selectedSchemas[j] = true;
170                            break;
171                        }
172                    }
173                }
174            }
175        }
176
177        Object [][] table = new Object [allSchemaNames.size()][2];
178
179        for (int i = 0; i < allSchemaNames.size(); i++) {
180            table[i][0] = new Boolean(selectedSchemas[i]);
181            table[i][1] = allSchemaNames.get(i);
182        }
183
184        return new javax.swing.table.DefaultTableModel(
185            table,
186            new String [] {
187                "Select", "Schema"
188            })
189        {
190            Class[] types = {Boolean.class, String.class};
191            boolean[] canEdit = {true, false};
192
193            public Class getColumnClass(int columnIndex) {
194                return types[columnIndex];
195            }
196
197            public boolean isCellEditable(int rowIndex, int columnIndex) {
198                return canEdit[columnIndex];
199            }
200        };
201    }
202
203    /**
204     * @return the workbench i18n converter
205     */
206    public I18n getResourceConverter() {
207        return preferences.getResourceConverter();
208    }
209
210    private javax.swing.JButton cancelButton;
211    private javax.swing.JButton okButton;
212    private javax.swing.JScrollPane jScrollPane1;
213    private javax.swing.JTable jTable1;
214}
215
216// End PreferencesSchemasDialog.java