4. DataSource.

Listing 4. DataSource.


import espial.awt.item.*;
import espial.util.*;
import espial.image.*;
import espial.datasource.list.*;
import java.awt.*;

/** A specialized data source which stored PhoneDirRecord 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.
* We rely on the DefaultListDataSource 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.
    *   Developers note: By subclassing this method and setting the 
    *   m_renderer member before calling the superclass method allows the
    *   subclass to override the Item that is used. Be careful though that
    *   the Item the assigns is compatible with the one used by the
    *   superclass 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;
    }
}