/*
    WinRegistry.java
    Change the Windows registry

    Notes: 
     1. You must adapt this class to change the registry entries that 
        you want changed. The ones included here are simply to provide 
        an example.
     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 this class to meet your needs, compile it, and move
        the resulting .class file into the JExpressInstaller subdirectory. Then
        add WinRegistry to the "Customs" panel. If you need to manage the registry
        in different ways during the installation, then simply make copies of this
        class and name it appropriately.


    Copyright (C) 1998-2008 DeNova
    All rights reserved worldwide.
*/


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

            ok = WindowsRegistry. replaceData ( PrimaryKey, packageName, "" );

            if ( ok ) {
                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. deleteSubkeyEntry ( PrimaryKey + "\\" + packageName, "Version" );
                   customUninstaller. deleteSubkeyEntry ( PrimaryKey + "\\" + packageName, "StartDir" );
                   customUninstaller. deleteSubkeyEntry ( 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

