Changeset 3024 for DataCleaner


Ignore:
Timestamp:
01/13/12 15:12:44 (4 months ago)
Author:
kasper
Message:

Ticket #746: Fixed JDBC datastore dialog "Test connection" button

File:
1 edited

Legend:

Unmodified
Added
Removed
  • DataCleaner/trunk/core/src/main/java/org/eobjects/datacleaner/windows/JdbcDatastoreDialog.java

    r3023 r3024  
    2727import java.awt.event.ActionListener; 
    2828import java.awt.event.KeyEvent; 
     29import java.sql.Connection; 
    2930import java.util.ArrayList; 
    3031import java.util.List; 
     
    3233import javax.inject.Inject; 
    3334import javax.inject.Provider; 
     35import javax.sql.DataSource; 
    3436import javax.swing.AbstractAction; 
    3537import javax.swing.Action; 
     
    3739import javax.swing.Icon; 
    3840import javax.swing.JButton; 
    39 import javax.swing.JCheckBox; 
    4041import javax.swing.JComponent; 
    4142import javax.swing.JLabel; 
     
    4849import javax.swing.KeyStroke; 
    4950 
    50 import org.eobjects.analyzer.connection.DatastoreConnection; 
    5151import org.eobjects.analyzer.connection.JdbcDatastore; 
    52 import org.eobjects.analyzer.connection.UpdateableDatastoreConnection; 
    5352import org.eobjects.analyzer.util.StringUtils; 
    5453import org.eobjects.datacleaner.bootstrap.WindowContext; 
     
    6261import org.eobjects.datacleaner.util.WidgetFactory; 
    6362import org.eobjects.datacleaner.util.WidgetUtils; 
     63import org.eobjects.datacleaner.widgets.DCCheckBox; 
    6464import org.eobjects.datacleaner.widgets.DCComboBox; 
    6565import org.eobjects.datacleaner.widgets.DCComboBox.Listener; 
    6666import org.eobjects.datacleaner.widgets.DCLabel; 
     67import org.eobjects.metamodel.util.FileHelper; 
    6768import org.jdesktop.swingx.JXTextField; 
    6869 
     
    8889        private final JXTextField _usernameTextField; 
    8990        private final JPasswordField _passwordField; 
    90         private final JCheckBox _multipleConnectionsCheckBox; 
     91        private final DCCheckBox<Object> _multipleConnectionsCheckBox; 
    9192        private final DCComboBox<Object> _databaseDriverComboBox; 
    9293        private final Provider<OptionsDialog> _optionsDialogProvider; 
     
    105106                _databaseDriverCatalog = databaseDriverCatalog; 
    106107 
    107                 _multipleConnectionsCheckBox = new JCheckBox("Allow multiple concurrent connections", true); 
     108                _multipleConnectionsCheckBox = new DCCheckBox<Object>("Allow multiple concurrent connections", true); 
    108109                _multipleConnectionsCheckBox 
    109110                                .setToolTipText("Indicates whether multiple connections (aka. connection pooling) may be created or not. " 
     
    346347                row++; 
    347348 
    348                 final JButton testButton = WidgetFactory.createButton("Test connection", "images/actions/refresh.png"); 
     349                final JButton testButton = WidgetFactory.createButton(getTestButtonText(), "images/actions/refresh.png"); 
    349350                testButton.addActionListener(new ActionListener() { 
    350351 
     
    352353                        public void actionPerformed(ActionEvent event) { 
    353354                                JdbcDatastore datastore = createDatastore(); 
    354                                 final List<DatastoreConnection> connections = new ArrayList<DatastoreConnection>(); 
     355 
     356                                final List<Connection> connections = new ArrayList<Connection>(); 
     357 
    355358                                try { 
    356                                         for (int i = 0; i < TEST_CONNECTION_COUNT; i++) { 
    357                                                 UpdateableDatastoreConnection connection = datastore.openConnection(); 
    358                                                 connections.add(connection); 
     359                                        if (datastore.isMultipleConnections()) { 
     360                                                DataSource ds = datastore.createDataSource(); 
     361                                                for (int i = 0; i < TEST_CONNECTION_COUNT; i++) { 
     362                                                        Connection connection = ds.getConnection(); 
     363                                                        connections.add(connection); 
     364                                                } 
     365                                        } else { 
     366                                                Connection connnection = datastore.createConnection(); 
     367                                                connections.add(connnection); 
    359368                                        } 
    360369                                } catch (Throwable e) { 
     
    362371                                        return; 
    363372                                } finally { 
    364                                         for (DatastoreConnection connection : connections) { 
    365                                                 connection.close(); 
     373                                        for (Connection connection : connections) { 
     374                                                FileHelper.safeClose(connection); 
    366375                                        } 
    367376                                } 
    368377 
    369378                                JOptionPane.showMessageDialog(JdbcDatastoreDialog.this, "Connection successful!"); 
     379                        } 
     380                }); 
     381                _multipleConnectionsCheckBox.addListener(new DCCheckBox.Listener<Object>() { 
     382                        @Override 
     383                        public void onItemSelected(Object item, boolean selected) { 
     384                                testButton.setText(getTestButtonText()); 
    370385                        } 
    371386                }); 
     
    393408 
    394409                return panel; 
     410        } 
     411 
     412        private String getTestButtonText() { 
     413                if (_multipleConnectionsCheckBox.isSelected()) { 
     414                        return "Test connections"; 
     415                } 
     416                return "Test connection"; 
    395417        } 
    396418 
Note: See TracChangeset for help on using the changeset viewer.