package cn.hopefulme.jfortran.client;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.*;

 public class UI extends JFrame implements ActionListener{

	private JButton btn_compile, btn_run, btn_input;

	private JPanel pl_io, pl_tools;

	private GridBagLayout gbl_all, gbl_io, gbl_tools;
	private GridBagConstraints gbc;

	private JTextField tf_input;

	private JScrollPane sp_code, sp_output;
	private JTextArea ta_code, ta_output;

 	public UI () { 
		super("JFortran-Client");
		setSize(800,600);
		setLocation(200,100);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		// Container and LayoutManager
		gbl_all = new GridBagLayout();
		setLayout(gbl_all);
		gbl_io = new GridBagLayout();
		pl_io = new JPanel(gbl_io);
		gbl_tools = new GridBagLayout();
		pl_tools = new JPanel(gbl_tools);
		gbc = new GridBagConstraints();
		// Buttons
		btn_compile = new JButton("Compile");
		btn_run = new JButton("Run");
		btn_input = new JButton("Input");
		// TextField
		tf_input = new JTextField(30);
		// ScrollPane and TextArea
		ta_code = new JTextArea(35, 40);
		ta_code.setLineWrap(true);
		ta_code.setBackground(new Color(1.0f, 1.0f, 0.8f));
		ta_code.setTabSize(4);
		sp_code = new JScrollPane(ta_code, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		ta_output = new JTextArea(35, 30);
		ta_output.setLineWrap(true);
		ta_output.setEditable(false);
		ta_output.setBackground(Color.BLACK);
		ta_output.setForeground(Color.GREEN);
		sp_output = new JScrollPane(ta_output, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		// layout
		gbc.fill=GridBagConstraints.BOTH;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbc.weighty	= 1.0;
		gbl_all.setConstraints(pl_io, gbc);
		add(pl_io);
		add(pl_tools);
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridwidth = 1;
		gbc.weightx = 1.0;
		gbl_io.setConstraints(sp_code, gbc);
		pl_io.add(sp_code);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbl_io.setConstraints(sp_output, gbc);
		pl_io.add(sp_output);
		pl_tools.add(btn_compile);
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 1.0;
		gbc.gridwidth = 1;
		gbl_tools.setConstraints(tf_input, gbc);
		pl_tools.add(tf_input);
		pl_tools.add(btn_input);
		pl_tools.add(btn_run);
		// add listener
		btn_compile.setActionCommand("compile");
		btn_run.setActionCommand("run");
		btn_input.setActionCommand("input");
		btn_compile.addActionListener(this);
		btn_run.addActionListener(this);
		btn_input.addActionListener(this);
		setResizable(false);
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		switch(event.getActionCommand()) {
			case "compile":
				Client.start(new Client.Callback() {
					@Override
					public void output(String msg) {
						ta_output.append(msg + "\n");
					}
					@Override
					public void connected() {
						Client.sendCommand("compile");
						Client.sendCode(ta_code.getText());
					}
					@Override
					public void connectFailed() {
						ta_output.append("Connected Failed!\n");
					}
				});
				break;
			case "run":
				Client.start(new Client.Callback() {
					@Override
					public void output(String msg) {
						ta_output.append(msg + "\n");
					}
					@Override
					public void connected() {
						Client.sendCommand("run");
						Client.sendCode(ta_code.getText());
					}
					@Override
					public void connectFailed() {
						ta_output.append("Connected Failed!\n");
					}
				});
				break;
			case "input":

				break;
		}
	}

}

@@ -128,16 +128,82 @@
 nEvent;%0A
+import java.awt.event.KeyAdapter;%0Aimport java.awt.event.KeyEvent;%0A
 import j
@@ -1183,16 +1183,221 @@
 ld(30);%0A
+%09%09tf_input.addKeyListener(new KeyAdapter() %7B%0A%09%09%09@Override%0A%09%09%09public void keyReleased(KeyEvent e) %7B%0A%09%09%09%09if(e.getKeyCode() == KeyEvent.VK_ENTER) %7B%0A%09%09%09%09%09Client.sendInput(tf_input.getText());%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D);%0A
 %09%09// Scr
@@ -3212,35 +3212,29 @@
 %09%09%09%09
-ta_output.append(msg + %22%5Cn%22
+updateOutput(msg+'%5Cn'
 );%0A%09
@@ -3427,32 +3427,28 @@
 %7B%0A%09%09%09%09%09%09
-ta_output.append
+updateOutput
 (%22Connec
@@ -3609,35 +3609,29 @@
 %09%09%09%09
-ta_output.append(msg + %22%5Cn%22
+updateOutput(msg+'%5Cn'
 );%0A%09
@@ -3824,24 +3824,20 @@
 %09%09%09%09
-ta_output.append
+updateOutput
 (%22Co
@@ -3904,23 +3904,207 @@
 t%22:%0A
-%0A%09%09%09%09break;%0A%09%09%7D
+%09%09%09%09Client.sendInput(tf_input.getText());%0A%09%09%09%09break;%0A%09%09%7D%0A%09%7D%0A%0A%09private void updateOutput(String output) %7B%0A%09%09ta_output.append(output + %22%5Cn%22);%0A%09%09ta_output.setCaretPosition(ta_output.getText().length());
 %0A%09%7D%0A

update client