1 56 package org.objectstyle.cayenne.modeler.editor; 57 58 import java.util.ArrayList ; 59 import java.util.Collection ; 60 import java.util.Iterator ; 61 62 import javax.swing.JOptionPane ; 63 64 import org.objectstyle.cayenne.dba.TypesMapping; 65 import org.objectstyle.cayenne.map.DbAttribute; 66 import org.objectstyle.cayenne.map.DbEntity; 67 import org.objectstyle.cayenne.map.DbRelationship; 68 import org.objectstyle.cayenne.map.event.AttributeEvent; 69 import org.objectstyle.cayenne.modeler.Application; 70 import org.objectstyle.cayenne.modeler.ProjectController; 71 import org.objectstyle.cayenne.modeler.util.CayenneTableModel; 72 import org.objectstyle.cayenne.modeler.util.ProjectUtil; 73 74 81 public class DbAttributeTableModel extends CayenneTableModel { 82 83 private static final int DB_ATTRIBUTE_NAME = 0; 85 private static final int DB_ATTRIBUTE_TYPE = 1; 86 private static final int DB_ATTRIBUTE_PRIMARY_KEY = 2; 87 private static final int DB_ATTRIBUTE_MANDATORY = 3; 88 private static final int DB_ATTRIBUTE_MAX = 4; 89 private static final int DB_ATTRIBUTE_PRECISION = 5; 90 private static final int DB_ATTRIBUTE_GENERATED = 6; 91 92 protected DbEntity entity; 93 94 public DbAttributeTableModel(DbEntity entity, ProjectController mediator, 95 Object eventSource) { 96 this(entity, mediator, eventSource, new ArrayList (entity.getAttributes())); 97 this.entity = entity; 98 } 99 100 public DbAttributeTableModel(DbEntity entity, ProjectController mediator, 101 Object eventSource, java.util.List objectList) { 102 super(mediator, eventSource, objectList); 103 } 104 105 public int nameColumnInd() { 106 return DB_ATTRIBUTE_NAME; 107 } 108 109 public int typeColumnInd() { 110 return DB_ATTRIBUTE_TYPE; 111 } 112 113 public int mandatoryColumnInd() { 114 return DB_ATTRIBUTE_MANDATORY; 115 } 116 117 120 public Class getElementsClass() { 121 return DbAttribute.class; 122 } 123 124 127 public int getColumnCount() { 128 return 7; 129 } 130 131 public DbAttribute getAttribute(int row) { 132 return (row >= 0 && row < objectList.size()) 133 ? (DbAttribute) objectList.get(row) 134 : null; 135 } 136 137 public String getColumnName(int col) { 138 switch (col) { 139 case DB_ATTRIBUTE_NAME: 140 return "Name"; 141 case DB_ATTRIBUTE_TYPE: 142 return "Type"; 143 case DB_ATTRIBUTE_PRIMARY_KEY: 144 return "PK"; 145 case DB_ATTRIBUTE_PRECISION: 146 return "Precision"; 147 case DB_ATTRIBUTE_MANDATORY: 148 return "Mandatory"; 149 case DB_ATTRIBUTE_MAX: 150 return "Max Length"; 151 case DB_ATTRIBUTE_GENERATED: 152 return "Generated"; 153 default: 154 return ""; 155 } 156 } 157 158 public Class getColumnClass(int col) { 159 switch (col) { 160 case DB_ATTRIBUTE_PRIMARY_KEY: 161 case DB_ATTRIBUTE_MANDATORY: 162 case DB_ATTRIBUTE_GENERATED: 163 return Boolean .class; 164 default: 165 return String .class; 166 } 167 } 168 169 public Object getValueAt(int row, int column) { 170 DbAttribute attr = getAttribute(row); 171 172 if (attr == null) { 173 return ""; 174 } 175 176 switch (column) { 177 case DB_ATTRIBUTE_NAME: 178 return getAttributeName(attr); 179 case DB_ATTRIBUTE_TYPE: 180 return getAttributeType(attr); 181 case DB_ATTRIBUTE_PRIMARY_KEY: 182 return isPrimaryKey(attr); 183 case DB_ATTRIBUTE_PRECISION: 184 return getPrecision(attr); 185 case DB_ATTRIBUTE_MANDATORY: 186 return isMandatory(attr); 187 case DB_ATTRIBUTE_MAX: 188 return getMaxLength(attr); 189 case DB_ATTRIBUTE_GENERATED: 190 return isGenerated(attr); 191 default: 192 return ""; 193 } 194 } 195 196 public void setUpdatedValueAt(Object newVal, int row, int col) { 197 DbAttribute attr = getAttribute(row); 198 if (attr == null) { 199 return; 200 } 201 202 AttributeEvent e = new AttributeEvent(eventSource, attr, entity); 203 204 switch (col) { 205 case DB_ATTRIBUTE_NAME: 206 e.setOldName(attr.getName()); 207 attr.setName((String )newVal); 208 fireTableCellUpdated(row, col); 210 break; 211 case DB_ATTRIBUTE_TYPE: 212 setAttributeType((String ) newVal, attr); 213 break; 214 case DB_ATTRIBUTE_PRIMARY_KEY: 215 if (!setPrimaryKey(((Boolean ) newVal), attr, row)) { 216 return; 217 } 218 break; 219 case DB_ATTRIBUTE_PRECISION: 220 setPrecision((String ) newVal, attr); 221 break; 222 case DB_ATTRIBUTE_MANDATORY: 223 setMandatory((Boolean ) newVal, attr); 224 break; 225 case DB_ATTRIBUTE_GENERATED: 226 setGenerated((Boolean ) newVal, attr); 227 break; 228 case DB_ATTRIBUTE_MAX: 229 setMaxLength((String ) newVal, attr); 230 break; 231 } 232 233 mediator.fireDbAttributeEvent(e); 234 } 235 236 public String getMaxLength(DbAttribute attr) { 237 return (attr.getMaxLength() >= 0) ? String.valueOf(attr.getMaxLength()) : ""; 238 } 239 240 public String getAttributeName(DbAttribute attr) { 241 return attr.getName(); 242 } 243 244 public String getAttributeType(DbAttribute attr) { 245 return TypesMapping.getSqlNameByType(attr.getType()); 246 } 247 248 public String getPrecision(DbAttribute attr) { 249 return (attr.getPrecision() >= 0) ? String.valueOf(attr.getPrecision()) : ""; 250 } 251 252 public Boolean isPrimaryKey(DbAttribute attr) { 253 return (attr.isPrimaryKey()) ? Boolean.TRUE : Boolean.FALSE; 254 } 255 256 public Boolean isMandatory(DbAttribute attr) { 257 return (attr.isMandatory()) ? Boolean.TRUE : Boolean.FALSE; 258 } 259 260 public Boolean isGenerated(DbAttribute attr) { 261 return (attr.isGenerated()) ? Boolean.TRUE : Boolean.FALSE; 262 } 263 264 public void setMaxLength(String newVal, DbAttribute attr) { 265 if (newVal == null || newVal.trim().length() <= 0) { 266 attr.setMaxLength(-1); 267 } 268 else { 269 try { 270 attr.setMaxLength(Integer.parseInt(newVal)); 271 } 272 catch (NumberFormatException ex) { 273 JOptionPane.showMessageDialog( 274 null, 275 "Invalid Max Length (" + newVal + "), only numbers are allowed", 276 "Invalid Maximum Length", 277 JOptionPane.ERROR_MESSAGE); 278 return; 279 } 280 } 281 } 282 283 public void setAttributeName(String newVal, DbAttribute attr) { 284 String newName = newVal.trim(); 285 ProjectUtil.setAttributeName(attr, newName); 286 } 287 288 public void setAttributeType(String newVal, DbAttribute attr) { 289 attr.setType(TypesMapping.getSqlTypeByName(newVal)); 290 } 291 292 public void setPrecision(String newVal, DbAttribute attr) { 293 if (newVal == null || newVal.trim().length() <= 0) { 294 attr.setPrecision(-1); 295 } 296 else { 297 try { 298 attr.setPrecision(Integer.parseInt(newVal)); 299 } 300 catch (NumberFormatException ex) { 301 JOptionPane.showMessageDialog( 302 null, 303 "Invalid precision (" + newVal + "), only numbers are allowed.", 304 "Invalid Precision Value", 305 JOptionPane.ERROR_MESSAGE); 306 } 307 } 308 } 309 310 public boolean setPrimaryKey(Boolean newVal, DbAttribute attr, int row) { 311 312 boolean flag = newVal.booleanValue(); 313 314 if (!flag) { 316 Collection relationships = ProjectUtil 317 .getRelationshipsUsingAttributeAsTarget(attr); 318 relationships 319 .addAll(ProjectUtil.getRelationshipsUsingAttributeAsSource(attr)); 320 321 if (relationships.size() > 0) { 322 Iterator it = relationships.iterator(); 323 while (it.hasNext()) { 324 DbRelationship relationship = (DbRelationship) it.next(); 325 if (!relationship.isToDependentPK()) { 326 it.remove(); 327 } 328 } 329 330 if (relationships.size() > 0) { 332 String message = (relationships.size() == 1) 333 ? "Fix \"To Dep PK\" relationship using this attribute?" 334 : "Fix " 335 + relationships.size() 336 + " \"To Dep PK\" relationships using this attribute?"; 337 338 int answer = JOptionPane.showConfirmDialog( 339 Application.getFrame(), 340 message); 341 if (answer != JOptionPane.YES_OPTION) { 342 return false; 344 } 345 346 Iterator fixIt = relationships.iterator(); 348 while (fixIt.hasNext()) { 349 DbRelationship relationship = (DbRelationship) fixIt.next(); 350 relationship.setToDependentPK(false); 351 } 352 } 353 } 354 } 355 356 attr.setPrimaryKey(flag); 357 if (flag) { 358 attr.setMandatory(true); 359 fireTableCellUpdated(row, DB_ATTRIBUTE_MANDATORY); 360 } 361 return true; 362 } 363 364 public void setMandatory(Boolean newVal, DbAttribute attr) { 365 attr.setMandatory(newVal.booleanValue()); 366 } 367 368 public void setGenerated(Boolean newVal, DbAttribute attr) { 369 attr.setGenerated(newVal.booleanValue()); 370 } 371 372 public boolean isCellEditable(int row, int col) { 373 DbAttribute attrib = getAttribute(row); 374 if (null == attrib) { 375 return false; 376 } 377 else if (col == mandatoryColumnInd()) { 378 if (attrib.isPrimaryKey()) { 379 return false; 380 } 381 } 382 return true; 383 } 384 } | Popular Tags |