package com.sun.sysid.panels;

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

public class NameSvrIDPanel extends WizardLeaf
{
  
  static String nameSvrIDString;
  Panel mainPanel = new Panel(new VerticalLayout());
  
  TextField svrHostField;
  TextField svrIPField;
  
  String nameServerName;
  String nameServerAddr;
  
  public NameSvrIDPanel()
    {
    }
  
  public NameSvrIDPanel(WizardState wizardState, String name)
    {
      super(wizardState, name);
    }
  
  public NameSvrIDPanel(WizardState wizardState)
    {
      super(wizardState, "Name Server Information");
    }
  
  public NameSvrIDPanel(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", 
					  "Name Server Information");

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

      nameServerName = 
	(String) wizardManager.callServerObjectMethod(getRoute(), "getData",
				new String[] { "java.lang.String.class" },
				new Object[] { "nameServerName" })
	;
      nameServerAddr = 
	(String) wizardManager.callServerObjectMethod(getRoute(),"getData",
				 new String[] { "java.lang.String.class" },
				 new Object[] { "nameServerAddr" });
      
      mainPanel.add(new FlowLabel(nameSvrIDString = MsgCat.getString("com.sun.sysid.Sysid", 
						  "nameSvrIDString")),"1");
      svrHostField = new TextField(nameServerName, 16);
      svrIPField = new TextField(nameServerAddr, 16);
      mainPanel.add(new FlowLabel("<bold>" + MsgCat.getString("com.sun.sysid.Sysid", 
					       "Server's host name:")), "2");
      
      Panel p = new Panel(new FlowLayout(FlowLayout.LEFT));
      p.add(svrHostField);
      mainPanel.add(p, "3");
      mainPanel.add(new FlowLabel("<bold>" + MsgCat.getString("com.sun.sysid.Sysid", 
					       "Server's IP Address:")), "4");
      p = new Panel(new FlowLayout(FlowLayout.LEFT));
      p.add(svrIPField);
      mainPanel.add(p, "5");
      add(mainPanel,("Center"));
      svrHostField.requestFocus();
    }
  public void beginDisplay()
    {
      super.beginDisplay();
      if (svrHostField != null)
	svrHostField.requestFocus();
    }
  
   public void consoleInteraction()
     {
       String prompt1 = MsgCat.getString("com.sun.sysid.Sysid", "cli_idPrompt1");
       String prompt2 = MsgCat.getString("com.sun.sysid.Sysid", "cli_idPrompt2");
       
       nameSvrIDString = MsgCat.getString("com.sun.sysid.Sysid", 
                                          "cli_nameSvrIDString");
 
       String hn = TTYDisplay.queryValue(prompt1, svrHostField.getText(), 
                                         nameSvrIDString, this, "name");
       svrHostField.setText(hn);
 
       String ip = TTYDisplay.queryValue(prompt2, svrIPField.getText(), 
                                         nameSvrIDString, this, "addr");
       svrIPField.setText(ip);
     }
 
   public boolean callback(String arg, String id) 
     {
       PanelServices panelsvc = new PanelServices();
       
       if(id.equals("name")) {
         return panelsvc.validHostName(arg, this);
       } else {
         return panelsvc.validIPAddress(arg, this);
       }
     }

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

  public boolean isDisplayComplete()
    {
      WizardTreeManager wizardManager = getTreeManager();
      PanelServices panelsvc = new PanelServices();
      nameServerName = svrHostField.getText();
      nameServerAddr = svrIPField.getText();

      if(! panelsvc.validHostName(nameServerName, this))
	return(false);
      if (!panelsvc.validIPAddress(nameServerAddr,this))
	return(false);

      wizardManager.callServerObjectMethod(getRoute(), "setData",
					   new String[] { "java.lang.String.class",
							    "java.lang.Object.class" },
					   new Object[] { "nameServerName", nameServerName });
      wizardManager.callServerObjectMethod(getRoute(), "setData",
					   new String[] { "java.lang.String.class",
							    "java.lang.Object.class" },
					   new Object[] { "nameServerAddr", nameServerAddr });
      return(true);
      
    }

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