List Models

  • The ComboBoxModel interface extends the ListModel interface.
  • We extend AbstractListModel to use its listener methods.
  • Implementing the interfaces supports Swing interactions.
  • We implement our own methods to enable resource package actions.
ListModel
+ addListDataListener(listener)
+ removeListDataListener(listener)
+ getElementAt(int) : Object
+ getSize() : int
AbstractListModel
# listenerList : EventListenerList
# fireContentsChanged(Object, int, int)
+ addListDataListener(ListDataListener)
+ removeListDataListener(ListDataListener)
ComboBoxModel
+ getSelectedItem() : Object
+ setSelectedItem(Object)
ResourceListModel
ResourceListModel(ServerPanel)
+ getElementAt(int) : Object
+ getSize() : int
* setFilter(ResourceFilter)
* setCategory(Category)
CategoryComboBoxModel
CategoryComboBoxModel(Category)
+ getSize() : int
+ getElementAt(int) : Object
+ getSelectedItem() : Object
+ setSelectedItem(Object)
* setCategory(Category)
class ResourceListModel extends AbstractListModel {
    private Vector listItems = new Vector();
    private Category currentCategory = null;
    private ResourceFilter filter = new AcceptAllFilter();

    public int getSize() {
        return listItems.size();
    }

    public Object getElementAt(int index) {
        return listItems.get(index);
    }

    synchronized void setFilter(ResourceFilter filter) {
        if (this.filter != filter) {
            this.filter = filter;
            redoList(currentCategory);
        }
    }

    synchronized void setCategory(Category category) {
        if (!category.equals(currentCategory)) {
            redoList(category);
        }
    }

    private void redoList(Category category) {
        listItems.removeAllElements();

        Category[] categories = category.getSubCategories();
        Arrays.sort(categories, catComp);
        for (int i = 0; i < categories.length; i++) {
            listItems.add(categories[i]);
        }

        Resource[] resources = category.getResources();
        Arrays.sort(resources, resComp);
        for (int i = 0; i < resources.length; i++) {
            if (filter.accept(resources[i])) {
                listItems.add(resources[i]);
            }
        }
        currentCategory = category;

        fireContentsChanged(this, -1, -1);
    }
}