/*
    WinImportRegFile.java
    Import install.reg file into the Windows registry

    Notes: 
     1. You must prepare install.reg so it is compatible with
        Regedit.exe and Regedit32.exe. Regedit.exe or regedit32.exe
        must be in the user's Windows directory for this class to succeed.
     2. The install.reg file must be in the JExpressInstaller 
        subdirectory when you create the installer.
     3. You may include this class in any of the fields on the Custom panel.
     4. You are welcome to adapt this to meet your needs as long as you 
        only use it with the JExpress product

    Copyright (C) 1998-2008 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.CustomInstaller;


public class WinImportRegFile extends WizardPanel {

    public WinImportRegFile ( 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, import the registry file
            if ( isWindows() ) {
                importRegistry ();
            }

        }
        catch ( Exception e ) {

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

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

    public void importRegistry ()
    throws Exception {

        // clear any errors
        WindowsUtils. clearError ();

        // get the .reg file from the JAR
        File regFile = new File ( regFilename );
        CustomInstaller. getResourceAsFile ( regFile. getPath () );
        log ( "extracted " + regFile. getPath () );

        if ( ! WindowsRegistry. importEntries ( regFile. getName () ) ) {
            log ( "error while importing reg file: " + WindowsUtils. getLastError () );
        }
    }

    // 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 ) {
            startLog ();
            regLog. setLogging ( true );
            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" );
        }
    }

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

    static final private String regFilename = "install.reg";

    static Log regLog;
    static final boolean debugging = false;

}  // WinImportRegFile