// 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;

// On Windows, this action deletes entries from the program
// group you're installing to. This is useful if you want to
// clean up the program group during an update, e.g. if you
// have moved certain entries to a sub-folder and the old
// entries should be removed.
//
// Please see CreateProgramGroupEntriesAction.java on how to
// programatically create entries in a program group.
//
// This class should be registered as a post-install action in
// the install4j GUI.
public class DeleteFromProgramGroupAction 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("Cleaning up program group ...");
            removeFromProgramGroup(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 remove the entries
    // as well, you have to override the performUnattendedAction
    // method. The actual work has been factored out to the
    // separate removeFromProgramGroup method.
    public boolean performUnattendedAction(InstallerContext context) {
        // Only perform this action on Windows
        if (Util.isWindows()) {
            removeFromProgramGroup(context);
        }
        return true;
    }

    private void removeFromProgramGroup(InstallerContext context) {
        // A previous installation might have installed a program
        // group for all users or for the current user only,
        // so we have to delete in two different locations
        cleanupProgramGroup(context, true);
        cleanupProgramGroup(context, false);
    }

    private void cleanupProgramGroup(InstallerContext context, boolean allUsers) {

        // The WinFileSystem class provides the location of the program group folder
        File parentFolder = WinFileSystem.getSpecialFolder(WinFileSystem.FOLDER_PROGRAMS, allUsers);
        // The selected program group is available from the context
        File programGroupFolder = new File(parentFolder, context.getProgramGroup());

        // Delete the entry "My old entry". The actual file has the extension ".lnk"
        deleteFile(programGroupFolder, "My old entry.lnk");
        // Also delete the entry "My other old entry".
        deleteFile(programGroupFolder, "My other old entry.lnk");
    }

    // Helper method to delete a file
    private void deleteFile(File programGroupFolder, String fileName) {
        new File(programGroupFolder, fileName).delete();
    }

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