/*
    Warn the user if they selected a drive on Windows with low disk space

    Must be called "After selecting install dir"

    Copyright (c)1999-2008 DeNova
    
*/


import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import com.denova.runtime.OS;
import com.denova.runtime.WindowsCommands;
import com.denova.ui.Fonts;
import com.denova.ui.GridBagControl;
import com.denova.ui.WizardPanel;
import com.denova.util.PropertyList;
import com.denova.JExpress.Installer.CustomInstaller;
import com.denova.JExpress.Installer.InstallPropertyNames;



public class WinFreeDiskSpace extends WizardPanel
                              implements InstallPropertyNames {

    public WinFreeDiskSpace ( PropertyList properties) {

        // initialize WizardPanel superclass
        super ( properties);

        // 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 ();
    
        status = new JLabel ( "Checking available disk space..." );
        status.setFont(Fonts.Bold);
        gc. addCentered ( status );
        gc. addVerticalSpace ();
    
        add ( p );
    }

    /** 
     * Enter the panel. 
     *
     * Do not call this method; only the wizard should call it.
     */
     public synchronized void enter()
     {
        long totalBytesNeeded = (long) 0;
        long totalBytesForFiles = getPropertyList (). getLongProperty ( TotalBytesNeeded );
        CustomInstaller.logToInstaller("total bytes needed to install: " + String. valueOf ( totalBytesNeeded ));
        
        if ( totalBytesForFiles > (long) 0 ) {
            
            // we'll need space for the files plus the JAR file where they're stored
            // if we assume a 60% compression for the JAR file plus 10% extra for
            // different hard drive allocation, then we should have enough
            totalBytesNeeded = ( totalBytesForFiles * ( (long) 1.1 ) ) +
                               ( totalBytesForFiles * ( (long) 0.6 ) );
                               
        }
        
        if ( diskSpaceOk ( totalBytesNeeded ) ) {

            showNextPanel ();
        }

        else {

            String productName = getPropertyList (). getProperty ( PackageName, "" );
            String [] msg = { "The available disk space is too low",
                              "to install " + productName,
                              "You need approximately " + String. valueOf ( totalBytesNeeded ),
                              "available to complete the installation.",
                              "\n",
                              "Please free up enough disk space or select",
                              "another drive for your installation directory."};
            note ( msg );

            showPreviousPanel ();
        }
    }

    boolean diskSpaceOk ( long totalBytesNeeded ) {

        boolean ok = true;

        if ( OS. isWindows () ) {

            String applicationDirectory =
                    getPropertyList (). getProperty ( ApplicationDirectory, "" );

            // see if there's the minimum space on the intended application directory
            Long freeDiskSpace = WindowsCommands. getFreeDiskSpace ( applicationDirectory );
            CustomInstaller.logToInstaller("Free disk space: " + String.valueOf(freeDiskSpace));
            
            if ( freeDiskSpace. longValue() < totalBytesNeeded ) {
                CustomInstaller. logToInstaller ( "not enough disk space: " + freeDiskSpace. toString () );
                ok = false;
            }
        }

        return ok;
    }
    

    public boolean isNextButtonEnabled () {
	return false;
    }

    public boolean isBackButtonEnabled () {
	    return false;
    }

    private JLabel status;

}