LogConsole.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.leanwo.management.console;
  2. import java.awt.BorderLayout;
  3. import java.awt.Rectangle;
  4. import java.awt.Toolkit;
  5. import java.awt.datatransfer.Clipboard;
  6. import java.awt.datatransfer.StringSelection;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.util.Date;
  10. import javax.swing.JButton;
  11. import javax.swing.JCheckBox;
  12. import javax.swing.JPanel;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.JTextArea;
  15. import javax.swing.SwingUtilities;
  16. public class LogConsole extends JPanel{
  17. private String name;
  18. private JTextArea textArea;
  19. private JCheckBox autoScrollCheckBox;
  20. private JButton clearButton;
  21. private JButton copyButton;
  22. private Process process;
  23. private StreamOutputThread outReaderThread;
  24. private StreamOutputThread errorReaderThread;
  25. /** 字符串最大的个数 */
  26. private static final int MAX_CHARACTER_COUNT = 100000;
  27. private StringBuilder cacheStringBuilder = null;
  28. private long currentMills = 0L;
  29. private long lastUpdateMills = 0L;
  30. /** 自动滚动 */
  31. private boolean autoScroll;
  32. Clipboard clipboard;
  33. public LogConsole(String name) {
  34. this.name = name;
  35. initData();
  36. initView();
  37. }
  38. public void setProcess(Process process) {
  39. if(outReaderThread != null) {
  40. outReaderThread.stopThread();
  41. outReaderThread = null;
  42. }
  43. if(errorReaderThread != null) {
  44. errorReaderThread.stopThread();
  45. errorReaderThread = null;
  46. }
  47. this.process = process;
  48. initData();
  49. }
  50. /**
  51. * 初始化数据
  52. */
  53. private void initData() {
  54. // 自动滚动
  55. autoScroll = false;
  56. cacheStringBuilder = new StringBuilder();
  57. if(process != null) {
  58. outReaderThread=new StreamOutputThread(process.getInputStream(), new LogEvent() {
  59. @Override
  60. public void appendLog(String data) {
  61. LogConsole.this.appendLog(data);
  62. }
  63. } );
  64. outReaderThread.setDaemon(true);
  65. outReaderThread.setName(name = "正常消息输出线程。");
  66. outReaderThread.start();
  67. errorReaderThread=new StreamOutputThread(process.getErrorStream(), new LogEvent() {
  68. @Override
  69. public void appendLog(String data) {
  70. LogConsole.this.appendLog(data);
  71. }
  72. });
  73. errorReaderThread.setDaemon(true);
  74. errorReaderThread.setName(name = "异常消息输出线程。");
  75. errorReaderThread.start();
  76. }
  77. }
  78. /**
  79. * 初始化界面
  80. */
  81. private void initView() {
  82. textArea=new JTextArea();
  83. textArea.setEditable(false);
  84. textArea.setText("");
  85. JPanel bottomPanel = new JPanel();
  86. bottomPanel.setLayout(new BorderLayout());
  87. autoScrollCheckBox = new JCheckBox("自动滚动");
  88. autoScrollCheckBox.setSelected(autoScroll);
  89. bottomPanel.add(autoScrollCheckBox, BorderLayout.WEST);
  90. copyButton=new JButton("复制日志");
  91. bottomPanel.add(copyButton, BorderLayout.EAST);
  92. clearButton=new JButton("清空");
  93. bottomPanel.add(clearButton);
  94. setLayout(new BorderLayout());
  95. add(new JScrollPane(textArea),BorderLayout.CENTER);
  96. add(bottomPanel,BorderLayout.SOUTH);
  97. copyButton.addActionListener(new ActionListener() {
  98. @Override
  99. public void actionPerformed(ActionEvent e) {
  100. //创建能传输指定 String 的 Transferable。
  101. StringSelection editText =
  102. new StringSelection(textArea.getText());
  103. /**
  104. 将剪贴板的当前内容设置到指定的 transferable 对象,
  105. 并将指定的剪贴板所有者作为新内容的所有者注册。
  106. */
  107. clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  108. clipboard.setContents(editText,editText);
  109. }
  110. });
  111. clearButton.addActionListener(new ActionListener() {
  112. @Override
  113. public void actionPerformed(ActionEvent e) {
  114. textArea.setText("");
  115. }
  116. });
  117. autoScrollCheckBox.addActionListener(new ActionListener() {
  118. @Override
  119. public void actionPerformed(ActionEvent e) {
  120. autoScroll = autoScrollCheckBox.isSelected();
  121. scrollToEnd();
  122. }
  123. });
  124. }
  125. /**
  126. * 保留最多的字符串
  127. */
  128. public void reserveMaxCharacter() {
  129. String text = textArea.getText();
  130. if(text.length() > MAX_CHARACTER_COUNT) {
  131. text = text.substring(text.length() - MAX_CHARACTER_COUNT *2/3);
  132. textArea.setText(text);
  133. }
  134. appendLog(null);
  135. }
  136. /**
  137. * 添加日志
  138. * @param text
  139. */
  140. private void appendLog(String text) {
  141. if(text != null) {
  142. cacheStringBuilder.append(text).append("\r\n");
  143. }
  144. currentMills = (new Date()).getTime();
  145. if((currentMills - lastUpdateMills > 500)) {
  146. SwingUtilities.invokeLater(new Runnable() {
  147. @Override
  148. public void run() {
  149. textArea.append(cacheStringBuilder.toString());
  150. cacheStringBuilder.setLength(0);
  151. lastUpdateMills = currentMills;
  152. if(autoScroll) {
  153. scrollToEnd();
  154. }
  155. }
  156. });
  157. }
  158. }
  159. /**
  160. * 滚动到最底端
  161. */
  162. private void scrollToEnd() {
  163. textArea.setCaretPosition(textArea.getText().length());
  164. }
  165. }