package asdx;
import asd.*;
import java.io.*;
import java.util.*;
import java.awt.*; // Font
import java.awt.event.*; // ActionEvent, ActionListener,
// WindowAdapter, WindowEvent
import javax.swing.*; // JFrame, JPanel
/**
An ASDTester permits a user to test an ASD grammar by parsing
utterances with it. It provides a graphical user interface that
permits initialization of a parse, stepping the parse, and displaying
the phrase structure as the parse proceeds.
Command-line usage:
In an MS-Windows command-line window:
java -cp asddigraphs.jar;asdx.jar;. asdx/ASDTester
Or under UNIX:
java -cp asddigraphs.jar:asdx.jar:. asdx/ASDTester
OR if asddigraphs.jar and asdx.jar have been put in the system classpath:
java asdx/ASDTester
An optional command-line parameter can be used to specify the maximum
number of parsing advance steps before pausing.
Example: java asdx/ASDTester 50000
(The default maximum number of advance steps between pauses is 10000.)
@author James A. Mason
@version 1.06 2002 Jan; 2003 Jul
*/
public class ASDTester
{ public static void main(String[] args)
{ int maxSteps = 0;
if (args.length == 0)
maxSteps = MAXSTEPS;
else
maxSteps = Integer.parseInt(args[0]);
ASDTester thisTester = new ASDTester(maxSteps);
}
ASDTester(int maxSteps)
{ window = new ASDTesterWindow(this);
window.setTitle("Augmented Syntax Diagram Tester - version " + VERSION);
window.setVisible(true);
maximumSteps = maxSteps;
parser = new ASDParser();
}
void advance()
{ if (!grammarLoaded)
{ JOptionPane.showMessageDialog(window,
"No grammar file is currently loaded.");
window.clearGrammarFileNameField();
return;
}
if (utterance == null || utterance.equals("")) return;
String advanceResult = parser.advance();
if (advanceResult.equals(parser.QUIT))
{ window.getOutputPane().append(
"\nParse quit after " + stepsThisTry + " new and "
+ steps + " total advance steps,\n"
+ "and " + backupStepsThisTry + " new and "
+ backupSteps + " total backup steps, leaving structure:\n");
showPhraseStructure();
// prepare for an attempt at an alternative parse
stepsThisTry = 0;
backupStepsThisTry = 0;
stepsSincePause = 0;
}
else if (advanceResult.equals(parser.SUCCEED))
{ steps++;
stepsThisTry++;
stepsSincePause++;
window.getOutputPane().append(
"\n" + steps + " - parse advanced to:\n");
showPhraseStructure();
if (parser.done())
{ window.getOutputPane().append(
"\nSuccessful parse after " + stepsThisTry + " new and "
+ steps + " total advance steps,\n"
+ "and " + backupStepsThisTry + " new and "
+ backupSteps + " total backup steps.\n");
// prepare for an attempt at an alternative parse
stepsThisTry = 0;
backupStepsThisTry = 0;
stepsSincePause = 0;
}
}
else if (advanceResult.equals(parser.NOADVANCE))
{ if (parser.backup())
{ ++backupSteps; ++backupStepsThisTry;
window.getOutputPane().append("\nparse backed up to:\n");
showPhraseStructure();
}
else
{ window.getOutputPane().append(
"\nParse failed after " + stepsThisTry + " new and "
+ steps + " total advance steps,\n"
+ "and " + backupStepsThisTry + " new and "
+ backupSteps + " total backup steps, leaving structure:\n");
showPhraseStructure();
// prepare for an attempt at an alternative parse
stepsThisTry = 0;
backupStepsThisTry = 0;
stepsSincePause = 0;
}
}
else // this shouldn't occur
window.getOutputPane().append(
"\nInvalid result of ASDParser advance(maxSteps):"
+ advanceResult + "\n");
} // end advance
void allParses()
{ if (!grammarLoaded)
{ JOptionPane.showMessageDialog(window,
"No grammar file is currently loaded.");
window.clearGrammarFileNameField();
return;
}
if (utterance == null || utterance.equals("")) return;
while (completeParse())
;
}
boolean completeParse()
{ if (!grammarLoaded)
{ JOptionPane.showMessageDialog(window,
"No grammar file is currently loaded.");
window.clearGrammarFileNameField();
return false;
}
if (utterance == null || utterance.equals(""))
return false;
String advanceResult; // SUCCEED, NOADVANCE, or QUIT
while(stepsSincePause < maximumSteps)
{ advanceResult = parser.advance();
if (advanceResult.equals(parser.QUIT))
{ window.getOutputPane().append(
"\nParse quit after " + stepsThisTry + " new and "
+ steps + " total advance steps,\n"
+ "and " + backupStepsThisTry + " new and "
+ backupSteps + " total backup steps, leaving structure:\n");
showPhraseStructure();
// prepare for an attempt at an alternative parse
stepsThisTry = 0;
backupStepsThisTry = 0;
stepsSincePause = 0;
return false;
}
else if (advanceResult.equals(parser.SUCCEED))
{ ++steps;
++stepsThisTry;
++stepsSincePause;
if (parser.done())
{ window.getOutputPane().append(
"\nSuccessful parse in " + stepsThisTry + " new and "
+ steps + " total advance steps,\n"
+ "and " + backupStepsThisTry + " new and "
+ backupSteps + " total backup steps.\n");
showPhraseStructure();
// prepare for an attempt at an alternative parse
stepsThisTry = 0;
backupStepsThisTry = 0;
stepsSincePause = 0;
return true;
}
}
else if (advanceResult.equals(parser.NOADVANCE))
{ if (parser.backup())
{ ++backupSteps; ++backupStepsThisTry;
}
else
{ window.getOutputPane().append(
"\nParse failed after " + stepsThisTry + " new and "
+ steps + " total advance steps,\n"
+ "and " + backupStepsThisTry + " new and "
+ backupSteps + " total backup steps, leaving structure:\n");
showPhraseStructure();
// prepare for an attempt at an alternative parse
stepsThisTry = 0;
backupStepsThisTry = 0;
stepsSincePause = 0;
return false;
}
}
else // this shouldn't happen
{ window.getOutputPane().append(
"Invalid result of ASDParser advance(maxSteps):"
+ steps);
return false;
}
}
// JOptionPane.showMessageDialog(window,
// "Attempt to Complete parse or to find All Remaining parses "
// + "\npaused after step " + steps + ",\n" + maximumSteps
// + " steps since start or last pause."
// +"\nIt can be resumed by menu selection.");
window.getOutputPane().append(
"\nAttempt to Complete parse or to find All Remaining parses "
+ "\npaused after advance step " + steps + ", " + maximumSteps
+ " advance steps since start or last pause."
+"\nIt can be resumed by menu selection.\n");
stepsSincePause = 0;
return false;
} // end completeParse
ASDParser getParser() { return parser; }
boolean initializeParse()
{ if (!grammarLoaded)
{ JOptionPane.showMessageDialog(window,
"No grammar file is currently loaded.");
window.clearGrammarFileNameField();
return false;
}
if (utterance == null || utterance.length() == 0)
{ JOptionPane.showMessageDialog(window,
"The phrase to be parsed must not be empty.");
return false;
}
if (expectedTypes == null || expectedTypes.size() == 0)
{ JOptionPane.showMessageDialog(window,
"The list of expected phrase types must not be empty.");
return false;
}
parser.initialize(utterance, expectedTypes);
phraseInitialized = true;
steps = 0;
stepsThisTry = 0;
stepsSincePause = 0;
backupSteps = 0;
backupStepsThisTry = 0;
window.getOutputPane().append("\nInitialized phrase structure:\n");
showPhraseStructure();
return true;
} // end initializeParse
void setExpectedTypeList(String types)
{ if (!grammarLoaded)
{ JOptionPane.showMessageDialog(window,
"No grammar file is currently loaded.");
window.clearGrammarFileNameField();
window.clearExpectedTypeListField();
return;
}
expectedTypes = new ArrayList();
if (types == null || types.length() == 0)
{ JOptionPane.showMessageDialog(window,
"The list of expected phrase types must not be empty.");
return;
}
StringTokenizer st = new StringTokenizer(types);
while (st.hasMoreTokens())
expectedTypes.add(st.nextToken());
if (utterance != null && utterance.length() > 0)
initializeParse();
} // end setExpectedTypeList
void setHyphenAsSpecialCharacter(boolean useHyphen)
{ String special = parser.SPECIALCHARS;
if (useHyphen && special.indexOf("-") < 0)
parser.setSPECIALCHARS("-" + special);
else if (!useHyphen)
{ int hyphenPos = special.indexOf("-");
if (hyphenPos < 0) return; // already no hyphen
parser.setSPECIALCHARS(special.substring(0, hyphenPos)
+ special.substring(hyphenPos + 1));
}
if (grammarLoaded && utterance != null && utterance.length() > 0)
initializeParse();
}
void setSaveUniquelyParsedSubphrases(boolean save)
{ parser.setSaveUniquelyParsedSubphrases(save);
}
void setUtterance(String newUtterance)
{ if (!grammarLoaded)
{ JOptionPane.showMessageDialog(window,
"No grammar file is currently loaded.");
window.clearGrammarFileNameField();
window.clearUtteranceField();
return;
}
utterance = newUtterance;
if (utterance == null || utterance.length() == 0)
{ JOptionPane.showMessageDialog(window,
"The phrase to be parsed must not be empty.");
return;
}
initializeParse();
} // end setUtterance
void setUtteranceNull() { utterance = null; }
void showAboutInfo()
{ // responds to About ASDTester choice in Help menu
JOptionPane.showMessageDialog(window,
"ASDTester version " + VERSION +
"\nAuthor: James A. Mason" +
"\nEmail: jmason@yorku.ca" +
"\nhttp://www.yorku.ca/jmason/");
}
void showBracketedPhrase()
{ if (phraseInitialized)
{ window.getOutputPane().append("\nAfter advance step " + steps + ":");
window.getOutputPane().append("\n" + parser.bracketPhrase() + "\n");
}
else
JOptionPane.showMessageDialog(window,
"No phrase has been initialized.");
}
void showPhraseStructure()
{ if (!phraseInitialized)
JOptionPane.showMessageDialog(window,
"No phrase has been initialized.");
else
{ window.getOutputPane().append("\n" + parser.bracketPhrase());
ASDPhraseNode head = parser.phraseStructure();
ASDPhraseNode currentNode = parser.currentNode();
window.showTree(head, currentNode);
}
} // end showPhraseStructure
void showTree()
{ if (phraseInitialized)
{ window.getOutputPane().append("\nAfter advance step " + steps + ":");
ASDPhraseNode head = parser.phraseStructure();
ASDPhraseNode currentNode = parser.currentNode();
window.showTree(head, currentNode);
}
else
JOptionPane.showMessageDialog(window,
"No phrase has been initialized.");
} // end showTree
boolean useGrammar(String fileName)
{ if (parser.useGrammar(fileName))
{ grammarLoaded = true;
return true;
}
else
{ JOptionPane.showMessageDialog(window,
"Grammar file with that name could not be loaded.");
grammarLoaded = false;
expectedTypes = null;
utterance = null;
return false;
}
} // end useGrammar
static final int MAXSTEPS = 10000;
static final String VERSION = "1.06";
static final Font FONT
= new Font("Monospaced", Font.PLAIN, 12);
private ASDTesterWindow window;
private ASDParser parser;
private boolean grammarLoaded = false;
private boolean phraseInitialized = false;
private ArrayList expectedTypes;
private int steps = 0; // total advance steps since phrase initialization
private int stepsSincePause = 0; // steps since last pause
private int stepsThisTry = 0; // advance steps since phrase
// initialization or last successful parse
private int backupSteps = 0; // total backup steps since phrase init.
private int backupStepsThisTry = 0; // since init. or last succ. parse
private int maximumSteps = MAXSTEPS;
private String utterance;
private class ASDTesterWindow extends JFrame
{ ASDTesterWindow(ASDTester givenTester)
{ tester = givenTester;
grammarFileNameField = new JTextField(40);
grammarFileNameField.addActionListener(
new GrammarFileNameFieldListener(this));
expectedTypeListField = new JTextField(40);
expectedTypeListField.addActionListener(
new ExpectedTypeListFieldListener(this));
utteranceField = new JTextField(40);
utteranceField.addActionListener(
new UtteranceFieldListener(this));
hyphenAsLexicalItemBox = new JCheckBox(
"Treat hyphens (-) in words as separate lexical items");
hyphenAsLexicalItemBox.addActionListener(
new HyphenAsLexicalItemBoxListener(this));
hyphenAsLexicalItemBox.setSelected(false);
uniquelyParsedSubphrasesBox = new JCheckBox(
"Save all uniquely-parsed subphrases");
uniquelyParsedSubphrasesBox.addActionListener(
new UniquelyParsedSubphrasesBoxListener(this));
uniquelyParsedSubphrasesBox.setSelected(true);
JPanel pane = new JPanel();
pane.setLayout(
new BoxLayout(pane, BoxLayout.Y_AXIS));
pane.add(
new LabeledTextField("Grammar file: ", grammarFileNameField));
pane.add(
new LabeledTextField("Expected types:", expectedTypeListField));
pane.add(
new LabeledTextField("Phrase parsed: ", utteranceField));
middlePanel = new JPanel();
middlePanel.add(hyphenAsLexicalItemBox);
middlePanel.add(uniquelyParsedSubphrasesBox);
middlePanel.setMaximumSize(new Dimension(800, 20));
pane.add(middlePanel);
outputPane = new JTextArea();
outputPane.setMinimumSize(new Dimension(DEFAULT_WIDTH,
DEFAULT_HEIGHT));
outputPane.setFont(ASDTester.FONT);
OutputPaneMenu menu = new OutputPaneMenu(outputPane, tester);
MouseListener popupListener = new PopupListener(menu);
outputPane.addMouseListener(popupListener);
pane.add(new JScrollPane(outputPane));
getContentPane().add(pane, BorderLayout.CENTER);
addWindowListener(new WindowCloser(this));
// listens for window closing events (see below)
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
ActionMenu aMenu = new ActionMenu(this);
aMenu.setMnemonic(KeyEvent.VK_A);
menuBar.add(aMenu);
HelpMenu hMenu = new HelpMenu(this);
hMenu.setMnemonic(KeyEvent.VK_H);
menuBar.add(hMenu);
} // end ASDTesterWindow(ASDTester givenTester)
void clearExpectedTypeListField() { expectedTypeListField.setText(""); }
void clearGrammarFileNameField() { grammarFileNameField.setText(""); }
void clearUtteranceField() { utteranceField.setText(""); }
JTextField getExpectedTypeListField() { return expectedTypeListField; }
JTextField getGrammarFileNameField() { return grammarFileNameField; }
JTextArea getOutputPane() { return outputPane; }
ASDTester getTester() { return tester; }
JTextField getUtteranceField() { return utteranceField; }
void grammarFileNameFieldChanged()
{ clearUtteranceField();
tester.setUtteranceNull();
if (!tester.useGrammar(grammarFileNameField.getText().trim()))
// grammar file was not loaded
{ clearExpectedTypeListField();
// Note: the grammarFileNameField is intentionally NOT
// reset to empty here, so the user can edit the incorrect
// file name if desired.
return;
}
Set expectedTypes = tester.getParser().lexicon().phraseTypes();
String expected = "";
for (Iterator it = expectedTypes.iterator(); it.hasNext(); )
{ String type = (String)it.next();
expected += type + " ";
}
expectedTypeListField.setText(expected);
expectedTypeListFieldChanged();
} // end grammarFileNameChanged
void expectedTypeListFieldChanged()
{ tester.setExpectedTypeList(expectedTypeListField.getText().trim());
}
void hyphenAsLexicalItemBoxChanged()
{ tester.setHyphenAsSpecialCharacter(
hyphenAsLexicalItemBox.isSelected());
}
void uniquelyParsedSubphrasesBoxChanged()
{ tester.setSaveUniquelyParsedSubphrases(
uniquelyParsedSubphrasesBox.isSelected());
}
void utteranceFieldChanged()
{ tester.setUtterance(utteranceField.getText().trim());
}
/**
Displays the tree rooted at the given head node,
with node currentNode indicated by an asterisk and an arrow.
@param head the header node of the phrase structure
@param currentNode the current node at the top level
in the phrase structure
*/
void showTree(ASDPhraseNode head, ASDPhraseNode currentNode)
{ showTreeMark(head, "", currentNode);
outputPane.append("\n");
} // end showTree
/**
Displays the portion of the tree starting at the
given node and indented with the given indentString as
prefix for each line that does not represent a top-
level node. Top-level nodes are prefixed with three
blanks or, in the case of the given aNode, an asterisk
and an arrow whose purpose is to indicate the node
which is the current node during a parse.
@param indentString prefix for indenting of the
current subtree
@param aNode the node to be marked with an arrow
*/
private void showTreeMark(ASDPhraseNode givenNode, String indentString,
ASDPhraseNode markNode)
{ outputPane.append("\n");
if (givenNode == markNode)
outputPane.append("*->");
else
outputPane.append(" ");
outputPane.append(indentString + givenNode.word() + " ");
if (givenNode.instance() != null)
outputPane.append(givenNode.instance().instance());
else
outputPane.append("nil");
if (givenNode.subphrase() != null)
showTreeMark(givenNode.subphrase(),indentString + " ",
markNode);
if (givenNode.nextNode() != null)
showTreeMark(givenNode.nextNode(), indentString, markNode);
} // end showTreeMark
static final int DEFAULT_WIDTH = 800; // window width
static final int DEFAULT_HEIGHT = 600; // window height
private ASDTester tester;
private JTextField grammarFileNameField;
private JTextField expectedTypeListField;
private JTextField utteranceField;
private JCheckBox hyphenAsLexicalItemBox;
private JCheckBox uniquelyParsedSubphrasesBox;
private JPanel middlePanel;
private JTextArea outputPane;
} // end class ASDTesterWindow
private class LabeledTextField extends JPanel
{ LabeledTextField(String labelText, JTextField textField)
{ setMaximumSize(new Dimension(800,10));
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
JLabel label = new JLabel(labelText);
textField.setFont(ASDTester.FONT);
this.add(label);
this.add(textField);
}
} // end class LabeledTextField
/**
An instance defines what should happen when a window
closes.
*/
class WindowCloser extends WindowAdapter
{ WindowCloser(ASDTesterWindow w)
{ window = w;
}
public void windowClosing(WindowEvent e)
{
System.exit(0); // stop the program
}
ASDTesterWindow window;
} // end class WindowCloser
private class GrammarFileNameFieldListener implements ActionListener
{
GrammarFileNameFieldListener(ASDTesterWindow w)
{ window = w;
}
public void actionPerformed(ActionEvent e)
{ window.grammarFileNameFieldChanged();
}
private ASDTesterWindow window;
} // end class GrammarFileNameFieldListener
private class ExpectedTypeListFieldListener implements ActionListener
{
ExpectedTypeListFieldListener(ASDTesterWindow w)
{ window = w;
}
public void actionPerformed(ActionEvent e)
{ window.expectedTypeListFieldChanged();
}
private ASDTesterWindow window;
} // end class ExpectedTypeListFieldListener
private class UtteranceFieldListener implements ActionListener
{
UtteranceFieldListener(ASDTesterWindow w)
{ window = w;
}
public void actionPerformed(ActionEvent e)
{ window.utteranceFieldChanged();
}
private ASDTesterWindow window;
} // end class UtteranceFieldListener
private class HyphenAsLexicalItemBoxListener implements
ActionListener
{ HyphenAsLexicalItemBoxListener(
ASDTesterWindow w)
{ window = w;
}
public void actionPerformed(ActionEvent e)
{ window.hyphenAsLexicalItemBoxChanged();
}
private ASDTesterWindow window;
} // end class HyphenAsLexicalItemBoxListener
private class UniquelyParsedSubphrasesBoxListener implements
ActionListener
{ UniquelyParsedSubphrasesBoxListener(
ASDTesterWindow w)
{ window = w;
}
public void actionPerformed(ActionEvent e)
{ window.uniquelyParsedSubphrasesBoxChanged();
}
private ASDTesterWindow window;
} // end class UniquelyParsedSubphrasesBoxListener
private class OutputPaneMenu extends JPopupMenu implements ActionListener
{ OutputPaneMenu(JTextArea p, ASDTester t)
{ pane = p;
tester = t;
setInvoker(pane);
JMenuItem initializeItem = new JMenuItem("Initialize parse");
initializeItem.addActionListener(this);
add(initializeItem);
addSeparator();
JMenuItem advanceItem = new JMenuItem("Advance one step");
advanceItem.addActionListener(this);
add(advanceItem);
JMenuItem completeParseItem = new JMenuItem("Complete parse");
completeParseItem.addActionListener(this);
add(completeParseItem);
JMenuItem allParsesItem = new JMenuItem("All remaining parses");
allParsesItem.addActionListener(this);
add(allParsesItem);
addSeparator();
JMenuItem showBracketsItem = new JMenuItem("Show bracketed phrase");
showBracketsItem.addActionListener(this);
add(showBracketsItem);
JMenuItem showTreeItem = new JMenuItem("Show phrase structure tree");
showTreeItem.addActionListener(this);
add(showTreeItem);
addSeparator();
JMenuItem selectAllItem = new JMenuItem("Select all");
selectAllItem.addActionListener(this);
add(selectAllItem);
JMenuItem copyItem = new JMenuItem("Copy selection");
copyItem.addActionListener(this);
add(copyItem);
addSeparator();
JMenuItem clearItem = new JMenuItem("Erase output pane");
clearItem.addActionListener(this);
add(clearItem);
} // end OutputPaneMenu(JTextArea p, ASDTester t)
public void actionPerformed(ActionEvent e)
{ if (tester == null) return;
String command = e.getActionCommand();
if (command.equals("Initialize parse"))
tester.initializeParse();
else if (command.equals("Advance one step"))
tester.advance();
else if (command.equals("All remaining parses"))
tester.allParses();
else if (command.equals("Complete parse"))
tester.completeParse();
else if (command.equals("Show bracketed phrase"))
tester.showBracketedPhrase();
else if (command.equals("Show phrase structure tree"))
tester.showTree();
else if (command.equals("Select all"))
{ pane.requestFocus();
pane.selectAll();
}
else if (command.equals("Copy selection"))
pane.copy();
else if (command.equals("Erase output pane"))
pane.setText("");
} // end actionPerformed
ASDTester tester;
JTextArea pane; // the pane to which the menu is attached.
} // end class OutputPaneMenu
class ActionMenu extends JMenu implements ActionListener
{ ActionMenu(ASDTesterWindow w)
{ super("Action");
window = w;
tester = window.getTester();
outputPane = window.getOutputPane();
JMenuItem initializeMenuItem = new JMenuItem("Initialize parse",
KeyEvent.VK_I);
initializeMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_I, ActionEvent.ALT_MASK));
add(initializeMenuItem);
initializeMenuItem.addActionListener(this);
JMenuItem advanceMenuItem = new JMenuItem("Advance one Step",
KeyEvent.VK_S);
advanceMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_S, ActionEvent.ALT_MASK));
add(advanceMenuItem);
advanceMenuItem.addActionListener(this);
JMenuItem completeParseMenuItem = new JMenuItem("Complete Parse",
KeyEvent.VK_P);
completeParseMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_P, ActionEvent.ALT_MASK));
add(completeParseMenuItem);
completeParseMenuItem.addActionListener(this);
JMenuItem remainingMenuItem = new JMenuItem("All Remaining Parses",
KeyEvent.VK_R);
remainingMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_R, ActionEvent.ALT_MASK));
remainingMenuItem.addActionListener(this);
add(remainingMenuItem);
addSeparator();
JMenuItem showBracketsMenuItem = new JMenuItem(
"Show bracketed phrase", KeyEvent.VK_B);
showBracketsMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_B, ActionEvent.ALT_MASK));
showBracketsMenuItem.addActionListener(this);
add(showBracketsMenuItem);
JMenuItem showTreeMenuItem = new JMenuItem(
"Show phrase structure tree", KeyEvent.VK_T);
showTreeMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_T, ActionEvent.ALT_MASK));
showTreeMenuItem.addActionListener(this);
add(showTreeMenuItem);
addSeparator();
JMenuItem copyAllMenuItem = new JMenuItem(
"Select All of output pane", KeyEvent.VK_A);
copyAllMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_A, ActionEvent.CTRL_MASK));
copyAllMenuItem.addActionListener(this);
add(copyAllMenuItem);
JMenuItem copySelectionMenuItem = new JMenuItem("Copy Selection",
KeyEvent.VK_C);
copySelectionMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_C, ActionEvent.CTRL_MASK));
copySelectionMenuItem.addActionListener(this);
add(copySelectionMenuItem);
addSeparator();
JMenuItem eraseMenuItem = new JMenuItem("Erase output pane",
KeyEvent.VK_E);
eraseMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_E, ActionEvent.CTRL_MASK));
eraseMenuItem.addActionListener(this);
add(eraseMenuItem);
}
/**
Listens for menu item events.
*/
public void actionPerformed(ActionEvent e)
{ String command = e.getActionCommand();
if (command.equals("Initialize parse"))
tester.initializeParse();
else if (command.equals("Advance one Step"))
tester.advance();
else if (command.equals("Complete Parse"))
tester.completeParse();
else if (command.equals("All Remaining Parses"))
tester.allParses();
else if (command.equals("Show bracketed phrase"))
tester.showBracketedPhrase();
else if (command.equals("Show phrase structure tree"))
tester.showTree();
else if (command.equals("Select All of output pane"))
{ outputPane.requestFocus();
outputPane.selectAll();
}
else if (command.equals("Copy Selection"))
outputPane.copy();
else if (command.equals("Erase output pane"))
outputPane.setText("");
}
ASDTester tester;
ASDTesterWindow window;
JTextArea outputPane;
} // end class ActionMenu
class HelpMenu extends JMenu implements ActionListener
{ HelpMenu(ASDTesterWindow w)
{ super("Help");
window = w;
tester = window.getTester();
JMenuItem aboutMenuItem = new JMenuItem("About ASDTester",
KeyEvent.VK_A);
add(aboutMenuItem);
aboutMenuItem.addActionListener(this);
}
/**
Listens for menu item events.
*/
public void actionPerformed(ActionEvent e)
{ String command = e.getActionCommand();
if (command.equals("About ASDTester"))
tester.showAboutInfo();
}
ASDTester tester;
ASDTesterWindow window;
} // end class HelpMenu
} // end class ASDTester
/**
This class can be used by any others in the asdx package,
to pop up a given menu in response to a right mouse click.
*/
class PopupListener extends MouseAdapter
{ PopupListener(JPopupMenu m)
{ menu = m;
}
public void mousePressed(MouseEvent e)
{ maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e)
{ maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e)
{ if (e.isPopupTrigger())
menu.show(e.getComponent(), e.getX(), e.getY());
}
private JPopupMenu menu;
} // end class PopupListener