/**
    WinRegistry.java
    Change the Windows registry

    Notes: 
     1. You must adapt the setRegistry() method to meet your needs.
     2. If you prefer to simply import a .reg file and you feel confident
        that your customers' machines will have Regedit.exe or Regedit32.exe
        in their Windows or WinNT directory, then you could use the sample
        WinImportRegFile class.
     3. You are welcome to adapt this to meet your needs as long as you only use 
        it with the JExpress product
     4. After you change the source, compile it, and specify the directory
        name where the resulting class file is in the "Custom" panel of the
        Advanced interface to JExpress. Then click the "After uninstaller created"
        button and select the WinRegistry class.
        
    Copyright(C) 1998-2011 DeNova    
*/


import javax.swing.JLabel;
import java.io.File;
import java.awt.BorderLayout;
import com.denova.io.Log;
import com.denova.runtime.WindowsRegistry;
import com.denova.runtime.WindowsUtils;
import com.denova.ui.Fonts;
import com.denova.ui.WizardPanel;
import com.denova.util.PropertyList;
import com.denova.JExpress.Installer.CustomUninstaller;
import com.denova.JExpress.Installer.InstallPropertyNames;



public class WinRegistry extends WizardPanel
                         implements InstallPropertyNames {

    public WinRegistry(PropertyList properties) {

        // initialize WizardPanel superclass
        super(properties);

        // let them know what's happening
        setLayout(new BorderLayout());
        JLabel notice = new JLabel("Configuring the registry", JLabel.CENTER);
        notice.setFont(Fonts.Bold);
        add(notice);
    }

    /** 
     * Enter the panel. 
     *
     * Do not call this method; only the wizard should call it.
     */
     public synchronized void enter()
     {
        // we don't actually want to interact with the user
        try {

            // if we're on Windows, add the environment variable to autoexec.bat
            if(isWindows()) {
                setRegistry();
            }

        }
        catch(Exception e) {

            // if there's no file, then there's nothing to do
            debug("exception while configuring registry");
            log(e);
        }

        // we don't want any user interaction
        // so move them to the next panel automatically
        showNextPanel();
    }

    public void setRegistry()
    throws Exception {

        // clear any errors
        WindowsUtils. clearError();

        String packageName = getPropertyList(). getProperty(PackageName, "");
        String version = getPropertyList(). getProperty(PackageVersion, "");
        String applicationDirectory = getPropertyList(). getProperty(ApplicationDirectory, "");

        if(packageName. length() > 0) {
            boolean ok;

            final String PrimaryKey = "HKEY_LOCAL_MACHINE\\SOFTWARE";

            WindowsRegistry.addKey(PrimaryKey, packageName);
            ok = WindowsRegistry.replaceData(PrimaryKey + "\\" + packageName,
                                                 "Version",
                                                 version);
            ok = WindowsRegistry.replaceData(PrimaryKey + "\\" + packageName,
                                                 "StartDir",
                                                 applicationDirectory);

           // remove these registry entries during the uninstall
           String directory = applicationDirectory + File. separator + "JExpress";
           CustomUninstaller customUninstaller = new CustomUninstaller();
           try {
               customUninstaller.deleteAttribute(PrimaryKey + "\\" + packageName, "Version");
               customUninstaller.deleteAttribute(PrimaryKey + "\\" + packageName, "StartDir");
               customUninstaller.deleteKey(PrimaryKey, packageName);

               customUninstaller.append(applicationDirectory);
           }

           catch(Exception e) {
               // if anything goes wrong, just don't blow up
           }
        }
    }

    // don't let the user bypass this command before we're done
    public boolean isNextButtonEnabled() {
        return false;
    }

    // don't let the user back up before we're done
    public boolean isPreviousButtonEnabled() {
        return false;
    }

    void debug(String s) {
        if(debugging) {
            log(s);
        }
    }

    static private void log(String s) {
        startLog();
        regLog. write(s);
    }

    static private void log(Exception e) {
        startLog();
        regLog. setLogging(true);
        regLog. write(e);
    }

    static private void startLog() {
        if(regLog == null) {
            regLog = new Log("winreg");
            regLog. setLogging(true);
        }
    }

    static private void stopLogging() {
        if(regLog != null) {
            regLog. stopLogging();
        }
    }

    static Log regLog;
    static final boolean debugging = false;

}  // WinRegistry