/**
*
*/
package de.tutorials;
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.MessageFormat;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListModel;
/**
* @author Tom
*
*/
public class WordFilterExample extends JFrame {
JTextField textField;
FilterableJList list;
final static String DEFAULT_FILTER = "^#.*";
public WordFilterExample() {
super("WordFilterExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
textField = new JTextField(20);
textField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
list
.setFilter(DEFAULT_FILTER.replace("#", textField
.getText()));
list.updateUI();
}
});
list = new FilterableJList(createDummyData());
add(textField, BorderLayout.NORTH);
add(list, BorderLayout.CENTER);
pack();
setVisible(true);
}
private Object[] createDummyData() {
return new Object[] { "a", "b", "ba", "bac", "bbc", "c" };
}
/**
* @param args
*/
public static void main(String[] args) {
new WordFilterExample();
}
class FilterableJList extends JList {
String filter;
FilteredListModel filteredListModel;
int currentFilterMatches;
int[] currentFilterMapping;
String TOOLTIP_MESSAGE = "{0} Items filtered out of {1}";
public FilterableJList(Object[] data) {
super(data);
filteredListModel = new FilteredListModel(super.getModel(), null);
}
public String getFilter() {
return filter;
}
public void setFilter(String filter) {
this.filter = filter;
}
private void doFilter() {
ListModel model = super.getModel();
int modelSize = model.getSize();
int[] tmpMapping = new int[modelSize];
currentFilterMatches = 0;
for (int i = 0; i < modelSize; i++) {
if (model.getElementAt(i).toString().matches(this.filter)) {
tmpMapping[currentFilterMatches++] = i;
}
}
currentFilterMapping = new int[currentFilterMatches];
System.arraycopy(tmpMapping, 0, currentFilterMapping, 0,
currentFilterMatches);
filteredListModel.setFilterMapping(currentFilterMapping);
}
public ListModel getModel() {
if (filter == null || filter.equals("")) {
setToolTipText("");
return super.getModel();
} else {
doFilter();
setToolTipText(MessageFormat.format(TOOLTIP_MESSAGE,
new Object[] { Integer.valueOf(currentFilterMatches),
Integer.valueOf(super.getModel().getSize()) }));
return filteredListModel;
}
}
}
class FilteredListModel extends AbstractListModel {
ListModel delegate;
int[] filterMapping;
public FilteredListModel(ListModel delegate, int[] filterMapping) {
this.delegate = delegate;
this.filterMapping = filterMapping;
}
public int getSize() {
return this.filterMapping != null ? this.filterMapping.length : 0;
}
public Object getElementAt(int index) {
return delegate.getElementAt(filterMapping[index]);
}
public ListModel getDelegate() {
return delegate;
}
public void setDelegate(ListModel delegate) {
this.delegate = delegate;
}
public int[] getFilterMapping() {
return filterMapping;
}
public void setFilterMapping(int[] filterMapping) {
this.filterMapping = filterMapping;
}
}
}