package com.sun.sysid.panels;

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

public class HostNamePanel extends WizardLeaf
{
  
  static String hostNameString; 
  TextField hostField;
  Panel hostPanel = null;
  String hostName;
  boolean initialize = false;
  
  public HostNamePanel()
    {
    }
  
  public HostNamePanel(WizardState wizardState, String name)
    {
      super(wizardState, name);
    }
  
  public HostNamePanel(WizardState wizardState)
    {
      super(wizardState, "Host Name");
      wizardState.addChildObject("exececuteSysidCmd", 
				 new SysIDServerObject());
      wizardState.addChildObject("addSysStates", 
				 new SysIDServerObject());
    }
  
  public HostNamePanel(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();
      hostPanel = new Panel(new VerticalLayout());

      String  heading = MsgCat.getString("com.sun.sysid.Sysid", "Host Name");
      if(headerPanel != null) {
        headerPanel.setText(heading);
      }
       
      hostName = getHostName();
      
      hostNameString = MsgCat.getString("com.sun.sysid.Sysid", 
					"hostNameString");
      hostPanel.add(new FlowLabel(hostNameString), "1");
      
      Panel p = new Panel();
      String promptString = MsgCat.getString("com.sun.sysid.Sysid",
					     "Host Name:");
      p.add(new FlowLabel("<bold>" + promptString));
      hostPanel.add(p,"2");
      p = new Panel(new FlowLayout(FlowLayout.LEFT));
      hostField = new TextField(hostName, 16);
      p.add(hostField);
      hostPanel.add(p,"3");
      add(hostPanel, "Center");
      hostField.requestFocus();
    }

      private String getHostName()
      {
	 String hostName = 
	(String) wizardManager.callServerObjectMethod(getRoute(),
						      "getData",
				 new String[] { "java.lang.String.class" },
				 new Object[] { "hostName"});
	 return hostName;
      }

   public boolean callback( String arg, String id ) 
     {   
       PanelServices panelsvc = new PanelServices();
       return panelsvc.validHostName(arg, this);
     }

      public void consoleInteraction()
      {
	 TTYDisplay.showNewline();
	 String prompt = MsgCat.getString("com.sun.sysid.Sysid", "cli_hostNamePrompt");
	 hostNameString = MsgCat.getString("com.sun.sysid.Sysid",  "cli_hostNameString");
	 String result = TTYDisplay.queryValue(prompt, hostField.getText(), 
					       hostNameString, this, null);
	 hostField.setText(result);
	 TTYDisplay.showNewline();
      }

  public boolean isDisplayComplete()
  {
    WizardTreeManager wizardManager = getTreeManager();
    PanelServices panelsvc = new PanelServices();
      
    if(hostField == null)
      return(false);

    if(!panelsvc.validHostName(hostField.getText(), this))
      return(false);
		
    hostName = hostField.getText();

    wizardManager.callServerObjectMethod(getRoute(), "setData",
				new String[] { "java.lang.String.class", 
					       "java.lang.Object.class" },
				new Object[] { "hostName", hostName });
    wizardManager.callServerObjectMethod(getRoute(), "setData",
				new String[] { "java.lang.String.class", 
					       "java.lang.Object.class" },
				new Object[] { "confirmHostName", 
				new Boolean(true) });
    hostField.transferFocus();
		

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

  }

  public void addRuntimeResources(Vector resourceVector)
  {
    super.addRuntimeResources(resourceVector);
    resourceVector.addElement(new String[] {null,
			"com.sun.sysid.panels.HostNamePanel"});
    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.wizards.awt.Pinwheel"});
    resourceVector.addElement(new String[] {null, 
			"com.sun.sysid.core.PanelServices"});
    resourceVector.addElement(new String[] {null,
			"com.sun.wizards.awt.Separator"});
  }
}
