|
@@ -8,13 +8,16 @@ import java.io.InputStream;
|
|
|
import java.io.PipedInputStream;
|
|
import java.io.PipedInputStream;
|
|
|
|
|
|
|
|
import javax.swing.JButton;
|
|
import javax.swing.JButton;
|
|
|
|
|
+import javax.swing.JCheckBox;
|
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JPanel;
|
|
|
import javax.swing.JScrollPane;
|
|
import javax.swing.JScrollPane;
|
|
|
import javax.swing.JTextArea;
|
|
import javax.swing.JTextArea;
|
|
|
|
|
+import javax.swing.SwingUtilities;
|
|
|
|
|
|
|
|
public class LogConsole extends JPanel{
|
|
public class LogConsole extends JPanel{
|
|
|
|
|
|
|
|
private JTextArea textArea;
|
|
private JTextArea textArea;
|
|
|
|
|
+ private JCheckBox autoScrollCheckBox;
|
|
|
private JButton clearButton;
|
|
private JButton clearButton;
|
|
|
|
|
|
|
|
private PipedInputStream outPipedInputStream;
|
|
private PipedInputStream outPipedInputStream;
|
|
@@ -25,7 +28,10 @@ public class LogConsole extends JPanel{
|
|
|
private Thread errorReaderThread;
|
|
private Thread errorReaderThread;
|
|
|
|
|
|
|
|
/** 字符串最大的个数 */
|
|
/** 字符串最大的个数 */
|
|
|
- private static final int MAX_CHARACTER_COUNT = 100000;
|
|
|
|
|
|
|
+ private static final int MAX_CHARACTER_COUNT = 10000;
|
|
|
|
|
+
|
|
|
|
|
+ /** 自动滚动 */
|
|
|
|
|
+ private boolean autoScroll;
|
|
|
|
|
|
|
|
public LogConsole(PipedInputStream outPipedInputStream, PipedInputStream errorPipedInputStream) {
|
|
public LogConsole(PipedInputStream outPipedInputStream, PipedInputStream errorPipedInputStream) {
|
|
|
this.outPipedInputStream = outPipedInputStream;
|
|
this.outPipedInputStream = outPipedInputStream;
|
|
@@ -46,6 +52,9 @@ public class LogConsole extends JPanel{
|
|
|
// signals the Threads that they should exit
|
|
// signals the Threads that they should exit
|
|
|
quit=false;
|
|
quit=false;
|
|
|
|
|
|
|
|
|
|
+ // 自动滚动
|
|
|
|
|
+ autoScroll = true;
|
|
|
|
|
+
|
|
|
// Starting two seperate threads to read from the PipedInputStreams
|
|
// Starting two seperate threads to read from the PipedInputStreams
|
|
|
outReaderThread=new Thread(outRunnable);
|
|
outReaderThread=new Thread(outRunnable);
|
|
|
outReaderThread.setDaemon(true);
|
|
outReaderThread.setDaemon(true);
|
|
@@ -62,16 +71,34 @@ public class LogConsole extends JPanel{
|
|
|
private void initView() {
|
|
private void initView() {
|
|
|
textArea=new JTextArea();
|
|
textArea=new JTextArea();
|
|
|
textArea.setEditable(false);
|
|
textArea.setEditable(false);
|
|
|
|
|
+ textArea.setText("");
|
|
|
|
|
+
|
|
|
|
|
+ JPanel bottomPanel = new JPanel();
|
|
|
|
|
+ bottomPanel.setLayout(new BorderLayout());
|
|
|
|
|
+
|
|
|
|
|
+ autoScrollCheckBox = new JCheckBox("自动滚动");
|
|
|
|
|
+ autoScrollCheckBox.setSelected(autoScroll);
|
|
|
|
|
+ bottomPanel.add(autoScrollCheckBox, BorderLayout.WEST);
|
|
|
|
|
+
|
|
|
clearButton=new JButton("清空");
|
|
clearButton=new JButton("清空");
|
|
|
|
|
+ bottomPanel.add(clearButton);
|
|
|
|
|
|
|
|
setLayout(new BorderLayout());
|
|
setLayout(new BorderLayout());
|
|
|
add(new JScrollPane(textArea),BorderLayout.CENTER);
|
|
add(new JScrollPane(textArea),BorderLayout.CENTER);
|
|
|
- add(clearButton,BorderLayout.SOUTH);
|
|
|
|
|
|
|
+ add(bottomPanel,BorderLayout.SOUTH);
|
|
|
|
|
|
|
|
clearButton.addActionListener(new ActionListener() {
|
|
clearButton.addActionListener(new ActionListener() {
|
|
|
@Override
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
- textArea.setText(null);
|
|
|
|
|
|
|
+ textArea.setText("");
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ autoScrollCheckBox.addActionListener(new ActionListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void actionPerformed(ActionEvent e) {
|
|
|
|
|
+ autoScroll = autoScrollCheckBox.isSelected();
|
|
|
|
|
+ scrollToEnd();
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
@@ -82,17 +109,17 @@ public class LogConsole extends JPanel{
|
|
|
try {
|
|
try {
|
|
|
while (!quit) {
|
|
while (!quit) {
|
|
|
try {
|
|
try {
|
|
|
- this.wait(100);
|
|
|
|
|
|
|
+ this.wait(10);
|
|
|
} catch (InterruptedException ie) {
|
|
} catch (InterruptedException ie) {
|
|
|
}
|
|
}
|
|
|
if (outPipedInputStream != null && outPipedInputStream.available() != 0) {
|
|
if (outPipedInputStream != null && outPipedInputStream.available() != 0) {
|
|
|
String input = readLine(outPipedInputStream);
|
|
String input = readLine(outPipedInputStream);
|
|
|
- textArea.append(input);
|
|
|
|
|
|
|
+ appendLog(input);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- textArea.append("\nConsole reports an Internal error.");
|
|
|
|
|
- textArea.append("The error is: " + e);
|
|
|
|
|
|
|
+ String text = "\r\nConsole reports an Internal error.\r\nThe error is:" + e.getMessage();
|
|
|
|
|
+ appendLog(text);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
@@ -104,17 +131,17 @@ public class LogConsole extends JPanel{
|
|
|
try {
|
|
try {
|
|
|
while (!quit) {
|
|
while (!quit) {
|
|
|
try {
|
|
try {
|
|
|
- this.wait(100);
|
|
|
|
|
|
|
+ this.wait(10);
|
|
|
} catch (InterruptedException ie) {
|
|
} catch (InterruptedException ie) {
|
|
|
}
|
|
}
|
|
|
if (errorPipedInputStream != null && errorPipedInputStream.available() != 0) {
|
|
if (errorPipedInputStream != null && errorPipedInputStream.available() != 0) {
|
|
|
String input = readLine(errorPipedInputStream);
|
|
String input = readLine(errorPipedInputStream);
|
|
|
- textArea.append(input);
|
|
|
|
|
|
|
+ appendLog(input);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- textArea.append("\nConsole reports an Internal error.");
|
|
|
|
|
- textArea.append("The error is: " + e);
|
|
|
|
|
|
|
+ String text = "\r\nConsole reports an Internal error.\r\nThe error is:" + e.getMessage();
|
|
|
|
|
+ appendLog(text);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
@@ -141,5 +168,29 @@ public class LogConsole extends JPanel{
|
|
|
text = text.substring(text.length() - MAX_CHARACTER_COUNT *2/3);
|
|
text = text.substring(text.length() - MAX_CHARACTER_COUNT *2/3);
|
|
|
textArea.setText(text);
|
|
textArea.setText(text);
|
|
|
}
|
|
}
|
|
|
|
|
+ textArea.setCaretPosition(textArea.getText().length());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 添加日志
|
|
|
|
|
+ * @param text
|
|
|
|
|
+ */
|
|
|
|
|
+ private void appendLog(String text) {
|
|
|
|
|
+ SwingUtilities.invokeLater(new Runnable() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void run() {
|
|
|
|
|
+ textArea.append(text);
|
|
|
|
|
+ scrollToEnd();
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 滚动到最底端
|
|
|
|
|
+ */
|
|
|
|
|
+ private void scrollToEnd() {
|
|
|
|
|
+ if(autoScroll) {
|
|
|
|
|
+ textArea.setCaretPosition(textArea.getText().length());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|