com.ccg.swing
Class ClassComboBoxModel

java.lang.Object
  extended by javax.swing.AbstractListModel
      extended by com.ccg.swing.ClassComboBoxModel
All Implemented Interfaces:
Serializable, ComboBoxModel, ListModel

public final class ClassComboBoxModel
extends AbstractListModel
implements ComboBoxModel

A ComboBoxModel implementation where each of the available items is a ObjectFactory item.

This class is designed to manage a collection of ObjectFactory objects in a manner that a user can choose from them via a GUI widget. You can build the model in code, but it is primarily intended to be used in conjunction with a resource file (see addFromLookup for details).

Since:
1.0
Version:
$Revision: 1.1 $
Author:
$Author: pkb $
See Also:
ObjectFactory, Serialized Form

Field Summary
 
Fields inherited from class javax.swing.AbstractListModel
listenerList
 
Constructor Summary
ClassComboBoxModel()
          Construction of empty set of choices.
 
Method Summary
 int addFromLookup(Lookup l, String tag)
          Dynamically creates and adds ObjectFactory instances based upon a lookup table.
 void addObjectFactories(ObjectFactory[] ofa)
          Add an array of choices to the model.
 void addObjectFactory(ObjectFactory of)
          Add a single ObjectFactory instance.
 void clear()
          Clears the entire contents of the model.
 Object getElementAt(int i)
          Get a reference to a particular choice.
 ObjectFactory getObjectFactory(String name)
          Searches the model for a ObjectFactory whose class name or "toString" name matches the name specified.
 int getObjectFactoryIndex(String name)
          Searches the model for a ObjectFactory whose class name or "toString" name matches the name specified.
 Object getSelectedItem()
          Get the currently selected ObjectFactory (or null if none).
 int getSize()
          Get number of choices which are available.
 void setSelectedItem(Object val)
          Set the currently selected item (or null if nothing should be selected).
 
Methods inherited from class javax.swing.AbstractListModel
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.ListModel
addListDataListener, removeListDataListener
 

Constructor Detail

ClassComboBoxModel

public ClassComboBoxModel()
Construction of empty set of choices.

This initializes the list with a empty set of choices.

Since:
1.0
Method Detail

addObjectFactory

public void addObjectFactory(ObjectFactory of)
Add a single ObjectFactory instance.

This method adds a new item to the list of available choices.

Parameters:
of - ObjectFactory to add - must not be null.
Since:
1.0

addObjectFactories

public void addObjectFactories(ObjectFactory[] ofa)
Add an array of choices to the model.

This entry is used to add an entire array of menu choices to the model.

Parameters:
ofa - Array of ObjectFactory objects - must not be null and none of the array entries are permitted to be null.
Since:
1.0

clear

public void clear()
Clears the entire contents of the model.

This entry is used to remove ALL entries from the model (leaving no available choices).

Since:
1.0

addFromLookup

public int addFromLookup(Lookup l,
                         String tag)
                  throws IllegalArgumentException
Dynamically creates and adds ObjectFactory instances based upon a lookup table.

This method allows one to dynamically add entries to the model based upon the contents of lookup table. For example, assume one had loaded a LookupProperties object with a property file containing the following:

 ocm.0.class=java.util.Date
 ocm.0.text=Time Stamp
 ocm.1.class=java.lang.Object
 ocm.1.text=New Object
 

The following could then be used to construct and intialize the model:


 ClassComboBoxModel model = new ClassComboBoxModel();
 model.addFromLookup(myLookup,"ocm");
 

You could then create combo boxes and lists in the following manner:


 JList mlist = new JList(model);
 JComboBox mbox = new JComboBox(model);
 

The user would see the text names "Time Stamp" and "New Object" as the available choices, however, as a developer, you would have access to the real ObjectFactory when looking at what the user chose.

The entries must have the form:

 TAG.N.class=CLASS
 TAG.N.text=TEXT
 

Where TAG is used to identify your collection (and must match the 'tag' parameter passed). N is a integer counter starting at 0 and incremented by 1 for each additional choice. CLASS is the fully qualified class name to associate with the entry - typically you will want to make sure the class has a default constructor available. TEXT is the text that you want the user to see.

Parameters:
l - Lookup table to search for entries (must not be null).
tag - The TAG which identifies your collection (must not be null).
Returns:
Number of entries found and added.
Throws:
IllegalArgumentException - If we could not resolve one of the string value(s) retrieved for the TAG.N.class key to a available class within the CLASSPATH.
Since:
1.0

getElementAt

public Object getElementAt(int i)
Get a reference to a particular choice.

Specified by:
getElementAt in interface ListModel
Parameters:
idx - Index to retrieve name of config (in range of [0,getSize()-1].
Returns:
Object as specified index (will ALWAYS be a instance of ObjectFactory.
Since:
1.0

getSize

public int getSize()
Get number of choices which are available.

Specified by:
getSize in interface ListModel
Returns:
Number of available choices.
Since:
1.0

getObjectFactoryIndex

public int getObjectFactoryIndex(String name)
Searches the model for a ObjectFactory whose class name or "toString" name matches the name specified.

Parameters:
name - Name to match (case insensitive) must not be null.
Returns:
Index in model of object which matched, or -1 if no match found.
Since:
1.0

getObjectFactory

public ObjectFactory getObjectFactory(String name)
Searches the model for a ObjectFactory whose class name or "toString" name matches the name specified.

Parameters:
name - Name to match (case insensitive) must not be null.
Returns:
ObjectFactory in model which matches, or null if no match found.
Since:
1.0

setSelectedItem

public void setSelectedItem(Object val)
Set the currently selected item (or null if nothing should be selected).

Specified by:
setSelectedItem in interface ComboBoxModel
Parameters:
val - The ObjectFactory to select from the model (it may either be a reference to a actual ObjectFactory instance), or the string name as recognized by getObjectFactory(String).
See Also:
getSelectedItem()

getSelectedItem

public Object getSelectedItem()
Get the currently selected ObjectFactory (or null if none).

Specified by:
getSelectedItem in interface ComboBoxModel
Returns:
Current value selected (it will ALWAYS be a ObjectFactory instance), or null if nothing selected.
See Also:
setSelectedItem(java.lang.Object)


Copyright 1998-1998-2006 null. All Rights Reserved.