// $Id: LogPane.java,v 1.1 2004/04/29 21:06:56 m31 Exp $
//
// This file is part of the program suite "Principia Mathematica II"
// which is a working prototype for the main project:
// "Hilbert II" - http://www.qedeq.org
//
// Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
// "Principia Mathematica II" is free software; you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package com.meyling.principia.gui.jtabbedpane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import com.meyling.principia.log.Log;
import com.meyling.principia.log.Trace;
import com.meyling.principia.module.*;

/**
 * View for {@link ModuleProperties}.
 *
 * @author      Michael Meyling
 */

public class LogPane extends JPanel implements Log {


    private JTextPane text = new JTextPane();
    
    private boolean blocked = false;

    private ModuleAddress currentAddress = null;

    private ModuleState currentState;

    private final SimpleAttributeSet errorAttrs = new SimpleAttributeSet();

    private final SimpleAttributeSet successAttrs = new SimpleAttributeSet();

    private final SimpleAttributeSet messageAttrs = new SimpleAttributeSet();

    private final SimpleAttributeSet requestAttrs = new SimpleAttributeSet();


    /**
     * Creates new Panel.
     */
    public LogPane() {
        super(false);
        setupView();
        StyleContext sc = new StyleContext();
        DefaultStyledDocument doc = new DefaultStyledDocument(sc);
        this.text.setDocument(doc);
    }

 

    /**
     * Assembles the gui components of the panel.
     */
    private final void setupView() {
        text.setDragEnabled(true);
        text.setAutoscrolls(true);
        text.setCaretPosition(0);
        text.setEditable(false);
        text.getCaret().setVisible(false);
        text.setFocusable(true);
        final JScrollPane scroller = new JScrollPane();
        final JViewport vp = scroller.getViewport();
        vp.add(text);
        this.setLayout(new BorderLayout(1, 1));
        this.add(scroller);

        this.addComponentListener(new ComponentAdapter() {
            public void componentHidden(ComponentEvent e) {
                Trace.trace(this, "componentHidden", e);
            }
            public void componentShown(ComponentEvent e) {
                Trace.trace(this, "componentHidden", e);
            }
        });
        
        StyleConstants.setForeground(
            this.errorAttrs, Color.red);
        StyleConstants.setForeground(
            this.successAttrs, Color.green);
        StyleConstants.setForeground(
            this.messageAttrs, Color.black);
        StyleConstants.setForeground(
            this.requestAttrs, Color.blue);
    }


    /* (non-Javadoc)
     * @see Log#logMessageState(String, String)
     */
    public final void logMessageState(final String text, final String url) {
        try {
            this.text.getDocument().insertString(this.text.getDocument().getLength(), 
                text + "\n\t" + url + "\n", this.messageAttrs);
        } catch (BadLocationException e) {
            Trace.trace(this, "logMessageState", e);
        }
        
    }


    /* (non-Javadoc)
     * @see Log#logFailureState(String, String)
     */
    public final void logFailureState(final String text, final String url, final String description) {
        try {
            this.text.getDocument().insertString(this.text.getDocument().getLength(), 
                text + "\n\t" + url + "\n" + "\n\t" + description + "\n", this.errorAttrs);
        } catch (BadLocationException e) {
            Trace.trace(this, "logFailureState", e);
        }
    }


    /* (non-Javadoc)
     * @see Log#logSuccessfulState(String, String)
     */
    public final void logSuccessfulState(final String text, final String url) {
        try {
            this.text.getDocument().insertString(this.text.getDocument().getLength(), 
                text + "\n\t" + url + "\n", this.successAttrs);
        } catch (BadLocationException e) {
            Trace.trace(this, "logSuccessfulState", e);
        }
    }



    /* (non-Javadoc)
     * @see Log#logRequest(String)
     */
    public void logRequest(String text) {
        try {
            this.text.getDocument().insertString(this.text.getDocument().getLength(), 
                text + "\n", this.requestAttrs);
        } catch (BadLocationException e) {
            Trace.trace(this, "logRequest", e);
        }
    }



    /* (non-Javadoc)
     * @see Log#logSuccessfulReply(String)
     */
    public void logSuccessfulReply(String text) {
        try {
            this.text.getDocument().insertString(this.text.getDocument().getLength(), 
                text + "\n", this.successAttrs);
        } catch (BadLocationException e) {
            Trace.trace(this, "logSuccessfulReply", e);
        }
    }



    /* (non-Javadoc)
     * @see Log#logFailureReply(String, String)
     */
    public void logFailureReply(String text, String description) {
        try {
            this.text.getDocument().insertString(this.text.getDocument().getLength(), 
                text + "\n" + description + "\n", this.errorAttrs);
        } catch (BadLocationException e) {
            Trace.trace(this, "logFailureReply", e);
        }
    }



    /* (non-Javadoc)
     * @see com.meyling.principia.log.Log#logMessage(java.lang.String)
     */
    public void logMessage(String text) {
        try {
            this.text.getDocument().insertString(this.text.getDocument().getLength(), 
                text + "\n", this.messageAttrs);
        } catch (BadLocationException e) {
            Trace.trace(this, "logFailureReply", e);
        }
    }




}
