package espial.demo.phoneapp;

import espial.awt.item.*;
import espial.util.*;
import espial.image.*;

import espial.datasource.list.*;

import java.awt.*;

/**
 * This is a specialized data source which stored <code>PhoneDirRecord</code>
 * entries.  This implementation stores the list in memory and as
 * a result the data is non-persistent.  The idea here is that by 
 * re-implementing this class it is possible to modify the backend data source for
 * the phone directory without the directory being any wiser.
 * <p>
 * We rely on the <code>DefaultListDataSource</code> object to manage the data in
 * memory.
 */
public class PhoneDirListDataSource extends DefaultListDataSource {
	
	ImageBundle m_icon;
	
	public PhoneDirListDataSource (ImageBundle icon) {
		m_icon = icon;
	}
	
	/**
	 * Get an instance of espial.awt.item.Item that can render the data
	 * at a specific index.<p>
	 * <b>Developers note:</b> By subclassing this method and setting the 
	 * 					<code>m_renderer</code> member before calling the
	 * 					super class method allows the subclass to override the Item
	 * 					that is used.  <b>Be careful though</b> that the Item the
	 * 					assigns is compatible with the one used by the super class
	 * 					unless you're completely overriding this method.
	 * @return an instance of Item.  This method does not validate index, 
	 *         so it always returns an instance, even though it may draw 
	 *         nothing.
	 */
	public Item getDataRenderer (int ix) {
		if (m_renderer == null) {
			ImageItem it;
			m_renderer = (it = new ImageItem (m_icon.getImage ()));
			it.setImagePosition (ImageItem.LEFT);
		}
		Object data = getData (ix);
		if (data instanceof PhoneDirRecord) {
			m_renderer.setName (((PhoneDirRecord) data).name);
		}
		else {
			m_renderer.setName ("** Bad Record **");
		}
		return m_renderer;
	}
	
}

