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

// This action creates a key in the Windows registry and writes
// data to it.
//
// Please see DeleteFromRegistryAction.java on how to remove
// the association in an UninstallAction.
//
// This class should be registered as a pre- or post-install action in
// the install4j GUI.
public class WriteToRegistryAction 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 writeToRegistry();
    }

    // If you want the unattended installer to write to the registry
    // 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 writeToRegistry();
    }

    // Helper method that writes to the Windows regstry
    private boolean writeToRegistry() {

        // The WinRegistry class does the actual work. The createKey method
        // creates a key (this is like a folder for values) and
        // returns if the creation was sucessful.
        if (WinRegistry.createKey(WinRegistry.HKEY_CURRENT_USER, "myKey")) {
            // The setValue method creates a value in a specified key.
            // You can set different data types, like String, Integer, String[] or byte[]
            // that can be retreived with the getValue method.
            WinRegistry.setValue(WinRegistry.HKEY_CURRENT_USER, "myKey", "myValue", "test");
        }
        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;
    }
}
