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 java.awt.Color;
 import java.util.List;
 
 
 import  org.apache.cayenne.access.DataDomain;
 import  org.apache.cayenne.exp.Expression;
 import  org.apache.cayenne.map.DataMap;
 import  org.apache.cayenne.map.DbEntity;
 import  org.apache.cayenne.map.ObjEntity;
 import  org.apache.cayenne.map.ObjAttribute;
 import  org.apache.cayenne.map.event.EntityEvent;
 import  org.apache.cayenne.util.CayenneMapEntry;
 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 ObjEntity properties.
 
 public class ObjEntityTab extends JPanel implements ObjEntityDisplayListener,
         ExistingSelectionProcessor {
 
     private static final Object noInheritance = new CayenneMapEntry() {
 
         public String getName() {
             return "Direct Mapping to Table/View";
         }
 
         public Object getParent() {
             return null;
         }
 
         public void setParent(Object parent) {
         }
     };
 
     protected ProjectController mediator;
     protected TextAdapter name;
     protected TextAdapter className;
 
     protected JLabel superclassLabel;
     protected TextAdapter superClassName;
     protected TextAdapter qualifier;
    protected JComboBox dbEntityCombo;
    protected JButton syncWithDbEntityButton;
    protected JComboBox superEntityCombo;
    protected JButton tableLabel;
    protected JCheckBox readOnly;
    protected JCheckBox optimisticLocking;
    protected JComponent clientSeparator;
    protected JLabel isAbstractLabel;
    protected JLabel serverOnlyLabel;
    protected JLabel clientClassNameLabel;
    protected JCheckBox serverOnly;
    protected JCheckBox isAbstract;
    protected TextAdapter clientClassName;
    public ObjEntityTab(ProjectController mediator) {
        this. = mediator;
        initView();
        initController();
    }
    private void initView() {
        this.setLayout(new BorderLayout());
        JToolBar toolBar = new JToolBar();
        Application app = Application.getInstance();
        toolBar.add(app.getAction(ObjEntitySyncAction.getActionName()).buildButton());
        toolBar.add(app.getAction(CreateAttributeAction.getActionName()).buildButton());
        toolBar
                .add(app
                        .getAction(CreateRelationshipAction.getActionName())
                        .buildButton());
        add(toolBar.);
        // create widgets
         = new TextAdapter(new JTextField()) {
            @Override
            protected void updateModel(String text) {
                setEntityName(text);
            }
        };
         = new TextAdapter(new JTextField()) {
            @Override
            protected void updateModel(String text) {
                setSuperClassName(text);
            }
        };
         = new TextAdapter(new JTextField()) {
            @Override
            protected void updateModel(String text) {
                setClassName(text);
            }
        };
         = new TextAdapter(new JTextField()) {
            @Override
            protected void updateModel(String text) {
                setQualifier(text);
            }
        };
         = CayenneWidgetFactory.createComboBox();
         = CayenneWidgetFactory.createComboBox();
        
        AutoCompletion.enable();
        AutoCompletion.enable();
         = new JCheckBox();
         = new JCheckBox();
         = new JCheckBox();
         = new JCheckBox();
         = CayenneWidgetFactory.createLabelButton("Table/View:");
         = CayenneWidgetFactory.createButton("Sync w/DbEntity");
        .setIcon(ModelerUtil.buildIcon("icon-sync.gif"));
        .setToolTipText("Sync this ObjEntity with its DBEntity");
         = new JCheckBox();
         = new JCheckBox();
         = new TextAdapter(new JTextField()) {
            @Override
            protected void updateModel(String text) {
                setClientClassName(text);
            }
        };
         = new TextAdapter(new JTextField()) {
            @Override
            protected void updateModel(String text) {
                setClientSuperClassName(text);
            }
        };
        // assemble
        FormLayout layout = new FormLayout(
                "right:100dlu, 3dlu, fill:135dlu, 3dlu, pref",
                "");
        DefaultFormBuilder builder = new DefaultFormBuilder(layout);
        builder.setDefaultDialogBorder();
        builder.appendSeparator("ObjEntity Configuration");
        builder.append("ObjEntity Name:".getComponent(), 3);
        builder.append("Inheritance:", 3);
        builder.append();
         = builder.append("Abstract class:", 3);
        builder.appendSeparator();
        builder.append("Java Class:".getComponent(), 3);
         = builder.append("Superclass:".getComponent(), 3);
        builder.append("Qualifier:".getComponent(), 3);
        builder.append("Read-Only:", 3);
        builder.append("Optimistic Locking:", 3);
        // add callback-related stuff
        builder.append("Exclude superclass listeners:", 3);
        builder.append("Exclude default listeners:", 3);
         = builder.appendSeparator("Java Client");
         = builder.append("Not for Client Use:", 3);
         = builder.append("Client Java Class:"
                .getComponent(), 3);
         = builder.append(
                "Client Superclass:",
                .getComponent(),
                3);
        add(builder.getPanel(), .);
    }
    private void initController() {
        // initialize events processing and tracking of UI updates...
            public void actionPerformed(ActionEvent e) {
                // Change DbEntity for current ObjEntity
                ObjEntity entity = .getCurrentObjEntity();
                DbEntity dbEntity = (DbEntity) .getSelectedItem();
                .setEnabled(dbEntity != null);
                if (dbEntity != entity.getDbEntity()) {
                    entity.setDbEntity(dbEntity);
                    .fireObjEntityEvent(new EntityEvent(thisentity));
                }
            }
        });
            public void actionPerformed(ActionEvent e) {
                // Change super-entity
                CayenneMapEntry superEntity = (CayenneMapEntry) 
                        .getSelectedItem();
                String name = (superEntity==null || superEntity == ) ? null : superEntity
                        .getName();
                ObjEntity entity = .getCurrentObjEntity();
                if (!Util.nullSafeEquals(nameentity.getSuperEntityName())) {
                    List<ObjAttribute> duplicateAttributes = null;
                    if(name!=null){
                        duplicateAttributes = getDuplicatedAttributes((ObjEntity) superEntity);
                    }
                    if (duplicateAttributes!=null && duplicateAttributes.size() > 0) {
                        DuplicatedAttributesDialog.showDialog(Application.getFrame(), duplicateAttributes, (ObjEntity) superEntityentity);
                        if (DuplicatedAttributesDialog.getResult().equals(.)) {
                            .setSelectedItem(entity.getSuperEntity());
                            .setText(entity.getSuperClassName());
                            return;
                        }
                        
                    }
                    entity.setSuperEntityName(name);
                    // if a super-entity selected, disable table selection
                    // and also update parent DbEntity selection...
                    toggleEnabled(name == null, !.isSelected());
                    .getModel().setSelectedItem(entity.getDbEntity());
                    .setText(entity.getSuperClassName());
                    // fire both EntityEvent and EntityDisplayEvent;
                    // the later is to update attribute and relationship display
                    DataDomain domain = .getCurrentDataDomain();
                    DataMap map = .getCurrentDataMap();
                    .fireObjEntityEvent(new EntityEvent(thisentity));
                    .fireObjEntityDisplayEvent(new EntityDisplayEvent(
                            this,
                            entity,
                            map,
                            domain));
                }
            }
        });
            public void actionPerformed(ActionEvent e) {
                // Jump to DbEntity of the current ObjEntity
                DbEntity entity = .getCurrentObjEntity().getDbEntity();
                if (entity != null) {
                    DataDomain dom = .getCurrentDataDomain();
                    .fireDbEntityDisplayEvent(new EntityDisplayEvent(
                            this,
                            entity,
                            entity.getDataMap(),
                            dom));
                }
            }
        });
                .getApplication()));
        .addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ObjEntity entity = .getCurrentObjEntity();
                if (entity != null) {
                    entity.setReadOnly(.isSelected());
                    .fireObjEntityEvent(new EntityEvent(thisentity));
                }
            }
        });
            public void actionPerformed(ActionEvent e) {
                ObjEntity entity = .getCurrentObjEntity();
                if (entity != null) {
                    entity.setDeclaredLockType(.isSelected()
                            ? ObjEntity.LOCK_TYPE_OPTIMISTIC
                            : ObjEntity.LOCK_TYPE_NONE);
                    .fireObjEntityEvent(new EntityEvent(thisentity));
                }
            }
        });
            public void actionPerformed(ActionEvent e) {
                ObjEntity entity = .getCurrentObjEntity();
                if (entity != null) {
                    entity.setExcludingSuperclassListeners(
                            .isSelected());
                    .fireObjEntityEvent(new EntityEvent(thisentity));
                }
            }
        });
            public void actionPerformed(ActionEvent e) {
                ObjEntity entity = .getCurrentObjEntity();
                if (entity != null) {
                    entity.setExcludingDefaultListeners(
                            .isSelected());
                    .fireObjEntityEvent(new EntityEvent(thisentity));
                }
            }
        });
            public void actionPerformed(ActionEvent e) {
                ObjEntity entity = .getCurrentObjEntity();
                if (entity != null) {
                    entity.setServerOnly(.isSelected());
                    toggleEnabled(.isEnabled(), !.isSelected());
                    .fireObjEntityEvent(new EntityEvent(thisentity));
                }
            }
        });
        
            public void actionPerformed(ActionEvent e) {
                ObjEntity entity = .getCurrentObjEntity();
                if (entity != null) {
                    entity.setAbstract(.isSelected());
                    .fireObjEntityEvent(new EntityEvent(thisentity));
                }
            }
        });
    }

    
Updates the view from the current model state. Invoked when a currently displayed ObjEntity is changed.

Parameters:
entity current entity
    private void initFromModel(final ObjEntity entity) {
        // TODO: this is a hack until we implement a real MVC
        .setText(entity.getName());
        .setText(entity.getSuperClassName());
        .setText(entity.getClassName());
        .setSelected(entity.isReadOnly());
        
        .setSelected(entity.isAbstract());
        .setSelected(entity.isServerOnly());
        .setText(entity.getClientClassName());
        .setText(entity.getClientSuperClassName());
                .getDeclaredQualifier()));
        // TODO: fix inheritance - we should allow to select optimistic
        // lock if superclass is not already locked,
        // otherwise we must keep this checked in but not editable.
        
                .setSelected(entity.getDeclaredLockType() == ObjEntity.LOCK_TYPE_OPTIMISTIC);
        .setSelected(entity.isExcludingSuperclassListeners());
        .setSelected(entity.isExcludingDefaultListeners());
        // init DbEntities
        DataMap map = .getCurrentDataMap();
        Object[] dbEntities = map.getNamespace().getDbEntities().toArray();
        Arrays.sort(dbEntities, Comparators.getDataMapChildrenComparator());
        DefaultComboBoxModel dbModel = new DefaultComboBoxModel(dbEntities);
        dbModel.setSelectedItem(entity.getDbEntity());
        .setRenderer(CellRenderers.entityListRendererWithIcons(map));
        .setModel(dbModel);
        // toggle visibilty and editability rules
        toggleClientFieldsVisible(map.isClientSupported());
        toggleEnabled(entity.getSuperEntityName() == null, !entity.isServerOnly());
        // init ObjEntities for inheritance
        Predicate inheritanceFilter = new Predicate() {
            public boolean evaluate(Object object) {
                // do not show this entity or any of the subentities
                if (entity == object) {
                    return false;
                }
                if (object instanceof ObjEntity) {
                    return !((ObjEntity) object).isSubentityOf(entity);
                }
                return false;
            }
        };
        Object[] objEntities = CollectionUtils.select(
                map.getNamespace().getObjEntities(),
                inheritanceFilter).toArray();
        Arrays.sort(objEntities, Comparators.getDataMapChildrenComparator());
        Object[] finalObjEntities = new Object[objEntities.length + 1];
        finalObjEntities[0] = ;
        System.arraycopy(objEntities, 0, finalObjEntities, 1, objEntities.length);
        DefaultComboBoxModel superEntityModel = new DefaultComboBoxModel(finalObjEntities);
        superEntityModel.setSelectedItem(entity.getSuperEntity());
        .setRenderer(CellRenderers.entityListRendererWithIcons(map));
        .setModel(superEntityModel);
    }
    void setClassName(String className) {
        if (className != null && className.trim().length() == 0) {
            className = null;
        }
        ObjEntity entity = .getCurrentObjEntity();
        // "ent" may be null if we quit editing by changing tree selection
        if (entity != null && !Util.nullSafeEquals(entity.getClassName(), className)) {
            entity.setClassName(className);
            .fireObjEntityEvent(new EntityEvent(thisentity));
        }
    }
    void setSuperClassName(String text) {
        if (text != null && text.trim().length() == 0) {
            text = null;
        }
        ObjEntity ent = .getCurrentObjEntity();
        if (ent != null && !Util.nullSafeEquals(ent.getSuperClassName(), text)) {
            ent.setSuperClassName(text);
            .fireObjEntityEvent(new EntityEvent(thisent));
        }
    }
    void setClientClassName(String className) {
        if (className != null && className.trim().length() == 0) {
            className = null;
        }
        ObjEntity entity = .getCurrentObjEntity();
        // "ent" may be null if we quit editing by changing tree selection
        if (entity != null
                && !Util.nullSafeEquals(entity.getClientClassName(), className)) {
            entity.setClientClassName(className);
            .fireObjEntityEvent(new EntityEvent(thisentity));
        }
    }
    void setClientSuperClassName(String text) {
        if (text != null && text.trim().length() == 0) {
            text = null;
        }
        ObjEntity ent = .getCurrentObjEntity();
        if (ent != null && !Util.nullSafeEquals(ent.getClientSuperClassName(), text)) {
            ent.setClientSuperClassName(text);
            .fireObjEntityEvent(new EntityEvent(thisent));
        }
    }
    void setQualifier(String text) {
        if (text != null && text.trim().length() == 0) {
            text = null;
        }
        ObjEntity entity = .getCurrentObjEntity();
        if (entity != null) {
            ExpressionConvertor convertor = new ExpressionConvertor();
            try {
                String oldQualifier = convertor.valueAsString(entity
                        .getDeclaredQualifier());
                if (!Util.nullSafeEquals(oldQualifiertext)) {
                    Expression exp = (Expression) convertor.stringAsValue(text);
                    entity.setDeclaredQualifier(exp);
                    .fireObjEntityEvent(new EntityEvent(thisentity));
                }
            }
            catch (IllegalArgumentException ex) {
                // unparsable qualifier
                throw new ValidationException(ex.getMessage());
            }
        }
    }
    void setEntityName(String newName) {
        if (newName != null && newName.trim().length() == 0) {
            newName = null;
        }
        ObjEntity entity = .getCurrentObjEntity();
        if (entity == null) {
            return;
        }
        if (Util.nullSafeEquals(newNameentity.getName())) {
            return;
        }
        if (newName == null) {
            throw new ValidationException("Entity name is required.");
        }
        else if (entity.getDataMap().getObjEntity(newName) == null) {
            // completely new name, set new name for entity
            EntityEvent e = new EntityEvent(thisentityentity.getName());
            entity.setName(newName);
            .fireObjEntityEvent(e);
            // suggest to update class name
            ClassNameUpdater nameUpdater = new ClassNameUpdater(Application
                    .getInstance()
                    .getFrameController(), entity);
            if (nameUpdater.doNameUpdate()) {
                .setText(entity.getClassName());
                .setText(entity.getClientClassName());
            }
        }
        else {
            // there is an entity with the same name
            throw new ValidationException("There is another entity with name '"
                    + newName
                    + "'.");
        }
    }
    void toggleClientFieldsVisible(boolean visible) {
        .setVisible(visible);
        .setVisible(visible);
        .setVisible(visible);
        .setVisible(visible);
        .getComponent().setVisible(visible);
        .getComponent().setVisible(visible);
        .setVisible(visible);
    }
    void toggleEnabled(boolean directTableMappingboolean clientFieldsEnabled) {
        .getComponent().setEnabled(directTableMapping);
        .setEnabled(directTableMapping);
                directTableMapping && clientFieldsEnabled);
        .setEnabled(directTableMapping && clientFieldsEnabled);
        .setEnabled(clientFieldsEnabled);
        .getComponent().setEnabled(clientFieldsEnabled);
    }
    public void processExistingSelection(EventObject e) {
        EntityDisplayEvent ede = new EntityDisplayEvent(this
                .getCurrentObjEntity(), .getCurrentDataMap(), 
                .getCurrentDataDomain());
    }
    public void currentObjEntityChanged(EntityDisplayEvent e) {
        ObjEntity entity = (ObjEntity) e.getEntity();
        if (entity == null || !e.isEntityChanged()) {
            return;
        }
        initFromModel(entity);
    }
    
    private List<ObjAttribute> getDuplicatedAttributes(ObjEntity superEntity) {
        List<ObjAttribute> result = new LinkedList<ObjAttribute>();
        ObjEntity entity = .getCurrentObjEntity();
        for (ObjAttribute attribute : entity.getAttributes()) {
            if(superEntity.getAttribute(attribute.getName()) != null) {
                result.add(attribute);
            }
        }
        
        return result;
    }