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.editor.datanode;
 
 import java.util.Map;
 
 
 import  org.apache.cayenne.access.DataDomain;
 import  org.apache.cayenne.access.DataNode;
 import  org.apache.cayenne.access.dbsync.CreateIfNoSchemaStrategy;
 import  org.apache.cayenne.access.dbsync.ThrowOnPartialOrCreateSchemaStrategy;
 import  org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy;
 import  org.apache.cayenne.access.dbsync.ThrowOnPartialSchemaStrategy;
 import  org.apache.cayenne.conf.Configuration;
 import  org.apache.cayenne.conf.DBCPDataSourceFactory;
 import  org.apache.cayenne.conf.DriverDataSourceFactory;
 import  org.apache.cayenne.conf.JNDIDataSourceFactory;
 import  org.apache.cayenne.map.event.DataNodeEvent;
 import  org.apache.cayenne.project.ApplicationProject;
 import  org.apache.cayenne.validation.ValidationException;

A controller for the main tab of the DataNode editor panel.
 
 public class MainDataNodeEditor extends CayenneController {
 
     protected static final String NO_LOCAL_DATA_SOURCE = "Select DataSource for Local Work...";
 
     final static String[] standardDataSourceFactories = new String[] {
             DriverDataSourceFactory.class.getName(),
             JNDIDataSourceFactory.class.getName(), DBCPDataSourceFactory.class.getName()
     };
 
     final static String[] standardSchemaUpdateStrategy = new String[] {
             SkipSchemaUpdateStrategy.class.getName(),
             CreateIfNoSchemaStrategy.class.getName(),
             ThrowOnPartialSchemaStrategy.class.getName(),
             ThrowOnPartialOrCreateSchemaStrategy.class.getName()
     };
 
     protected MainDataNodeView view;
     protected DataNodeEditor tabbedPaneController;
     protected DataNode node;
     protected Map datasourceEditors;
     protected Map localDataSources;
 
     protected DataSourceEditor defaultSubeditor;
     protected BindingDelegate nodeChangeProcessor;
     protected ObjectBinding[] bindings;
 
     public MainDataNodeEditor(ProjectController parentDataNodeEditor tabController) {
 
         super(parent);
 
         this. = tabController;
         this. = new MainDataNodeView((ProjectControllergetParent());
         this. = new HashMap();
         this. = new HashMap();
 
         this. = new BindingDelegate() {
 
             public void modelUpdated(
                     ObjectBinding binding,
                    Object oldValue,
                    Object newValue) {
                DataNodeEvent e = new DataNodeEvent(MainDataNodeEditor.this);
                if (binding != null && binding.getView() == .getDataNodeName()) {
                    e.setOldName(oldValue != null ? oldValue.toString() : null);
                }
                ((ProjectControllergetParent()).fireDataNodeEvent(e);
            }
        };
        this. = new CustomDataSourceEditor(parent);
        initController();
    }
    // ======= properties
    public Component getView() {
        return ;
    }
    public String getFactoryName() {
        return ( != null) ? .getDataSourceFactory() : null;
    }
    public void setFactoryName(String factoryName) {
        if ( != null) {
            .setDataSourceFactory(factoryName);
            showDataSourceSubview(factoryName);
        }
    }
    public String getSchemaUpdateStrategy() {
        return ( != null) ? .getSchemaUpdateStrategyName() : null;
    }
    public void setSchemaUpdateStrategy(String schemaUpdateStrategy) {
        if ( != null) {
            .setSchemaUpdateStrategyName(schemaUpdateStrategy);
        }
    }
    public String getNodeName() {
        return ( != null) ? .getName() : null;
    }
    public void setNodeName(String newName) {
        if ( == null) {
            return;
        }
        // validate...
        if (newName == null) {
            throw new ValidationException("Empty DataNode Name");
        }
        ProjectController parent = (ProjectControllergetParent();
        Configuration config = ((ApplicationProject) parent.getProject())
                .getConfiguration();
        DataNode matchingNode = null;
        for (DataDomain domain : config.getDomains()) {
            DataNode nextNode = domain.getNode(newName);
            if (nextNode == ) {
                continue;
            }
            if (nextNode != null) {
                matchingNode = nextNode;
                break;
            }
        }
        if (matchingNode != null) {
            // there is an entity with the same name
            throw new ValidationException("There is another DataNode named '"
                    + newName
                    + "'. Use a different name.");
        }
        // passed validation, set value...
        // TODO: fixme....there is a slight chance that domain is different than the one
        // cached node belongs to
        ProjectUtil.setDataNodeName(parent.getCurrentDataDomain(), newName);
    }
    // ======== other stuff
    protected void initController() {
        .getDataSourceDetail().add(.getView(), "default");
        .getFactories().setEditable(true);
        // init combo box choices
        .getFactories().setModel(
                new DefaultComboBoxModel());
                new DefaultComboBoxModel());
        // init listeners
        ((ProjectControllergetParent())
                .addDataNodeDisplayListener(new DataNodeDisplayListener() {
                    public void currentDataNodeChanged(DataNodeDisplayEvent e) {
                        refreshView(e.getDataNode());
                    }
                });
            public void componentShown(ComponentEvent e) {
                refreshView( != null ?  : ((ProjectControllergetParent())
                        .getCurrentDataNode());
            }
        });
        BindingBuilder builder = new BindingBuilder(
                getApplication().getBindingFactory(),
                this);
         = builder.bindToComboSelection(
                .getLocalDataSources(),
                "parent.dataNodePreferences.localDataSource",
                );
        // use delegate for the rest of them
        builder.setDelegate();
         = new ObjectBinding[3];
        [0] = builder.bindToTextField(.getDataNodeName(), "nodeName");
        [1] = builder.bindToComboSelection(.getFactories(), "factoryName");
        [2] = builder.bindToComboSelection(
                .getSchemaUpdateStrategy(),
                "schemaUpdateStrategy");
        // one way bindings
        builder
                .bindToAction(
                        .getConfigLocalDataSources(),
                        "dataSourceConfigAction()");
    }
    public void dataSourceConfigAction() {
        PreferenceDialog prefs = new PreferenceDialog(this);
        refreshLocalDataSources();
    }
    protected void refreshLocalDataSources() {
        .clear();
                DBConnectionInfo.class);
        int len = sources.size();
        Object[] keys = new Object[len + 1];
        // a slight chance that a real datasource is called NO_LOCAL_DATA_SOURCE...
        keys[0] = ;
        Iterator it = sources.iterator();
        for (int i = 1; i <= leni++) {
            DBConnectionInfo info = (DBConnectionInfoit.next();
            keys[i] = info.getKey();
            .put(keys[i], info);
        }
    }

    
Reinitializes widgets to display selected DataNode.
    protected void refreshView(DataNode node) {
        this. = node;
        if (node == null) {
            getView().setVisible(false);
            return;
        }
        refreshLocalDataSources();
        for (ObjectBinding binding : ) {
            binding.updateView();
        }
    }

    
Selects a subview for a currently selected DataSource factory.
    protected void showDataSourceSubview(String factoryName) {
        DataSourceEditor c = (DataSourceEditor.get(factoryName);
        // create subview dynamically...
        if (c == null) {
            if (DriverDataSourceFactory.class.getName().equals(factoryName)) {
                c = new JDBCDataSourceEditor(
                        (ProjectControllergetParent(),
                        );
            }
            else if (JNDIDataSourceFactory.class.getName().equals(factoryName)) {
                c = new JNDIDataSourceEditor(
                        (ProjectControllergetParent(),
                        );
            }
            else if (DBCPDataSourceFactory.class.getName().equals(factoryName)) {
                c = new DBCPDataSourceEditor(
                        (ProjectControllergetParent(),
                        );
            }
            else {
                // special case - no detail view, just show it and bail..
                .setNode();
                disabledTab("default");
                .getDataSourceDetailLayout().show(
                        .getDataSourceDetail(),
                        "default");
                return;
            }
            .put(factoryNamec);
            .getDataSourceDetail().add(c.getView(), factoryName);
            // this is needed to display freshly added panel...
            .getDataSourceDetail().getParent().validate();
        }
        // this will refresh subview...
        c.setNode();
        disabledTab(factoryName);
        // display the right subview...
        .getDataSourceDetailLayout().show(.getDataSourceDetail(), factoryName);
    }
    protected void disabledTab(String name) {
        if (name.equals([0])) {
            .getTabComponent().setEnabledAt(2, true);
        }
        else {
            .getTabComponent().setEnabledAt(2, false);
        }
    }
New to GrepCode? Check out our FAQ X