1 56 package org.objectstyle.cayenne.dataview; 57 58 import java.text.Format ; 59 60 import org.apache.commons.lang.ObjectUtils; 61 import org.apache.commons.lang.Validate; 62 import org.objectstyle.cayenne.DataObject; 63 import org.objectstyle.cayenne.map.ObjAttribute; 64 import org.objectstyle.cayenne.map.ObjRelationship; 65 66 72 public class ObjEntityViewField { 73 74 private ObjEntityView owner; 76 private ObjAttribute objAttribute; 80 81 private ObjRelationship objRelationship; 82 83 private String name = ""; 85 86 private DataTypeEnum dataType = DataTypeEnum.UNKNOWN_TYPE; 88 89 private CalcTypeEnum calcType = CalcTypeEnum.NO_CALC_TYPE; 91 92 private String caption = ""; 95 96 private Format displayFormat = null; 101 102 private Format editFormat = null; 104 105 private int preferredIndex = -1; 108 109 private int index = -1; 111 112 private boolean editable = true; 114 115 private boolean visible = true; 117 118 private Object defaultValue = null; 120 121 private ObjEntityViewField lookupField = null; 124 125 public ObjEntityViewField() { 126 } 127 128 public ObjEntityView getOwner() { 129 return owner; 130 } 131 132 public DataView getRootOwner() { 133 return getOwner().getOwner(); 134 } 135 136 public int getIndex() { 137 return index; 138 } 139 140 public int getPreferredIndex() { 141 return preferredIndex; 142 } 143 144 public void setPreferredIndex(int preferredIndex) { 145 Validate.isTrue(preferredIndex >= -1); 146 this.preferredIndex = preferredIndex; 147 } 148 149 public boolean isVisible() { 150 return visible; 151 } 152 153 public void setVisible(boolean visible) { 154 this.visible = visible; 155 } 156 157 public String getName() { 158 return name; 159 } 160 161 public void setName(String name) { 162 Validate.notNull(name); 163 this.name = name; 164 } 165 166 public ObjAttribute getObjAttribute() { 167 return objAttribute; 168 } 169 170 public void setObjAttribute(ObjAttribute objAttribute) { 171 Validate.notNull(objAttribute); 172 this.objAttribute = objAttribute; 173 } 174 175 public boolean isEditable() { 176 return editable; 177 } 178 179 public void setEditable(boolean editable) { 180 this.editable = editable; 181 } 182 183 public String getCaption() { 184 return caption; 185 } 186 187 public void setCaption(String caption) { 188 this.caption = caption; 189 } 190 191 void setOwner(ObjEntityView owner) { 192 this.owner = owner; 193 } 194 195 void setIndex(int index) { 196 Validate.isTrue(owner == null || index >= 0); 197 this.index = index; 198 } 199 200 public Class getJavaClass() { 201 return getRootOwner().getDataTypeSpec().getJavaClass(dataType); 202 } 203 204 public Object getValue(DataObject obj) { 205 Object rawValue = getRawValue(obj); 206 return toValue(rawValue); 207 } 208 209 public Object toValue(Object rawValue) { 210 Object v = null; 211 if (isLookup()) { 212 if (rawValue instanceof DataObject) 213 v = lookupField.getValue((DataObject) rawValue); 214 return v; 215 } 216 if (rawValue == null) 217 return null; 218 219 v = getRootOwner().getDataTypeSpec().toDataType(dataType, rawValue); 220 return v; 221 } 222 223 public Object toRawValue(Object value) { 224 if (value == null) 225 return null; 226 DataView rootOwner = getOwner().getOwner(); 227 if (isLookup()) { 228 return rootOwner.getLookupCache().getDataObject(lookupField, value); 229 } 230 if (objAttribute == null) 231 return null; 232 233 String type = objAttribute.getType(); 234 Object v = null; 235 try { 236 Class untypedValueClass = Class.forName(type); 237 v = rootOwner.getDataTypeSpec().fromDataType( 238 untypedValueClass, 239 dataType, 240 value); 241 } 242 catch (ClassNotFoundException ex) { 243 ex.printStackTrace(); 244 } 245 return v; 246 } 247 248 public void setValue(DataObject obj, Object value) { 249 Object rawValue = toRawValue(value); 250 Object oldValue = getValue(obj); 251 setRawValue(obj, rawValue); 252 getRootOwner().fireFieldValueChangeEvent(this, obj, oldValue, value); 253 } 254 255 public String getFormattedValue(DataObject obj) { 256 Object value = getRawValue(obj); 257 String formattedValue = null; 258 if (!isLookup()) { 259 Format f = (displayFormat != null ? displayFormat : editFormat); 260 if (f == null) 261 formattedValue = ObjectUtils.toString(value); 262 else { 263 try { 264 formattedValue = f.format(value); 265 } 266 catch (Exception ex) { 267 formattedValue = ""; 268 } 269 } 270 } 271 else { 272 formattedValue = lookupField.getFormattedValue((DataObject) value); 273 } 274 return formattedValue; 275 } 276 277 public Object getRawValue(DataObject obj) { 278 if (obj == null) 279 return null; 280 Object value = null; 281 if (!isLookup() && objAttribute != null) { 282 value = obj.readProperty(objAttribute.getName()); 283 } 284 else if (isLookup() && objRelationship != null) { 285 value = obj.readProperty(objRelationship.getName()); 286 } 287 return value; 288 } 289 290 public void setRawValue(DataObject obj, Object value) { 291 if (obj != null) { 292 if (!isLookup() && objAttribute != null) { 293 obj.writeProperty(objAttribute.getName(), value); 294 } 295 else if (isLookup() && objRelationship != null) { 296 obj.setToOneTarget( 297 objRelationship.getName(), 298 (DataObject) value, 299 objRelationship.getReverseRelationship() != null); 300 } 301 } 302 } 303 304 public DataTypeEnum getDataType() { 305 return dataType; 306 } 307 308 public void setDataType(DataTypeEnum dataType) { 309 Validate.notNull(dataType); 310 this.dataType = dataType; 311 } 312 313 public CalcTypeEnum getCalcType() { 314 return calcType; 315 } 316 317 public void setCalcType(CalcTypeEnum calcType) { 318 Validate.notNull(calcType); 319 this.calcType = calcType; 320 } 321 322 public Object getDefaultValue() { 323 return defaultValue; 324 } 325 326 public void setDefaultValue(Object defaultValue) { 327 this.defaultValue = defaultValue; 328 } 329 330 public ObjRelationship getObjRelationship() { 331 return objRelationship; 332 } 333 334 public void setObjRelationship(ObjRelationship objRelationship) { 335 Validate.notNull(objRelationship); 336 this.objRelationship = objRelationship; 337 } 338 339 public boolean isLookup() { 340 return CalcTypeEnum.LOOKUP_TYPE.equals(calcType); 341 } 342 343 public Object [] getLookupValues() { 344 if (!isLookup()) 345 return null; 346 return getRootOwner().getLookupCache().getCachedValues(lookupField); 347 } 348 349 public ObjEntityViewField getLookupField() { 350 return lookupField; 351 } 352 353 public void setLookupField(ObjEntityViewField lookupField) { 354 this.lookupField = lookupField; 355 } 356 357 public Format getDisplayFormat() { 358 if (displayFormat == null 359 && isLookup() 360 && lookupField != null 361 && lookupField != this) 362 return lookupField.getDisplayFormat(); 363 return displayFormat; 364 } 365 366 public void setDisplayFormat(Format displayFormat) { 367 this.displayFormat = displayFormat; 368 } 369 370 public Format getEditFormat() { 371 if (editFormat == null 372 && isLookup() 373 && lookupField != null 374 && lookupField != this) 375 return lookupField.getEditFormat(); 376 return editFormat; 377 } 378 379 public void setEditFormat(Format editFormat) { 380 this.editFormat = editFormat; 381 } 382 383 public boolean isSameObjAttribute(ObjEntityViewField field) { 384 if (objAttribute != null) { 385 ObjAttribute fieldAttribute = field.getObjAttribute(); 386 if (fieldAttribute == null) 387 return false; 388 boolean same = (objAttribute.equals(fieldAttribute) || (objAttribute 389 .getParent() 390 .equals(fieldAttribute.getParent()) && objAttribute.getName().equals( 391 fieldAttribute.getName()))); 392 return same; 393 } 394 else if (isLookup()) { 395 if (field.isLookup()) 396 return getLookupField().isSameObjAttribute(field.getLookupField()); 397 else 398 return getLookupField().isSameObjAttribute(field); 399 } 400 return false; 401 } 402 } | Popular Tags |