/*
 *  Copyright (C) 2006 Mikhail A. Kryshen
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id: DemoFrame.java,v 1.3 2006/12/14 15:59:48 mikhail Exp $
 */

package kryshen.tema.demo;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;

import kryshen.tema.*;

/**
 * TEMA demonstation console.
 */
public class DemoFrame extends Frame {
    private TextArea input;
    private TextArea output;
    private TextArea error;
    
    public DemoFrame() {
        super("TEMA demo console");

        GridBagLayout gbl = new GridBagLayout();

        GridBagConstraints c = new GridBagConstraints();

        setLayout(gbl);

        input = new TextArea(25, 40);
        input.setEditable(true);

        output = new TextArea(25, 40);
        output.setEditable(false);

        error = new TextArea(5, 80);
        error.setEditable(false);

        Label l;

        c.insets = new Insets(3, 3, 3, 3);
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = GridBagConstraints.RELATIVE;

        l = new Label("Enter your code and click \"Process\".");

        gbl.setConstraints(l, c);
        add(l);

        Panel buttons = new Panel();
        buttons.setLayout(new FlowLayout(FlowLayout.LEFT));
        
        Button process = new Button("Process");
        Button clear = new Button("Clear");
        Button close = new Button("Close");

        buttons.add(process);
        buttons.add(clear);
        buttons.add(close);

        c.gridwidth = GridBagConstraints.REMAINDER; 

        gbl.setConstraints(buttons, c);
        add(buttons);

        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.CENTER;
        c.gridwidth = GridBagConstraints.RELATIVE;

        l = new Label("Input:");

        gbl.setConstraints(l, c);
        add(l);

        c.gridwidth = GridBagConstraints.REMAINDER; 

        l = new Label("Output:");

        gbl.setConstraints(l, c);
        add(l);

        c.gridwidth = GridBagConstraints.RELATIVE; 

        gbl.setConstraints(input, c);
        add(input);
       
        c.gridwidth = GridBagConstraints.REMAINDER; 

        gbl.setConstraints(output, c);
        add(output);

        l = new Label("Errors:");

        gbl.setConstraints(l, c);
        add(l);

        gbl.setConstraints(error, c);
        add(error);      

        process.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    process();
                }
            });

        clear.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    error.setText("");
                    output.setText("");
                    input.setText("");
                    
                }
            });

        close.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });

        addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    dispose();
                }
            });
        
        URL source = getClass().getResource("demo.template");
        StringBuffer buffer = new StringBuffer();

        try {
            Reader in = new BufferedReader
                (new InputStreamReader(source.openStream(), "UTF-8"));           
            
            for (int ch = in.read(); ch >= 0; ch = in.read()) {
                buffer.append((char)ch);
            }

            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        input.setText(buffer.toString());
    }

    private void process() {
        String s = input.getText();
        
        TemplateReader tr = new TemplateReader
            (new LineNumberReader(new StringReader(s)), "input");
        TemplateParser tp = new TemplateParser();
        StringWriter sw = new StringWriter();

        error.setText("");

        try {
            tp.parse(tr, sw);
        } catch (Exception e) {
            error.setText(e.toString());
        }

        output.setText(sw.toString());
    }
}