/*
* File name: GrammarFrame.java (package eas.users.lukas.demos.grammar)
* Author(s): lko
* Java version: 7.0
* Generation date: 30.07.2012 (22:02:05)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.users.demos.grammar;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import eas.math.fundamentalAlgorithms.type2grammars.EarleyGrammar;
import eas.math.fundamentalAlgorithms.type2grammars.EarleyParser;
import eas.miscellaneous.datatypes.JGridBagPanel;
import eas.miscellaneous.system.windowFrames.GeneralDialog;
import eas.startSetup.GlobalVariables;
/**
* @author lko
*/
public class GrammarFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = -9220912956086441504L;
private JGridBagPanel panel = new JGridBagPanel();
private GrammarScheduler scheduler;
private JButton button = new JButton("Refresh - 1");
private JButton button2 = new JButton("Go with selected");
private JButton button3 = new JButton("Parse (type 1/2)...");
private JButton button4 = new JButton("Stop");
// private JButton button2 = new JButton("Choose only selected entry");
private JTextArea area = new JTextArea("", 15, 15);
public GrammarFrame(final String title, final GrammarScheduler fatherSched) throws HeadlessException {
super(title);
this.getContentPane().add(panel);
JPanel panel2 = new JPanel(new GridLayout(4, 1));
panel2.add(button);
panel2.add(button2);
panel2.add(button3);
panel2.add(button4);
panel.addGB(panel2, 2, 2);
// panel.addGB(button2, 2, 2);
JScrollPane scroll = new JScrollPane(area);
panel.addGB(scroll, 2, 0);
button.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
this.scheduler = fatherSched;
if (scheduler.getGrammar().retrieveHighestGrammarType() < 2) {
button3.setEnabled(false);
}
}
public void addComponent(final JComponent component) {
this.panel.add(component);
}
public void setAreaText(final String TextToSet) {
this.area.setText(TextToSet);
this.area.setCaretPosition(0);
}
private void parseProcess() {
GeneralDialog diaAllg = new GeneralDialog(
this,
"",
"Type in word to be checked by Earley parsing (leave field empty for 'lambda')",
GeneralDialog.OK_CANCEL_BUTT,
"",
50,
75,
true);
diaAllg.setVisible(true);
if (diaAllg.getResult().equals(GeneralDialog.OK)) {
EarleyGrammar gr = new EarleyGrammar(scheduler.getGrammar());
EarleyParser p = new EarleyParser(gr, GlobalVariables.getPrematureParameters());
String parseString = diaAllg.getText().replace(",", "").replace(" ", "");
boolean b = p.erkenne(parseString);
if (parseString.length() == 0) {
parseString = "lambda";
}
String erkannt;
if (b) {
erkannt = "the string CAN be produced by the grammar";
} else {
erkannt = "the string CANNOT be produced by the grammar";
}
GeneralDialog diaAllg2 = new GeneralDialog(
this,
"Here comes the result of parsing string '"
+ parseString
+ "' with current grammar:\n \n"
+ b + " (" + erkannt + ")",
"Result of parsing " + parseString,
GeneralDialog.OK_BUTT,
null,
50,
75,
true);
diaAllg2.setVisible(true);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().toString().startsWith("Refresh")) {
this.button.setText("Refresh - " + this.scheduler.refreshWordList());
} else if (e.getActionCommand().toString().startsWith("Go ")) {
if (this.scheduler.continueWithSelectedWords()) {
this.button.setText("Refresh - " + this.scheduler.refreshWordList());
}
} else if (e.getActionCommand().toString().startsWith("Parse")) {
parseProcess();
} else if (e.getActionCommand().toString().startsWith("Stop")) {
this.button4.setText("Continue");
this.scheduler.setPaused(true);
} else if (e.getActionCommand().toString().startsWith("Continue")) {
this.button4.setText("Stop");
this.scheduler.setPaused(false);
}
}
}