/*
    Prompt the user for their user id and password
    and modify the update.control file with the info.
    
    This panel should only be called "After uninstaller created"
    so we know where to store the information

    Copyright (C) 1998-2011 DeNova
    
*/


import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.io.File;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import com.denova.ui.Fonts;
import com.denova.ui.GridBagControl;
import com.denova.ui.UserNotices;
import com.denova.ui.WizardPanel;
import com.denova.util.PropertyList;
import com.denova.JExpress.Installer.InstallPropertyNames;
import com.denova.JExpress.Updater.UpdatePropertyNames;


public class AuthenticateUpdatePanel extends WizardPanel
                                     implements InstallPropertyNames,
                                                UpdatePropertyNames {

    public AuthenticateUpdatePanel(PropertyList properties) {

        // initialize WizardPanel superclass
        super ( properties);

        addPanel();
    }
    

    /** 
     * Enter the panel. 
     *
     * Do not call this method; only the wizard should call it.
     */
    public synchronized void enter()
    {
        // there's nothing special to do because the WizardPanel
        // will automatically let the user enter the info and
        // then move to the next panel when the user
        // presses the Next button *if* all information is correct
    }

    public boolean isOk() {

        boolean ok = false;

        if ( userId != null &&
             password != null &&
             userId. getText (). length () > 0 &&
             password. getText (). length () > 0 ) {

            // save the information
            programDir = getPropertyList (). getProperty ( ApplicationDirectory, "" );

            // only do this after we know where the installation directory is
            if ( programDir != null  ) {

                properties = new PropertyList ();
                if ( loadPropertyList () ) {

                    properties. setProperty ( UpdateUserName, userId. getText () );
                    properties. setProperty ( UpdatePassword, password. getText () );
    
                    savePropertyList ();
                    
                    ok = true;
                }
            }
        }
        
        else {
            
            UserNotices un = new UserNotices();
            un.note("Enter the user id and password");
        }
             
        return ok;
    }


    public boolean isPreviousButtonEnabled () {
        return false;
    }

    private void addPanel()
    {
        // set an outer border on the panel
        final int Margin = 20;
        Border emptyBorder = new EmptyBorder ( Margin, Margin, Margin, Margin );

        setLayout ( new BorderLayout());
        setBorder ( emptyBorder );

        // use DeNova's magic class that tames GridBag
        GridBagConstraints labelConstraints = GridBagControl. getDefaultConstraints ();
        labelConstraints. anchor = GridBagConstraints. CENTER;

        GridBagConstraints textConstraints = GridBagControl. getDefaultConstraints ();
        textConstraints. weightx = 1.0;
        textConstraints. fill = GridBagConstraints. HORIZONTAL;

        GridBagConstraints buttonConstraints = GridBagControl. getDefaultConstraints ();
        buttonConstraints. fill = GridBagConstraints. HORIZONTAL;

        JPanel p = new JPanel ();
        GridBagControl gc = new GridBagControl ( p );

        gc. addVerticalSpace ();

        JLabel title = new JLabel ( "Authentication" );
        title.setFont(Fonts.Bold);
        gc. endRow ( title );

        gc. add ( labelConstraints, new JLabel ( "User id:" ) );
        userId = new JTextField();
        userId . setEditable ( true );
        gc. add ( textConstraints, userId );
        gc. addVerticalSpace ();

        gc. add ( labelConstraints, new JLabel ( "Password:" ) );
        password = new JPasswordField();
        password. setEditable ( true );
        gc. add ( textConstraints, password);
        gc. addVerticalSpace ();

        add ( p );
    }

    private boolean loadPropertyList () {

        boolean ok = true;

        try {
            properties.load(updatePropertiesFilename());
        }
        catch ( Exception pe ) {
            ok = false;
        }

        return ok;
    }

    
    private boolean savePropertyList () {
        boolean ok;
        try {
            properties.save(updatePropertiesFilename());
            ok = true;
        }
        catch ( Exception e ) {
            ok = false;
        }

        return ok;
    }

    private String updatePropertiesFilename () {

        if ( updatePropertiesName == null ||
             updatePropertiesName. length() <= 0 ) {

            updatePropertiesName = programDir +
                                   File. separatorChar +
                                   "JExpress" +
                                   File. separatorChar +
                                   "update.control";
        }

        return updatePropertiesName;
    }


    JTextField userId;
    JPasswordField password;

    PropertyList properties;
    String programDir;
    String updatePropertiesName;

}  // CustomerUpdatePanel