package com.sun.solarisinstall.panels;

import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;
import com.sun.wizards.*;
import com.sun.wizards.panels.*;
import com.sun.wizards.core.*;
import com.sun.wizards.awt.*;
import com.sun.install.products.*;

import com.sun.solarisinstall.*;

/**
 * ScanCDPanel - Load Product CD and its Web Start Ready install data
 */

public class ScanCDPanel extends LoadingCDPanel 
{
  /*
   * Creation Methods
   */

  public ScanCDPanel()
    {
    }

  public ScanCDPanel(WizardState wizardState, String name)
    {
      super(wizardState, name);
    }

  public ScanCDPanel(String name, Route route, WizardTreeManager wizardManager)
    {
      super(name, route, wizardManager);
    }

  /**
   * Return the heading for this panel
   */

   public String getHeading()
   {
      return(MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
                              SolarisInstallResources.MSG_HDR_SCANNING_CD));
   }

  /**
   * Return this panel's "loading CD string" 
   */

  public String getLoadingString()
  {
    CDName = MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
			SolarisInstallResources.MSG_ADD_PRODUCTS);
    return(MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
			SolarisInstallResources.MSG_SCAN_FOR_WSR_PROD));
  }

  public boolean isDisplayComplete()
  {
    WizardTreeManager wizardManager = getTreeManager();

    /*
     * The CD has been mounted.  Check if its Web Start Ready.
     */

    Boolean wsrFilesExist =
        (Boolean) wizardManager.callServerObjectMethod(
                  getRoute().getChildServerRoute("profileObject"),
                  "doesFileExist",
                  new String[] {"java.lang.String.class"},
                  new Object[] {ProfileConstants.CDmount +
                  		ProfileConstants.wsrDir});


    if (wsrFilesExist.booleanValue()) {

      Integer copyStatus =
        (Integer) wizardManager.callServerObjectMethod(
                  getRoute().getChildServerRoute("profileObject"),
                  "importWSR",
                  new String[] {"java.lang.String.class"},
                  new Object[] {ProfileConstants.CDmount});

      String cdNameDir =
            (String) wizardManager.callServerObjectMethod(
                  getRoute().getChildServerRoute("profileObject"),
                  "readFileLine",
                  new String[] {"java.lang.String.class"},
                  new Object[] {ProfileConstants.CDmount +
                                ProfileConstants.wsrCDName});

      String cdsDirPath = ProfileConstants.CDsBaseDir + File.separator
                              + cdNameDir;

      wizardManager.callServerObjectMethod(
                  getRoute().getChildServerRoute("profileObject"),
                  "writeStringToFile",
                  new String[] {"java.lang.String.class",
                                "java.lang.String.class"},
                  new Object[] {cdsDirPath + ProfileConstants.addSource,
                                 String.valueOf(CDNode.CD)});

    } else {

      /*
       * Pop up the not WSR notice box
       */

      String title = MsgCat.getString("com.sun.wizards.Wizard", 
				       WizardResources.MSG_NOTICE);
      wizardManager.displayQuery( this, title, 
        MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
                         SolarisInstallResources.MSG_WSR_CD_NOTFOUND),
        new String[] {MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
                            SolarisInstallResources.MSG_OK)},
        new String[] {"goBack"});
    }

    /*
     * Eject the CD
     */

    wizardManager.callServerObjectMethod(
                getRoute().getChildServerRoute("profileObject"),
                "UmountEjectCD",
                null, null);

    /*
     * Reset product source back to NONE for next loop
     */

    wizardManager.callServerObjectMethod(
		getRoute(),
                "setData",
                new String[] {"com.sun.wizards.core.Route.class",
                              "java.lang.String.class",
                              "java.lang.Object.class"},
                new Object[] {null, "prodSource", 
			new Integer(AddProductsPanel.PROD_SOURCE_NONE)});

    if (wizardManager.hasGUI()) {
      return(wsrFilesExist.booleanValue());
    } else {
      // CLI mode always goes forward here.  No "back button".
      return(true);
    }
  }


  public void goBack()
  {
    /* 
     * If in GUI mode, go back one panel and prepare for retry.
     * Don't go back in CLI mode.  Continue forward to loops end.
     * (ie generally no "back button" in CLI mode). 
     */
    if (wizardManager.hasGUI()) {
	super.goBack();
    }
  }


  /**
   * Get the runtime classes required by this panel.
   */
  public void addRuntimeResources(Vector resourceVector)
  {
    super.addRuntimeResources(resourceVector);
    resourceVector.addElement(new String[] {null, 
	"com.sun.solarisinstall.SolarisInstallResources"});
    resourceVector.addElement(new String[] {null, 
	"com.sun.solarisinstall.panels.ScanCDPanel"});
  }
}
