org.bidib.wizard.script.client.model.ScriptClientLogModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bidibwizard-script-client Show documentation
Show all versions of bidibwizard-script-client Show documentation
jBiDiB BiDiB Wizard Script Client POM
package org.bidib.wizard.script.client.model;
import java.awt.Color;
import java.util.LinkedList;
import java.util.List;
import javax.swing.UIManager;
import com.jgoodies.binding.beans.Model;
public class ScriptClientLogModel extends Model {
private static final long serialVersionUID = 1L;
public static final String PROPERTY_CONSOLE_CONTENT = "consoleContent";
public static final String PROPERTY_CONSOLE_CONTENT_SIZE = "consoleContentSize";
private List consoleMessages = new LinkedList<>();
private Color colorRed;
private Color colorBlue;
private Color colorGreen;
private Color colorBlack;
public enum ConsoleColor {
red, blue, green, black;
}
public static final class ConsoleLine {
private final Color color;
private final String message;
public ConsoleLine(Color color, String message) {
this.color = color;
this.message = message;
}
/**
* @return the color
*/
public Color getColor() {
return color;
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
}
public ScriptClientLogModel() {
this.colorRed = UIManager.getColor("Console.red");
this.colorBlue = UIManager.getColor("Console.blue");
this.colorGreen = UIManager.getColor("Console.green");
this.colorBlack = UIManager.getColor("Console.black");
}
public void addConsoleLine(ConsoleColor consoleColor, String line) {
Color color = this.colorBlack;
switch (consoleColor) {
case red:
color = this.colorRed;
break;
case blue:
color = this.colorBlue;
break;
case green:
color = this.colorGreen;
break;
default:
break;
}
ConsoleLine consoleLine = new ConsoleLine(color, line);
// only keep 250 in memory
if (consoleMessages.size() > 250) {
consoleMessages.remove(0);
}
consoleMessages.add(consoleLine);
int index = consoleMessages.size() - 1;
fireIndexedPropertyChange(PROPERTY_CONSOLE_CONTENT, index, null, consoleLine);
}
public void clear() {
int oldSize = consoleMessages.size();
consoleMessages.clear();
firePropertyChange(PROPERTY_CONSOLE_CONTENT_SIZE, oldSize, consoleMessages.size());
}
}