/*
    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 is included in the list of distributed files.
     2. Include RunThirdPartyProgram in "After files installed", 
        "After menus installed", or "After uninstaller created"
     3. You are welcome to adapt this to meet your needs as long as you
        only use it with the JExpress product
    Copyright 1998-2010 DeNova
    
*/

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.Swinger;
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 InstallPropertyNames
{
    private static final String ProgramName = "myprogram";

    private boolean panelActivated = false;
    private String progressLabel = "Installing " + ProgramName;

    
    private class ThirdPartySwinger extends Swinger
    {
        public void swingBefore()
        {
            updateProgressBarLabel(progressLabel);
        }
        
        public void workInBackground()
        {
            String applicationDirectory = getPropertyList().getProperty(ApplicationDirectory, "");
            File exeFile = new File(applicationDirectory, ProgramName);
            if (exeFile.exists())
            {
                try {
                    String [] command = {exeFile. getPath()};
    
                    Exec. runCommand  (command);
                }
                
                catch ( Exception e )
                {
                   CustomInstaller.logException ( "Unable to run program",
                                                  e );
                }
            }
            
            else {
                CustomInstaller. logToInstaller  ( ProgramName + 
                                                   " is not located in " + 
                                                   applicationDirectory +
                                                   "\nUnable to run " + ProgramName);
            }
        }
        
        public void swingAfter()
        {
            panelActivated = true;
            
            // since there's no user interaction, simply move to the next panel
            showNextPanel ();
        }
    }
    
    
    /**Constructor for the RunThirdPartyProgram object */
    public RunThirdPartyProgram(PropertyList properties)
    {
        super(properties);
        
        updateProgressBarLabel(progressLabel);
    }


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

        }

        else
        {
            ThirdPartySwinger swinger = new ThirdPartySwinger();
            swinger.execute();
        }
    }

    public void reset () {

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