001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.util;
029
030import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
031
032import com.google.gwt.dom.client.Element;
033import com.google.gwt.dom.client.Style;
034import com.google.gwt.dom.client.Style.BorderStyle;
035import com.google.gwt.dom.client.Style.Overflow;
036import com.google.gwt.dom.client.Style.Position;
037import com.google.gwt.dom.client.Style.Unit;
038import com.google.gwt.user.client.DOM;
039import com.google.gwt.user.client.ui.RootPanel;
040
041/**
042 * A basic debug log, to print messages into the client window.<p>
043 *
044 * @since 8.0.0
045 */
046public final class CmsDebugLog {
047
048    /** The debug log element id. */
049    private static final String CMS_DEBUG_LOG_ID = "cms_debug_log";
050
051    /** Global debugging flag. */
052    private static final boolean DEBUG = false;
053
054    /** Debug log displayed within the client window. */
055    private static CmsDebugLog m_debug;
056
057    /** The wrapped widget. */
058    protected Element m_html;
059
060    /**
061     * Constructor.<p>
062     */
063    private CmsDebugLog() {
064
065        if (!DEBUG) {
066            return;
067        }
068        m_html = DOM.getElementById(CMS_DEBUG_LOG_ID);
069        if (m_html == null) {
070            m_html = DOM.createDiv();
071            m_html.setAttribute("id", CMS_DEBUG_LOG_ID);
072            Style style = m_html.getStyle();
073            style.setWidth(400, Unit.PX);
074            style.setHeight(700, Unit.PX);
075            style.setPadding(10, Unit.PX);
076            style.setOverflow(Overflow.AUTO);
077            style.setBorderStyle(BorderStyle.SOLID);
078            style.setBorderColor(I_CmsLayoutBundle.INSTANCE.constants().css().borderColor());
079            style.setBorderWidth(1, Unit.PX);
080            style.setPosition(Position.FIXED);
081            style.setTop(50, Unit.PX);
082            style.setRight(50, Unit.PX);
083            style.setBackgroundColor(I_CmsLayoutBundle.INSTANCE.constants().css().backgroundColorDialog());
084            style.setZIndex(I_CmsLayoutBundle.INSTANCE.constants().css().zIndexPopup());
085            RootPanel.getBodyElement().appendChild(m_html);
086        }
087    }
088
089    /**
090     * Logs a message to the browser console if possible.<p>
091     *
092     * @param message the message to log
093     */
094    public static native void consoleLog(String message) /*-{
095        var cns = $wnd.top.console;
096        if (cns && cns.log) {
097            cns.log(message);
098        }
099    }-*/;
100
101    /**
102     * Logs a message and includes the current stack trace.
103     * 
104     * @param message the message to log 
105     */
106    public static native void consoleTrace(String message) /*-{
107        var cns = $wnd.top.console;
108        if (cns && cns.trace) {
109            cns.trace(message);
110        }
111    }-*/;
112
113    /**
114     * Returns the debug log.<p>
115     *
116     * @return the debug log
117     */
118    public static CmsDebugLog getInstance() {
119
120        if (m_debug == null) {
121            m_debug = new CmsDebugLog();
122        }
123        return m_debug;
124    }
125
126    /**
127     * Clears the debug log.<p>
128     */
129    public void clear() {
130
131        if (!DEBUG) {
132            return;
133        }
134        m_html.setInnerHTML("");
135    }
136
137    /**
138     * Prints a new line into the log window by adding a p-tag including given text as HTML.<p>
139     *
140     * @param text the text to print
141     */
142    public void printLine(String text) {
143
144        if (!DEBUG) {
145            return;
146        }
147        Element child = DOM.createElement("p");
148        child.setInnerHTML(text);
149        m_html.insertFirst(child);
150    }
151}