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.dba.TypesMapping;
 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.Entity;
 import  org.apache.cayenne.map.ObjAttribute;
 import  org.apache.cayenne.map.ObjEntity;
 import  org.apache.cayenne.map.EmbeddedAttribute;
 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.Util;
 import  org.apache.cayenne.CayenneRuntimeException;

Model for the Object Entity attributes and for Obj to DB Attribute Mapping tables. Allows adding/removing attributes, modifying the types and the names.
 
 public class ObjAttributeTableModel extends CayenneTableModel {
 
     // Columns
     static final int INHERITED = 0;
     static final int OBJ_ATTRIBUTE = 1;
     static final int OBJ_ATTRIBUTE_TYPE = 2;
     static final int DB_ATTRIBUTE = 3;
     static final int DB_ATTRIBUTE_TYPE = 4;
     static final int LOCKING = 5;
 
     protected ObjEntity entity;
     protected DbEntity dbEntity;
     private CayenneTable table;
 
     public ObjAttributeTableModel(ObjEntity entityProjectController mediator,
             Object eventSource) {
         super(mediatoreventSourcenew ArrayList<Attribute>(entity.getAttributes()));
         // take a copy
         this. = entity;
         this. = entity.getDbEntity();
 
         // order using local comparator
         Collections.sort(new AttributeComparator());
     }
 
     protected void orderList() {
         // NOOP
     }
     
     
     public CayenneTable getTable() {
         return ;
     }
 
     public Class getColumnClass(int col) {
         switch (col) {
             case :
                 return Boolean.class;
            default:
                return String.class;
        }
    }

    
Returns ObjAttribute class.
    @Override
    public Class<?> getElementsClass() {
        return ObjAttribute.class;
    }
    public DbEntity getDbEntity() {
        return ;
    }
    public ObjAttribute getAttribute(int row) {
        return (row >= 0 && row < .size())
                ? (ObjAttribute) .get(row)
                : null;
    }

    
Refreshes DbEntity to current db entity within ObjEntity.
    public void resetDbEntity() {
        if ( == .getDbEntity()) {
            return;
        }
        boolean wasShowing = isShowingDb();
         = .getDbEntity();
        boolean isShowing = isShowingDb();
        if (wasShowing != isShowing) {
            fireTableStructureChanged();
        }
        fireTableDataChanged();
    }
    private boolean isShowingDb() {
        return  != null;
    }
    public int getColumnCount() {
        return 6;
    }
    public String getColumnName(int column) {
        switch (column) {
            case :
                return "In";
            case :
                return "ObjAttribute";
            case :
                return "Java Type";
            case :
                return "DbAttribute";
            case :
                return "DB Type";
            case :
                return "Used for Locking";
            default:
                return "";
        }
    }
    public Object getValueAt(int rowint column) {
        ObjAttribute attribute = getAttribute(row);
        if (column == ) {
            return attribute.isInherited();
        }
        else if (column == ) {
            return attribute.getName();
        }
        else if (column == ) {
            return attribute.getType();
        }
        else if (column == ) {
            return attribute.isUsedForLocking() ? . : .;
        }
        else {
            DbAttribute dbAttribute = attribute.getDbAttribute();
            if (column == ) {
                if (dbAttribute == null) {
                    if (!attribute.isInherited()
                            && ((ObjEntity) attribute.getEntity()).isAbstract()) {
                        return attribute.getDbAttributePath();
                    }
                    else {
                        return null;
                    }
                }
                else if (attribute.getDbAttributePath() != null
                        && attribute.getDbAttributePath().contains(".")) {
                    return attribute.getDbAttributePath();
                }
                return dbAttribute.getName();
            }
            else if (column == ) {
                int type;
                if (dbAttribute == null) {
                    if (!(attribute instanceof EmbeddedAttribute)) {
                        try {
                            type = TypesMapping.getSqlTypeByJava(getAttribute(row)
                                    .getJavaClass());
                            // have to catch the exception here to make sure that
                            // exceptional situations
                            // (class doesn't exist, for example) don't prevent the gui
                            // from properly updating.
                        }
                        catch (CayenneRuntimeException cre) {
                            return null;
                        }
                    }
                    else {
                        return null;
                    }
                }
                else {
                    type = dbAttribute.getType();
                }
                return TypesMapping.getSqlNameByType(type);
            }
            else {
                return null;
            }
        }
    }
        this. = new CellEditorForAttributeTable(table, CayenneWidgetFactory.createComboBox(
                nameAttr,
                true));
        this. = table;
        return ;
    }
        return ;
    }
    public void setUpdatedValueAt(Object valueint rowint column) {
        ObjAttribute attribute = getAttribute(row);
        AttributeEvent event = new AttributeEvent(attribute);
        String path = null;
        Collection<StringnameAttr = null;
        if (column == ) {
            event.setOldName(attribute.getName());
            ProjectUtil.setAttributeName(attributevalue != null ? value
                    .toString()
                    .trim() : null);
            fireTableCellUpdated(rowcolumn);
        }
        else if (column == ) {
            String oldType = attribute.getType();
            attribute.setType(value != null ? value.toString() : null);
            String newType = attribute.getType();
            String[] registeredTypes = ModelerUtil.getRegisteredTypeNames();
            Collection<StringregisteredTypesList =  Arrays.asList(registeredTypes); ;
            if(oldType!=null && newType!=null && ! (registeredTypesList.contains(oldType) == registeredTypesList.contains(newType))){
                ObjAttribute attributeNew;
                
                ArrayList<Embeddable> embs = .getEmbeddableNamesInCurRentDataDomain();
                ArrayList<StringembNames = new ArrayList<String>();
                Iterator<Embeddable> it = embs.iterator();
                while (it.hasNext()) {
                    embNames.add(it.next().getClassName());
                }
                
                if(registeredTypesList.contains(newType) || !embNames.contains(newType)){
                    attributeNew = new ObjAttribute();
                } else {
                    attributeNew = new EmbeddedAttribute();
                    attribute.setDbAttributePath(null);
                }
                
                attributeNew.setDbAttributePath(attribute.getDbAttributePath());
                attributeNew.setName(attribute.getName());
                attributeNew.setEntity(attribute.getEntity());
                attributeNew.setParent(attribute.getParent());
                attributeNew.setType(attribute.getType());
                attributeNew.setUsedForLocking(attribute.isUsedForLocking());
                Entity ent = attribute.getEntity();
                ent.removeAttribute(attribute.getName());
                ent.addAttribute(attributeNew);
                
                .fireObjEntityEvent(new EntityEvent(
                        this,
                        ent,
                        MapEvent.CHANGE));
                EntityDisplayEvent ev = new EntityDisplayEvent(this
                        .getCurrentObjEntity(), .getCurrentDataMap(), 
                        .getCurrentDataDomain());
                .fireObjEntityDisplayEvent(ev);
                .fireObjAttributeEvent(new AttributeEvent(thisattributeNewent, MapEvent.CHANGE));
                AttributeDisplayEvent eventAttr = new AttributeDisplayEvent(
                        this,
                        attributeNew,
                        .getCurrentObjEntity(),
                        .getCurrentDataMap(),
                        .getCurrentDataDomain());
                .fireObjAttributeDisplayEvent(eventAttr);
            }
            
            fireTableCellUpdated(rowcolumn);
        }
        else if (column == ) {
            attribute.setUsedForLocking((value instanceof Boolean)
                    && ((Booleanvalue).booleanValue());
            fireTableCellUpdated(rowcolumn);
        }
        else {
            if (column == ) {
                // If db attrib exist, associate it with obj attribute
                if (value != null) {
                    path = value.toString();
                    String[] pathSplit = path.split("\\.");
                    // If flattened attribute
                    if (pathSplit.length > 1) {
                        DbEntity currentEnt = ;
                        StringBuilder pathBuf = new StringBuilder();
                        boolean isTruePath = true;
                        if ( != null) {
                            nameAttr = ModelerUtil
                                    .getDbAttributeNames();
                            for (int j = 0; j < pathSplit.lengthj++) {
                                if (j == pathSplit.length - 1 && isTruePath) {
                                    DbAttribute dbAttribute = (DbAttribute) currentEnt
                                            .getAttribute(pathSplit[j]);
                                    if (dbAttribute != null) {
                                        pathBuf.append(dbAttribute.getName());
                                    }
                                    else {
                                        isTruePath = false;
                                    }
                                }
                                else if (isTruePath) {
                                    DbRelationship dbRelationship = (DbRelationship) currentEnt
                                            .getRelationship(pathSplit[j]);
                                    if (dbRelationship != null) {
                                        currentEnt = (DbEntity) dbRelationship
                                                .getTargetEntity();
                                        pathBuf.append(dbRelationship.getName());
                                        pathBuf.append(".");
                                    }
                                    else {
                                        isTruePath = false;
                                    }
                                }
                            }
                        }
                        path = isTruePath ? pathBuf.toString() : null;
                    }
                    else {
                        if ( != null) {
                            DbAttribute dbAttribute = (DbAttribute) 
                                    .getAttribute(value.toString());
                            path = dbAttribute != null ? dbAttribute.getName() : null;
                        }
                    }
                    attribute.setDbAttributePath(path);
                }
                // If name is erased, remove db attribute from obj attribute.
                else if (attribute.getDbAttribute() != null) {
                    attribute.setDbAttributePath(null);
                }
                // If path is flattened attribute, update the JComboBox
                if (path != null && path.split("\\.").length > 1 && !=null) {
                    setComboBoxes(nameAttrcolumn);
                }
            }
            fireTableRowsUpdated(rowrow);
        }
        .fireObjAttributeEvent(event);
    }
    
    public void setComboBoxes(Collection<StringnameAttrint column) {
        int count = getRowCount();
        for (int i = 0; i < counti++) {
            if (getAttribute(i).getDbAttributePath() != null
                    && getAttribute(i).getDbAttributePath().contains(".")) {
                Collection<StringattributeComboForRow = new ArrayList<String>();
                attributeComboForRow.addAll(nameAttr);
                attributeComboForRow
                        .add(getAttribute(i).getDbAttributePath());
                JComboBox comboBoxForRow = CayenneWidgetFactory
                        .createComboBox(attributeComboForRowtrue);
                .setEditorAt(new Integer(i), new DefaultCellEditor(
                        comboBoxForRow));
                
            }
        }
        .getColumnModel().getColumn(column).setCellEditor(
                );
    }
    public boolean isCellEditable(int rowint col) {
        if (getAttribute(row).isInherited()) {
            return col == ;
        }
        return col !=  && col != ;
    }
    public ObjEntity getEntity() {
        return ;
    }
    final class AttributeComparator implements Comparator {
        public int compare(Object o1Object o2) {
            Attribute a1 = (Attribute) o1;
            Attribute a2 = (Attribute) o2;
            int delta = getWeight(a1) - getWeight(a2);
            return (delta != 0) ? delta : Util.nullSafeCompare(truea1.getName(), a2
                    .getName());
        }
        private int getWeight(Attribute a) {
            return a.getEntity() ==  ? 1 : -1;
        }
    }
New to GrepCode? Check out our FAQ X