Each of the issues contains a 'reported by' user. With the
appropriate transformation, you can create an
EventList
of users from that
EventList
of issues. As issues list is changed,
the users list changes automatically. If your first issue's user is
"jessewilson", then the first element in the derived users list will be
"jessewilson". There will be a simple one-to-one relationship between
the issues list and the users list.
For this kind of arbitrary list transformation, extend the
abstract TransformedList
. By overriding the
get()
method to return an issue's user rather than
the issue itself, you make the issues list look like a users
list!
We're required to write some boilerplate code to complete our
users list transformation. First, we must observe the source issues list
by registering a listener:
source.addListEventListener(this)
. Second, when the
source EventList changes, we forward an equivalent event to our
listeners as well. This is taken care of by calling
updates.forwardEvent()
within the
listChanged()
method.
// glazed lists import ca.odell.glazedlists.*; import ca.odell.glazedlists.event.*; // a simple issues library import ca.odell.issuezilla.*; /** * An IssuesToUserList is a list of users that is obtained by getting * the reporters from the issues list. * * @author <a href="mailto:jesse@odel.on.ca">Jesse Wilson</a> */ public class IssueToUserList extends TransformedList { /** * Construct an IssuesToUserList from an EventList that contains only * Issue objects. */ public IssuesToUserList(EventList source) { super(source); source.addListEventListener(this); } /** * Gets the user at the specified index. */ public Object get(int index) { Issue issue = (Issue)source.get(index); return issue.getReporter(); } /** * When the source issues list changes, propogate the exact same changes * for the users list. */ public void listChanged(ListEvent listChanges) { updates.forwardEvent(listChanges); } }