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

// This action creates a link on the Windows desktop. In the
// install4j GUI you can configure that a desktop link should be
// created for a single launcher. If you need more flexibility, you
// can proceed as outlined in this action.
//
// This class should be registered as a post-install action in
// the install4j GUI.
public class CreateDesktopLinkAction 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 createDesktopLink(context);
    }

    // If you want the unattended installer to create the desktop
    // link as well, you have to override the performUnattendedAction
    // method. The actual installation has been factored out to the
    // separate createDesktopLink method.
    public boolean performUnattendedAction(InstallerContext context) {
        return createDesktopLink(context);
    }

    // Helper method that sets the environment variable
    private boolean createDesktopLink(InstallerContext context) {

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

        // The WinFileSystem class gives us the desktop folder. The second parameter
        // specifies if a global version for all users of a special folder should be used
        // if possible.
        File desktopFolder = WinFileSystem.getSpecialFolder(WinFileSystem.FOLDER_DESKTOP, false);

        // The actual file for the desktop link is:
        File linkFile = new File(desktopFolder, "Readme File");
        // The target file the desktop links to:
        File targetFile = new File(installationDirectory, "readme.txt");

        // The createShellLink method creates the actual link. The
        // third parameter describes a possible icon file.
        WinFileSystem.createShellLink(linkFile, targetFile, null);

        return true;
    }

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