package org.gwtoolbox.commons.util.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.DOM;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import org.gwtoolbox.commons.util.client.template.Template;
import org.gwtoolbox.commons.util.client.template.MapModel;
/**
* @author Uri Boness
*/
public class Console extends DialogBox {
private static Console instance;
private FlowPanel main;
private Logger.Level level = Logger.Level.DEBUG;
public static Console get() {
if (instance == null) {
instance = new Console();
}
instance.setPopupPositionAndShow(new PositionCallback() {
public void setPosition(int offsetWidth, int offsetHeight) {
int x = RootPanel.get().getOffsetWidth() - offsetWidth;
int y = 0;
instance.setPopupPosition(x, y);
}
});
return instance;
}
public static void close() {
if (instance != null) {
instance.hide();
}
}
public static void clean() {
if (instance != null) {
instance.doClean();
}
}
private Console() {
super(false, false);
setText("Console");
main = new FlowPanel();
FlowPanel buttons = new FlowPanel();
Button button = new Button("Close", new ClickHandler() {
public void onClick(ClickEvent event) {
hide();
}
});
DOM.setStyleAttribute(button.getElement(), "marginRight", "5px");
buttons.add(button);
button = new Button("Clear", new ClickHandler() {
public void onClick(ClickEvent event) {
clean();
}
});
DOM.setStyleAttribute(button.getElement(), "marginRight", "5px");
buttons.add(button);
SimplePanel sp = new SimplePanel();
sp.setWidget(main);
sp.setSize("100%", "100%");
DOM.setStyleAttribute(sp.getElement(), "overflow", "auto");
DockPanel dp = new DockPanel();
dp.setWidth("300px");
dp.setHeight("500px");
dp.add(sp, DockPanel.CENTER);
dp.add(buttons, DockPanel.SOUTH);
dp.setCellHeight(buttons, "30px");
dp.setCellHorizontalAlignment(buttons, DockPanel.ALIGN_RIGHT);
dp.setCellVerticalAlignment(buttons, DockPanel.ALIGN_MIDDLE);
setWidget(dp);
DOM.setStyleAttribute(getElement(), "zIndex", "5");
}
private void doClean() {
main.clear();
}
public static void trace(String message) {
trace(message, null);
}
public static void trace(String message, Throwable t) {
if (isTraceEnabled()) {
get().addMessage("TRACE", "green", message, t);
}
}
public static boolean isTraceEnabled() {
return get().level.isEnabled(Logger.Level.TRACE);
}
public static void debug(String message) {
debug(message, null);
}
public static void debug(String message, Throwable t) {
if (isDebugEnabled()) {
get().addMessage("DEBUG", "blue", message, t);
}
}
public static boolean isDebugEnabled() {
return get().level.isEnabled(Logger.Level.DEBUG);
}
public static void info(String message) {
info(message, null);
}
public static void info(String message, Throwable t) {
if (isInfoEnabled()) {
get().addMessage("INFO", "darkgray", message, t);
}
}
public static boolean isInfoEnabled() {
return get().level.isEnabled(Logger.Level.INFO);
}
public static void warn(String message) {
warn(message, null);
}
public static void warn(String message, Throwable t) {
if (isWarnEnabled()) {
get().addMessage("WARN", "purple", message, t);
}
}
public static boolean isWarnEnabled() {
return get().level.isEnabled(Logger.Level.WARN);
}
public static void error(String message) {
error(message, null);
}
public static void error(String message, Throwable t) {
if (isErrorEnabled()) {
get().addMessage("ERROR", "red", message, t);
}
}
public static boolean isErrorEnabled() {
return get().level.isEnabled(Logger.Level.ERROR);
}
public static void setLogLevel(Logger.Level level) {
get().level = level;
}
//================================================ Helper Methods ==================================================
private void addMessage(final String label, final String color, final String message, final Throwable t) {
MapModel model = new MapModel() {{
set("label", label);
set("color", color);
set("message", message);
}};
Template template = Template.compile("<span style=\"color:${color}\">${label}:</span> ${message}");
HTML html = new HTML(template.render(model));
GWT.log(template.render(model), null);
main.add(html);
}
}