| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- package com.leanwo.management.console;
- import java.awt.BorderLayout;
- import java.awt.Rectangle;
- import java.awt.Toolkit;
- import java.awt.datatransfer.Clipboard;
- import java.awt.datatransfer.StringSelection;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Date;
- import javax.swing.JButton;
- import javax.swing.JCheckBox;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.SwingUtilities;
- public class LogConsole extends JPanel{
- private String name;
-
- private JTextArea textArea;
- private JCheckBox autoScrollCheckBox;
- private JButton clearButton;
- private JButton copyButton;
-
- private Process process;
-
- private StreamOutputThread outReaderThread;
- private StreamOutputThread errorReaderThread;
-
- /** 字符串最大的个数 */
- private static final int MAX_CHARACTER_COUNT = 100000;
-
- private StringBuilder cacheStringBuilder = null;
- private long currentMills = 0L;
- private long lastUpdateMills = 0L;
- /** 自动滚动 */
- private boolean autoScroll;
-
- Clipboard clipboard;
-
- public LogConsole(String name) {
- this.name = name;
- initData();
- initView();
- }
-
- public void setProcess(Process process) {
- if(outReaderThread != null) {
- outReaderThread.stopThread();
- outReaderThread = null;
- }
-
- if(errorReaderThread != null) {
- errorReaderThread.stopThread();
- errorReaderThread = null;
- }
-
- this.process = process;
- initData();
- }
-
- /**
- * 初始化数据
- */
- private void initData() {
- // 自动滚动
- autoScroll = false;
-
- cacheStringBuilder = new StringBuilder();
-
- if(process != null) {
- outReaderThread=new StreamOutputThread(process.getInputStream(), new LogEvent() {
- @Override
- public void appendLog(String data) {
- LogConsole.this.appendLog(data);
- }
- } );
- outReaderThread.setDaemon(true);
- outReaderThread.setName(name = "正常消息输出线程。");
- outReaderThread.start();
-
- errorReaderThread=new StreamOutputThread(process.getErrorStream(), new LogEvent() {
- @Override
- public void appendLog(String data) {
- LogConsole.this.appendLog(data);
- }
- });
- errorReaderThread.setDaemon(true);
- errorReaderThread.setName(name = "异常消息输出线程。");
- errorReaderThread.start();
- }
- }
-
- /**
- * 初始化界面
- */
- private void initView() {
- textArea=new JTextArea();
- textArea.setEditable(false);
- textArea.setText("");
-
- JPanel bottomPanel = new JPanel();
- bottomPanel.setLayout(new BorderLayout());
-
- autoScrollCheckBox = new JCheckBox("自动滚动");
- autoScrollCheckBox.setSelected(autoScroll);
- bottomPanel.add(autoScrollCheckBox, BorderLayout.WEST);
- copyButton=new JButton("复制日志");
- bottomPanel.add(copyButton, BorderLayout.EAST);
-
- clearButton=new JButton("清空");
- bottomPanel.add(clearButton);
-
- setLayout(new BorderLayout());
- add(new JScrollPane(textArea),BorderLayout.CENTER);
- add(bottomPanel,BorderLayout.SOUTH);
-
- copyButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- //创建能传输指定 String 的 Transferable。
- StringSelection editText =
- new StringSelection(textArea.getText());
- /**
- 将剪贴板的当前内容设置到指定的 transferable 对象,
- 并将指定的剪贴板所有者作为新内容的所有者注册。
- */
- clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
- clipboard.setContents(editText,editText);
- }
- });
-
- clearButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- textArea.setText("");
- }
- });
-
- autoScrollCheckBox.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- autoScroll = autoScrollCheckBox.isSelected();
- scrollToEnd();
- }
- });
- }
-
- /**
- * 保留最多的字符串
- */
- 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);
- }
- appendLog(null);
- }
-
- /**
- * 添加日志
- * @param text
- */
- private void appendLog(String text) {
- if(text != null) {
- cacheStringBuilder.append(text).append("\r\n");
- }
- currentMills = (new Date()).getTime();
- if((currentMills - lastUpdateMills > 500)) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- textArea.append(cacheStringBuilder.toString());
- cacheStringBuilder.setLength(0);
- lastUpdateMills = currentMills;
- if(autoScroll) {
- scrollToEnd();
- }
- }
- });
- }
- }
-
- /**
- * 滚动到最底端
- */
- private void scrollToEnd() {
- textArea.setCaretPosition(textArea.getText().length());
- }
- }
|