Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 package org.apache.cayenne.modeler.dialog.db;
 import java.util.Map;
A subclass of ConnectionWizard that tests configured DataSource, but does not keep an open connection.

Andrei Adamchik
 public class DataSourceWizard extends CayenneController {
     protected DataSourceWizardView view;
     protected DBConnectionInfo altDataSource;
     protected String altDataSourceKey;
     protected ObjectBinding dataSourceBinding;
     protected Map dataSources;
     protected String dataSourceKey;
     // this object is a clone of an object selected from the dropdown, as we need to allow
     // local temporary modifications
     protected DBConnectionInfo connectionInfo;
     protected boolean canceled;
     public DataSourceWizard(CayenneController parentString title,
             String altDataSourceKeyDBConnectionInfo altDataSource) {
         this. = new DataSourceWizardView(this);
         this. = altDataSource;
         this. = altDataSourceKey;
         this. = new DBConnectionInfo();
     protected void initBindings() {
         BindingBuilder builder = new BindingBuilder(
          = builder.bindToComboSelection(
         builder.bindToAction(.getCancelButton(), "cancelAction()");
         builder.bindToAction(.getOkButton(), "okAction()");
         builder.bindToAction(.getConfigButton(), "dataSourceConfigAction()");
     public String getDataSourceKey() {
         return ;
     public void setDataSourceKey(String dataSourceKey) {
         this. = dataSourceKey;
         // update a clone object that will be used to obtain connection...
         DBConnectionInfo currentInfo = (DBConnectionInfo.get(dataSourceKey);
         if (currentInfo != null) {
        else {
             = new DBConnectionInfo();

Main action method that pops up a dialog asking for user selection. Returns true if the selection was confirmed, false - if canceled.
    public boolean startupAction() {
        this. = true;
        return !;
        return ;

Tests that the entered information is valid and can be used to open a conneciton. Does not store the open connection.
    public void okAction() {
        DBConnectionInfo info = getConnectionInfo();
        ClassLoadingService classLoader = getApplication().getClassLoadingService();
        // try making an adapter...
        try {
        catch (Throwable th) {
            reportError("DbAdapter Error"th);
        // doing connection testing...
        // attempt opening the connection, and close it right away
        try {
            Connection connection = info.makeDataSource(classLoader).getConnection();
            try {
            catch (SQLException ex) {
                // ignore close error
        catch (Throwable th) {
            reportError("Connection Error"th);
        // set success flag, and unblock the caller...
         = false;
    public void cancelAction() {
         = true;

Opens preferences panel to allow configuration of DataSource presets.
    public void dataSourceConfigAction() {
        PreferenceDialog prefs = new PreferenceDialog(this);
    public Component getView() {
        return ;
    protected void refreshDataSources() {
        if ( != null
                && !.containsKey()
                &&  != null) {
        Object[] keys = .keySet().toArray();
        .getDataSources().setModel(new DefaultComboBoxModel(keys));
        if (getDataSourceKey() == null) {
            String key = null;
            if ( != null) {
                key = ;
            else if (keys.length > 0) {
                key = keys[0].toString();
New to GrepCode? Check out our FAQ X