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.map.*;
 import  org.apache.cayenne.map.event.RelationshipEvent;
 import  org.apache.cayenne.util.Util;
 
Table model to display ObjRelationships.
 
 public class ObjRelationshipTableModel extends CayenneTableModel {
 
     // Columns
     static final int REL_NAME = 0;
     static final int REL_TARGET = 1;
     static final int REL_SEMANTICS = 2;
     static final int REL_DELETERULE = 3;
     static final int REL_LOCKING = 4;
 
     protected ObjEntity entity;
 
     public ObjRelationshipTableModel(ObjEntity entityProjectController mediator,
             Object eventSource) {
         super(mediatoreventSourcenew ArrayList(entity.getRelationships()));
         this. = entity;
 
         // order using local comparator
         Collections.sort(new RelationshipComparator());
     }
 
     @Override
     protected void orderList() {
         // NOOP
     }
 
     public ObjEntity getEntity() {
         return ;
     }

    
Returns ObjRelationship class.
 
     @Override
     public Class getElementsClass() {
         return ObjRelationship.class;
     }
 
     public int getColumnCount() {
         return 5;
     }
 
     @Override
     public String getColumnName(int column) {
         switch (column) {
             case :
                 return "Name";
             case :
                 return "Target";
             case :
                 return "Used for Locking";
             case :
                 return "Semantics";
             case :
                 return "Delete Rule";
 
             default:
                 return null;
         }
     }
 
     @Override
     public Class getColumnClass(int col) {
         switch (col) {
            case :
                return ObjEntity.class;
            case :
                return Boolean.class;
            default:
                return String.class;
        }
    }
    public ObjRelationship getRelationship(int row) {
        return (row >= 0 && row < .size()) ? (ObjRelationship) 
                .get(row) : null;
    }
    public Object getValueAt(int rowint column) {
        ObjRelationship relationship = getRelationship(row);
        if (column == ) {
            return relationship.getName();
        }
        else if (column == ) {
            return relationship.getTargetEntity();
        }
        else if (column == ) {
            return relationship.isUsedForLocking() ? . : .;
        }
        else if (column == ) {
            String semantics = relationship.isToMany() ? "to many" : "to one";
            if (relationship.isReadOnly()) {
                semantics += ", read-only";
            }
            if (relationship.isToMany()) {
                String collection = "list";
                if (relationship.getCollectionType() != null) {
                    int dot = relationship.getCollectionType().lastIndexOf('.');
                    collection = relationship
                            .getCollectionType()
                            .substring(dot + 1)
                            .toLowerCase();
                }
                semantics += ", " + collection;
            }
            return semantics;
        }
        else if (column == ) {
            return DeleteRule.deleteRuleName(relationship.getDeleteRule());
        }
        else {
            return null;
        }
    }
    @Override
    public void setUpdatedValueAt(Object valueint rowint column) {
        ObjRelationship relationship = getRelationship(row);
        RelationshipEvent event = new RelationshipEvent(relationship);
        if (column == ) {
            String text = (Stringvalue;
            event.setOldName(relationship.getName());
            ProjectUtil.setRelationshipName(relationshiptext);
            fireTableCellUpdated(rowcolumn);
        }
        else if (column == ) {
            ObjEntity target = (ObjEntity) value;
            relationship.setTargetEntity(target);
            
            
Clear existing relationships, otherwise addDbRelationship() might fail
            relationship.clearDbRelationships();
            
            // now try to connect DbEntities if we can do it in one step
            if (target != null) {
                DbEntity srcDB = ((ObjEntity) relationship.getSourceEntity())
                        .getDbEntity();
                DbEntity targetDB = target.getDbEntity();
                if (srcDB != null && targetDB != null) {
                    Relationship anyConnector = srcDB.getAnyRelationship(targetDB);
                    if (anyConnector != null) {
                        relationship.addDbRelationship((DbRelationship) anyConnector);
                    }
                }
            }
            fireTableRowsUpdated(rowrow);
        }
        else if (column == ) {
            relationship.setDeleteRule(DeleteRule.deleteRuleForName((Stringvalue));
            fireTableCellUpdated(rowcolumn);
        }
        else if (column == ) {
            relationship.setUsedForLocking((value instanceof Boolean)
                    && ((Booleanvalue).booleanValue());
            fireTableCellUpdated(rowcolumn);
        }
        .fireObjRelationshipEvent(event);
    }
    public void removeRow(int row) {
        if (row < 0)
            return;
        Relationship rel = getRelationship(row);
        RelationshipEvent e;
        e = new RelationshipEvent(rel, RelationshipEvent.REMOVE);
        .remove(row);
        .removeRelationship(rel.getName());
        fireTableRowsDeleted(rowrow);
    }
    private boolean isInherited(int row) {
        ObjRelationship relationship = getRelationship(row);
        return (relationship != null) ? relationship.getSourceEntity() !=  : false;
    }
    @Override
    public boolean isCellEditable(int rowint col) {
        return !isInherited(row) && col != ;
    }
    final class RelationshipComparator implements Comparator {
        public int compare(Object o1Object o2) {
            ObjRelationship r1 = (ObjRelationship) o1;
            ObjRelationship r2 = (ObjRelationship) o2;
            int delta = getWeight(r1) - getWeight(r2);
            return (delta != 0) ? delta : Util.nullSafeCompare(truer1.getName(), r2
                    .getName());
        }
        private int getWeight(ObjRelationship r) {
            return r.getSourceEntity() ==  ? 1 : -1;
        }
    }
New to GrepCode? Check out our FAQ X