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;
 
 
 
 import  org.apache.cayenne.access.DataDomain;
 import  org.apache.cayenne.access.DataNode;
 import  org.apache.cayenne.conf.Configuration;
 import  org.apache.cayenne.map.DataMap;
 import  org.apache.cayenne.map.ObjEntity;
 import  org.apache.cayenne.map.event.DataMapEvent;
 import  org.apache.cayenne.map.event.DataNodeEvent;
 import  org.apache.cayenne.project.ApplicationProject;
 import  org.apache.cayenne.util.Util;
 import  org.apache.cayenne.validation.ValidationException;
 
 import  com.jgoodies.forms.builder.DefaultFormBuilder;
 import  com.jgoodies.forms.layout.FormLayout;

Panel for editing a DataMap.
 
 public class DataMapView extends JPanel {
 
     protected ProjectController eventController;
 
     protected TextAdapter name;
     protected JLabel location;
     protected JComboBox nodeSelector;
     protected JCheckBox defaultLockType;
     protected TextAdapter defaultSchema;
     protected TextAdapter defaultPackage;
     protected TextAdapter defaultSuperclass;
     protected JCheckBox quoteSQLIdentifiers;
 
     protected JButton updateDefaultSchema;
     protected JButton updateDefaultPackage;
     protected JButton updateDefaultSuperclass;
     protected JButton updateDefaultLockType;
 
     // client stuff
     protected JCheckBox clientSupport;
 
     protected JLabel defaultClientPackageLabel;
     protected TextAdapter defaultClientPackage;
     protected TextAdapter defaultClientSuperclass;
     protected JButton updateDefaultClientPackage;
 
     public DataMapView(ProjectController eventController) {
         this. = eventController;
 
         initView();
         initController();
     }
 
    private void initView() {
        // create widgets
         = new TextAdapter(new JTextField()) {
            protected void updateModel(String text) {
                setDataMapName(text);
            }
        };
         = new JLabel();
         = CayenneWidgetFactory.createUndoableComboBox();
        .setRenderer(CellRenderers.listRendererWithIcons());
         = new JButton("Update...");
         = new TextAdapter(new JTextField()) {
            protected void updateModel(String text) {
                setDefaultSchema(text);
            }
        };
        
         = new JCheckBox();
         = new JButton("Update...");
         = new TextAdapter(new JTextField()) {
            protected void updateModel(String text) {
                setDefaultPackage(text);
            }
        };
         = new JButton("Update...");
         = new TextAdapter(new JTextField()) {
            protected void updateModel(String text) {
                setDefaultSuperclass(text);
            }
        };
         = new JButton("Update...");
         = new JCheckBox();
         = new JCheckBox();
         = new JButton("Update...");
         = new TextAdapter(new JTextField()) {
            protected void updateModel(String text) {
                setDefaultClientPackage(text);
            }
        };
         = new JButton("Update...");
         = new TextAdapter(new JTextField()) {
            protected void updateModel(String text) {
                setDefaultClientSuperclass(text);
            }
        };
        // assemble
        FormLayout layout = new FormLayout(
                "right:70dlu, 3dlu, fill:110dlu, 3dlu, fill:100",
                "");
        DefaultFormBuilder builder = new DefaultFormBuilder(layout);
        builder.setDefaultDialogBorder();
        builder.appendSeparator("DataMap Configuration");
        builder.append("DataMap Name:".getComponent(), 3);
        builder.append("File:", 3);
        builder.append("DataNode:", 3);
        builder.append("Quote SQL Identifiers:", 3);
        builder.appendSeparator("Entity Defaults");
        builder.append("DB Schema:".getComponent(), );
        builder.append(
                "Java Package:",
                .getComponent(),
                );
        builder.append(
                "Custom Superclass:",
                .getComponent(),
                );
        builder.append("Optimistic Locking:");
        builder.appendSeparator("Client Class Defaults");
        builder.append("Allow Client Entities:"new JPanel());
         = builder.append(
                "Client Java Package:",
                .getComponent(),
                );
        builder.append(
                "Custom Superclass:",
                .getComponent(),
                );
        this.setLayout(new BorderLayout());
        add(builder.getPanel(), .);
    }
    private void initController() {
            public void currentDataMapChanged(DataMapDisplayEvent e) {
                DataMap map = e.getDataMap();
                if (map != null) {
                    initFromModel(map);
                }
            }
        });
            public void actionPerformed(ActionEvent e) {
                setDataNode();
            }
        });
        
            public void actionPerformed(ActionEvent e) {
                setQuoteSQLIdentifiers(.isSelected());
            }
        });
            public void actionPerformed(ActionEvent e) {
                setDefaultLockType(.isSelected()
                        ? ObjEntity.LOCK_TYPE_OPTIMISTIC
                        : ObjEntity.LOCK_TYPE_NONE);
            }
        });
            public void actionPerformed(ActionEvent e) {
                setClientSupport(.isSelected());
            }
        });
            public void actionPerformed(ActionEvent e) {
                updateDefaultClientPackage();
            }
        });
            public void actionPerformed(ActionEvent e) {
                updateDefaultClientSuperclass();
            }
        });
            public void actionPerformed(ActionEvent e) {
                updateDefaultSchema();
            }
        });
            public void actionPerformed(ActionEvent e) {
                updateDefaultPackage();
            }
        });
            public void actionPerformed(ActionEvent e) {
                updateDefaultSuperclass();
            }
        });
            public void actionPerformed(ActionEvent e) {
                updateDefaultLockType();
            }
        });
    }

    
Updates the view from the current model state. Invoked when a currently displayed ObjEntity is changed.
    private void initFromModel(DataMap map) {
        .setText(map.getName());
        String locationText = map.getLocation();
        .setText((locationText != null) ? locationText : "(no file)");
        .setSelected(map.isQuotingSQLIdentifiers());
        // rebuild data node list
        Object nodes[] = .getCurrentDataDomain().getDataNodes().toArray();
        // add an empty item to the front
        Object[] objects = new Object[nodes.length + 1];
        // objects[0] = null;
        // now add the entities
        if (nodes.length > 0) {
            Arrays.sort(nodes, Comparators.getNamedObjectComparator());
            System.arraycopy(nodes, 0, objects, 1, nodes.length);
        }
        DefaultComboBoxModel model = new DefaultComboBoxModel(objects);
        // find selected node
        for (int i = 0; i < nodes.lengthi++) {
            DataNode node = (DataNode) nodes[i];
            if (node.getDataMaps().contains(map)) {
                model.setSelectedItem(node);
                break;
            }
        }
        .setModel(model);
        // init default fields
        .setSelected(map.getDefaultLockType() != ObjEntity.LOCK_TYPE_NONE);
        .setText(map.getDefaultPackage());
        .setText(map.getDefaultSchema());
        .setText(map.getDefaultSuperclass());
        // client defaults
        .setSelected(map.isClientSupported());
        .setText(map.getDefaultClientPackage());
        .setText(map.getDefaultClientSuperclass());
        toggleClientProperties(map.isClientSupported());
    }
    private void toggleClientProperties(boolean enabled) {
        .getComponent().setEnabled(enabled);
        .setEnabled(enabled);
        .setEnabled(enabled);
    }
    void setDefaultLockType(int lockType) {
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        int oldType = dataMap.getDefaultLockType();
        if (oldType == lockType) {
            return;
        }
        dataMap.setDefaultLockType(lockType);
        .fireDataMapEvent(new DataMapEvent(thisdataMap));
    }
    void setClientSupport(boolean flag) {
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (dataMap.isClientSupported() != flag) {
            dataMap.setClientSupported(flag);
            toggleClientProperties(flag);
            .fireDataMapEvent(new DataMapEvent(thisdataMap));
        }
    }
    
    void setQuoteSQLIdentifiers(boolean flag) {
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (dataMap.isQuotingSQLIdentifiers() != flag) {
            dataMap.setQuotingSQLIdentifiers(flag);
            
            .fireDataMapEvent(new DataMapEvent(thisdataMap));
        }
    }
    void setDefaultPackage(String newDefaultPackage) {
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (newDefaultPackage != null && newDefaultPackage.trim().length() == 0) {
            newDefaultPackage = null;
        }
        String oldPackage = dataMap.getDefaultPackage();
        if (Util.nullSafeEquals(newDefaultPackageoldPackage)) {
            return;
        }
        dataMap.setDefaultPackage(newDefaultPackage);
        // update class generation preferences
                newDefaultPackage,
                .);
        .fireDataMapEvent(new DataMapEvent(thisdataMap));
    }
    void setDefaultClientPackage(String newDefaultPackage) {
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (newDefaultPackage != null && newDefaultPackage.trim().length() == 0) {
            newDefaultPackage = null;
        }
        String oldPackage = dataMap.getDefaultClientPackage();
        if (Util.nullSafeEquals(newDefaultPackageoldPackage)) {
            return;
        }
        dataMap.setDefaultClientPackage(newDefaultPackage);
        // TODO: (andrus, 09/10/2005) - add the same logic for the client package
        // update class generation preferences
        // eventController.getDataMapPreferences().setSuperclassPackage(
        // newDefaultPackage,
        // DataMapDefaults.DEFAULT_SUPERCLASS_PACKAGE);
        .fireDataMapEvent(new DataMapEvent(thisdataMap));
    }
    void setDefaultClientSuperclass(String newSuperclass) {
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (newSuperclass != null && newSuperclass.trim().length() == 0) {
            newSuperclass = null;
        }
        String oldSuperclass = dataMap.getDefaultClientSuperclass();
        if (Util.nullSafeEquals(newSuperclassoldSuperclass)) {
            return;
        }
        dataMap.setDefaultClientSuperclass(newSuperclass);
        .fireDataMapEvent(new DataMapEvent(thisdataMap));
    }
    void setDefaultSchema(String newSchema) {
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (newSchema != null && newSchema.trim().length() == 0) {
            newSchema = null;
        }
        String oldSchema = dataMap.getDefaultSchema();
        if (Util.nullSafeEquals(newSchemaoldSchema)) {
            return;
        }
        dataMap.setDefaultSchema(newSchema);
        .fireDataMapEvent(new DataMapEvent(thisdataMap));
    }
    void setDefaultSuperclass(String newSuperclass) {
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (newSuperclass != null && newSuperclass.trim().length() == 0) {
            newSuperclass = null;
        }
        String oldSuperclass = dataMap.getDefaultSuperclass();
        if (Util.nullSafeEquals(newSuperclassoldSuperclass)) {
            return;
        }
        dataMap.setDefaultSuperclass(newSuperclass);
        .fireDataMapEvent(new DataMapEvent(thisdataMap));
    }
    void setDataMapName(String newName) {
        if (newName == null || newName.trim().length() == 0) {
            throw new ValidationException("Enter name for DataMap");
        }
        DataMap map = .getCurrentDataMap();
        // search for matching map name across domains, as currently they have to be
        // unique globally
        Configuration config = ((ApplicationProject) Application.getProject())
                .getConfiguration();
        DataMap matchingMap = null;
        for (DataDomain domain : config.getDomains()) {
            DataMap nextMap = domain.getMap(newName);
            if (nextMap == map) {
                continue;
            }
            if (nextMap != null) {
                matchingMap = nextMap;
                break;
            }
        }
        if (matchingMap != null) {
            // there is an entity with the same name
            throw new ValidationException("There is another DataMap named '"
                    + newName
                    + "'. Use a different name.");
        }
        String oldName = map.getName();
        if (Util.nullSafeEquals(newNameoldName)) {
            return;
        }
        // completely new name, set new name for domain
        DataMapEvent e = new DataMapEvent(thismapmap.getName());
        ProjectUtil.setDataMapName(.getCurrentDataDomain(), mapnewName);
        pref.rename(newName);
    }
    void setDataNode() {
        DataNode node = (DataNode) .getSelectedItem();
        DataMap map = .getCurrentDataMap();
        // no change?
        if (node != null && node.getDataMaps().contains(map)) {
            return;
        }
        boolean hasChanges = false;
        // unlink map from any nodes
        for (DataNode nextNode : .getCurrentDataDomain().getDataNodes()) {
            // Theoretically only one node may contain a datamap at each given time.
            // Being paranoid, we will still scan through all.
            if (nextNode != node && nextNode.getDataMaps().contains(map)) {
                nextNode.removeDataMap(map.getName());
                // announce DataNode change
                .fireDataNodeEvent(new DataNodeEvent(thisnextNode));
                hasChanges = true;
            }
        }
        // link to a selected node
        if (node != null) {
            node.addDataMap(map);
            hasChanges = true;
            // announce DataNode change
            .fireDataNodeEvent(new DataNodeEvent(thisnode));
        }
        if (hasChanges) {
            // TODO: maybe reindexing is an overkill in the modeler?
            .getCurrentDataDomain().reindexNodes();
        }
    }
    void updateDefaultSchema() {
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (dataMap.getDbEntities().size() > 0 || dataMap.getProcedures().size() > 0) {
            new SchemaUpdateController(dataMap).startup();
        }
    }
    void updateDefaultSuperclass() {
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (dataMap.getObjEntities().size() > 0) {
            new SuperclassUpdateController(dataMapfalse).startup();
        }
    }
    void updateDefaultPackage() {
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (dataMap.getObjEntities().size() > 0 || dataMap.getEmbeddables().size() > 0) {
            new PackageUpdateController(dataMapfalse).startup();
        }
    }
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (dataMap.getObjEntities().size() > 0) {
            new PackageUpdateController(dataMaptrue).startup();
        }
    }
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (dataMap.getObjEntities().size() > 0) {
            new SuperclassUpdateController(dataMaptrue).startup();
        }
    }
    void updateDefaultLockType() {
        DataMap dataMap = .getCurrentDataMap();
        if (dataMap == null) {
            return;
        }
        if (dataMap.getObjEntities().size() > 0) {
            new LockingUpdateController(dataMap).startup();
        }
    }
New to GrepCode? Check out our FAQ X