1 56 package org.objectstyle.cayenne.modeler.editor; 57 58 import java.util.ArrayList ; 59 60 import javax.swing.JOptionPane ; 61 62 import org.objectstyle.cayenne.map.DbEntity; 63 import org.objectstyle.cayenne.map.DbRelationship; 64 import org.objectstyle.cayenne.map.Relationship; 65 import org.objectstyle.cayenne.map.event.RelationshipEvent; 66 import org.objectstyle.cayenne.modeler.Application; 67 import org.objectstyle.cayenne.modeler.ProjectController; 68 import org.objectstyle.cayenne.modeler.util.CayenneTableModel; 69 71 77 public class DbRelationshipTableModel extends CayenneTableModel { 78 79 static final int NAME = 0; 81 static final int TARGET = 1; 82 static final int TO_DEPENDENT_KEY = 2; 83 static final int CARDINALITY = 3; 84 85 protected DbEntity entity; 86 87 public DbRelationshipTableModel(DbEntity entity, ProjectController mediator, 88 Object eventSource) { 89 90 super(mediator, eventSource, new ArrayList (entity.getRelationships())); 91 this.entity = entity; 92 } 93 94 97 public Class getElementsClass() { 98 return DbRelationship.class; 99 } 100 101 public int getColumnCount() { 102 return 4; 103 } 104 105 public String getColumnName(int col) { 106 switch (col) { 107 case NAME: 108 return "Name"; 109 case TARGET: 110 return "Target"; 111 case TO_DEPENDENT_KEY: 112 return "To Dep PK"; 113 case CARDINALITY: 114 return "To Many"; 115 default: 116 return null; 117 } 118 } 119 120 public Class getColumnClass(int col) { 121 switch (col) { 122 case TARGET: 123 return DbEntity.class; 124 case TO_DEPENDENT_KEY: 125 case CARDINALITY: 126 return Boolean .class; 127 default: 128 return String .class; 129 } 130 } 131 132 public DbRelationship getRelationship(int row) { 133 return (row >= 0 && row < objectList.size()) ? (DbRelationship) objectList 134 .get(row) : null; 135 } 136 137 public Object getValueAt(int row, int col) { 138 DbRelationship rel = getRelationship(row); 139 if (rel == null) { 140 return null; 141 } 142 143 switch (col) { 144 case NAME: 145 return rel.getName(); 146 case TARGET: 147 return rel.getTargetEntity(); 148 case TO_DEPENDENT_KEY: 149 return rel.isToDependentPK() ? Boolean.TRUE : Boolean.FALSE; 150 case CARDINALITY: 151 return rel.isToMany() ? Boolean.TRUE : Boolean.FALSE; 152 default: 153 return null; 154 } 155 } 156 157 public void setUpdatedValueAt(Object aValue, int row, int column) { 158 159 DbRelationship rel = getRelationship(row); 160 if (column == NAME) { 162 RelationshipEvent e = new RelationshipEvent( 163 eventSource, 164 rel, 165 entity, 166 rel.getName()); 167 rel.setName((String )aValue); 168 mediator.fireDbRelationshipEvent(e); 171 fireTableCellUpdated(row, column); 172 } 173 else if (column == TARGET) { 175 DbEntity target = (DbEntity) aValue; 176 177 rel.removeAllJoins(); 179 rel.setTargetEntity(target); 180 181 RelationshipEvent e = new RelationshipEvent(eventSource, rel, entity); 182 mediator.fireDbRelationshipEvent(e); 183 } 184 else if (column == TO_DEPENDENT_KEY) { 185 boolean flag = ((Boolean ) aValue).booleanValue(); 186 187 if (flag) { 189 DbRelationship reverse = rel.getReverseRelationship(); 190 if (reverse != null && reverse.isToDependentPK()) { 191 String message = "Unset reverse relationship's \"To Dep PK\" setting?"; 192 int answer = JOptionPane.showConfirmDialog(Application 193 .getFrame(), message); 194 if (answer != JOptionPane.YES_OPTION) { 195 return; 197 } 198 199 reverse.setToDependentPK(false); 201 } 202 } 203 204 rel.setToDependentPK(flag); 205 RelationshipEvent e = new RelationshipEvent(eventSource, rel, entity); 206 mediator.fireDbRelationshipEvent(e); 207 } 208 else if (column == CARDINALITY) { 209 Boolean temp = (Boolean ) aValue; 210 rel.setToMany(temp.booleanValue()); 211 RelationshipEvent e = new RelationshipEvent(eventSource, rel, entity); 212 mediator.fireDbRelationshipEvent(e); 213 } 214 fireTableRowsUpdated(row, row); 215 } 216 217 221 void removeRelationship(Relationship rel) { 222 objectList.remove(rel); 223 fireTableDataChanged(); 224 } 225 226 public boolean isCellEditable(int row, int col) { 227 DbRelationship rel = getRelationship(row); 228 if (rel == null) { 229 return false; 230 } 231 else if (col == TO_DEPENDENT_KEY) { 232 return rel.isValidForDepPk(); 233 } 234 return true; 235 } 236 } | Popular Tags |