package org.apache.jmeter.gui.action;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JDialog;
import javax.swing.JScrollPane;

import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.swing.HtmlPane;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.gui.ComponentUtil;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

public class Help implements Command
{
    transient private static Logger log =
        LoggingManager.getLoggerFor(JMeterUtils.GUI);

    public final static String HELP = "help";
    private static Set commands = new HashSet();

    private static String helpPage =
        "file:///"
            + JMeterUtils.getJMeterHome()
            + "/printable_docs/usermanual/component_reference.html";

    private static JDialog helpWindow;
    private static HtmlPane helpDoc;
    private static JScrollPane scroller;
    private static String currentPage;

    static
    {
        commands.add(HELP);
        helpDoc = new HtmlPane();
        scroller = new JScrollPane(helpDoc);
        helpDoc.setEditable(false);
        try
        {
            helpDoc.setPage(helpPage);
            currentPage = helpPage;
        }
        catch (IOException err)
        {
            log.error("Couldn't load " + helpPage, err);
        }
    }
    
    /**
     * @see org.apache.jmeter.gui.action.Command#doAction(ActionEvent)
     */
    public void doAction(ActionEvent e)
    {
        if (helpWindow == null)
        {
            helpWindow =
                new JDialog(
                    GuiPackage.getInstance().getMainFrame(),
                    JMeterUtils.getResString("help"),
                    false);
            helpWindow.getContentPane().setLayout(new GridLayout(1, 1));
            ComponentUtil.centerComponentInWindow(helpWindow, 60);
        }
        helpWindow.getContentPane().removeAll();
        helpWindow.getContentPane().add(scroller);
        helpWindow.show();
        if (e.getSource() instanceof String[])
        {
            String[] source = (String[]) e.getSource();
            resetPage(source[0]);
            helpDoc.scrollToReference(source[1]);
        }
        else
        {
            resetPage(helpPage);
            helpDoc.scrollToReference(
                GuiPackage
                    .getInstance()
                    .getTreeListener()
                    .getCurrentNode()
                    .getStaticLabel()
                    .replace(' ', '_'));
        }
    }

    private void resetPage(String source)
    {
        if (!currentPage.equals(source))
        {
            try
            {
                helpDoc.setPage(source);
                currentPage = source;
            }
            catch (IOException err)
            {
                log.error("Couldn't load page: " + source, err);
            }
        }
    }

    /**
     * @see org.apache.jmeter.gui.action.Command#getActionNames()
     */
    public Set getActionNames()
    {
        return commands;
    }
}

@@ -27,24 +27,47 @@
 gui.action;%0A
+import java.awt.Frame;%0A
 import java.
@@ -1679,24 +1679,27 @@
 Dialog(%0A
+%09%09%09
         
         
@@ -1694,60 +1694,85 @@
     
-            GuiPackage.getInstance().getMainF
+new Frame(),// independent frame to allow it to be overlaid by the main f
 rame
-(),
 %0A   

Allow Help Window to go to background by giving it an independent parent frame see bug 22688