package net.katsuster.semu.ui;

import java.lang.reflect.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;

/**
 * 標準出力の代わりに用いるテキスト表示領域です。
 *
 * System.out を置き換えて使います。
 *
 * @author katsuhiro
 */
public class SystemPane extends JPanel {
    //out -> outInner -> outPout -> outPin -> outRead -> outText
    //    `-> System.out
    private static final PipedOutputStream outPout = new PipedOutputStream();
    private static final PrintStream outInner = new PrintStream(outPout);

    private static final PipedInputStream outPin = new PipedInputStream(16384);
    private static final InputStreamReader outRead = new InputStreamReader(outPin);
    private static final JTextArea outText = new JTextArea();
    private static final Thread outDrainer = new Thread(new TextDrainer());

    private static final JScrollPane outScr = new JScrollPane(outText);

    //標準出力の代わりに用いる出力用ストリームです
    public static final PrintStream out = new ForkedPrintStream(outInner, System.out);

    public SystemPane() {
        super(new BorderLayout(), true);

        try {
            outPout.connect(outPin);
        } catch (IOException e) {
            //ignore
        }

        add(outScr);
        outScr.setPreferredSize(new Dimension(320, 240));
        outDrainer.start();
    }

    public void clear() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                outText.setText("");
            }
        });
    }

    static private class TextDrainer implements Runnable {
        public TextDrainer() {
            //do nothing
        }

        @Override
        public void run() {
            try {
                StringBuffer b = new StringBuffer();

                output:
                while (true) {
                    b.setLength(0);
                    do {
                        int ch = SystemPane.outRead.read();
                        if (ch == -1) {
                            //EOF
                            break output;
                        }

                        b.append((char) ch);
                    } while (SystemPane.outRead.ready());

                    try {
                        SwingUtilities.invokeAndWait(new StringAppender(b.toString()));
                    } catch (InterruptedException e) {
                        e.printStackTrace(System.err);
                    } catch (InvocationTargetException e) {
                        e.printStackTrace(System.err);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace(System.err);
            }
        }

        private class StringAppender implements Runnable {
            private String s;

            public StringAppender(String str) {
                s = str;
            }

            public void run() {
                SystemPane.outText.append(s);
            }
        }
    }
}

@@ -1397,24 +1397,47 @@
 nnable() %7B%0D%0A
+            @Override%0D%0A
             
@@ -1528,24 +1528,77 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * out %E3%81%AB%E5%87%BA%E5%8A%9B%E3%81%95%E3%82%8C%E3%81%9F%E6%96%87%E5%AD%97%E3%82%92%E7%94%BB%E9%9D%A2%E3%81%AB%E5%8D%B0%E5%AD%97%E3%81%99%E3%82%8B%E3%82%AF%E3%83%A9%E3%82%B9%E3%81%A7%E3%81%99%E3%80%82%0D%0A     */%0D%0A
     static p
@@ -2777,32 +2777,39 @@
    %7D%0D%0A%0D%0A        
+static 
 private class St
@@ -2961,32 +2961,55 @@
            %7D%0D%0A%0D%0A
+            @Override%0D%0A
             publ

add override annotate.