// $Id: AboutDialog.java,v 1.6 2004/09/18 12:21: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;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.meyling.principia.gui.puzzle.ToInfinityAndBeyond;
import com.meyling.principia.module.ModuleContext;


/**
 * Informations about this project.
 *
 * @version $Revision: 1.6 $
 * @author  Michael Meyling
 */
public final class AboutDialog extends JDialog {

    private JLabel imageLabel;
    private ImageIcon icon;

    private final String aboutText = "Prototyp \"Principia Mathematica II\" of project \"Hilbert II\"";;
    public final static String aboutTitle = "Hilbert II";
    private final String aboutCopyright = "(c) Michael Meyling";

    private JButton okButton;
    private JTextArea textArea;
    private JTextArea textArea2;
    private JPanel picPanel;
    private JPanel textPanel;
    private JPanel okPanel;

    private JScrollPane scrp;


    /**
     * Constructor, calls {@link #setUp()}.
     */
    public AboutDialog(final Frame owner) {
        super(owner);
        this.setUp();
    }


    /**
     * Set up gui.
     */
    private void setUp() {
        getContentPane().setLayout(new BorderLayout());
        icon = new ImageIcon(getClass().getResource("/com/meyling/principia/pic/about.gif"));
        imageLabel = new JLabel(icon);

        setTitle(aboutTitle);
        setBackground(new Color(192, 192, 192));

        okButton = new JButton("Ok");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                AboutDialog.this.setVisible(false);
            }
        });


        textArea = new JTextArea();
        textArea.append("\n" + this.aboutText + "\n");
        textArea.append("Version: " + ModuleContext.getKernelVersion() + "\n");
        textArea.append(aboutCopyright + "\n");
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setEditable(false);
        textArea.setBackground(getPicPanel().getBackground());

        textArea2 = new JTextArea();
        textArea2.append("Operating System: "
                + System.getProperty("os.name") + "\n");
        textArea2.append("Java Version: " +
            System.getProperty("java.version") + "\n" );
        textArea2.append("\n\n");
        textArea2.append("Production Babies:\n");
        textArea2.append("\tLennart\n");
        textArea2.setLineWrap(true);
        textArea2.setWrapStyleWord(true);
        textArea2.setEditable(false);
        textArea2.setBackground( new Color(221,221,221));
        textArea2.setCaretPosition(0);

        scrp = new JScrollPane (textArea2, JScrollPane
                .VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrp.setPreferredSize(new Dimension(320,140));

        getPicPanel().add(imageLabel);
        getPicPanel().setBorder(BorderFactory.createEmptyBorder(10,10,0,10));
        getTextPanel().add(textArea);
        getTextPanel().add(scrp);
        getOkPanel().add(okButton);

        getContentPane().add( getPicPanel(), BorderLayout.WEST);
        getContentPane().add( getTextPanel(), BorderLayout.EAST);
        getContentPane().add( getOkPanel(), BorderLayout.SOUTH);
        getRootPane().setDefaultButton(okButton);

        setResizable(false);
        setSize(500,300);
        pack();
        textArea2.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent evt) {
                if (evt.getKeyCode() == KeyEvent.VK_ENTER
                        || evt.getKeyCode() == KeyEvent.VK_SPACE)
                    okButton.doClick();
            }
        });
        textArea.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent evt) {
                if (evt.getKeyCode() == KeyEvent.VK_ENTER
                        || evt.getKeyCode() == KeyEvent.VK_SPACE)
                    okButton.doClick();
            }
        });
        this.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent event) {
                if (event.isShiftDown() && event.isControlDown()) {
                    AboutDialog.this.setVisible(false);

                    final ToInfinityAndBeyond infinity = new ToInfinityAndBeyond();
                    infinity.show(); 
/*                    
                    final Puzzle puzzle = new Puzzle();
//                    puzzle.setIgnoreRepaint(true);    // TODO clashes with Field!
                    puzzle.show();
*/                    
                }
            }
        });

    }


    /**
     * Liefert das TextPanel. Wenn noch keins da, wird eins erzeugt.
     * @return JPanel das TextPanel
     */
    private JPanel getTextPanel() {
        if (null == textPanel) {
            textPanel = new JPanel();
            textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));
        }
        return textPanel;
    }


    /**
     * Liefert das PicPanel. Wenn noch keins da, wird eins erzeugt.
     * @return JPanel das PicPanel
     */
    private JPanel getPicPanel() {
        if (null == picPanel) {
            picPanel = new JPanel();
        }
        return picPanel;
    }


    /**
     * Liefert das OKPanel. Wenn noch keins da, wird eins erzeugt.
     * @return JPanel das OkPanel
     */
    private JPanel getOkPanel() {
        if (null == okPanel) {
            okPanel = new JPanel();
        }
        return okPanel;
    }


}
