| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package com.leanwo.management.console;
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PipedInputStream;
- import javax.swing.JButton;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- public class LogConsole extends JPanel{
- private JTextArea textArea;
- private JButton clearButton;
-
- private PipedInputStream outPipedInputStream;
- private PipedInputStream errorPipedInputStream;
- private boolean quit;
- private Thread outReaderThread;
- private Thread errorReaderThread;
-
- /** 字符串最大的个数 */
- private static final int MAX_CHARACTER_COUNT = 100000;
-
- public LogConsole(PipedInputStream outPipedInputStream, PipedInputStream errorPipedInputStream) {
- this.outPipedInputStream = outPipedInputStream;
- this.errorPipedInputStream = errorPipedInputStream;
- initData();
- initView();
- }
-
- public void setPipedInputStream(PipedInputStream outPipedInputStream, PipedInputStream errorPipedInputStream) {
- this.outPipedInputStream = outPipedInputStream;
- this.errorPipedInputStream = errorPipedInputStream;
- }
-
- /**
- * 初始化数据
- */
- private void initData() {
- // signals the Threads that they should exit
- quit=false;
-
- // Starting two seperate threads to read from the PipedInputStreams
- outReaderThread=new Thread(outRunnable);
- outReaderThread.setDaemon(true);
- outReaderThread.start();
-
- errorReaderThread=new Thread(errorRunnable);
- errorReaderThread.setDaemon(true);
- errorReaderThread.start();
- }
-
- /**
- * 初始化界面
- */
- private void initView() {
- textArea=new JTextArea();
- textArea.setEditable(false);
- clearButton=new JButton("清空");
-
- setLayout(new BorderLayout());
- add(new JScrollPane(textArea),BorderLayout.CENTER);
- add(clearButton,BorderLayout.SOUTH);
-
- clearButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- textArea.setText(null);
- }
- });
- }
- private Runnable outRunnable = new Runnable() {
- @Override
- public synchronized void run() {
- try {
- while (!quit) {
- try {
- this.wait(100);
- } catch (InterruptedException ie) {
- }
- if (outPipedInputStream != null && outPipedInputStream.available() != 0) {
- String input = readLine(outPipedInputStream);
- textArea.append(input);
- }
- }
- } catch (Exception e) {
- textArea.append("\nConsole reports an Internal error.");
- textArea.append("The error is: " + e);
- }
- }
- };
-
-
- private Runnable errorRunnable = new Runnable() {
- @Override
- public synchronized void run() {
- try {
- while (!quit) {
- try {
- this.wait(100);
- } catch (InterruptedException ie) {
- }
- if (errorPipedInputStream != null && errorPipedInputStream.available() != 0) {
- String input = readLine(errorPipedInputStream);
- textArea.append(input);
- }
- }
- } catch (Exception e) {
- textArea.append("\nConsole reports an Internal error.");
- textArea.append("The error is: " + e);
- }
- }
- };
- private String readLine(InputStream inputStream) throws IOException {
- String input = "";
- do {
- int available = inputStream.available();
- if (available == 0)
- break;
- byte b[] = new byte[available];
- inputStream.read(b);
- input = input + new String(b, 0, b.length, "GBK");
- } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
- return input;
- }
-
- /**
- * 保留最多的字符串
- */
- public void reserveMaxCharacter() {
- String text = textArea.getText();
- if(text.length() > MAX_CHARACTER_COUNT) {
- text = text.substring(text.length() - MAX_CHARACTER_COUNT *2/3);
- textArea.setText(text);
- }
- }
- }
|