Table + BeanContainer + UTC Date Field + User Time Zone

Hello!

I have bean with java.util.Date field which holds UTC datetime. I want to show it in the Vaadin Table according to user preferences: user can select TimeZone. I do not want to change original datetime value, I want to make user preferrences aware view.

What is preferred way to do it with Vaadin 7?

Thanks in advance,
Anatoly Shirokov.

Application wide it would be best to extend table :

public class MyTable extends Table {

@Override
protected String formatPropertyValue(final Object rowId, final Object colId, final Property<?> property) {
final Class<?> type = property.getType();
if (java.util.Date.class.isAssignableFrom(type)) {
return formatDate((java.util.Date) property.getValue());
} else if (Integer.class.isAssignableFrom(type)) {
return formatInteger((Integer) property.getValue());
} else if (Long.class.isAssignableFrom(type)) {
return formatLong((Long) property.getValue());
} else if (Double.class.isAssignableFrom(type)) {
return formatDecimal((Double) property.getValue());
}
try {
return super.formatPropertyValue(rowId, colId, property);
} catch (Exception ex) {
return "Error: "+ ex.getMessage();
}

}


}

or add a GeneratedColumn ( this is more appropriate for specific view behaviour )

Dear Petrus! Thank you so much!

I have used the formatPropertyValue approach:

public class SettingsAwareTable extends Table {

    private Settings settings;
    private String lastTimeZoneID;
    private TimeZone optimizationTimeZone;
    private Calendar optimizationCalendar;
    private ObjectProperty<Date> optimizationDateProperty = new ObjectProperty<Date>(null, Date.class);
    
    public SettingsAwareTable(Settings settings) {
        setSettings(settings);
    }
    
    public Settings getSettings() {
        return settings;
    }
    
    public void setSettings(Settings settings) {
        this.settings = settings;
        prepareOptimizationParameters();
    }

    @Override
    protected String formatPropertyValue(Object rowId, Object colId,
            Property<?> property) {
        if( settings != null ) {
            final Class<?> type = property.getType();
            if( java.util.Date.class.isAssignableFrom(type) ) {
                Date date = (Date) property.getValue();
                if( date != null ) {
                    
                    prepareOptimizationParameters();
                    
                    optimizationCalendar.setTime(date);
                    int offset = optimizationTimeZone.getOffset(date.getTime());
                    optimizationCalendar.add(Calendar.MILLISECOND, offset);
                    optimizationDateProperty.setValue(optimizationCalendar.getTime());
                    
                    return super.formatPropertyValue(rowId, colId, optimizationDateProperty);
                }
            }
        }
        return super.formatPropertyValue(rowId, colId, property);
    }
    
    protected void prepareOptimizationParameters() {
        if( settings != null ) {
            String timeZoneID = settings.getTimeZoneID();
            if( timeZoneID!=null && !timeZoneID.equals(lastTimeZoneID) ) {
                lastTimeZoneID = timeZoneID;
                optimizationTimeZone = settings.getTimeZone();
                optimizationCalendar = Calendar.getInstance(optimizationTimeZone);
            }
        } else {
            lastTimeZoneID = null;
            optimizationTimeZone = null;
            optimizationCalendar = null;
        }
    }
}