/*
    RunThirdPartyProgram
    This is an example of how to run a third party program from the installer.
    Notes:
     1. Be sure that the third party program and this modified custom class
        are all in the custom install subdirectory.
     2. Include RunThirdPartyProgram in "After files installed", 
        "After menus installed", "After uninstaller", or "After install"
     3. You are welcome to adapt this to meet your needs as long as you
        only use it with the JExpress product
    Copyright 1998-2008 DeNova
    All rights reserved worldwide.
*/

import java.awt.BorderLayout;
import java.awt.Font;
import java.io.File;
import javax.swing.JLabel;
import com.denova.runtime.Exec;
import com.denova.ui.SwingCheckerThread;
import com.denova.util.PropertyList;
import com.denova.JExpress.Installer.CustomInstaller;
import com.denova.JExpress.Installer.InstallPropertyNames;
import com.denova.JExpress.Installer.StatusPanel;


/**  Run a third party program */
public class RunThirdPartyProgram extends StatusPanel
                                  implements Runnable,
                                             InstallPropertyNames
{
    private static final String ProgramName = "myprogram";

    private boolean panelActivated = false;

    
    /**Constructor for the RunThirdPartyProgram object */
    public RunThirdPartyProgram(PropertyList properties)
    {
        super(properties);

        String configuringMessage = getLocalizedString("Configuring");
        String copyingMessage = getLocalizedString("CopyingPlatformFiles");
        String barMessage = configuringMessage + " " + copyingMessage;

        setLabels("Installing", ProgramName, "Installing " + ProgramName);
     }


    /** 
     * Enter the panel. 
     *
     * Do not call this method; only the wizard should call it.
     */
    public void enter()
    {
        if ( panelActivated )
        {
            // if another panel moves to this one
            // twice, then just bypass this panel
            showNextPanel ();

        }

        else {
            new SwingCheckerThread(this).start();
            panelActivated = true;
        }
    }

    public boolean isPreviousButtonEnabled () {
        // this panel has no user interaction
        // so don't let the user get ahead of us
	    return false;
	}

	public boolean isNextButtonEnabled () {
        // this panel has no user interaction
        // so don't let the user get ahead of us
	    return false;
	}


    public void reset () {

        // another panel can reactive this panel by calling reset
        panelActivated = false;
    }

    public void run()
    {
        String applicationDirectory = getPropertyList().getProperty(ApplicationDirectory, "");
        File exeFile = new File(applicationDirectory, ProgramName);
        
        // extract the program from the installer
        File programExe = new File(ProgramName);
        if (CustomInstaller.getResourceAsFile(ProgramName) &&
            exeFile.exists())
        {
            try {
                String [] command = {exeFile. getPath()};

                Exec. runCommand  (command);
            }
            
            catch ( Exception e )
            {
               CustomInstaller.logException ( "Unable to install program",
                                                                 e );
            }
        }
        
        else {
            CustomInstaller. logToInstaller  ( ProgramName + 
                                               " is not located in " + 
                                               applicationDirectory +
                                               "\nUnable to install " + ProgramName);
        }

        // since there's no user interaction, simply move to the next panel
        showNextPanel ();
    }

} // RunThirdPartyProgram

