You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Edgar Merino <do...@gmail.com> on 2008/10/05 10:52:21 UTC

Reusable DateColumn component

Hello, a few days ago someone requested some advice on how to format the 
date within an IColumn, I suggested DateColumn that depended on his 
code, but I've been having this necessity in several ocassions too, so 
I've come up with a true reusable DateColumn component that inherits 
from PropertyColumn, so using it is as simple as:

(DateColumn class shown below)

List<IColumn> cols = new ArrayList<IColumn>();
cols.add(new DateColumn(new Model("header title"), "propertyExpression"));

No extra code needed, regards.
Edgar Merino


import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.lang.PropertyResolver;
import 
org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;

/**
 * PropertyColumn descendent, used to apply a format to a Date field to 
be displayed
 * by a Label in a DataTable
 *
 * @author Edgar Merino
 */
public class DateColumn extends PropertyColumn {       
    public  static final String DEFAULT_DATE_PATTERN = "dd MMM yyyy '@' 
hh:mm a";
    private String datePattern;               
   
    public DateColumn(IModel displayModel, String propertyExpression) {
        this(displayModel, DEFAULT_DATE_PATTERN, propertyExpression);
    }       
   
    public DateColumn(IModel displayModel, String datePattern, String 
propertyExpression) {
        this(displayModel, null, datePattern, propertyExpression);
    }
   
    public DateColumn(IModel displayModel, String sortProperty, String 
datePattern, String propertyExpression) {
        super(displayModel, propertyExpression);       
        this.datePattern = datePattern;
    }

    @Override
    protected IModel createLabelModel(IModel itemModel) {
        Date date = (Date) 
PropertyResolver.getValue(getPropertyExpression(), itemModel.getObject());
        SimpleDateFormat df = (SimpleDateFormat) 
DateFormat.getDateInstance();
        df.applyPattern(datePattern);
       
        return new Model(df.format(date));
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org