/**
 * MediaSniper 3.0 (2008-07-27)
 * Copyright 2007 - 2008 Zach Scrivena
 * zachscrivena@gmail.com
 * http://mediasniper.sourceforge.net/
 *
 * Simple program for downloading media files from popular websites.
 *
 * TERMS AND CONDITIONS:
 * This program 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 3 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.freeshell.zs.mediasniper;

import java.awt.Desktop;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URI;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import org.freeshell.zs.common.ResourceManipulator;
import org.freeshell.zs.common.SwingManipulator;


/**
 * Represent an "About" form.
 */
class About
        extends JFrame
{
    /** parent MediaSniper object */
    private final MediaSniper parent;


    /**
    * Constructor.
    *
    * @param parent
    *      parent MediaSniper object
    */
    About(
            final MediaSniper parent)
    {
        /*********************
        * INITIALIZE FIELDS *
        *********************/

        this.parent = parent;

        /******************************
        * INITIALIZE FORM COMPONENTS *
        ******************************/

        initComponents();

        /***************************
        * CONFIGURE FORM SETTINGS *
        ***************************/

        setTitle("About - " + parent.getTitle());

        final String header =
                parent.properties.getString("name") + " " +
                parent.properties.getString("version") +
                " (" + parent.properties.getString("date") + ")\n" +
                parent.properties.getString("copyright") + "\n" +
                parent.properties.getString("email") + "\n" +
                parent.properties.getString("homepage") + "\n" +
                "\nDefinitions last updated on " + parent.properties.getString("definitions.last.updated") + "." +
                "\n" + parent.properties.getString("definitions.comment");

        try
        {
            aboutText.setText(
                    header + "\n\n" +
                    ResourceManipulator.resourceAsString(parent.properties.getString("about")));
        }
        catch (Exception e)
        {
            aboutText.setText(header);

            SwingManipulator.showErrorDialog(parent, getTitle(),
                    "(INTERNAL) Failed to read \"About\" text from JAR file.");
        }

        aboutText.setToolTipText("About " + parent.properties.getString("name"));
        aboutText.setCaretPosition(0);
        aboutText.setFont(new Font(
                Font.DIALOG,
                Font.PLAIN,
                aboutText.getFont().getSize() - 2));

        addWindowListener(new WindowAdapter()
        {
            @Override
            public void windowClosing(WindowEvent e)
            {
                closeForm();
            }
        });

        /* inherit "always on top" behavior of parent */
        try
        {
            setAlwaysOnTop(parent.isAlwaysOnTop());
        }
        catch (Exception e)
        {
            /* ignore */
        }

        /* inherit program icon of parent */
        final List<Image> icons = parent.getIconImages();

        if (!icons.isEmpty())
        {
            setIconImage(icons.get(0));
        }

        /* button: "Visit Forum" */
        forumButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (Desktop.isDesktopSupported())
                {
                    try
                    {
                        Desktop.getDesktop().browse(new URI(parent.properties.getString("forum")));
                    }
                    catch (Exception ex)
                    {
                        /* ignore */
                    }
                }
            }
        });

        /* button: "Visit Homepage" */
        homeButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (Desktop.isDesktopSupported())
                {
                    try
                    {
                        Desktop.getDesktop().browse(new URI(parent.properties.getString("homepage")));
                    }
                    catch (Exception ex)
                    {
                        /* ignore */
                    }
                }
            }
        });

        /* button: "Report Bug" */
        bugButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (Desktop.isDesktopSupported())
                {
                    try
                    {
                        Desktop.getDesktop().browse(new URI(parent.properties.getString("bug.report")));
                    }
                    catch (Exception ex)
                    {
                        /* ignore */
                    }
                }
            }
        });

        /* button: "OK" */
        closeButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                closeForm();
            }
        });

        /* key binding: ESCAPE key */
        aboutPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
                .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "ESCAPE_CANCEL_BUTTON");

        aboutPane.getActionMap().put("ESCAPE_CANCEL_BUTTON", new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                closeButton.doClick();
            }
        });

        /* center form on the parent form */
        setLocationRelativeTo(parent);
    };


    /**
    * Close the "About" form.
    */
    private void closeForm()
    {
        setVisible(false);
    }

    /***************************
    * NETBEANS-GENERATED CODE *
    ***************************/

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        title = new javax.swing.JLabel();
        aboutPane = new javax.swing.JScrollPane();
        aboutText = new javax.swing.JTextArea();
        closeButton = new javax.swing.JButton();
        buttonsPanel = new javax.swing.JPanel();
        homeButton = new javax.swing.JButton();
        forumButton = new javax.swing.JButton();
        bugButton = new javax.swing.JButton();

        setResizable(false);

        title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        title.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/freeshell/zs/mediasniper/resources/splashscreen.png"))); // NOI18N

        aboutText.setColumns(20);
        aboutText.setEditable(false);
        aboutText.setFont(aboutText.getFont());
        aboutText.setLineWrap(true);
        aboutText.setRows(5);
        aboutText.setTabSize(4);
        aboutText.setWrapStyleWord(true);
        aboutPane.setViewportView(aboutText);

        closeButton.setMnemonic('C');
        closeButton.setText("Close");
        closeButton.setNextFocusableComponent(closeButton);

        buttonsPanel.setLayout(new java.awt.GridLayout(1, 0));

        homeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/freeshell/zs/mediasniper/resources/house.png"))); // NOI18N
        homeButton.setMnemonic('h');
        homeButton.setText("<html>Visit<br /><u>H</u>omepage</html>");
        homeButton.setIconTextGap(8);
        homeButton.setNextFocusableComponent(homeButton);
        buttonsPanel.add(homeButton);

        forumButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/freeshell/zs/mediasniper/resources/comments.png"))); // NOI18N
        forumButton.setMnemonic('f');
        forumButton.setText("<html>Visit<br /><u>F</u>orum</html>");
        forumButton.setIconTextGap(8);
        forumButton.setNextFocusableComponent(forumButton);
        buttonsPanel.add(forumButton);

        bugButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/freeshell/zs/mediasniper/resources/bug.png"))); // NOI18N
        bugButton.setMnemonic('b');
        bugButton.setText("<html>Report<br /><u>B</u>ug</hmtl>");
        bugButton.setIconTextGap(8);
        bugButton.setNextFocusableComponent(bugButton);
        buttonsPanel.add(bugButton);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonsPanel, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                    .addComponent(aboutPane, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                    .addComponent(title, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                    .addComponent(closeButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(title)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(aboutPane, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane aboutPane;
    private javax.swing.JTextArea aboutText;
    private javax.swing.JButton bugButton;
    private javax.swing.JPanel buttonsPanel;
    private javax.swing.JButton closeButton;
    private javax.swing.JButton forumButton;
    private javax.swing.JButton homeButton;
    private javax.swing.JLabel title;
    // End of variables declaration//GEN-END:variables
}