octavian-nita
2/24/2016 - 4:44 PM

Adapter design pattern applied

Adapter design pattern applied

package ...;

import static ...Validation.required;
import static org.apache.commons.lang.time.DurationFormatUtils.formatDuration;

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import org.tepi.filtertable.FilterTable;

import com.vaadin.addon.tableexport.ExportableFormattedProperty;
import com.vaadin.addon.tableexport.TableExport;
import com.vaadin.data.Container;
import com.vaadin.data.Property;
import com.vaadin.ui.CustomTable;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;

/**
 * <a href="https://en.wikipedia.org/wiki/Adapter_pattern#Object_Adapter_pattern">Object adapter</a> from the
 * {@link CustomTable} &quot;interface&quot;, usually used in {@link FilterTable filtering tables} to the
 * {@link Table} &quot;interface&quot;, used (at least) by classes in the {@link TableExport} Vaadin add-on.
 * <p />
 * As this class is used, more methods might need to be adapted. This should usually be fairly easy to do since the
 * <code>CustomTable</code> code is, to a large extent, based on (copied from) Vaadin's own <code>Table</code> (see
 * <a href="https://vaadin.com/forum/#!/thread/1168868/1238806">here</a>).
 * 
 * @see <a href="http://en.wikipedia.org/wiki/Adapter_pattern">The Adapter design pattern</a>
 * @author <a href="mailto:octavian.nita@gmail.com">Octavian NITA</a>
 * @version $Id: CustomTableToTableAdapter.java 20299 2016-02-09 17:34:32Z nitanoc $
 */
public class CustomTableToTableAdapter extends Table implements ExportableFormattedProperty {

    private static final long serialVersionUID = 1020160209L;

    protected CustomTable adaptee;

    public CustomTableToTableAdapter(CustomTable adaptee) {
        this.adaptee = required(adaptee);
    }

    public void setAdaptee(CustomTable adaptee) {
        this.adaptee = required(adaptee);
    }

    @Override
    public UI getUI() {
        // Apparently, this method gets called in the super() constructor invocation,
        // therefore before setting the adaptee instance...
        return adaptee == null ? super.getUI() : adaptee.getUI();
    }

    @Override
    public Container getContainerDataSource() {
        return adaptee.getContainerDataSource();
    }

    @Override
    public Align getColumnAlignment(Object propertyId) {
        CustomTable.Align align = adaptee.getColumnAlignment(propertyId);

        if (align == null) {
            return null;
        }

        switch (align) {
        case LEFT:
            return Table.Align.LEFT;
        case CENTER:
            return Table.Align.CENTER;
        case RIGHT:
            return Table.Align.RIGHT;
        default:
            return null; // see CustomTable.Align#convertStringToAlign()
        }
    }

    @Override
    public String getColumnHeader(Object propertyId) {
        return adaptee.getColumnHeader(propertyId);
    }

    @Override
    public Object[] getVisibleColumns() {
        return adaptee.getVisibleColumns();
    }

    @Override
    public boolean isColumnCollapsed(Object propertyId) {
        return adaptee.isColumnCollapsed(propertyId);
    }

    @Override
    public String getFormattedPropertyValue(Object rowId, Object colId, @SuppressWarnings("rawtypes") Property property) {
        return formatPropertyValue(rowId, colId, property);
    }

    @Override
    protected String formatPropertyValue(Object rowId, Object colId, Property<?> property) {
        Class<?> propertyType = property.getType();
        Object propertyValue = property.getValue();

        if (propertyValue != null) {
            if (propertyType == DateTime.class) {
                return dateTimeFormatter.print((DateTime) propertyValue);
            }

            if (propertyType == Duration.class) {
                return formatDuration(((Duration) propertyValue).getMillis(), "dd:HH:mm").replaceFirst(":", "  day(s) ");
            }
        }

        return super.formatPropertyValue(rowId, colId, property);
    }

    protected DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("dd-MM-yyyy HH:mm");
}