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.apache.log4j.Logger; 63 import org.objectstyle.cayenne.dba.TypesMapping; 64 import org.objectstyle.cayenne.map.DbAttribute; 65 import org.objectstyle.cayenne.map.DbEntity; 66 import org.objectstyle.cayenne.map.ObjAttribute; 67 import org.objectstyle.cayenne.map.ObjEntity; 68 import org.objectstyle.cayenne.map.event.AttributeEvent; 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 82 public class ObjAttributeTableModel extends CayenneTableModel { 83 static final int OBJ_ATTRIBUTE = 0; 85 static final int OBJ_ATTRIBUTE_TYPE = 1; 86 static final int DB_ATTRIBUTE = 2; 87 static final int DB_ATTRIBUTE_TYPE = 3; 88 static final int LOCKING = 4; 89 90 static final Logger logObj = Logger.getLogger(ObjAttributeTableModel.class); 91 92 protected ObjEntity entity; 93 protected DbEntity dbEntity; 94 95 public ObjAttributeTableModel( 96 ObjEntity entity, 97 ProjectController mediator, 98 Object eventSource) { 99 super(mediator, eventSource, new ArrayList (entity.getAttributes())); 100 this.entity = entity; 102 this.dbEntity = entity.getDbEntity(); 103 104 Collections.sort(objectList, new AttributeComparator()); 106 } 107 108 protected void orderList() { 109 } 111 112 public Class getColumnClass(int col) { 113 switch (col) { 114 case LOCKING : 115 return Boolean .class; 116 default : 117 return String .class; 118 } 119 } 120 121 124 public Class getElementsClass() { 125 return ObjAttribute.class; 126 } 127 128 public DbEntity getDbEntity() { 129 return dbEntity; 130 } 131 132 public ObjAttribute getAttribute(int row) { 133 return (row >= 0 && row < objectList.size()) 134 ? (ObjAttribute) objectList.get(row) 135 : null; 136 } 137 138 139 public void resetDbEntity() { 140 if (dbEntity == entity.getDbEntity()) { 141 return; 142 } 143 144 boolean wasShowing = isShowingDb(); 145 dbEntity = entity.getDbEntity(); 146 boolean isShowing = isShowingDb(); 147 148 if (wasShowing != isShowing) { 149 fireTableStructureChanged(); 150 } 151 152 fireTableDataChanged(); 153 } 154 155 private boolean isShowingDb() { 156 return dbEntity != null; 157 } 158 159 public int getColumnCount() { 160 return 5; 161 } 162 163 public String getColumnName(int column) { 164 switch (column) { 165 case OBJ_ATTRIBUTE : 166 return "ObjAttribute"; 167 case OBJ_ATTRIBUTE_TYPE : 168 return "Java Type"; 169 case DB_ATTRIBUTE : 170 return "DbAttribute"; 171 case DB_ATTRIBUTE_TYPE : 172 return "DB Type"; 173 case LOCKING : 174 return "Used for Locking"; 175 default : 176 return ""; 177 } 178 } 179 180 public Object getValueAt(int row, int column) { 181 ObjAttribute attribute = getAttribute(row); 182 183 if (column == OBJ_ATTRIBUTE) { 184 return attribute.getName(); 185 } 186 else if (column == OBJ_ATTRIBUTE_TYPE) { 187 return attribute.getType(); 188 } 189 else if (column == LOCKING) { 190 return attribute.isUsedForLocking() ? Boolean.TRUE : Boolean.FALSE; 191 } 192 else { 193 DbAttribute dbAttribute = attribute.getDbAttribute(); 194 if (dbAttribute == null) { 195 return null; 196 } 197 else if (column == DB_ATTRIBUTE) 198 return dbAttribute.getName(); 199 else if (column == DB_ATTRIBUTE_TYPE) { 200 return TypesMapping.getSqlNameByType(dbAttribute.getType()); 201 } 202 else { 203 return null; 204 } 205 } 206 } 207 208 public void setUpdatedValueAt(Object value, int row, int column) { 209 210 ObjAttribute attribute = getAttribute(row); 211 AttributeEvent event = new AttributeEvent(eventSource, attribute, entity); 212 213 if (column == OBJ_ATTRIBUTE) { 214 event.setOldName(attribute.getName()); 215 ProjectUtil.setAttributeName( 216 attribute, 217 value != null ? value.toString().trim() : null); 218 fireTableCellUpdated(row, column); 219 } 220 else if (column == OBJ_ATTRIBUTE_TYPE) { 221 attribute.setType(value != null ? value.toString() : null); 222 fireTableCellUpdated(row, column); 223 } 224 else if (column == LOCKING) { 225 attribute.setUsedForLocking( 226 (value instanceof Boolean ) && ((Boolean ) value).booleanValue()); 227 fireTableCellUpdated(row, column); 228 } 229 else { 230 DbAttribute dbAttribute = attribute.getDbAttribute(); 231 if (column == DB_ATTRIBUTE) { 232 if (value != null) { 234 dbAttribute = (DbAttribute) dbEntity.getAttribute(value.toString()); 235 attribute.setDbAttribute(dbAttribute); 236 } 237 else if (dbAttribute != null) { 239 attribute.setDbAttribute(null); 240 } 241 } 242 243 fireTableRowsUpdated(row, row); 244 } 245 246 mediator.fireObjAttributeEvent(event); 247 } 248 249 private boolean isInherited(int row) { 250 ObjAttribute attribute = getAttribute(row); 251 return (attribute != null) ? attribute.getEntity() != entity : false; 252 } 253 254 public boolean isCellEditable(int row, int col) { 255 if (isInherited(row)) { 256 return false; 257 } 258 259 if (dbEntity == null) { 260 return col != DB_ATTRIBUTE_TYPE && col != DB_ATTRIBUTE; 261 } 262 263 return col != DB_ATTRIBUTE_TYPE; 264 } 265 266 public ObjEntity getEntity() { 267 return entity; 268 } 269 270 final class AttributeComparator implements Comparator { 271 public int compare(Object o1, Object o2) { 272 ObjAttribute a1 = (ObjAttribute) o1; 273 ObjAttribute a2 = (ObjAttribute) o2; 274 275 int delta = getWeight(a1) - getWeight(a2); 276 277 return (delta != 0) 278 ? delta 279 : Util.nullSafeCompare(true, a1.getName(), a2.getName()); 280 } 281 282 private int getWeight(ObjAttribute a) { 283 return a.getEntity() == entity ? 1 : -1; 284 } 285 } 286 } 287 | Popular Tags |