1 56 package org.objectstyle.cayenne.modeler.editor; 57 58 import java.util.ArrayList ; 59 import java.util.Collections ; 60 import java.util.Comparator ; 61 62 import org.objectstyle.cayenne.map.DbEntity; 63 import org.objectstyle.cayenne.map.DbRelationship; 64 import org.objectstyle.cayenne.map.DeleteRule; 65 import org.objectstyle.cayenne.map.ObjEntity; 66 import org.objectstyle.cayenne.map.ObjRelationship; 67 import org.objectstyle.cayenne.map.Relationship; 68 import org.objectstyle.cayenne.map.event.RelationshipEvent; 69 import org.objectstyle.cayenne.modeler.ProjectController; 70 import org.objectstyle.cayenne.modeler.util.CayenneTableModel; 71 import org.objectstyle.cayenne.modeler.util.ProjectUtil; 72 import org.objectstyle.cayenne.util.Util; 73 74 80 public class ObjRelationshipTableModel extends CayenneTableModel { 81 static final int REL_NAME = 0; 83 static final int REL_TARGET = 1; 84 static final int REL_CARDINALITY = 2; 85 static final int REL_DELETERULE = 3; 86 static final int REL_LOCKING = 4; 87 88 protected ObjEntity entity; 89 90 public ObjRelationshipTableModel( 91 ObjEntity entity, 92 ProjectController mediator, 93 Object eventSource) { 94 super(mediator, eventSource, new ArrayList (entity.getRelationships())); 95 this.entity = entity; 96 97 Collections.sort(objectList, new RelationshipComparator()); 99 } 100 101 protected void orderList() { 102 } 104 105 public ObjEntity getEntity() { 106 return entity; 107 } 108 109 112 public Class getElementsClass() { 113 return ObjRelationship.class; 114 } 115 116 public int getColumnCount() { 117 return 5; 118 } 119 120 public String getColumnName(int column) { 121 switch (column) { 122 case REL_NAME : 123 return "Name"; 124 case REL_TARGET : 125 return "Target"; 126 case REL_LOCKING : 127 return "Used for Locking"; 128 case REL_CARDINALITY : 129 return "To Many"; 130 case REL_DELETERULE : 131 return "Delete Rule"; 132 133 default : 134 return null; 135 } 136 } 137 138 public Class getColumnClass(int col) { 139 switch (col) { 140 case REL_TARGET : 141 return ObjEntity.class; 142 case REL_CARDINALITY : 143 case REL_LOCKING : 144 return Boolean .class; 145 default : 146 return String .class; 147 } 148 } 149 150 public ObjRelationship getRelationship(int row) { 151 return (row >= 0 && row < objectList.size()) 152 ? (ObjRelationship) objectList.get(row) 153 : null; 154 } 155 156 public Object getValueAt(int row, int column) { 157 ObjRelationship rel = getRelationship(row); 158 159 if (column == REL_NAME) { 160 return rel.getName(); 161 } 162 else if (column == REL_TARGET) { 163 return rel.getTargetEntity(); 164 } 165 else if (column == REL_LOCKING) { 166 return rel.isUsedForLocking() ? Boolean.TRUE : Boolean.FALSE; 167 } 168 else if (column == REL_CARDINALITY) { 169 return rel.isToMany() ? Boolean.TRUE : Boolean.FALSE; 170 } 171 else if (column == REL_DELETERULE) { 172 return DeleteRule.deleteRuleName(rel.getDeleteRule()); 173 } 174 else { 175 return null; 176 } 177 } 178 179 public void setUpdatedValueAt(Object value, int row, int column) { 180 ObjRelationship relationship = getRelationship(row); 181 RelationshipEvent event = 182 new RelationshipEvent(eventSource, relationship, entity); 183 184 if (column == REL_NAME) { 185 String text = (String ) value; 186 event.setOldName(relationship.getName()); 187 ProjectUtil.setRelationshipName(entity, relationship, text); 188 fireTableCellUpdated(row, column); 189 } 190 else if (column == REL_TARGET) { 191 ObjEntity target = (ObjEntity) value; 192 relationship.setTargetEntity(target); 193 194 if (target != null) { 196 DbEntity srcDB = 197 ((ObjEntity) relationship.getSourceEntity()).getDbEntity(); 198 DbEntity targetDB = target.getDbEntity(); 199 if (srcDB != null && targetDB != null) { 200 Relationship anyConnector = srcDB.getAnyRelationship(targetDB); 201 if (anyConnector != null) { 202 relationship.addDbRelationship((DbRelationship) anyConnector); 203 } 204 } 205 } 206 207 fireTableRowsUpdated(row, row); 208 } 209 else if (column == REL_DELETERULE) { 210 relationship.setDeleteRule(DeleteRule.deleteRuleForName((String ) value)); 211 fireTableCellUpdated(row, column); 212 } 213 else if (column == REL_LOCKING) { 214 relationship.setUsedForLocking( 215 (value instanceof Boolean ) && ((Boolean ) value).booleanValue()); 216 fireTableCellUpdated(row, column); 217 } 218 219 mediator.fireObjRelationshipEvent(event); 220 } 221 222 public void removeRow(int row) { 223 if (row < 0) 224 return; 225 Relationship rel = getRelationship(row); 226 RelationshipEvent e; 227 e = new RelationshipEvent(eventSource, rel, entity, RelationshipEvent.REMOVE); 228 mediator.fireObjRelationshipEvent(e); 229 objectList.remove(row); 230 entity.removeRelationship(rel.getName()); 231 fireTableRowsDeleted(row, row); 232 } 233 234 private boolean isInherited(int row) { 235 ObjRelationship relationship = getRelationship(row); 236 return (relationship != null) ? relationship.getSourceEntity() != entity : false; 237 } 238 239 public boolean isCellEditable(int row, int col) { 240 return !isInherited(row) && col != REL_CARDINALITY; 241 } 242 243 final class RelationshipComparator implements Comparator { 244 public int compare(Object o1, Object o2) { 245 ObjRelationship r1 = (ObjRelationship) o1; 246 ObjRelationship r2 = (ObjRelationship) o2; 247 248 int delta = getWeight(r1) - getWeight(r2); 249 250 return (delta != 0) 251 ? delta 252 : Util.nullSafeCompare(true, r1.getName(), r2.getName()); 253 } 254 255 private int getWeight(ObjRelationship r) { 256 return r.getSourceEntity() == entity ? 1 : -1; 257 } 258 } 259 } 260 | Popular Tags |