package com.onaro.sanscreen.client.view.tabular; import java.awt.Component; import java.text.MessageFormat; import java.util.Date; import java.util.ResourceBundle; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; import org.apache.commons.lang3.StringUtils; import com.onaro.sanscreen.client.view.init.ConfigurableTableModelInitInfo; import com.onaro.sanscreen.util.Constant; import com.onaro.util.jfc.tables.filter.StringFilter; import com.onaro.util.jfc.tables.renderers.SimpleTableCellRenderer; /** * A column that displays dates given as the number of milliseconds since the epoch (given as long). * Enumeration support is based on the number of milliseconds and not on the date (because the date is locale depenant). */ public class DateColumn extends BasicCachingColumn { private static final long serialVersionUID = 1L; /** * Formats the dates. */ MessageFormat formatter; /** * Configure a date column.

It gets from the configuration the followig paramters: *

* * @param tableName * the name of the table this column belongs to * @param resources * a locale dependant resource bundle from which the column reads its strings and icons using the table * name and column name as the resource's name * @param enumerations * used for translation of enumerated valued * */ public DateColumn(String tableName, ConfigurableTableModelInitInfo.DateColumnInitInfo columnInitInfo, ResourceBundle resources, com.onaro.util.enumeration.EnumPresentation enumerations) { super(tableName, columnInitInfo, resources, enumerations); String formatKey = columnInitInfo.getFormat(); String format = resources.getString(formatKey); formatter = new MessageFormat(format); initialize(); } private void initialize() { // no enum should be defined for dates. setFilter(new StringFilter()); } @Override protected DefaultTableCellRenderer createCellRenderer() { return new DateTableCellRenderer(); } @Override protected Object createValue(Row row) { Object value = null; String valueStr = (String) row.getTabularDataModelValue(getTabularDataModelColumnName()); if (StringUtils.isNotBlank(valueStr)) { try { value = new Date(Long.parseLong(valueStr)); } catch (Exception e) { // ignore } } if (value == null) { value = super.createValue(row); } return value; } class DateTableCellRenderer extends SimpleTableCellRenderer { private static final long serialVersionUID = 1L; private final Date date; private final Date[] dateArgs; public DateTableCellRenderer() { date = new Date(); dateArgs = new Date[] { date, date }; } @Override public Component getTableCellRendererComponent(JTable table, final Object value, boolean isSelected, boolean hasFocus, int row, int column) { Object formattedValue = value; ; if (value instanceof Date) { Date dateValue = (Date) value; if (dateValue.getTime() != Constant.END_DATE) { date.setTime(dateValue.getTime()); formattedValue = formatter.format(dateArgs); } else { formattedValue = StringUtils.EMPTY; } } return super.getTableCellRendererComponent(table, formattedValue, isSelected, hasFocus, row, column); } } @Override protected String getCacheKey() { return getTabularDataModelColumnName(); } }