/*
 * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
 * 
 * http://izpack.org/
 * http://izpack.codehaus.org/
 * 
 * Copyrght 2008 Patrick Zbinden.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.izforge.izpack.installer.base;

import com.izforge.izpack.util.Debug;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

/**
 * Common HTML Help Window (modal)
 */
public class HelpWindow extends JDialog implements HyperlinkListener, ActionListener
{

    private static final long serialVersionUID = -357544689286217809L;

    private JPanel contentPane = null;

    private JEditorPane htmlHelp = null;

    private JButton closeButton = null;

    private JScrollPane scrollPane = null;

    private String closeButtonText = "Close";

    /**
     * This is the default constructor
     *
     * @param owner           - owner Frame
     * @param closeButtonText - Button Text for Close button
     */
    public HelpWindow(Frame owner, String closeButtonText)
    {
        super(owner, true);
        this.closeButtonText = closeButtonText;
        initialize();
    }

    /**
     * This method initializes Help Dialog
     */
    private void initialize()
    {
        this.setSize(600, 400);
        this.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        this.setContentPane(getJContentPane());
    }

    /**
     * This method initializes jContentPane
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane()
    {
        if (contentPane == null)
        {
            contentPane = new JPanel();
            contentPane.setLayout(new BoxLayout(getJContentPane(), BoxLayout.Y_AXIS));
            contentPane.add(getScrollPane(), null);
            contentPane.add(getCloseButton(), null);
        }
        return contentPane;
    }

    /**
     * This method initializes _htmlHelp
     *
     * @return javax.swing.JEditorPane
     */
    private JEditorPane getHtmlHelp()
    {
        if (htmlHelp == null)
        {
            try
            {
                htmlHelp = new JEditorPane();
                htmlHelp.setContentType("text/html"); // Generated
                htmlHelp.setEditable(false);
                htmlHelp.addHyperlinkListener(this);
            }
            catch (java.lang.Throwable e)
            {
                Debug.log(e.getLocalizedMessage());
            }
        }
        return htmlHelp;
    }

    private JScrollPane getScrollPane()
    {
        if (scrollPane == null)
        {
            try
            {
                scrollPane = new JScrollPane(getHtmlHelp());
            }
            catch (java.lang.Throwable e)
            {
                Debug.log(e.getLocalizedMessage());
            }
        }
        return scrollPane;
    }

    /**
     * This method initializes _btnClose
     *
     * @return javax.swing.JButton
     */
    private JButton getCloseButton()
    {
        if (closeButton == null)
        {
            try
            {
                closeButton = new JButton(closeButtonText);
                closeButton.setAlignmentX(CENTER_ALIGNMENT);
                closeButton.addActionListener(this);
            }
            catch (java.lang.Throwable e)
            {
                Debug.log(e.getLocalizedMessage());
            }
        }
        return closeButton;
    }

    public void hyperlinkUpdate(HyperlinkEvent e)
    {
        try
        {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
            {
                getHtmlHelp().setPage(e.getURL());
            }
        }
        catch (Exception err)
        {
            // Ignore exceptions
        }
    }

    /**
     * displays Help Text in a modal window
     *
     * @param title
     * @param helpDocument
     */
    public void showHelp(String title, URL helpDocument)
    {
        this.setTitle(title);
        try
        {
            getHtmlHelp().setPage(helpDocument);
        }
        catch (IOException e)
        {
            Debug.log(e.getLocalizedMessage());
        }
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        // Close button pressed
        this.setVisible(false);
    }
}

@@ -934,21 +934,17 @@
 ava.awt.
-Frame
+*
 ;%0Aimport
@@ -1060,16 +1060,60 @@
 t.URL;%0A%0A
+import static java.awt.Desktop.getDesktop;%0A%0A
 /**%0A * C
@@ -4305,42 +4305,338 @@
     
-getHtmlHelp().setPage(e.getURL());
+if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) %7B%0A    %09%09%09%09String url = e.getURL().toString();%0A    %09%09%09%09if (url.startsWith(%22#%22))%0A                    %7B%0A    %09%09%09%09%09getHtmlHelp().setPage(e.getURL());%0A    %09%09%09%09%7D%0A                    else%0A                    %7B%0A    %09%09%09%09%09getDesktop().browse(e.getURL().toURI());%0A    %09%09%09%09%7D%0A    %09%09%09%7D
 %0A   
@@ -4712,28 +4712,23 @@
     
-// Ignore exceptions
+Debug.log(err);
 %0A   

Fix for IZPACK-546.