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

import java.io.File;

// This uninstall action removes a file association on Windows.
// In the install4j GUI you can configure file associations
// with the launchers, that are automatically removed by the
// uninstaller but if you create your own associations,
// can proceed as outlined in this class.
//
// This is the corresponding class to the
// AssociateFileTypeAction.java InstallAction that
// creates an association during the installation.
//
// This class should be registered as a pre-uninstall action in
// the install4j GUI.
public class UnassociateFileTypeAction extends UninstallAction {

    // You implement the performAction method of the UninstallAction base class
    // to add your own code.
    public boolean performAction(Context context, ProgressInterface progressInterface) {
        return unassociateFileType(context);
    }

    // If you want the unattended installer to remove the file type
    // as well, you have to override the performUnattendedAction
    // method. The actual code has been factored out to the
    // separate unassociateFileType method.
    public boolean performUnattendedAction(Context context) {
        return unassociateFileType(context);
    }

    // Helper method that removes the association
    private boolean unassociateFileType(Context context) {

        // The installation directory is available in the context
        File installationDirectory = context.getInstallationDirectory();

        // The WinAssociations API does the actual work.
        // If no association exists, we do not try to remove it
        if (!WinAssociations.exists("xyz")) {

            // The currently associated executable can be retrieved in this way:
            File currentExecutable = new File(WinAssociations.getExecutable("xyz"));

            // The expected executable is:
            File ownExecutable = new File(installationDirectory, "xyzlauncher.exe");

            // We only want to remove the association, if it is associated to the
            // correct file, otherwise another application may have created the
            // association
            if (ownExecutable.equals(currentExecutable)) {
                WinAssociations.remove("xyz");
            }
        }

        return true;
    }

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