package com.onaro.sanscreen.client.view.tabular.columns; import javax.swing.table.TableCellRenderer; import org.apache.commons.lang3.StringUtils; import com.onaro.sanscreen.client.SANscreenClientPlugin; import com.onaro.util.enumeration.EnumPresentation; import com.onaro.util.enumeration.EnumPresentation.Attributes; import com.onaro.util.jfc.tables.filter.EnumFilter; import com.onaro.util.jfc.tables.filter.Filter; import com.onaro.util.jfc.tables.renderers.SimpleTableCellRenderer; public class EnumColumnValuePresenter extends AbstractColumnValuePresenter { protected final String enumType; protected final EnumPresentation enumPresentation; protected final SimpleTableCellRenderer renderer; protected boolean showUnknownValues = false; /** * Used for caching the enum attributes map so it wouldn't need to be retrieved every time getValueAt is called */ protected final EnumPresentation.AttributesMap enumAttributesMap; public EnumColumnValuePresenter(String enumType) { this(enumType, SANscreenClientPlugin.getDefault().getEnumPresentation()); } public > EnumColumnValuePresenter(Class enumClass) { this(enumClass, SANscreenClientPlugin.getDefault().getEnumPresentation()); } public > EnumColumnValuePresenter(Class enumClass, EnumPresentation enumPresentation) { this(enumClass.getName(), enumPresentation); } public EnumColumnValuePresenter(String enumType, EnumPresentation enumPresentation) { super(EnumPresentation.Attributes.class); this.enumType = enumType; this.enumPresentation = enumPresentation; this.renderer = new SimpleTableCellRenderer(); this.enumAttributesMap = enumPresentation.getEnumAttributes(enumType); } /** * Set the renderer alignment. This should be one of the SwingConstants. * @see javax.swing.SwingConstants * @param alignment the alignment to set */ public void setAlignment(int alignment) { renderer.setHorizontalAlignment(alignment); } public int getAlignment() { return renderer.getHorizontalAlignment(); } public boolean isShowUnknownValues() { return showUnknownValues; } public void setShowUnknownValues(boolean showUnknownValues) { this.showUnknownValues = showUnknownValues; } protected String getEnumKeyForRow(V value) { if (value instanceof Enum) { return ((Enum)value).name(); } return value == null ? null : value.toString(); } @Override public Attributes getPresentation(V value, String identifier) { String enumKey = getEnumKeyForRow(value); if (StringUtils.isBlank(enumKey)) { return null; } Attributes attributes = enumPresentation.getAttributes(enumAttributesMap, enumType, enumKey); if (attributes == null) { // try again with upper cased attributes = enumPresentation.getAttributes(enumAttributesMap, enumType, enumKey.toUpperCase()); } // If the value is not in the enum presentation and unknown values should be shown if (attributes == null && isShowUnknownValues()) { // Create a new attribute value to represent the key text attributes = new Attributes(enumKey,enumKey); } return attributes; } @Override public Filter createCustomFilter() { EnumPresentation.Attributes[] values = enumPresentation.getValues(enumType); if (values == null || values.length == 0) { throw new IllegalArgumentException("Unknown enum type: " + enumType); //$NON-NLS-1$ } return new EnumFilter(values); } @Override public TableCellRenderer createCustomRenderer() { return renderer; } }