001: package org.dbbrowser.db.engine.model;
002:
003: import java.sql.Types;
004: import java.text.DateFormat;
005: import java.text.DecimalFormat;
006: import java.text.SimpleDateFormat;
007: import java.util.Date;
008:
009: /**
010: * Represents a cell in a database table
011: */
012: public class DBTableCell {
013: //public static String DATE_FORMAT_STRING = "dd/MM/yyyy hh:mm:ss";
014: public static String DATE_FORMAT_STRING = "dd/MM/yyyy hh:mm a";
015: private ColumnInfo columnInfo = null;
016: private Object value = null;
017: private Boolean isChangedByUser = null;
018:
019: private static final DecimalFormat doubleFormatter = new DecimalFormat(
020: "0");
021: private static final DateFormat dateFormat = new SimpleDateFormat(
022: DATE_FORMAT_STRING);
023:
024: /**
025: * Build a object representing a value in the table cell
026: * @param columnInfo
027: * @param value
028: * @param isChangedByUser
029: */
030: public DBTableCell(ColumnInfo columnInfo, Object value,
031: Boolean isChangedByUser) {
032: this .columnInfo = columnInfo;
033: this .value = value;
034: this .isChangedByUser = isChangedByUser;
035: }
036:
037: /**
038: * Returns the value of the Object.
039: * @return
040: */
041: public Object getValue() {
042: return this .value;
043: }
044:
045: /**
046: * Return the value formatted as per the format of the data type. If the value is null, returns null
047: * @return
048: */
049: public String getFormattedValue() {
050: String formattedValue = null;
051: if (this .getValue() != null) {
052: if (this .getColumnInfo().getColumnType() == null) {
053: formattedValue = getValue().toString();
054: } else {
055: int columnType = this .getColumnInfo().getColumnType()
056: .intValue();
057:
058: if (this .value != null) {
059: if (columnType == Types.DOUBLE) {
060: formattedValue = doubleFormatter
061: .format(((Double) this .getValue())
062: .doubleValue());
063: } else if (columnType == Types.DATE
064: || columnType == Types.TIMESTAMP) {
065: formattedValue = dateFormat.format((Date) this
066: .getValue());
067: } else {
068: //Handles Strings, numbers, clobs, blobs etc
069: formattedValue = this .value.toString();
070: }
071: }
072: }
073: }
074: return formattedValue;
075: }
076:
077: /**
078: * Returns the column info describing this object
079: * @return
080: */
081: public ColumnInfo getColumnInfo() {
082: return this .columnInfo;
083: }
084:
085: /**
086: * Returns true if this has been changed by the user and so this cell should be updated
087: * @return
088: */
089: public Boolean isChangedByUser() {
090: return this .isChangedByUser;
091: }
092:
093: /**
094: * Returns the date format for all dates
095: * @return
096: */
097: public static DateFormat getDateFormat() {
098: return dateFormat;
099: }
100:
101: /**
102: * Returns the double format for formatting numbers
103: * @return
104: */
105: public static DecimalFormat getDecimalFormat() {
106: return doubleFormatter;
107: }
108:
109: /**
110: * toString - for debugging only
111: */
112: public String toString() {
113: StringBuffer buffer = new StringBuffer();
114: buffer.append("DBTableCell:\n");
115: buffer.append("Value: " + this .getValue() + "\n");
116: buffer.append("Is changed by user: " + this .isChangedByUser()
117: + "\n");
118:
119: return buffer.toString();
120: }
121: }
|