package com.sun.sysid.panels;

import java.awt.*;
import java.util.*;
import com.sun.wizards.panels.*;
import com.sun.wizards.core.*;
import com.sun.wizards.awt.*;
import com.sun.sysid.core.*;

public class DHCPPanel extends WizardLeaf
{
  
  CheckboxGroup cgroup;
  
  Panel mainPanel = new Panel(new VerticalLayout());
  Checkbox mainCb;
  
  Boolean isDHCP;
 
  String yesString;
  String noString;
  
  public DHCPPanel()
    {
    }
  
  public DHCPPanel(WizardState wizardState, String name)
    {
      super(wizardState, name);
    }
  
  public DHCPPanel(WizardState wizardState)
    {
      super(wizardState, "Name Service");
    }
  
  public DHCPPanel(String name, Route route, 
			  WizardTreeManager wizardManager)
    {
      super(name, route, wizardManager);
    }
  
  public void createUI()
    {
      WizardTreeManager wizardManager = getTreeManager();
      super.createUI();

      /*
       * Set the header text.
       */
      HeaderPanel headerPanel = getHeaderPanel();

      String heading = MsgCat.getString("com.sun.sysid.Sysid", 
					"MSG_DHCP_HEADER");

      if(headerPanel != null) {
        headerPanel.setText(heading);
      }

      mainPanel.add(new FlowLabel(MsgCat.getString("com.sun.sysid.Sysid", 
			       "MSG_DHCP_TEXT")), "1");
  
      mainPanel.add(new FlowLabel("<bold>" + 
				  MsgCat.getString("com.sun.sysid.Sysid",
			       "MSG_USE_DHCP")), "3");
      yesString = MsgCat.getString("com.sun.sysid.Sysid", "Yes");
      noString = MsgCat.getString("com.sun.sysid.Sysid", "No");
      isDHCP =
	(Boolean) wizardManager.callServerObjectMethod(getRoute(),
			      "getData",
			      new String[] { "java.lang.String.class" },
			      new Object[] { "isDHCP"});

      cgroup = new CheckboxGroup();
     
      Panel p = new Panel(new FlowLayout(FlowLayout.LEFT));
      Checkbox cb = new Checkbox(yesString, cgroup, false);
      if (isDHCP.booleanValue())
	cb.setState(true);
      
      mainCb = cb;
      p.add(cb);
      mainPanel.add(p, "4");
      p = new Panel(new FlowLayout(FlowLayout.LEFT));
      cb =  new Checkbox(noString, cgroup, false);
      if (! isDHCP.booleanValue())
	cb.setState(true);
      p.add(cb);
      mainPanel.add(p, "5");
      add(mainPanel, "Center");
    }

  public int getButtonMask() {
    return (BACK_BUTTON | NEXT_BUTTON);
  }

  public boolean isDisplayComplete()
    {
      WizardTreeManager wizardManager = getTreeManager();
      boolean isdhcp;
      
      yesString = MsgCat.getString("com.sun.sysid.Sysid", "Yes");
      if (wizardManager.hasGUI())
      {
	 isdhcp = 
	    cgroup.getSelectedCheckbox().getLabel().equals(yesString);
      }
      else
      {
	 isdhcp = isDHCP.booleanValue();
      }

      wizardManager.callServerObjectMethod(getRoute(), "setData",
		   new String[] { "java.lang.String.class", 
				     "java.lang.Object.class" },
		   new Object[] { "isDHCP", new Boolean(isdhcp)});

      if (!isdhcp) 
	{
	  wizardManager.callServerObjectMethod(getRoute(), "setData",
			new String[] { "java.lang.String.class", 
					"java.lang.Object.class" },
		        new Object[] { "skipHostName", new Boolean(false)});
	  wizardManager.callServerObjectMethod(getRoute(), "setData",
			new String[] { "java.lang.String.class", 
					"java.lang.Object.class" },
		        new Object[] { "skipNetworked", new Boolean(false)});
	} else {
	  wizardManager.callServerObjectMethod(getRoute(), "setData",
			new String[] { "java.lang.String.class", 
					"java.lang.Object.class" },
		        new Object[] { "skipHostName", new Boolean(true)});
	  wizardManager.callServerObjectMethod(getRoute(), "setData",
			new String[] { "java.lang.String.class", 
					"java.lang.Object.class" },
			new Object[] { "skipNetworked", new Boolean(true)});
	}
      return(true);
    }

public void consoleInteraction()
    {
      String promptText = 
	MsgCat.getString("com.sun.sysid.Sysid", "MSG_USE_DHCP_CLI");
      String yval = MsgCat.getString("com.sun.sysid.Sysid", "y" );
      String nval = MsgCat.getString("com.sun.sysid.Sysid", "n" );
      String defval = isDHCP.booleanValue()?yval:nval;
      String result = 
	TTYDisplay.queryValue(promptText, defval,
			      new String[] { yval,nval }, null);
      isDHCP = new Boolean(result.equalsIgnoreCase(yval)); 
    }

  public void addRuntimeResources(Vector resourceVector)
    {
      super.addRuntimeResources(resourceVector);
      resourceVector.addElement(new String[] {null, 
				"com.sun.sysid.panels.DHCPPanel"});
      resourceVector.addElement(new String[] {null, 
				"com.sun.sysid.SysidResources"});
      resourceVector.addElement(new String[] {null, 
				"com.sun.sysid.SysidResources"});
      resourceVector.addElement(new String[] {null, 
				"com.sun.wizards.awt.InsetsPanel"});
      resourceVector.addElement(new String[] {null, 
				"com.sun.wizards.awt.FullWidthPanel"});
      resourceVector.addElement(new String[] {null, 
				"com.sun.wizards.awt.FlowLabel"});
      resourceVector.addElement(new String[] {null, 
				"com.sun.wizards.awt.VerticalLayout"});
      resourceVector.addElement(new String[] {null, 
						"com.sun.sysid.core.PanelServices"});
      resourceVector.addElement(new String[] {null,
                        "com.sun.wizards.awt.Separator"});
    }

}
