/*
    ExampleCommand

    Note: 
     1. You should include this class "After install" on the Custom panel.
     2. You must change this class to do whatever you want it to do. In the original form, it'
        trying to run a program called mySpecialInstallerExecutable. Of course, you'd want to
        rename this to whatever program you want to run and if the program isn't cross platform,
        address that in the section of the code marked by <-------> comment lines.
     3. You're welcome to adapt this class to use with a JExpress Installer or Updater.
     
    Copyright (C) 1998-2008 DeNova
    All rights reserved worldwide.
  
    Last modified: 2008.03.19
*/

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


public class ExampleCommand
    extends WizardPanel
    implements Runnable,
               InstallPropertyNames,
               InstallerConstants
{
    // you'd need to include this file in the list on the Files panel in JExpress Developer
    private static final String SpecialInstallerFilename = "mySpecialInstallerExecutable";

    /**
     * Creates a new ExampleCommand object.
     * 
     * @param properties installer's properties
     */
    public ExampleCommand (PropertyList properties)
    {
        super (properties);
        setLayout ( new BorderLayout() );
        JLabel myLabel = new JLabel( "Working" );
        myLabel.setFont(Fonts.Bold);
        add ( myLabel, 
              BorderLayout.CENTER );
    }

    /**
     * Is the "previous" button enabled.
     * 
     * @return true if the previous button is active.
     */
    public boolean isPreviousButtonEnabled ()
    {
        // this panel has no user interaction
        // so don't let the user get ahead of us
        return false;
    }

    /**
     * Is the "next" button enabled.
     * 
     * @return true if the "next" button is active.
     */
    public boolean isNextButtonEnabled ()
    {
        // this panel has no user interaction
        // so don't let the user get ahead of us
        return false;
    }

    /** 
     * Enter the panel. Method where the work for the class is done.
     *
     * See RunThirdPartyProgram for a more complete example.
     *
     * Do not call this method; only the wizard should call it.
     */
    public void enter()
    {
        // Start another thread if the panel
        // is doing something time consuming.
        // If you want to do some work and then
        // update the user about the results, then
        // use SwingWorker(). Or, if you want to 
        // update Swing components, do some time
        // consuming work, and then update more
        // Swing components, use Swinger().
        new SwingCheckerThread( this ).start();
    }

    /**
     * Run your code in a separate thread.
     *
     * you always want to create a separate thread
     * if your custom command will be taking a long time
     * instead of just interacting with the user
     */
    public void run ()
    {

        // change the code from here <------------------>
        String applicationDirectory = getPropertyList ().getProperty ( 
                                              ApplicationDirectory, 
                                              "" );
        File f = new File( applicationDirectory, 
                           SpecialInstallerFilename );

        if ( f.exists () ) {

            try {

                String [] command = { f.getPath (), SpecialInstallerFilename };
                Exec.runCommand ( command );
                CustomInstaller.logToInstaller ( SpecialInstallerFilename + " run" );
            }
            catch ( Exception e ) {
                CustomInstaller.logException ( "Unable to install", 
                                               e );
            }
        }
        else {
            CustomInstaller.logToInstaller ( "Special installer (" + SpecialInstallerFilename + ") is not located in " + applicationDirectory + "\nUnable to install." );
        }

        // to here <------------------>

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