//*************************
package com.wordesign.glossary;
import java.awt.*;
import java.util.Vector;
public class FilteredList extends List
{
private Vector items = new Vector();
private String filter = "";
public FilteredList()
{
super();
}
public FilteredList(Vector items)
{
super();
this.items = (Vector)items.clone();
}
public void add(String item)
{
items.addElement(item);
filter(filter);
}
public void add(Vector items)
{
for (int i = 0; i < items.size(); i++)
{
this.items.addElement(items.elementAt(i));
}
}
public void filter(String filter)
{
super.removeAll();
if (filter != null && !filter.equals(""))
{
filter = filter.toLowerCase();
for (int i = 0; i < items.size(); i++)
{
String item = items.elementAt(i).toString();
if (item.toLowerCase().indexOf(filter) >= 0)
{
super.add(item);
}
}
}
else
{
for (int i = 0; i < items.size(); i++)
super.add(items.elementAt(i).toString());
}
this.filter = filter;
}
}
//*************************
package com.wordesign.glossary;
import java.applet.*;
import java.net.*;
public class GlossaryApplet extends Applet
{
public void init()
{
java.io.InputStream is = null;
try
{
URL url = new URL(getDocumentBase(), "glossary.properties");
is = url.openStream();
}
catch(Exception exception)
{
System.err.println(exception);
}
this.add(new GlossaryPanel(is));
}
}
//*************************
package com.wordesign.glossary;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class GlossaryPanel extends Panel
{
private FilteredList items = new FilteredList();
private TextField filter = new TextField();
private TextArea description = new TextArea();
private Properties glossary = new Properties();
public GlossaryPanel(InputStream is)
{
super();
try
{
glossary.load(is);
}
catch (Exception e)
{
description.setText(e.toString());
}
// setLayout(new GridBagLayout());
setLayout(new BorderLayout());
setupUI();
}
private void setupUI()
{
System.out.println("setupUI()");
add(filter, BorderLayout.NORTH);
add(items, BorderLayout.WEST);
add(description, BorderLayout.CENTER);
filter.addTextListener(
new TextListener() {
public void textValueChanged(TextEvent e) {
TextField tf = (TextField)e.getSource();
items.filter(tf.getText());
}
}
);
items.addItemListener(
new ItemListener() {
public void itemStateChanged(ItemEvent ie) {
int selection = items.getSelectedIndex();
System.out.println("Selected item: "+selection);
Object item = items.getItem(selection);
System.out.println("Selected item: "+item);
System.out.println("Glossary: "+glossary);
description.setText(glossary.get(item.toString()).toString());
}
}
);
Enumeration keys = glossary.keys();
while (keys.hasMoreElements())
{
items.add(keys.nextElement().toString());
}
}
}