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.dbentity;
 
 
 
 import  org.apache.cayenne.exp.Expression;
 import  org.apache.cayenne.map.DbAttribute;
 import  org.apache.cayenne.map.DbEntity;
 import  org.apache.cayenne.map.event.EntityEvent;
 import  org.apache.cayenne.util.Util;
 import  org.apache.cayenne.validation.ValidationException;
 
 import  com.jgoodies.forms.builder.DefaultFormBuilder;
 import  com.jgoodies.forms.layout.FormLayout;

Detail view of the DbEntity properties.
 
 public class DbEntityTab extends JPanel implements ExistingSelectionProcessor,
         DbEntityDisplayListener {
 
     static final String PK_DEFAULT_GENERATOR = "Default";
     static final String PK_DB_GENERATOR = "Database-Generated";
     static final String PK_CUSTOM_SEQUENCE_GENERATOR = "Custom Sequence";
 
     static final String[] PK_GENERATOR_TYPES = new String[] {
     };
 
     protected ProjectController mediator;
 
     protected TextAdapter name;
     protected TextAdapter schema;
     protected TextAdapter qualifier;
     protected JLabel schemaLabel;
 
     protected JComboBox pkGeneratorType;
     protected JPanel pkGeneratorDetail;
     protected CardLayout pkGeneratorDetailLayout;
 
     public DbEntityTab(ProjectController mediator) {
         super();
         this. = mediator;
 
         initView();
         initController();
     }
 
     private void initView() {
 
         JToolBar toolBar = new JToolBar();
         Application app = Application.getInstance();
         toolBar.add(app.getAction(CreateObjEntityAction.getActionName()).buildButton());
         toolBar.add(app.getAction(DbEntitySyncAction.getActionName()).buildButton());
         toolBar.addSeparator();
 
         toolBar.add(app.getAction(CreateAttributeAction.getActionName()).buildButton());
         toolBar
                .add(app
                        .getAction(CreateRelationshipAction.getActionName())
                        .buildButton());
        // create widgets
         = new TextAdapter(new JTextField()) {
            protected void updateModel(String text) {
                setEntityName(text);
            }
        };
         = new JLabel("Schema:");
         = new TextAdapter(new JTextField()) {
            protected void updateModel(String textthrows ValidationException {
                setSchema(text);
            }
        };
         = new TextAdapter(new JTextField()) {
            protected void updateModel(String qualifier) {
                setQualifier(qualifier);
            }
        };
         = new JComboBox();
        .setEditable(false);
         = new CardLayout();
        
                .add(new PKDefaultGeneratorPanel(), );
        .add(
                new PKCustomSequenceGeneratorPanel(),
                );
        // assemble
        FormLayout layout = new FormLayout("right:pref, 3dlu, fill:200dlu""");
        DefaultFormBuilder builder = new DefaultFormBuilder(layout);
        builder.setDefaultDialogBorder();
        builder.appendSeparator("DbEntity Configuration");
        builder.append("DbEntity Name:".getComponent());
        builder.append(.getComponent());
        builder.append("Qualifier".getComponent());
        builder.appendSeparator("Primary Key");
        builder.append("PK Generation Strategy:");
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(builder.getPanel(), .);
        mainPanel.add(.);
        setLayout(new BorderLayout());
        add(toolBar.);
        add(mainPanel.);
    }
    private void initController() {
            public void itemStateChanged(ItemEvent e) {
                        .getSelectedItem());
                for (int i = 0; i < .getComponentCount(); i++) {
                    if (.getComponent(i).isVisible()) {
                        DbEntity entity = .getCurrentDbEntity();
                        PKGeneratorPanel panel = (PKGeneratorPanel
                                .getComponent(i);
                        panel.onInit(entity);
                        break;
                    }
                }
            }
        });
    }
    public void processExistingSelection(EventObject e) {
        EntityDisplayEvent ede = new EntityDisplayEvent(this
                .getCurrentDbEntity(), .getCurrentDataMap(), 
                .getCurrentDataDomain());
    }
    public void currentDbEntityChanged(EntityDisplayEvent e) {
        DbEntity entity = (DbEntity) e.getEntity();
        if (entity == null) {
            return;
        }
        // if entity hasn't changed, still notify PK Generator panels, as entity PK may
        // have changed...
        for (int i = 0; i < .getComponentCount(); i++) {
            ((PKGeneratorPanel.getComponent(i)).setDbEntity(entity);
        }
        if (!e.isEntityChanged()) {
            //name.getComponent().requestFocusInWindow();
            return;
        }
        .setText(entity.getName());
        .setText(entity.getSchema());
        .setText(new ExpressionConvertor().valueAsString(entity.getQualifier()));
        String type = ;
        if (entity.getPrimaryKeyGenerator() != null) {
            type = ;
        }
        else {
            for (DbAttribute a : entity.getPrimaryKeys()) {
                if (a.isGenerated()) {
                    type = ;
                    break;
                }
            }
        }
        .setEnabled(true);
        .getComponent().setEnabled(true);
        .setVisible(true);
        .setVisible(true);
        .setSelectedItem(type);
    }
    void setEntityName(String newName) {
        if (newName != null && newName.trim().length() == 0) {
            newName = null;
        }
        DbEntity entity = .getCurrentDbEntity();
        if (entity == null || Util.nullSafeEquals(newNameentity.getName())) {
            return;
        }
        if (newName == null) {
            throw new ValidationException("Entity name is required.");
        }
        else if (entity.getDataMap().getDbEntity(newName) == null) {
            // completely new name, set new name for entity
            EntityEvent e = new EntityEvent(thisentityentity.getName());
            entity.setName(newName);
            // ProjectUtil.setDbEntityName(entity, newName);
            .fireDbEntityEvent(e);
        }
        else {
            // there is an entity with the same name
            throw new ValidationException("There is another entity with name '"
                    + newName
                    + "'.");
        }
    }
    void setSchema(String text) {
        if (text != null && text.trim().length() == 0) {
            text = null;
        }
        DbEntity ent = .getCurrentDbEntity();
        if (ent != null && !Util.nullSafeEquals(ent.getSchema(), text)) {
            ent.setSchema(text);
            .fireDbEntityEvent(new EntityEvent(thisent));
        }
    }
    
    void setQualifier(String qualifier) {
        if (qualifier != null && qualifier.trim().length() == 0) {
            qualifier = null;
        }
        DbEntity ent = .getCurrentDbEntity();
        if (ent != null && !Util.nullSafeEquals(ent.getQualifier(), qualifier)) {
            ExpressionConvertor convertor = new ExpressionConvertor();
            try {
                String oldQualifier = convertor.valueAsString(ent.getQualifier());
                if (!Util.nullSafeEquals(oldQualifierqualifier)) {
                    Expression exp = (Expression) convertor.stringAsValue(qualifier);
                    ent.setQualifier(exp);
                    .fireDbEntityEvent(new EntityEvent(thisent));
                }
            }
            catch (IllegalArgumentException ex) {
                // unparsable qualifier
                throw new ValidationException(ex.getMessage());
            }
            
        }
    }
New to GrepCode? Check out our FAQ X