package com.sun.solarisinstall.panels;

import com.sun.solarisinstall.*;
import com.sun.wizards.core.*;
import com.sun.wizards.awt.*;
import java.awt.*;
import java.io.*;
import java.util.*;

/**
 * The MediaKitPanel allows the user to choose which media Kit 
 * was purchased.
 */
public class MediaKitPanel extends WizardLeaf
{
  /**
   * The panel that will display the product choices
   */
  private Panel osPanel = null;
  private CheckboxGroup cbgroup;

  private String lines[];
  private int selected = 0;

  /**
   * Creates a MediaKitPanel with no name.
   */
  public MediaKitPanel()
  {
  }

  /**
   * Creates a MediaKitPanel with the specified name,
   * and contains profile entries indexed by the specified key list.
   *
   * @param wizardState	The buildtime WizardState.
   * @param name		The name for this MediaKitPanel.
   */
  public MediaKitPanel(WizardState wizardState, String name)
  {
    super(wizardState, name);
  }

  /**
   * Creates a MediaKitPanel with the specified name, the specified
   * route and wizard manager.
   *
   * @param name	The name for this MediaKitPanel.
   * @param route	The route to the appropriate server side object.
   * @param wizardManager	The wizardManager responsible for this component.
   */
  public MediaKitPanel(String name,Route route, WizardTreeManager wizardManager)
  {
    super(name, route, wizardManager);
  }


  /**
   * This method creates the user interface.
   */
  public void createUI()
  {
    super.createUI();

    WizardTreeManager wizardManager = getTreeManager();

    setLayout(new BorderLayout());

    /*
     * Add the header.
     */
    String heading = MsgCat.getString("com.sun.solarisinstall.SolarisInstall",
                             SolarisInstallResources.MSG_HDR_SELECT_MEDIA_KIT);
    add(new HeaderPanel(heading, null), "North");

    /*
     * Add the text.
     */
    Panel textPanel = new Panel(new VerticalLayout());
    FlowLabel textLabel = new FlowLabel(MsgCat.getString("com.sun.solarisinstall.SolarisInstall", SolarisInstallResources.MSG_SELECT_MEDIA_KIT));
    if (wizardManager.hasGUI())
    {
       textLabel.setFont(new Font(getFont().getName(),
				  Font.BOLD, getFont().getSize()));
    }
    textPanel.add(textLabel, "0");

    osPanel = new Panel(new ColumnLayout());

    Route objectRoute = getRoute().getChildServerRoute("profileObject");

    /*
     * get list of all mk's present per media_kits.toc
     */
    Vector mediaKitNames =
        (Vector)wizardManager.callServerObjectMethod(objectRoute.copy(),
        "getMediaKitList",
        null, null);


    cbgroup = new CheckboxGroup();
    Checkbox checkbox;
    String line;
    boolean firstOne = true;
    Panel choicePanel = new Panel(new ColumnLayout());

    lines = new String[ mediaKitNames.size() ];

    for (int index = 0; index < mediaKitNames.size(); index++) {
      line = (String)mediaKitNames.elementAt(index);
      line = "  "+line;
      checkbox = new Checkbox(line, cbgroup, firstOne);
      lines[ index ] = line;
      firstOne = false;
      choicePanel.add(checkbox);
    }

    ScrollPane scrollPane = new ScrollPane();
    textPanel.add(scrollPane, "30");
    scrollPane.setSize(100, (getParent().getSize().height)-175);
    scrollPane.add(choicePanel);

    add(textPanel, "Center");
  }

      public void consoleInteraction()
      {
	 String header = MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
					  SolarisInstallResources.MSG_HDR_MEDIAKIT_SUMMARY);

	 String promptString = MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
					      SolarisInstallResources.MSG_SELECT_MEDIA_KIT);
	 
	 selected = TTYDisplay.queryValue(promptString, header, lines, selected,
					  "" );
      }

  /**
   * Returns true if this MediaKitPanel is finished displaying.
   * This method can be used to verify user input.
   *
   * @return	true if the display of this MediaKitPanel is
   *		complete; false otherwise.
   */
  public boolean isDisplayComplete()
  {
    WizardTreeManager wizardManager = getTreeManager();
    String selectedMedia = null;

    if (wizardManager.hasGUI())
    {
       Checkbox selectedButton = cbgroup.getSelectedCheckbox();
       selectedMedia = selectedButton.getLabel();
    }
    else
    {
       selectedMedia = lines[selected]; 
    }
    wizardManager.callServerObjectMethod(
        getRoute(),
        "setData",
        new String[] {"com.sun.wizards.core.Route.class",
                      "java.lang.String.class",
                      "java.lang.Object.class"},
        new Object[] {null, ProfileConstants.mediaName, selectedMedia});

    return(true);
  }


  /**
   * Reset this component, given the specified information.
   * This method may be called when a user backtracks in
   * the client wizard tree and changes a fundamental setting.
   * The panel that enabled the modification might traverse
   * subsequent panels and call this method resynchronize
   * all panels with the user's choice.
   *
   * When this method is called, the user selected "Default" installation.
   * This panel must reset its install directory information.
   *
   * @param Object	The information required to perform a reset.
   */
  public void reset(Object info)
  {
  }


  /**
   * Returns information suggesting which buttons should be
   * displayed for this panel.
   *
   * The MediaKitPanel displays all the navigation
   * buttons.
   *
   * @return	A button mask identifying valid navigation buttons.
   */
  public int getButtonMask()
  {
    return(BACK_BUTTON | NEXT_BUTTON | EXIT_BUTTON );
  }

  /**
   * Get the runtime classes required by this panel.
   */
  public void addRuntimeResources(Vector resourceVector)
  {
    super.addRuntimeResources(resourceVector);
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.MediaKitPanel"});
    resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.HeaderPanel"});
    resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.VerticalLayout"});
    resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.FlowLabel"});
    resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.ColumnLayout"});
  }
}
