165 lines
4.6 KiB
Java
165 lines
4.6 KiB
Java
package me.srvstr.ui.debug;
|
|
|
|
import java.awt.BorderLayout;
|
|
import java.awt.Color;
|
|
import java.awt.FlowLayout;
|
|
import java.awt.Font;
|
|
import java.awt.event.ActionEvent;
|
|
import java.io.PrintStream;
|
|
import javax.swing.BorderFactory;
|
|
import javax.swing.BoxLayout;
|
|
import javax.swing.JButton;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.JScrollPane;
|
|
import javax.swing.JTextArea;
|
|
import me.srvstr.ui.UICreator;
|
|
|
|
public class Console
|
|
extends JPanel {
|
|
private static final long serialVersionUID = -1800165403047521827L;
|
|
private JPanel consoleLogger;
|
|
private JScrollPane scrollPane;
|
|
private JPanel toolBar;
|
|
private JButton clear;
|
|
private JLabel queueSize;
|
|
private JLabel info;
|
|
private ConsoleWriteStream stdout;
|
|
private ConsoleWriteStream stderr;
|
|
private Entry lastEntry = Entry.NORMAL;
|
|
|
|
public Console() {
|
|
setLayout(new BorderLayout());
|
|
|
|
createUI();
|
|
|
|
setupUI();
|
|
|
|
createListener();
|
|
|
|
createWriteStreams();
|
|
|
|
add(this.scrollPane, "Center");
|
|
add(this.toolBar, "North");
|
|
|
|
createLog("Console was created successfully\n", Entry.INFORMATION);
|
|
}
|
|
|
|
private void createListener() {
|
|
this.clear.addActionListener(e -> {
|
|
this.consoleLogger.removeAll();
|
|
this.consoleLogger.revalidate();
|
|
this.consoleLogger.repaint();
|
|
this.queueSize.setText("| Elements: (none)");
|
|
this.info.setText((String) null);
|
|
});
|
|
}
|
|
|
|
private void setupUI() {
|
|
this.consoleLogger.setLayout(new BoxLayout(this.consoleLogger, 1));
|
|
this.info.setForeground(Color.YELLOW);
|
|
|
|
this.clear.setToolTipText(
|
|
"Clear the console logger's buffer.\nThis means deleting all current log entries in the console.");
|
|
|
|
this.scrollPane.setVerticalScrollBarPolicy(22);
|
|
this.scrollPane.setHorizontalScrollBarPolicy(30);
|
|
this.scrollPane.setAutoscrolls(true);
|
|
|
|
this.toolBar.add(new JLabel("Console"));
|
|
this.toolBar.add(this.clear);
|
|
this.toolBar.add(this.queueSize);
|
|
this.toolBar.add(this.info);
|
|
}
|
|
|
|
private void createUI() {
|
|
this.toolBar = new JPanel(new FlowLayout(0, 4, 0));
|
|
|
|
this.consoleLogger = new JPanel();
|
|
|
|
this.scrollPane = new JScrollPane(this.consoleLogger);
|
|
|
|
this.clear = UICreator.createIconifiedButton('c');
|
|
this.queueSize = new JLabel();
|
|
this.info = new JLabel();
|
|
}
|
|
|
|
private void createWriteStreams() {
|
|
this.stdout = new ConsoleWriteStream(this, Entry.NORMAL);
|
|
this.stderr = new ConsoleWriteStream(this, Entry.ERROR);
|
|
}
|
|
|
|
public final synchronized void createLog(String message, Entry entryType) {
|
|
JTextArea lastLog = getLastEntryLog();
|
|
|
|
if (this.lastEntry == entryType && lastLog != null && (int) lastLog.getText().lines().count() < 48) {
|
|
|
|
lastLog.setText(String.format("%s%s", new Object[] { lastLog.getText(), message }));
|
|
|
|
this.scrollPane.getVerticalScrollBar().setValue(this.scrollPane.getVerticalScrollBar().getMaximum());
|
|
this.scrollPane.repaint();
|
|
|
|
} else {
|
|
|
|
boolean isQueueFull = ((this.consoleLogger.getComponents()).length > 36);
|
|
|
|
this.info.setText(isQueueFull ? "Elements were deleted form Console buffer due to limited queue size" : null);
|
|
|
|
if (isQueueFull) {
|
|
|
|
this.consoleLogger.remove(this.consoleLogger.getComponents()[0]);
|
|
}
|
|
|
|
createBasicConsoleLog(message, Entry.getColorByEnum(entryType));
|
|
}
|
|
|
|
this.lastEntry = entryType;
|
|
}
|
|
|
|
private JTextArea getLastEntryLog() {
|
|
int elementIndex = (this.consoleLogger.getComponents()).length - 1;
|
|
|
|
if (elementIndex < 0) {
|
|
return null;
|
|
}
|
|
|
|
return (JTextArea) this.consoleLogger.getComponents()[elementIndex];
|
|
}
|
|
|
|
private void createBasicConsoleLog(String message, Color color) {
|
|
JTextArea messageBlock = new JTextArea(message);
|
|
|
|
messageBlock.setBorder(BorderFactory.createEmptyBorder());
|
|
|
|
messageBlock.setFont(new Font("Consolas", 0, 14));
|
|
|
|
messageBlock.setEditable(false);
|
|
|
|
messageBlock.setBackground(new Color(1579032));
|
|
|
|
messageBlock.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.DARK_GRAY));
|
|
messageBlock.setForeground(color);
|
|
|
|
this.consoleLogger.add(messageBlock);
|
|
this.scrollPane.getVerticalScrollBar().setValue(this.scrollPane.getVerticalScrollBar().getMaximum());
|
|
|
|
this.queueSize.setText(
|
|
String.format("Elements: (%d)", new Object[] { Integer.valueOf((this.consoleLogger.getComponents()).length) }));
|
|
}
|
|
|
|
public PrintStream getStdOut() {
|
|
return new PrintStream(this.stdout);
|
|
}
|
|
|
|
public PrintStream getStdErr() {
|
|
return new PrintStream(this.stderr);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Location:
|
|
* /home/teridax/Projects/Gytebot/GYTEBOT.jar!/org/ui/debug/Console.class
|
|
* Java compiler version: 8 (52.0)
|
|
* JD-Core Version: 1.1.3
|
|
*/
|