if (file == null) {
ButtonBar.this.error("no setup file");
return;
}
StreamTokenizer setup = null;
InputStream is = null;
try {
is = getClass().getResourceAsStream(file);
} catch (Exception e) {
// ignore any errors here
}
// if the resource access fails, try URL
if (is == null)
try {
is = new URL(file).openStream();
} catch (Exception ue) {
ButtonBar.this.error("could not find: " + file);
return;
}
// create a new stream tokenizer to read the file
try {
InputStreamReader ir = new InputStreamReader(is);
setup = new StreamTokenizer(new BufferedReader(ir));
} catch (Exception e) {
ButtonBar.this.error("cannot load " + file + ": " + e);
return;
}
setup.commentChar('#');
setup.quoteChar('"');
fields = new HashMap();
buttons = new HashMap();
choices = new HashMap();
GridBagLayout l = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
panel.setLayout(l);
c.fill = GridBagConstraints.BOTH;
int token;
int ChoiceCount = 0;
JList list;
// parse the setup file
try {
while ((token = setup.nextToken()) != StreamTokenizer.TT_EOF) {
switch (token) {
case StreamTokenizer.TT_WORD:
// reset the constraints
c.gridwidth = 1;
c.weightx = 0.0;
c.weighty = 0.0;
// keyword found, parse arguments
if (setup.sval.equals("button")) {
if ((token = setup.nextToken()) != StreamTokenizer.TT_EOF) {
String descr = setup.sval;
if ((token = setup.nextToken()) != StreamTokenizer.TT_EOF) {
JButton b = new JButton(descr);
buttons.put(b, setup.sval);
b.addActionListener(ButtonBar.this);
l.setConstraints(b, constraints(c, setup));
panel.add(b);
} else
ButtonBar.this.error(descr + ": missing button command");
} else
ButtonBar.this.error("unexpected end of file");
} else if (setup.sval.equals("label")) {
if ((token = setup.nextToken()) != StreamTokenizer.TT_EOF) {
String descr = setup.sval;
JLabel b = new JLabel(descr);
l.setConstraints(b, constraints(c, setup));
panel.add(b);
} else
ButtonBar.this.error("unexpected end of file");
/* choice - new stuff added APS 07-dec-2001 for Choice
* buttons
* Choice info is held in the choices hash. There are two
* sorts of hash entry:
* 1) for each choices button on the terminal, key=choice
* object, value=ID ("C1.", "C2.", etc)
* 2) for each item, key=ID+user's text (eg "C1.opt1"),
* value=user's command
*/
} else if (setup.sval.equals("choice")) {
ChoiceCount++;
String ident = "C" + ChoiceCount + ".";
list = new JList();
choices.put(list, ident);
list.addListSelectionListener(ButtonBar.this);// Choices use ItemListener, not Action
l.setConstraints(list, constraints(c, setup));
panel.add(list);
while ((token = setup.nextToken()) != StreamTokenizer.TT_EOF) {
if (isKeyword(setup.sval)) {// Got command ... Back off.
setup.pushBack();
break;
}
String descr = setup.sval; // This is the hash key.
token = setup.nextToken();
if (token == StreamTokenizer.TT_EOF) {
ButtonBar.this.error("unexpected end of file");
} else {
String value = setup.sval;
if (isKeyword(value)) { // Missing command - complain but continue
System.err.println(descr + ": missing choice command");
setup.pushBack();
break;
}
System.out.println("choice: name='" + descr + "', value='" + value);
list.add(descr, new JLabel(descr));
choices.put(ident + descr, value);
}
}
ButtonBar.this.error("choices hash: " + choices);
} else if (setup.sval.equals("input")) {
if ((token = setup.nextToken()) != StreamTokenizer.TT_EOF) {
String descr = setup.sval;
if ((token = setup.nextToken()) ==
StreamTokenizer.TT_NUMBER) {
int size = (int) setup.nval;
String init = "", command = "";
token = setup.nextToken();
if (isKeyword(setup.sval))
setup.pushBack();
else
command = setup.sval;
token = setup.nextToken();
if (isKeyword(setup.sval)) {
setup.pushBack();
init = command;
} else
init = setup.sval;
JTextField t = new JTextField(init, size);
if (!init.equals(command)) {
buttons.put(t, command);
t.addActionListener(ButtonBar.this);
}
fields.put(descr, t);
l.setConstraints(t, constraints(c, setup));
panel.add(t);
} else
ButtonBar.this.error(descr + ": missing field size");
} else
ButtonBar.this.error("unexpected end of file");
}
break;
default:
ButtonBar.this.error("syntax error at line " + setup.lineno());
}
}
} catch (IOException e) {
ButtonBar.this.error("unexpected error while reading setup: " + e);
}