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

import com.install4j.api.*;
import com.install4j.api.windows.WinFileSystem;

import java.io.File;
import java.io.IOException;

// On Windows, this action creates a sub-folder and entries
// for an entire directory in the selected program group.
//
// Please see DeleteFromProgramGroupAction.java on how to
// programatically remove entries from a program group.
//
// This class should be registered as a post-install action in
// the install4j GUI.
public class CreateProgramGroupEntriesAction 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 {
        // Only perform this action on Windows
        if (Util.isWindows()) {
            // Notify the user about what you're doing
            progressInterface.setStatusMessage("Creating additional program group entries ...");
            createProgramGroupEntries(context);
            // Advance the progress bar. The 100% refers to the progress range that
            // this action has been assigned by the installer.
            progressInterface.setPercentCompleted(100);
        }
        return true;
    }

    // If you want the unattended installer to create the entries
    // as well, you have to override the performUnattendedAction
    // method. The actual work has been factored out to the
    // separate createProgramGroupEntries method.
    public boolean performUnattendedAction(InstallerContext context) {
        // Only perform this action on Windows
        if (Util.isWindows()) {
            createProgramGroupEntries(context);
        }
        return true;
    }

    private void createProgramGroupEntries(InstallerContext context) {

        // The target files are in the directory "doc"
        // in the installation directory, for example a number of
        // PDF files.
        File docDir = new File(context.getInstallationDirectory(), "doc");
        File[] files = docDir.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                // For each file, we create an entry
                createProgramGroupEntry(files[i], context);
            }
        }
    }

    // Helper method to create a program group entry
    private void createProgramGroupEntry(File file, InstallerContext context) {
        try {
            // Here, the name of the program group entry is simply the file name.
            // All entries are placed in the "Documentation" sub-folder.
            String entryName = "Documentation\\" + file.getName();
            // With the WinFileSystem class we can create program group entries.
            // The context tells WinFileSystem whether the user wishes to create program
            // group entries at all or is they should be created for all users or not,
            // so you don't have to handle these cases.
            WinFileSystem.createStartMenuEntry(context, entryName, file, null);
        } catch (IOException e) {
            // we continue the installation, even if it does not work for some reason
        }
    }

    // How much do we want of the progress bar? Only on Windows we require 5%.
    public int getPercentOfTotalInstallation() {
        return Util.isWindows() ? 5 : 0;
    }
}
