// 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 action creates a file association on Windows.
// In the install4j GUI you can configure file associations
// with the launchers, but if you need more flexibility, you
// can proceed as outlined in this class.
//
// Please see UnassociateFileTypeAction.java on how to remove
// the association in an UninstallAction.
//
// This class should be registered as a post-install action in
// the install4j GUI.
public class AssociateFileTypeAction 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 {
        return associateFileType(context);
    }

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

    // Helper method that creates the association
    private boolean associateFileType(InstallerContext context) {

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

        // For a file association, we need an executable, that will be invoked
        // when a corresponding file is double-clicked in the explorer.
        File executable = new File(installationDirectory, "xyzlauncher.exe");

        // The WinAssociations API does the actual work.
        // Here, we do not want to create an association, if an assocation already exists.
        if (!WinAssociations.exists("xyz")) {
            WinAssociations.create("xyz", "XYZ project file", null, executable);
        }

        return true;
    }

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