import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class DictionaryTester extends Applet
implements ItemListener {
Dictionary dictionary;
DictionaryVector dictionaryVector;
TextArea result;
Checkbox arrayBox;
Checkbox vectorBox;
CheckboxGroup ops;
Panel opsPad;
Choice fileURL;
Label label;
String datafile[] = { "datafile1.html",
"datafile2.html",
"datafile3.html",
"datafile4.html" };
public void init() {
opsPad = new Panel();
opsPad.setLayout(new GridLayout(2,1));
ops = new CheckboxGroup();
arrayBox = new Checkbox("Array", ops, true);
vectorBox = new Checkbox("Vector", ops, false);
opsPad.add(arrayBox);
opsPad.add(vectorBox);
result = new TextArea(15,80);
label = new Label("Test Case");
fileURL = new Choice();
// add items to fileURL list
for (int i = 0; i < datafile.length; i++)
fileURL.add(datafile[i]);
// add choice lists to window
add(opsPad);
add(label);
add(fileURL);
add(result);
// register to receive item events
fileURL.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie) {
try {
URL url = new URL("http://personal.cityu.edu.hk/~dcywchan/2003dco10103/" +
fileURL.getSelectedItem());
URLConnection data = url.openConnection();
BufferedReader inFile = new BufferedReader(new InputStreamReader(data.getInputStream()));
if (arrayBox.getState()) {
dictionary = new Dictionary();
} else {
dictionaryVector = new DictionaryVector();
}
String textLine;
//process every line in the file
while((textLine = inFile.readLine()) != null) {
//use StringTokenizer to break line into words
StringTokenizer st = new StringTokenizer(textLine, "_");
while (st.hasMoreTokens()) {
//add every word
if (arrayBox.getState()) {
dictionary.addWord(st.nextToken());
} else {
dictionaryVector.addWord(st.nextToken());
}
}
}
if (arrayBox.getState()) {
result.setText("");
result.setText("Dictionary using Array: " + '\n' + dictionary.toString());
} else {
result.setText("");
result.setText("Dictionary using Vector " + '\n' + dictionaryVector.toString());
}
inFile.close();
} catch(MalformedURLException mue) {}
catch(IOException ioe) {}
}
}