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.access.DataDomain;
 import  org.apache.cayenne.map.DbEntity;
 import  org.apache.cayenne.map.DbRelationship;
 import  org.apache.cayenne.map.ObjEntity;
 import  org.apache.cayenne.map.ObjRelationship;
 import  org.apache.cayenne.map.Relationship;
 import  org.apache.cayenne.map.event.RelationshipEvent;
Table model for DbRelationship table.
 
 public class DbRelationshipTableModel extends CayenneTableModel {
 
     // Columns
     static final int NAME = 0;
     static final int TARGET = 1;
     static final int TO_DEPENDENT_KEY = 2;
     static final int CARDINALITY = 3;
 
     protected DbEntity entity;
 
     public DbRelationshipTableModel(DbEntity entityProjectController mediator,
             Object eventSource) {
 
         super(mediatoreventSourcenew ArrayList(entity.getRelationships()));
         this. = entity;
     }

    
Returns DbRelationship class.
 
     public Class getElementsClass() {
         return DbRelationship.class;
     }
 
     public int getColumnCount() {
         return 4;
     }
 
     public String getColumnName(int col) {
         switch (col) {
             case :
                 return "Name";
             case :
                 return "Target";
             case :
                 return "To Dep PK";
             case :
                 return "To Many";
             default:
                 return null;
         }
     }
 
     public Class getColumnClass(int col) {
         switch (col) {
             case :
                 return DbEntity.class;
             case :
             case :
                 return Boolean.class;
             default:
                 return String.class;
         }
     }
 
     public DbRelationship getRelationship(int row) {
         return (row >= 0 && row < .size()) ? (DbRelationship) 
                 .get(row) : null;
     }
    public Object getValueAt(int rowint col) {
        DbRelationship rel = getRelationship(row);
        if (rel == null) {
            return null;
        }
        switch (col) {
            case :
                return rel.getName();
            case :
                return rel.getTargetEntity();
            case :
                return rel.isToDependentPK() ? . : .;
            case :
                return rel.isToMany() ? . : .;
            default:
                return null;
        }
    }
    public void setUpdatedValueAt(Object aValueint rowint column) {
        DbRelationship rel = getRelationship(row);
        // If name column
        if (column == ) {
            RelationshipEvent e = new RelationshipEvent(relrel
                    .getName());
            rel.setName((StringaValue);
            .fireDbRelationshipEvent(e);
            fireTableCellUpdated(rowcolumn);
        }
        // If target column
        else if (column == ) {
            DbEntity target = (DbEntity) aValue;
            // clear joins...
            rel.removeAllJoins();
            rel.setTargetEntity(target);
            RelationshipEvent e = new RelationshipEvent(rel);
            .fireDbRelationshipEvent(e);
        }
        else if (column == ) {
            boolean flag = ((BooleanaValue).booleanValue();
            // make sure reverse relationship "to-dep-pk" is unset.
            if (flag) {
                DbRelationship reverse = rel.getReverseRelationship();
                if (reverse != null && reverse.isToDependentPK()) {
                    String message = "Unset reverse relationship's \"To Dep PK\" setting?";
                    int answer = JOptionPane.showConfirmDialog(
                            Application.getFrame(),
                            message);
                    if (answer != .) {
                        // no action needed
                        return;
                    }
                    // unset reverse
                    reverse.setToDependentPK(false);
                }
            }
            rel.setToDependentPK(flag);
            RelationshipEvent e = new RelationshipEvent(rel);
            .fireDbRelationshipEvent(e);
        }
        else if (column == ) {
            Boolean temp = (BooleanaValue;
            rel.setToMany(temp.booleanValue());
            RelationshipEvent e = new RelationshipEvent(rel);
            .fireDbRelationshipEvent(e);
            updateDependentObjRelationships(rel);
        }
        fireTableRowsUpdated(rowrow);
    }

    
Relationship just needs to be removed from the model. It is already removed from the DataMap.
    void removeRelationship(Relationship rel) {
        .remove(rel);
        fireTableDataChanged();
    }
    void updateDependentObjRelationships(DbRelationship relationship) {
        DataDomain domain = .getCurrentDataDomain();
        if (domain != null) {
            for (ObjEntity entity : domain.getEntityResolver().getObjEntities()) {
                for (ObjRelationship objRelationship : entity.getRelationships()) {
                    for (DbRelationship dbRelationship : objRelationship
                            .getDbRelationships()) {
                        if (dbRelationship == relationship) {
                            objRelationship.recalculateToManyValue();
                            objRelationship.recalculateReadOnlyValue();
                            break;
                        }
                    }
                }
            }
        }
    }
    public boolean isCellEditable(int rowint col) {
        DbRelationship rel = getRelationship(row);
        if (rel == null) {
            return false;
        }
        else if (col == ) {
            return rel.isValidForDepPk();
        }
        return true;
    }
New to GrepCode? Check out our FAQ X