package com.onaro.sanscreen.client.view.tabular; import java.text.MessageFormat; import java.util.ResourceBundle; import javax.swing.SwingConstants; import javax.swing.table.TableCellRenderer; import com.onaro.sanscreen.client.SANscreenClientPlugin; import com.onaro.sanscreen.client.view.init.ConfigurableTableModelInitInfo; import com.onaro.util.enumeration.EnumPresentation; import com.onaro.util.jfc.tables.filter.EnumFilter; import com.onaro.util.jfc.tables.filter.Filter; import com.onaro.util.jfc.tables.filter.StringFilter; import com.onaro.util.jfc.tables.renderers.SimpleTableCellRenderer; /** * Defines a table column's name, title and possible enumeration translation. A configuratble column is built from * configuration and the strings and icons it is using are fetched from a resource bundle.

It also provides a * custom {@link TableCellRenderer} for rendering the column's values. */ public class BasicColumn extends AbstractResourceColumn { private static final long serialVersionUID = 1L; /** * Specifies the enumeration-type-name used to translate a value in this column into the displayed value. No * translation takes place when set to null. */ private String enumName; /** * Used for translating enumerated values. */ protected EnumPresentation enumerations; /** * Used for caching the enum attributes map so it wouldn't need to be retreived every time getValueAt is called */ private EnumPresentation.AttributesMap enumAttributesMap; /** * Configure a column for the given table reading the definition from an XML element and the locale dependant * resources (displayed strings and icons) from the resources.

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 BasicColumn(String tableName, ConfigurableTableModelInitInfo.BasicColumnInitInfo columnInitInfo, ResourceBundle resources, EnumPresentation enumerations) { super(tableName, columnInitInfo, resources, Object.class); this.enumerations = enumerations; enumName = columnInitInfo.getEnumName(); if (enumName != null) { enumAttributesMap = enumerations.getEnumAttributes(enumName); } if (getEnumName() != null) { setValueType(EnumPresentation.Attributes.class); } initialize(); } private void initialize() { setFilter(createFilter()); } /** * Gets the type name for enumeration translation. * * @return enumeration type name or null if no translation is required */ private String getEnumName() { return enumName; } /** * Gets this value for the requested row from the model. * */ @Override public Object getValue(Row row) { //fast fail if (row == null) return null; Object rawValue = row.getTabularDataModelValue(getTabularDataModelColumnName()); Object translatedValue = translate(enumerations, rawValue); return translatedValue; } @Override protected ConfigurableTableModelInitInfo.BasicColumnInitInfo getColumnInitInfo() { return (ConfigurableTableModelInitInfo.BasicColumnInitInfo) super.getColumnInitInfo(); } /** * By default, calls createSimpleTableCellRenderer * * @see BasicColumn#createSimpleTableCellRenderer() */ @Override protected TableCellRenderer createCellRenderer() { SimpleTableCellRenderer simpleTableCellRenderer = createSimpleTableCellRenderer(); return simpleTableCellRenderer; } /** * By default the BasicColumn creates this renderer. * */ protected SimpleTableCellRenderer createSimpleTableCellRenderer() { SimpleTableCellRenderer renderer = new SimpleTableCellRenderer(); ConfigurableTableModelInitInfo.BasicColumnInitInfo columnInitInfo = getColumnInitInfo(); if (columnInitInfo != null) { Alignment alignment = columnInitInfo.getAlignment(); if (alignment != null && !alignment.equals(Alignment.none)) { renderer.setHorizontalAlignment(alignment.getAlignmentConstant()); } Alignment textAlignment = columnInitInfo.getTextPosition(); if (textAlignment != null && !textAlignment.equals(Alignment.none)) { renderer.setHorizontalTextPosition(textAlignment.getAlignmentConstant()); } } return renderer; } /** * Translate a value if this column is configured with an enumeration. * * @param enumPresentation * the translation service * @param value * the value to translate * @return if this is an enumeration column, return the translated value otherwise, return the value itself */ protected Object translate(EnumPresentation enumPresentation, Object value) { if (enumName != null) { EnumPresentation.Attributes attributes = enumPresentation.getAttributes(enumAttributesMap, enumName, value); if (attributes != null) { value = attributes; } } return value; } private Filter createFilter() { String enumeration = getColumnInitInfo().getEnumName(); if (enumeration != null) { EnumPresentation.Attributes[] legalValues = SANscreenClientPlugin.getDefault().getEnumPresentation() .getValues(enumeration); if (legalValues == null) { String msgPattern = "Could not find definition for enumeration: {0}"; //$NON-NLS-1$ String msg = MessageFormat.format(msgPattern, enumeration); throw new IllegalArgumentException(msg); } return new EnumFilter(legalValues); } return new StringFilter(); } public enum Alignment { right(SwingConstants.RIGHT), left(SwingConstants.LEFT), leading(SwingConstants.LEADING), trailing( SwingConstants.TRAILING), center(SwingConstants.CENTER), none(-1); private int alignmentConstant; Alignment(int alignmentConstant) { this.alignmentConstant = alignmentConstant; } public int getAlignmentConstant() { return alignmentConstant; } } }