// $Id: ModulePropertiesPane.java,v 1.5 2004/05/10 06:51:10 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 javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JViewport;

import com.meyling.principia.module.ModuleProperties;

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

public class ModulePropertiesPane extends JPanel {

    private ModuleProperties prop;

    private JLabel stateLabel;

    private JTextField stateTextField;

    private JLabel nameLabel;

    private JTextField nameTextField;

    private JLabel moduleVersionLabel;

    private JTextField moduleVersionTextField;

    private JLabel ruleVersionLabel;

    private JTextField ruleVersionTextField;

    private JLabel urlLabel;

    private JTextArea urlTextField;

    private JLabel failureLabel;

    private JTextArea failureTextField;


    /**
     * Creates new Panel.
     */
    public ModulePropertiesPane(final ModuleProperties prop) {
        super();
        this.prop = prop;
        setupView();
        updateView();
    }



    /**
     * Assembles the gui components of the panel.
     */
    public final void setupView() {
        this.setLayout(null);

        this.stateLabel = new JLabel("State");
        this.add(this.stateLabel);
        this.stateLabel.setBounds(33, 20, 150, 17);

        this.stateTextField = new JTextField();
        this.stateTextField.setEditable(false);
        this.add(this.stateTextField);
        this.stateTextField.setBounds(140, 20, 440, 21);

        this.nameLabel = new JLabel("Name");
        this.add(this.nameLabel);
        this.nameLabel.setBounds(33, 60, 150, 17);

        this.nameTextField = new JTextField();
        this.nameTextField.setEditable(false);
        this.add(this.nameTextField);
        this.nameTextField.setBounds(140, 60, 440, 21);

        this.moduleVersionLabel = new JLabel("Module version");
        this.add(this.moduleVersionLabel);
        this.moduleVersionLabel.setBounds(33, 100, 150, 17);

        this.moduleVersionTextField = new JTextField();
        this.moduleVersionTextField.setEditable(false);
        this.add(this.moduleVersionTextField);
        this.moduleVersionTextField.setBounds(140, 100, 440, 21);

        this.ruleVersionLabel = new JLabel("Rule version");
        this.add(this.ruleVersionLabel);
        this.ruleVersionLabel.setBounds(33, 140, 150, 17);

        this.ruleVersionTextField = new JTextField();
        this.ruleVersionTextField.setEditable(false);
        this.add(this.ruleVersionTextField);
        this.ruleVersionTextField.setBounds(140, 140, 440, 21);

        this.urlLabel = new JLabel("URL");
        this.add(this.urlLabel);
        this.urlLabel.setBounds(33, 180, 150, 17);

        this.urlTextField = new JTextArea();
        this.urlTextField.setEditable(false);
        this.urlTextField.setLineWrap(true);
        this.urlTextField.setBackground(this.getBackground());
        this.urlTextField.setFont(this.ruleVersionTextField.getFont());
        this.urlTextField.setCaretPosition(0);
        this.urlTextField.setBorder(this.ruleVersionTextField.getBorder());
        this.add(this.urlTextField);
        this.urlTextField.setBounds(140, 180, 440, 21 * 2);

        this.failureLabel = new JLabel("Failure");
        this.add(this.failureLabel);
        this.failureLabel.setBounds(33, 241, 150, 17);

        final JScrollPane scroller = new JScrollPane();
        this.failureTextField = new JTextArea();
        this.failureTextField.setEditable(false);
        this.failureTextField.setLineWrap(true);
        this.failureTextField.setWrapStyleWord(true);
        this.failureTextField.setAutoscrolls(true);
        this.failureTextField.setBackground(this.getBackground());
        this.failureTextField.setFont(this.ruleVersionTextField.getFont());
        this.failureTextField.setCaretPosition(0);
        final JViewport port = scroller.getViewport();
        port.add(failureTextField);

        this.add(scroller);
        scroller.setBounds(140, 240, 441, 21 * 4);
    }


    final public void setModel(final ModuleProperties prop) {
        this.prop = prop;
        updateView();
    }

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



    /**
     * Update from model.
     */
    final public void updateView() {
        if (this.prop != null) {
            this.stateTextField.setText(this.prop.getStateDescription());
            this.nameTextField.setText(this.prop.getName());
            this.moduleVersionTextField.setText(this.prop.getModuleVersion());
            this.ruleVersionTextField.setText(this.prop.getRuleVersion());
            this.urlTextField.setText(this.prop.getUrl());
            this.failureTextField.setText(this.prop.getException() == null ? ""
                : this.prop.getException().getMessage());
            this.repaint();
        } else {
            this.stateTextField.setText("");
            this.nameTextField.setText("");
            this.moduleVersionTextField.setText("");
            this.ruleVersionTextField.setText("");
            this.urlTextField.setText("");
            this.failureTextField.setText("");
            this.repaint();
        }
    }


}
