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 http://www.apache.org/licenses/LICENSE-2.0 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.

Author(s):
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) {
         super(parent);
 
         this. = new DataSourceWizardView(this);
         this..setTitle(title);
         this. = altDataSource;
         this. = altDataSourceKey;
         this. = new DBConnectionInfo();
 
         initBindings();
     }
 
     protected void initBindings() {
         BindingBuilder builder = new BindingBuilder(
                 getApplication().getBindingFactory(),
                 this);
 
          = builder.bindToComboSelection(
                 .getDataSources(),
                 "dataSourceKey");
 
         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) {
             currentInfo.copyTo();
         }
        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;
        refreshDataSources();
        .pack();
        .setModal(true);
        makeCloseableOnEscape();
        centerView();
        .setVisible(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 {
            info.makeAdapter(classLoader);
        }
        catch (Throwable th) {
            reportError("DbAdapter Error"th);
            return;
        }
        // doing connection testing...
        // attempt opening the connection, and close it right away
        try {
            Connection connection = info.makeDataSource(classLoader).getConnection();
            try {
                connection.close();
            }
            catch (SQLException ex) {
                // ignore close error
            }
        }
        catch (Throwable th) {
            reportError("Connection Error"th);
            return;
        }
        // set success flag, and unblock the caller...
         = false;
        .dispose();
    }
    public void cancelAction() {
         = true;
        .dispose();
    }

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