// This is example code for the install4j API
// Please feel free to adapt it for your own purposes.

import com.install4j.api.*;

import java.io.File;

// This action shows how the ProgressInterface can be used
// in a custom action.
public class ProgressInterfaceExampleAction extends InstallAction {

    // You override the performAction method of the InstallAction base class
    // to add your own code.
    public boolean performAction(InstallerWizardContext context, ProgressInterface progressInterface) throws UserCanceledException {

        // With the setStatusMessage you give feedback to the user on what
        // you're doing
        progressInterface.setStatusMessage("Performing 90 steps");

        for (int i = 0; i <= 90; i++) {
            // With the setDetailMessage you can signal activity to the user,
            // e.g. by printing the names of installed files or similar.
            progressInterface.setDetailMessage("Step " + i);

            // With the setPercentCompleted method you can advance the progess bar.
            // The percent range at your disposal is always 0-100%
            progressInterface.setPercentCompleted(i);

            // Now do something that takes time
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {

            }
        }

        // If you have your own method to extract, generate or install files,
        // you might find the ask* methods of the ProgressInterface useful
        if (progressInterface.askContinue(new File("test.txt"))) {
            // If things go bad, you can present an error message to the user
            // with the showFailure method:
            progressInterface.showFailure("Installation failed. Good bye.");
            // In such a case you should return false from performAction
            // in order to stop the installation.
            return false;
        }

        // at the end it's a good idea to call setPercentCompleted with the 100% value.
        progressInterface.setPercentCompleted(100);

        return true;
    }


    // has to be implemented in order to tell the installer how much
    // you want of the progress bar.
    public int getPercentOfTotalInstallation() {
        return 50;
    }
}
