import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
 *
 * GUI zum Verarbeiten von Termen
 * Hier sollte nichts verändert werden.
 *
 * @version 1.0 vom 05.11.2020
 * @author Rainer Helfrich
 */

public class TermFrame extends JFrame 
{
    private JTextField jTextTerm = new JTextField();
    private JTextArea jTextTree = new JTextArea("");
    private JTextField jTextErgebnis = new JTextField();

    public TermFrame() { 
        super();
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        int frameWidth = 408; 
        int frameHeight = 580;
        setSize(frameWidth, frameHeight);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (d.width - getSize().width) / 2;
        int y = (d.height - getSize().height) / 2;
        setLocation(x, y);
        setTitle("Termauswertung");
        setResizable(true);
        Container cp = getContentPane();
        cp.setLayout(null);

        jTextTerm.setBounds(96, 8, 289, 25);
        jTextTerm.setFont(new Font("Courier New", Font.PLAIN, 12));
        cp.add(jTextTerm);
        JScrollPane jTextArea1ScrollPane = new JScrollPane(jTextTree);
        jTextArea1ScrollPane.setBounds(8, 40, 377, 465);
        jTextTree.setEditable(false);
        jTextTree.setFont(new Font("Courier New", Font.PLAIN, 12));
        cp.add(jTextArea1ScrollPane);
        jTextErgebnis.setBounds(96, 512, 289, 25);
        jTextErgebnis.setEditable(false);
        jTextErgebnis.setFont(new Font("Courier New", Font.PLAIN, 12));
        cp.add(jTextErgebnis);
        JLabel lErgebnis = new JLabel();
        lErgebnis.setBounds(8, 512, 78, 20);
        lErgebnis.setText("Ergebnis:");
        cp.add(lErgebnis);
        JLabel lTerm = new JLabel();
        lTerm.setBounds(8, 8, 78, 20);
        lTerm.setText("Term:");
        cp.add(lTerm);

        jTextTerm.getDocument().addDocumentListener(new DocumentListener() {
                public void changedUpdate(DocumentEvent e) {
                    parseTerm();
                }

                public void removeUpdate(DocumentEvent e) {
                    parseTerm();
                }

                public void insertUpdate(DocumentEvent e) {
                    parseTerm();
                }

            });
        addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                jTextArea1ScrollPane.setSize(getWidth()-31, getHeight()-115);
                jTextErgebnis.setSize(getWidth()-119, 25);
                jTextTerm.setSize(getWidth()-119, 25);
                jTextErgebnis.setLocation(96, getHeight()-68);
                lErgebnis.setLocation(8, getHeight()-68);
            }
        });
        setMinimumSize(new Dimension(300, 300));
        setVisible(true);
    } 

    private void parseTerm()
    {
        jTextTree.setText("");
        jTextErgebnis.setText("");
        try
        {
            Binaerbaum<Symbol> rechenbaum = TermParser.parse(jTextTerm.getText());
            String ausgabe = baumAnzeigen(rechenbaum);
            if (rechenbaum != null)
            {
                TermAuswerter ta = new TermAuswerter();
                int ergebnis = ta.auswerten(rechenbaum);
                jTextErgebnis.setText(String.valueOf(ergebnis));
                ausgabe += "\n=====================================\n\n";
                ausgabe += ta.alsString(rechenbaum);
            }
            jTextTree.setText(ausgabe);
        }
        catch(Exception x)
        {
            jTextTree.setText(x.getMessage());
        }
    }

    private String baumAnzeigen(Binaerbaum b)
    {
        if (b == null)
        {
            return "(leerer Baum)";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(b.daten+"\n");
        if (b.links != null || b.rechts != null)
        {
            baumAnzeigen(b.links, "", true, sb);
            baumAnzeigen(b.rechts, "", false, sb);
        }
        return sb.toString();
    }

    private void baumAnzeigen(Binaerbaum b, String indent, boolean left, StringBuilder sb)
    {
        sb.append(indent + (left ? "\u251C " : "\u2514 "));
        if (b != null)
        {
            sb.append(b.daten+"\n");
            if (b.links != null || b.rechts != null)
            {
                indent += (left ? "\u2502 " : "  ");
                baumAnzeigen(b.links, indent, true, sb);
                baumAnzeigen(b.rechts, indent, false, sb);
            }
        }
        else
        {
            sb.append("\n");
        }
    }

} 
