package com.ketherware.models; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class TerritoryListModelFilter extends AbstractListModelFilter { // Collection of indices that pass the filtering // criteria, namely, the territories that have the // specified salesperson. protected ArrayList indexList = new ArrayList(); // Current salesperson protected String salesperson; public TerritoryListModelFilter(ListModel delegate) { super(delegate); } public synchronized void setSalesperson(String salesperson) { // Assign the property value and clear the collection this.salesperson = salesperson; indexList.clear(); // Iterate through the model for (int z = 0; z < delegate.getSize(); z++) { // If the salesperson of the element matches // the filter value, add the index as an // element to the index collection Territory terr = (Territory) delegate.getElementAt(z); if (salesperson.equals(terr.salesperson)) indexList.add(new Integer(z)); } // Tell the component that the data have changed this.fireContentsChanged(this, 0, indexList.size()); } public String getSalesperson() { return this.salesperson; } public int getSize() { // Return size of index collection. If no filter // has been set, or the filter refers to a salesman // that does not exist, zero may be returned return indexList.size(); } public Object getElementAt(int index) { // return the filtered data element int newIndex = ((Integer) indexList.get(index)).intValue(); return delegate.getElementAt(newIndex); } } class TestFrame extends JFrame implements ActionListener { JComboBox salesCombo = new JComboBox( TerritoryListModel.getSalespersons()); JList territoryList = new JList(); TerritoryListModel model = new TerritoryListModel(); TerritoryListModelFilter exclusionFilter = new TerritoryListModelFilter(model); public static void main(String[] args) { new TestFrame().setVisible(true); } TestFrame() { super("Sales Territories"); this.setSize(350, 200); this.setLocation(150, 150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add the combo box this.getContentPane().add(salesCombo, BorderLayout.NORTH); salesCombo.addActionListener(this); // Add the list in a scroll pane and set the // model of the list to be the exclusion filter territoryList.setFont(new Font("Dialog", 0, 14)); this.getContentPane().add(new JScrollPane(territoryList), BorderLayout.CENTER); territoryList.setModel(exclusionFilter); actionPerformed(null); } // Invoked when user selects a salesperson from the list public void actionPerformed(ActionEvent event) { // Get the new salesperson from the combo box and // set the value into the filter String salesperson = (String) salesCombo.getSelectedItem(); exclusionFilter.setSalesperson(salesperson); } } class Territory { public String name; public String salesperson; public int sales; Territory(String name, String salesperson, int sales) { this.name = name; this.salesperson = salesperson; this.sales = sales; } public String toString() { return this.name; } } class TerritoryListModel extends AbstractListModel { static String[] salespersons = { "Wanda Pinnick", "Robert Taylor", "Jay Goldberg" }; static Territory[] territories = new Territory[] { new Territory("Winslow County", salespersons[0], 4000 ), new Territory("Kashden County", salespersons[2], 5100 ), new Territory("Beaver Falls County", salespersons[1], 3250 ), new Territory("Hamilton County", salespersons[1], 11216 ), new Territory("Parris County", salespersons[0], 4773 ), new Territory("Belvedere County", salespersons[2], 8820 ), new Territory("Gossage County", salespersons[2], 1230 ), new Territory("Renfrew County", salespersons[2], 7360 ), new Territory("Davis County", salespersons[1], 13080 ) }; public int getSize() { // return size of static data return territories.length; } public Object getElementAt(int index) { // return static data element return territories[index]; } public static String[] getSalespersons() { return salespersons; } }