// 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 uninstall action deletes a value from the Windows registry
//
// This is the corresponding class to the
// WriteToRegistryAction.java InstallAction that
// writes to the registry during the installation.
//
// This class should be registered as a pre- or post-uninstall action in
// the install4j GUI.
public class DeleteFromRegistryAction 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 deleteFromRegistry();
    }

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

    // Helper method that deletes a value from the registry
    private boolean deleteFromRegistry() {

        // Prior to deleting the key we check if it exists at all
        if (WinRegistry.keyExists(WinRegistry.HKEY_CURRENT_USER, "myKey")) {
            // Here, we delete the value "myValue" only if its value is equal to "test".
            // That's what we wrote to this value in WriteToRegistryAction.java
            if (WinRegistry.getValue(WinRegistry.HKEY_CURRENT_USER, "myKey", "myValue").equals("test")) {
                WinRegistry.deleteValue(WinRegistry.HKEY_CURRENT_USER, "myKey", "myValue");
                // if the key has no further values, we would like to delete it, too.
                WinRegistry.deleteKey(WinRegistry.HKEY_CURRENT_USER, "myKey", true);
            }
        }

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