package com.onaro.sanscreen.client.view.tabular.columns; import java.text.MessageFormat; import org.eclipse.core.runtime.IAdaptable; import com.onaro.client.leekui.runtime.OnaroAdapterUtils; import com.onaro.client.swing.table.functional.IFunction; import com.onaro.client.swing.table.functional.IFunctionGroup; import com.onaro.client.swing.table.functional.IFunctionManager; /** * Class used to retrieve a value to present from a given row {@link IAdaptable}. Instances of this class * are paired with a {@link AbstractColumnValuePresenter} instance to provide a decoupled column retrieval * and presentation pattern in the column {@link CompoundColumn}. Instances of this class can also be used * in the {@link ColumnBuilder} class. * * @param type of the values retrieved by the retriever * * @see AbstractColumnValuePresenter * @see CompoundColumn * @see ColumnBuilder */ public abstract class AbstractColumnValueRetriever { private IFunctionManager functionManager; /** * Retrieve the type of values retrieved by this class. * This value may be null if unknown * @return the value type */ public abstract Class getValueType(); /** * Retrieve the column value for the given row. * @param rowObj the row in the table * @return the value for the column (may be null) */ public abstract V getValue(IAdaptable rowObj); /** * Get the unique identifier to associate with the value. By default, this is the * ID of the rowObj, but subclasses can override to provide different behaviors. * * @param rowObj the row to retrieve the identifier for * @return the identifier for the value retrieved for the row. */ public String getIdentifier(IAdaptable rowObj) { Long id = OnaroAdapterUtils.getAdapter(rowObj, Long.class); if (id != null) { return id.toString(); } return null; } /** * Subclasses should override this method to declare the functions required for this retriever. * @param functionGroup the function group to add the function interfaces to. */ public void getRequiredFunctions(IFunctionGroup functionGroup) { // no functions are required by default } /** * Method used by the CompoundColumn to give the retriever access to the function manager. * @param functionManager */ final void setFunctionManager(IFunctionManager functionManager) { this.functionManager = functionManager; } /** * Get a function for use in the retriever. * * @param type of the object returned by the function * @param type of the function * @param functionInterface interface class for the requested function * @return the function implementation */ protected final > F getRequiredFunction(Class functionInterface) { return getRequiredFunction(functionInterface, null); } /** * Get a function for use in the retriever. * * @param type of the object returned by the function * @param type of the function * @param functionInterface interface class for the requested function * @param identifier identifier to use to look up a particular instance of the function implementation * @return the function implementation */ protected final > F getRequiredFunction(Class functionInterface, Object identifier) { if (functionInterface == null) throw new IllegalArgumentException("functionClass"); //$NON-NLS-1$ F function = functionManager.getFunction(functionInterface, identifier); if (function == null) { String msgPattern = "Function implementation not found for function: {0}:{1}"; //$NON-NLS-1$ String msg = MessageFormat.format(msgPattern, functionInterface.getName(), String.valueOf(identifier)); throw new IllegalStateException(msg); } return function; } }