package com.onaro.sanscreen.client.view.tabular; import org.apache.commons.lang3.StringUtils; import org.eclipse.core.runtime.IAdaptable; 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.renderers.SimpleTableCellRenderer; public abstract class EnumTabularColumn extends AdaptableTabularColumn { private static final long serialVersionUID = 1L; protected EnumPresentation enumPresentation; /** * Used for caching the enum attributes map so it wouldn't need to be retrieved every time getValueAt is called */ protected EnumPresentation.AttributesMap enumAttributesMap; protected String enumType; protected SimpleTableCellRenderer simpleTableCellRenderer; protected boolean showUnknownValues = false; public EnumTabularColumn(String id, String name, String enumType) { this(id, name, SANscreenClientPlugin.getDefault().getEnumPresentation(), enumType); } public EnumTabularColumn(String id, EnumPresentation enumPresentation, String enumType) { this(id, StringUtils.EMPTY, enumPresentation, enumType); } public EnumTabularColumn(String id, String name, EnumPresentation enumPresentation, String enumType) { super(id, EnumPresentation.Attributes.class, name); if (enumPresentation == null) throw new IllegalArgumentException("enumPresentation"); //$NON-NLS-1$ if (StringUtils.isBlank(enumType)) throw new IllegalArgumentException("enumType"); //$NON-NLS-1$ this.enumPresentation = enumPresentation; this.enumType = enumType; EnumPresentation.Attributes[] values = enumPresentation.getValues(enumType); if (values == null || values.length == 0) { throw new IllegalArgumentException("Unknown enum type: " + enumType); //$NON-NLS-1$ } setFilter(new EnumFilter(values)); simpleTableCellRenderer = new SimpleTableCellRenderer(); setCellRenderer(simpleTableCellRenderer); 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) { simpleTableCellRenderer.setHorizontalAlignment(alignment); } public int getAlignment() { return simpleTableCellRenderer.getHorizontalAlignment(); } public boolean isShowUnknownValues() { return showUnknownValues; } public void setShowUnknownValues(boolean showUnknownValues) { this.showUnknownValues = showUnknownValues; } /** * Retrieve the enum value key to use to represent this row * @param rowObj the row object * @return the enum key, null if there's no value for this row * @throws Exception if there's an error retrieving the value for the row */ public abstract String getEnumKeyForRow(IAdaptable rowObj) throws Exception; @Override public EnumPresentation.Attributes getValue(IAdaptable rowObj) throws Exception { String enumKey = getEnumKeyForRow(rowObj); 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; } }