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.objentity;
 
 import java.awt.Color;
 import java.util.List;
 import java.util.Map;
 
 
 import  org.apache.cayenne.map.Attribute;
 import  org.apache.cayenne.map.DbAttribute;
 import  org.apache.cayenne.map.DbEntity;
 import  org.apache.cayenne.map.DbRelationship;
 import  org.apache.cayenne.map.Embeddable;
 import  org.apache.cayenne.map.EmbeddableAttribute;
 import  org.apache.cayenne.map.EmbeddedAttribute;
 import  org.apache.cayenne.map.Entity;
 import  org.apache.cayenne.map.ObjAttribute;
 import  org.apache.cayenne.map.ObjEntity;
 import  org.apache.cayenne.map.Relationship;
 import  org.apache.cayenne.map.event.AttributeEvent;
 import  org.apache.cayenne.map.event.EntityEvent;
 import  org.apache.cayenne.map.event.MapEvent;
 import  org.apache.cayenne.util.CayenneMapEntry;
 
 public class ObjAttributeInfoDialog extends CayenneController implements
         TreeSelectionListener {
 
     private ObjAttributeTableModel model;
     private int row;
     protected ObjAttributeInfoDialogView view;
     protected ObjAttribute attribute;
     protected ObjAttribute attributeSaved;
 
     protected List<DbEntity> relTargets;
 
     protected List<ObjEntity> objectTargets;
 
     protected Map<String, Embeddable> stringToEmbeddables;
     protected List<StringembeddableNames;
 
     protected ProjectController mediator;
     private Object lastObjectType;
 
     public ObjAttributeInfoDialog(ProjectController mediatorint row,
             ObjAttributeTableModel model) {
         super(mediator);
         this. = new ObjAttributeInfoDialogView(mediator);
         this. = mediator;
         this. = model;
         this. = row;
         this. = new HashMap<String, Embeddable>();
         this. = new ArrayList<String>();
 
         Iterator<Embeddable> embs = mediator.getEmbeddableNamesInCurRentDataDomain().iterator();
        while (embs.hasNext()) {
            Embeddable emb = (Embeddable) embs.next();
            .put(emb.getClassName(), emb);
            .add(emb.getClassName());
        }
        initController(model.getAttribute(row));
    }
    @Override
    public Component getView() {
        return ;
    }

    
Starts options dialog.
    public void startupAction() {
        .pack();
        .setModal(true);
        makeCloseableOnEscape();
        centerView();
        .setVisible(true);
    }
    private void initController(ObjAttribute attr) {
        for (int i = 0; i < .size(); i++) {
                    .get(i)
                    .toString());
        }
        this. = attr;
        if ( instanceof EmbeddedAttribute
                || .contains(.getType())) {
            this. = new EmbeddedAttribute();
        }
        else {
            this. = new ObjAttribute();
        }
         = new ArrayList<DbEntity>(
                .getEntity()
                .getDataMap()
                .getDbEntities());

        
Register auto-selection of the target
        .getAttributeName().setText(.getName());
        if (.getDbAttributePath() != null) {
            if (.getDbAttributePath().contains(".")) {
                String path = .getDbAttributePath();
                .getCurrentPathLabel().setText(path.replace("."" -> "));
            }
            else {
                .getCurrentPathLabel().setText(.getDbAttributePath());
            }
        }
        else {
            .getCurrentPathLabel().setText("");
        }
        .getSourceEntityLabel().setText(.getEntity().getName());
        .getType().setSelectedItem(.getType());
        BindingBuilder builder = new BindingBuilder(
                getApplication().getBindingFactory(),
                this);
        builder.bindToAction(.getCancelButton(), "closeAction()");
        builder.bindToAction(.getSelectPathButton(), "setPath(true)");
        builder.bindToAction(.getSaveButton(), "saveMapping()");
        /*
         * set filter for ObjAttributePathBrowser
         */
        if (.getPathBrowser().getModel() == null) {
            Entity firstEntity = null;
            if (.getDbAttribute() == null) {
                if (.getParent() instanceof ObjEntity) {
                    DbEntity dbEnt = ((ObjEntity) .getParent()).getDbEntity();
                    if (dbEnt != null) {
                        Collection<DbAttribute> attrib = dbEnt.getAttributes();
                        Collection<DbRelationship> rel = dbEnt.getRelationships();
                        if (attrib.size() > 0) {
                            Iterator<DbAttribute> iter = attrib.iterator();
                            firstEntity = iter.next().getEntity();
                        }
                        else if (rel.size() > 0) {
                            Iterator<DbRelationship> iter = rel.iterator();
                            firstEntity = iter.next().getSourceEntity();
                        }
                    }
                }
            }
            else {
                firstEntity = getFirstEntity();
            }
            if (firstEntity != null) {
                EntityTreeModel treeModel = new EntityTreeModel(firstEntity);
                treeModel.setFilter(new EntityTreeFilter() {
                    public boolean attributeMatch(Object node, Attribute attr) {
                        if (!(node instanceof Attribute)) {
                            return true;
                        }
                        return false;
                    }
                    public boolean relationshipMatch(Object node, Relationship rel) {
                        if (!(node instanceof Relationship)) {
                            return true;
                        }

                        
We do not allow A->B->A chains, where relationships are to-one
                        DbRelationship prev = (DbRelationship) node;
                        return !(!rel.isToMany() && prev.getReverseRelationship() == rel);
                    }
                });
                .getPathBrowser().setModel(treeModel);
            }
        }
        if (.getDbAttribute() != null) {
            setSelectionPath();
        }
        .getType().addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if ( != null) {
                    if (!.equals(e.getItemSelectable())) {
                        if (.contains(e
                                .getItemSelectable()
                                .getSelectedObjects()[0].toString())) {
                            if ( instanceof ObjAttribute) {
                                EmbeddedAttribute copyAttrSaved = new EmbeddedAttribute();
                                copyObjAttribute(copyAttrSaved);
                                 = copyAttrSaved;
                            }
                        }
                        else {
                            if ( instanceof EmbeddedAttribute) {
                                ObjAttribute copyAttrSaved = new ObjAttribute();
                                copyObjAttribute(copyAttrSaved);
                                 = copyAttrSaved;
                            }
                        }
                        
                                .setType(e.getItemSelectable().getSelectedObjects()[0]
                                        .toString());
                        rebuildTable();
                        setEnabledSaveButton();
                    }
                }
            }
        });
            public void keyPressed(KeyEvent e) {
                if (!.getAttributeName().getText().equals(.getName())) {
                    setEnabledSaveButton();
                }
            }
            public void keyReleased(KeyEvent e) {
                if (!.getAttributeName().getText().equals(.getName())) {
                    setEnabledSaveButton();
                }
            }
            public void keyTyped(KeyEvent e) {
            }
        });
        rebuildTable();
    }
    private void setEnabledSaveButton() {
        if (!.getDbPathIterator().hasNext()) {
            .getSaveButton().setEnabled(true);
        }
        else {
            boolean isAttributeLast = false;
            Iterator<CayenneMapEntry> it = .getDbPathIterator();
            while (it.hasNext()) {
                Object obj = it.next();
                if (obj instanceof DbAttribute && !it.hasNext()) {
                    isAttributeLast = true;
                }
            }
            .getSaveButton().setEnabled(isAttributeLast);
        }
    }
    private void setUpTableStructure() {
        DefaultTableCellRenderer renderer = new CellRenderer();
        TableColumn nameColumn = 
                .getOverrideAttributeTable()
                .getColumnModel()
        nameColumn.setMinWidth(180);
        nameColumn.setCellRenderer(renderer);
        TableColumn typeColumn = 
                .getOverrideAttributeTable()
                .getColumnModel()
        typeColumn.setMinWidth(200);
        typeColumn.setCellRenderer(renderer);
        TableColumn dbAttrColumn = 
                .getOverrideAttributeTable()
                .getColumnModel()
        dbAttrColumn.setMinWidth(180);
        dbAttrColumn.setCellRenderer(renderer);
        TableColumn dbAttrTypeColumn = 
                .getOverrideAttributeTable()
                .getColumnModel()
        dbAttrTypeColumn.setMinWidth(180);
        dbAttrTypeColumn.setCellRenderer(renderer);
        initComboBoxes();
    }
    private void initComboBoxes() {
        Collection<StringnameAttr = null;
        if ( != null) {
            DbEntity currentEnt = ((ObjEntity) .getEntity()).getDbEntity();
            if (currentEnt != null) {
                nameAttr = ModelerUtil.getDbAttributeNames(currentEnt);
                .setCellEditor(nameAttr.getOverrideAttributeTable());
                .setComboBoxes(
                        nameAttr,
                        .);
            }
        }
    }
    private void rebuildTable() {
        String typeName = null;
        Collection<EmbeddableAttribute> embAttrTempCopy = new ArrayList<EmbeddableAttribute>();
        if (.getType() != null) {
            typeName = .getType().toString();
        }
        if (.contains(typeName)) {
            Collection<EmbeddableAttribute> embAttrTemp = ((Embeddable) 
                    .get(typeName)).getAttributes();
            Iterator<EmbeddableAttribute> it = embAttrTemp.iterator();
            while (it.hasNext()) {
                EmbeddableAttribute temp = (EmbeddableAttribute) it.next();
                EmbeddableAttribute at = new EmbeddableAttribute();
                at.setDbAttributeName(temp.getDbAttributeName());
                at.setName(temp.getName());
                at.setType(temp.getType());
                at.setEmbeddable(temp.getEmbeddable());
                embAttrTempCopy.add(at);
            }
        }
                ,
                this,
                embAttrTempCopy,
                );
        setUpTableStructure();
        if (.getType().getSelectedItem() == null) {
             = "";
        }
        else {
             = .getType().getSelectedItem();
        }
    }
    public void closeAction() {
        .dispose();
    }
    public boolean setPath(boolean isChange) {
        if (isChange()) {
            .setType(.getType().getSelectedItem().toString());
            .setName(.getAttributeName().getText());
        }
        if (!( instanceof EmbeddedAttribute)
                || isRegistredType(.getType())) {
            StringBuilder attributePath = new StringBuilder();
            StringBuilder pathStr = new StringBuilder();
            if (((ObjEntity) .getEntity()).getDbEntity() != null) {
                TreePath path = .getPathBrowser().getSelectionPath();
                if (path.getLastPathComponent() instanceof DbAttribute) {
                    Object[] pathComponents = path.getPath();
                    for (int i = 0; i < pathComponents.lengthi++) {
                        boolean attrOrRel = true;
                        if (pathComponents[iinstanceof DbAttribute) {
                            pathStr.append(((DbAttribute) pathComponents[i]).getName());
                            attributePath.append(((DbAttribute) pathComponents[i])
                                    .getName());
                        }
                        else if (pathComponents[iinstanceof DbRelationship) {
                            pathStr
                                    .append(((DbRelationship) pathComponents[i])
                                            .getName());
                            attributePath.append(((DbRelationship) pathComponents[i])
                                    .getName());
                        }
                        else {
                            attrOrRel = false;
                        }
                        if (i != pathComponents.length - 1 && attrOrRel) {
                            pathStr.append(" -> ");
                            attributePath.append(".");
                        }
                    }
                }
            }
            else {
                .getCurrentPathLabel().setText("");
            }
            .getCurrentPathLabel().setText(pathStr.toString());
            if (.getDbAttributePath() != null
                    && !.contains(
                            .getType()
                            .getSelectedItem()
                            .toString())) {
                if (!.getDbAttributePath().equals(attributePath.toString())) {
                    .setDbAttributePath(attributePath.toString());
                    if (!.getDbAttributePath().equals(attributePath.toString())
                            && isChange) {
                        .setUpdatedValueAt(
                                .getDbAttributePath(),
                                ,
                                3);
                    }
                    return true;
                }
            }
            else {
                if (attributePath.length() > 0
                        || ( instanceof EmbeddedAttribute && !( instanceof EmbeddedAttribute))) {
                    .setDbAttributePath(attributePath.toString());
                    if (attributePath.length() == 0) {
                        .setUpdatedValueAt(
                                .getDbAttributePath(),
                                ,
                                3);
                        return false;
                    }
                    return true;
                }
            }
        }
        return false;
    }
    public boolean isChange() {
        boolean isOvverideTableChange = ((OverrideEmbeddableAttributeTableModel
                .getOverrideAttributeTable()
                .getModel()).isAttributeOverrideChange();
        return isOvverideTableChange
                || !.getName().equals(.getAttributeName().getText())
                || (.getType() == null && 
                        .getType()
                        .getSelectedItem()
                        .toString() != null)
                || !.getType().equals(
                        .getType().getSelectedItem().toString());
    }
    public void saveMapping() {
        if (setPath(false)) {
            if (JOptionPane.showConfirmDialog(
                    (ComponentgetView(),
                    "You have changed Db Attribute path. Do you want it to be saved?",
                    "Save ObjAttribute",
                    .) == .) {
                if ( instanceof EmbeddedAttribute) {
                    changeAttributeObject();
                }
                else {
                    .setUpdatedValueAt(.getName(), , 1);
                    .setUpdatedValueAt(.getType(), , 2);
                }
                .setUpdatedValueAt(.getDbAttributePath(), , 3);
            }
            else {
                .setUpdatedValueAt(.getName(), , 1);
                .setUpdatedValueAt(.getType(), , 2);
            }
        }
        else {
            if (( instanceof EmbeddedAttribute && !( instanceof EmbeddedAttribute))
                    || (!( instanceof EmbeddedAttribute) &&  instanceof EmbeddedAttribute)) {
                changeAttributeObject();
            }
            else if (( instanceof EmbeddedAttribute &&  instanceof EmbeddedAttribute)
                    || (!( instanceof EmbeddedAttribute) && !( instanceof EmbeddedAttribute))) {
                if ( instanceof EmbeddedAttribute
                        && .isAttributeOverrideChange()) {
                    Map<StringStringoverrides = ((EmbeddedAttribute) )
                            .getAttributeOverrides();
                    Map<StringStringcurrentOverrAttr = getCurrentOverrideAttribute();
                    compareAndSetOverrideInEmbeddedAttribute(
                            ,
                            overrides,
                            currentOverrAttr);
                }
                .setUpdatedValueAt(.getName(), , 1);
                .setUpdatedValueAt(.getType(), , 2);
                .setUpdatedValueAt(.getDbAttributePath(), , 3);
            }
            if ( instanceof EmbeddedAttribute
                    &&  instanceof EmbeddedAttribute) {
                .setUpdatedValueAt(.getDbAttributePath(), , 3);
                if (.isAttributeOverrideChange()) {
                    Map<StringStringoverrides;
                    overrides = ((EmbeddedAttribute) ).getAttributeOverrides();
                    Map<StringStringcurrentOverrAttr = ((EmbeddedAttribute) )
                            .getAttributeOverrides();
                    compareAndSetOverrideInEmbeddedAttribute(
                            ,
                            overrides,
                            currentOverrAttr);
                }
            }
        }
        closeAction();
    }
    private void changeAttributeObject() {
        if ( instanceof EmbeddedAttribute
                && .isAttributeOverrideChange()) {
            Map<StringStringoverrides = ((EmbeddedAttribute) )
                    .getAttributeOverrides();
            Map<StringStringcurrentOverrAttr = getCurrentOverrideAttribute();
                    ,
                    overrides,
                    currentOverrAttr);
        }
        if ( instanceof EmbeddedAttribute) {
            .setDbAttributePath(null);
            .setUpdatedValueAt(.getDbAttributePath(), , 3);
        }
        .getEntity().removeAttribute(.getName());
        .getEntity().addAttribute();
        .fireObjEntityEvent(new EntityEvent(
                this,
                .getEntity(),
                MapEvent.CHANGE));
        EntityDisplayEvent event = new EntityDisplayEvent(this
                .getCurrentObjEntity(), .getCurrentDataMap(), 
                .getCurrentDataDomain());
        .fireObjEntityDisplayEvent(event);
        .fireObjAttributeEvent(new AttributeEvent(this
                .getEntity(), MapEvent.CHANGE));
        AttributeDisplayEvent eventAttr = new AttributeDisplayEvent(
                this,
                ,
                .getCurrentObjEntity(),
                .getCurrentDataMap(),
                .getCurrentDataDomain());
        .fireObjAttributeDisplayEvent(eventAttr);
    }
        Map<StringStringcurrentEmbeddableOverrite = new HashMap<StringString>();
        Collection<EmbeddableAttribute> embList = .getEmbeddableList();
        Embeddable emb = .get(.getType());
        Iterator<EmbeddableAttribute> it = embList.iterator();
        while (it.hasNext()) {
            EmbeddableAttribute e = it.next();
            if ((emb.getAttribute(e.getName()).getDbAttributeName() == null && e
                    .getDbAttributeName() != null)
                    || (emb.getAttribute(e.getName()).getDbAttributeName() != null && !emb
                            .getAttribute(e.getName())
                            .getDbAttributeName()
                            .equals(e.getDbAttributeName()))) {
                currentEmbeddableOverrite.put(e.getName(), e.getDbAttributeName());
            }
        }
        return currentEmbeddableOverrite;
    }
    public void valueChanged(TreeSelectionEvent e) {
    }
    private Entity getFirstEntity() {
        Iterator<CayenneMapEntry> it = .getDbPathIterator();
        Entity firstEnt = .getDbAttribute().getEntity();
        boolean setEnt = false;
        while (it.hasNext()) {
            Object ob = it.next();
            if (ob instanceof DbRelationship) {
                if (!setEnt) {
                    firstEnt = ((DbRelationship) ob).getSourceEntity();
                    setEnt = true;
                }
            }
            else if (ob instanceof DbAttribute) {
                if (!setEnt) {
                    firstEnt = ((DbAttribute) ob).getEntity();
                }
            }
        }
        return firstEnt;
    }

    
Selects path in browser
    public void setSelectionPath() {
        List list = new ArrayList();
        boolean isAttributeLast = false;
        Iterator<CayenneMapEntry> it = .getDbPathIterator();
        while (it.hasNext()) {
            Object obj = it.next();
            list.add(obj);
            if (obj instanceof DbAttribute && !it.hasNext()) {
                isAttributeLast = true;
            }
        }
        if (isAttributeLast) {
            Object[] path = new Object[list.size() + 1];
            path[0] = getFirstEntity();
            System.arraycopy(list.toArray(), 0, path, 1, list.size());
            .getPathBrowser().setSelectionPath(new TreePath(path));
            .getSaveButton().setEnabled(true);
        }
    }
    public boolean isRegistredType(String typeName) {
        boolean isType = false;
        String[] typeNames = ModelerUtil.getRegisteredTypeNames();
        for (int i = 0; i < typeNames.lengthi++) {
            if (typeNames[i].equals(typeName)) {
                isType = true;
            }
        }
        return isType;
    }
    // custom renderer used for inherited attributes highlighting
    final class CellRenderer extends DefaultTableCellRenderer {
        @Override
                JTable table,
                Object value,
                boolean isSelected,
                boolean hasFocus,
                int row,
                int column) {
            super.getTableCellRendererComponent(
                    table,
                    value,
                    isSelected,
                    hasFocus,
                    row,
                    column);
                    .getModel();
            if (!model.isCellEditable(rowcolumn)) {
                setForeground(.);
            }
            else {
                setForeground(isSelected && !hasFocus
                        ? table.getSelectionForeground()
                        : table.getForeground());
            }
            setBackground(isSelected && !hasFocus
                    ? table.getSelectionBackground()
                    : table.getBackground());
            return this;
        }
    }
    private void copyObjAttribute(ObjAttribute attributeSaved, ObjAttribute attribute) {
        attributeSaved.setDbAttributePath(attribute.getDbAttributePath());
        attributeSaved.setName(attribute.getName());
        attributeSaved.setEntity(attribute.getEntity());
        attributeSaved.setParent(attribute.getParent());
        attributeSaved.setType(attribute.getType());
        attributeSaved.setUsedForLocking(attribute.isUsedForLocking());
        if (attributeSaved instanceof EmbeddedAttribute) {
            Map<StringStringattrOverrides;
            if (attribute instanceof EmbeddedAttribute) {
                attrOverrides = ((EmbeddedAttribute) attribute).getAttributeOverrides();
            }
            else {
                attrOverrides = new HashMap<StringString>();
            }
            if (attrOverrides.size() > 0) {
                Iterator it = attrOverrides.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry attrOv = (Map.Entryit.next();
                    ((EmbeddedAttribute) attributeSaved).addAttributeOverride(attrOv
                            .getKey()
                            .toString(), attrOv.getValue().toString());
                }
            }
        }
    }
            ObjAttribute attribute,
            Map<StringStringoverrides,
            Map<StringStringcurrentOverrAttr) {
        ArrayList<StringkeysForDelete = new ArrayList<String>();
        ArrayList<StringkeysForAdd = new ArrayList<String>();
        Iterator it = overrides.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry obj = (Map.Entryit.next();
            String key = (Stringobj.getKey();
            if (currentOverrAttr.get(key) == null
                    || !(obj.getValue().equals(currentOverrAttr.get(key)))) {
                keysForDelete.add(key);
            }
        }
        Iterator iter = currentOverrAttr.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry obj = (Map.Entryiter.next();
            String key = (Stringobj.getKey();
            if (overrides.get(key) == null
                    || !(obj.getValue().equals(overrides.get(key)))) {
                keysForAdd.add(key);
            }
        }
        for (int i = 0; i < keysForDelete.size(); i++) {
            ((EmbeddedAttribute) attribute).removeAttributeOverride(keysForDelete.get(i));
        }
        for (int i = 0; i < keysForAdd.size(); i++) {
            String key = keysForAdd.get(i);
            ((EmbeddedAttribute) attribute).addAttributeOverride(keycurrentOverrAttr
                    .get(key));
        }
    }