// $Id: HtmlPane.java,v 1.2 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.Font;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.IOException;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

import com.meyling.principia.html.Module2JHtml;

import com.meyling.principia.log.Trace;
import com.meyling.principia.module.*;

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

public class HtmlPane extends JPanel {

    private ModuleProperties prop;

//    private JEditorPane html = new JEditorPane();
// TODO test example:   
    private JEditorPane html = new JEditorPane("text/html","\u1200");


    private boolean blocked = false;

    private ModuleAddress currentAddress = null;

    private ModuleState currentState;


    /**
     * Creates new Panel.
     */
    public HtmlPane(final ModuleProperties prop) {
        super(false);
        this.prop = prop;
        this.currentAddress = prop == null ? null : prop.getModuleAddress();
        this.currentState = prop == null ? null : prop.getState();
        setupView();
        updateView();
    }


    /**
     * Assembles the gui components of the panel.
     */
    private final void setupView() {
        html.setDragEnabled(true);
        html.setAutoscrolls(true);
        html.setCaretPosition(0);
        html.setEditable(false);
        html.getCaret().setVisible(false);
        html.setFocusable(true);
        html.setDebugGraphicsOptions(DebugGraphics.LOG_OPTION);
// TODO work out:
//        html.addHyperlinkListener(createHyperLinkListener());
        final JScrollPane scroller = new JScrollPane();
        final JViewport vp = scroller.getViewport();
        vp.add(html);
        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);
            }
        });

    }


    /**
     * Set new model. To make the new model visible {@link #updateView} must be called.
     *
     * @param   prop
     */
    final public void setModel(final ModuleProperties prop) {
        Trace.trace(this, "setModel", prop);
        this.prop = prop;
    }


    final public ModuleProperties getModel() {
        return this.prop;
    }



    /**
     * Update from model.
     */
    final public synchronized void updateView() {
        Trace.traceBegin(this, "updateView");
        boolean refresh = false;
        if (prop == null && this.currentAddress != null) {
            refresh = true;
        }
        if (prop != null && !prop.getModuleAddress().equals(this.currentAddress)) {
            this.currentAddress = prop.getModuleAddress();
            refresh = true;
        }
        if (prop != null && this.currentState != prop.getState()) {
            this.currentState = prop.getState();
            refresh = true; 
        }
        if (refresh && this.prop != null && (this.prop.getState() == ModuleState.STATE_LOADED_AND_VERIFIED)
                && !this.blocked) {
            this.blocked = true;
            try {
                final Module2JHtml converter = new Module2JHtml(prop.getModuleAddress());
                converter.writeModule();
                html.setFont(new Font("Lucida Bright", Font.PLAIN, 12));
                html.setText("<html><body>" +
                    "\u2227 Hallo \u2227 Werter \u2227 \u2227 \u2227" +
                    "<br>" +
                    "<table>" +
                    "<tr><td>Hei</td><td>ho</td></tr>" +
                    "<tr><td>Ostern</td><td>weihnacht</td></tr>" +
                    "</table>" +
                    "</body></html>");
//                html.setPage(converter.getGeneratedUrl());
                html.setText(converter.getText());

            } catch (Exception e) {
                Trace.trace(this, "updateView", e);
                html.setText("");
            }
            this.blocked = false;
        } else {
            html.setText("");
        }
        this.repaint();
        Trace.traceEnd(this, "updateView");
    }



    public HyperlinkListener createHyperLinkListener() {
        return new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    if (e instanceof HTMLFrameHyperlinkEvent) {
                        ((HTMLDocument)html.getDocument()).processHTMLFrameHyperlinkEvent(
                            (HTMLFrameHyperlinkEvent)e);
                    } else {
                        try {
                            html.setPage(e.getURL());
                        } catch (IOException ioe) {
                            System.out.println("IOE: " + ioe);
                        }
                    }
                }
            }
        };
    }


}
