// $Id: PmiiTabbedPaneView.java,v 1.15 2004/09/18 12:20:07 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.*;
import javax.swing.*;

import com.meyling.principia.log.Logger;
import com.meyling.principia.log.Trace;
import com.meyling.principia.module.ModuleProperties;


/**
 * Tabbed pane view.
 *
 * @version $Revision: 1.15 $
 * @author  Michael Meyling
 */
public final class PmiiTabbedPaneView extends JPanel {

    private JTabbedPane tabbedPane = new JTabbedPane();

    private QedeqPane qedeqPane;

    private HtmlPane htmlPane;

    private LogPane logPane;

    private ModuleProperties prop;

    private ModulePropertiesPane propertiesPane;
    
    private boolean viewHtml = false;


    /**
     * Constructor.
     */
    public PmiiTabbedPaneView(final ModuleProperties model) {
        ImageIcon icon = new ImageIcon(
            this.getClass().getResource("/com/meyling/principia/pic/tabular.png"));

        this.prop = model;

        try {
            this.propertiesPane = new ModulePropertiesPane(this.prop);
            this.qedeqPane = new QedeqPane(this.prop);
            this.htmlPane = new HtmlPane(this.prop);
            this.logPane = new LogPane();
            Logger.getInstance().addLog(this.logPane);

            this.tabbedPane.addTab("State", icon, this.propertiesPane, "Shows Status of Module");
            this.tabbedPane.addTab("Qedeq", icon, this.qedeqPane, "Shows Module in QEDEQ Syntax");
            if (viewHtml) {
                this.tabbedPane.addTab("Html", icon,  this.htmlPane, "Shows Module in Html-View");
            }
            this.tabbedPane.addTab("Log", icon, this.logPane, "Logging");
        } catch (RuntimeException e) { 
            Trace.trace(this, "constructor", e);
            throw e;
        }
        tabbedPane.setSelectedIndex(0);

        // Add the tabbed pane to this panel.
        setLayout(new GridLayout(1, 1));
        add(tabbedPane);
    }


    public final void setHtmlView(final boolean enable) {
        this.viewHtml = enable;
        if (enable) {
            ImageIcon icon = new ImageIcon(
                this.getClass().getResource("/com/meyling/principia/pic/tabular.png"));
            this.tabbedPane.addTab("Html", icon,  this.htmlPane, "Shows Module in Html-View");
            this.htmlPane.updateView();
        } else {
            this.tabbedPane.remove(this.htmlPane);
        }
    }


    public final boolean getHtmlView() {
        return this.viewHtml;
    }


    public final void setModel(final ModuleProperties model) {
        this.propertiesPane.setModel(model);
        this.qedeqPane.setModel(model);
        this.htmlPane.setModel(model);
        if (this.prop != model) {
            this.prop = model;
            updateView(); 
        } else {
            
        }
        
        // TODO else
    }


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


    public final void updateView() {
        this.propertiesPane.updateView();
        this.qedeqPane.updateView();
        if (this.viewHtml) {
            this.htmlPane.updateView();
        }
    }


    public final void setLineWrap(final boolean wrap) {
        this.qedeqPane.setLineWrap(wrap);
    }

    public final boolean getLineWrap() {
        return this.qedeqPane.getLineWrap();
    }

    /**
     * Get edited qedeq text, if text is editable.
     * 
     * @return  qedeq text
     * @throws  IllegalStateException   qedeq text is not editable
     */
    public final String getEditedQedeq() {
        return this.qedeqPane.getEditedQedeq();
    }



}
