package com.onaro.sanscreen.client.view.tabular; import java.util.Collection; import javax.swing.JLabel; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.TableCellRenderer; import org.eclipse.core.runtime.IAdaptable; import com.onaro.commons.util.SwingCollectionPrinter; import com.onaro.util.jfc.tables.filter.StringFilter; /** * Column that joins the values of a collection together into a String representation using a {@link SwingCollectionPrinter}. * * @param element type of the collection * * @see SwingCollectionPrinter */ public abstract class AdaptableJoinedCollectionColumn extends AdaptableTabularColumn { private static final long serialVersionUID = 1L; private SwingCollectionPrinter printer; public AdaptableJoinedCollectionColumn(String id, String name, SwingCollectionPrinter printer) { super(id, String.class, name); setFilter(new StringFilter()); setPrinter(printer); } public AdaptableJoinedCollectionColumn(String id, String name) { this(id, name, new SwingCollectionPrinter(SwingCollectionPrinter.COMMA_SEPARATED)); } public AdaptableJoinedCollectionColumn(String id, SwingCollectionPrinter printer) { this(id, null, printer); } public SwingCollectionPrinter getPrinter() { return printer; } public void setPrinter(SwingCollectionPrinter printer) { if (printer == null) { this.printer = new SwingCollectionPrinter(SwingCollectionPrinter.COMMA_SEPARATED); } else { this.printer = printer; } // Align the cells according to the printer type (right align numbers). TableCellRenderer renderer = getCellRenderer(); if (renderer == null) { // Note DefaultTableCellRenderer is a JLabel. renderer = new DefaultTableCellRenderer(); setCellRenderer (renderer); } if (renderer instanceof JLabel) { ((JLabel)renderer).setHorizontalAlignment (this.printer.getAlignment()); } } /** * Retrieve the collection value to display for the specified row. * * @param rowObj the row * @return the collection to display in the column */ public abstract Collection getCollectionValue(IAdaptable rowObj) throws Exception; @Override public String getValue(IAdaptable rowObj) throws Exception { Collection collection = getCollectionValue(rowObj); if (collection == null) return null; String joinedValue = getPrinter().toString(collection); return joinedValue; } }