Newer
Older
KisoJikkenNWP / src / main / java / istlab / KisoJikken / JTAConsole.java
package istlab.KisoJikken;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Stack;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;

public class JTAConsole extends JTextArea implements Runnable, WindowListener, KeyListener, MouseListener {
    // static JTAConsole lastWindow;
    // static Point lastActivePoint;
    static Stack<JTAConsole> winStack = new Stack<JTAConsole>();
    JFrame frame;
    Process process;
    Thread thread;
    InputStream instream;
    InputStreamReader isreader;
    BufferedReader reader;
    OutputStream outstream;
    // ProcessBuilder pb;

    // public JTAConsole(ProcessBuilder prob) {
    public JTAConsole(String title) {
        // pb = prob;

        frame = new JFrame(title);
        frame.getContentPane().add(new JScrollPane(this));
        frame.setSize(500, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        frame.addWindowListener(this);
        if (winStack.size() > 0) {
            JTAConsole last = winStack.peek();
            if (last.isVisible()) {
                Point lastFrameP = last.frame.getLocationOnScreen();
                frame.setLocation(lastFrameP.x + 470, lastFrameP.y);
            }
        } else {
            if (Editor.lastOpened != null) {
                Point lastEditorP = Editor.lastOpened.getLocationOnScreen();
                Dimension lastEditorD = Editor.lastOpened.getSize();
                frame.setLocation(lastEditorP.x, lastEditorP.y + lastEditorD.height);
            }

        }
        addKeyListener(this);
        addMouseListener(this);

        // if (lastActivePoint != null){
        // frame.setLocation(lastActivePoint.x+500, lastActivePoint.y);
        // } else if (lastWindow != null && lastWindow.isVisible()){
        // Point lastFrameP = lastWindow.frame.getLocationOnScreen();
        // frame.setLocation(lastFrameP.x+500, lastFrameP.y);
        // }
        // lastWindow = this;
        winStack.push(this);
    }

    public void Systemoutprintln(String s) {
        append(s + "\n");
        int len = getDocument().getLength();
        setCaretPosition(len);
    }

    public void startBR(Process proc) {
        process = proc;
        try {
            reader = new BufferedReader(new InputStreamReader(proc.getInputStream(), "UTF-8"));
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        // isreader = new InputStreamReader(proc.getInputStream());
        outstream = process.getOutputStream();
        String line;
        try {
            // while(true){
            // if (isreader.ready()){
            // char[] buf = new char[1000];
            // isreader.read(buf);
            // }
            // }
            while ((line = reader.readLine()) != null) {
                Systemoutprintln(line);
            }
            process.waitFor();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Systemoutprintln("=== end ===");

        // thread = new Thread(this);
        // thread.start();
    }

    @Override
    public void run() {
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                Systemoutprintln(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        Systemoutprintln("=== end ===");
    }

    public static void main(String[] arg) {
        ProcessBuilder processBuilder = new ProcessBuilder("ping", "localhost");
        Process process = null;
        try {
            process = processBuilder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JTAConsole con = new JTAConsole("Demo");
        con.startBR(process);

    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        try {
            if (outstream != null)
                outstream.close();
            if (instream != null)
                instream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        process.destroyForcibly();
        winStack.remove(this);
        setVisible(false);
        if (!process.isAlive()) {
            Launcher.allProcs.remove(process);
        }
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {
        // JTAConsole.lastActivePoint = frame.getLocationOnScreen();

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == 10) {
            sendLastLine();
        }
        if (e.getKeyCode() > 64 && e.getKeyCode() < 64 + 32) {
            if (e.isControlDown()) {
                System.out.print("CTRL+");
            }
            if (e.isAltDown()) {
                System.out.print("ALT+");
            }
            if (e.isAltGraphDown()) {
                System.out.print("AltGraph+");
            }
            if (e.isMetaDown()) {
                System.out.print("META+");
            }
            System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
        }

        if (e.isControlDown() || e.isAltDown() || e.isAltGraphDown() || e.isMetaDown()) {
            if (e.getKeyCode() == 67 || e.getKeyCode() == 68 || e.getKeyCode() == 87) { // CTRL+C or CTRL+D or Alt-W
                windowClosing(null);
                frame.dispose();
            }
        }
    }

    private void sendLastLine() {
        int start;
        try {
            start = getLineStartOffset(getLineCount() - 1);
            int end = getLineEndOffset(getLineCount() - 1);
            String lastLineText = getText(start, end - start) + "\n";
            // System.out.println(lastLineText);

            byte[] sbyte = lastLineText.getBytes();
            outstream.write(sbyte);
            outstream.flush();

        } catch (BadLocationException | IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getButton()==MouseEvent.BUTTON3){
            JTAConsolePopup popupMenu = new JTAConsolePopup(this);
            popupMenu.show(this,e.getX(),e.getY());
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
}