package data.swing;

import data.*;

import javax.swing.table.*;

/**
  * A {@link util.swing.TableEntryDescriptor} that can be used to model Currencies with a
  * {@link CatalogTableModel}.
  *
  * <p>Essentially this is a {@link DefaultCatalogItemTED} that renders its value column using the Currency to
  * format the values.</p>
  *
  * @author Steffen Zschaler
  * @version 2.0 23/08/1999
  * @since v2.0
  */
public class DefaultCurrencyItemTED extends DefaultCatalogItemTED {

  /**
    * The Currency used for formatting.
    *
    * @serial
    */
  private Currency m_cCurrency;

  /**
    * The cell renderer that renders the value column.
    */
  private transient TableCellRenderer m_tcrValueRenderer;

  /**
    * Internal helper function that gets (and if necessary creates) the cell renderer for the value column.
    *
    * @override Never
    */
  protected TableCellRenderer getValueRenderer() {
    if (m_tcrValueRenderer == null) {
      m_tcrValueRenderer = new CurrencyRenderer (m_cCurrency);
    }

    return m_tcrValueRenderer;
  }

  /**
    * Create a new DefaultCurrencyItemTED.
    *
    * @param c the Currency used for formatting. Must be the same as the Currency modelled by the associated
    * {@link CatalogTableModel}.
    */
  public DefaultCurrencyItemTED (Currency c) {
    super();

    m_cCurrency = c;
  }

  /**
    * @return <code>String.class</code> for the first, <code>{@link NumberValue}.class</code> for the second
    * column.
    * @override Sometimes
    */
  public Class getColumnClass (int nIdx) {
    Class[] acClasses = {String.class, NumberValue.class};
    return acClasses[nIdx];
  }

  /**
    * @return <code>null</code> for the first, {@link #getValueRenderer} for the second column.
    * @override Sometimes
    */
  public TableCellRenderer getCellRenderer (int nIdx) {
    if (nIdx == 1) {
      return getValueRenderer();
    }
    else {
      return super.getCellRenderer (nIdx);
    }
  }
}