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 DnsNameServerPanel extends WizardLeaf
{
  Panel mainPanel = new Panel(new VerticalLayout());
    TextField svrField[] = new TextField[3]; 
  
  static int maxEnt = 3;
    private static String dnsNameServerString;

  String[] nameServers = new String[3];
  
  public DnsNameServerPanel()
    {
    }
  
  public DnsNameServerPanel(WizardState wizardState, String name)
    {
      super(wizardState, name);
    }
  
  public DnsNameServerPanel(WizardState wizardState)
    {
      super(wizardState, "DNS Server Addresses");
    }
  
  public DnsNameServerPanel(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", 
					  "DNS Server Addresses");

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

      // Init text fields
     
     
      mainPanel.add(new FlowLabel(dnsNameServerString =  
				  MsgCat.getString("com.sun.sysid.Sysid", 
			       "DNSnameSvrString")),"1");

      svrField[0] = new TextField("", 16); 
      svrField[1] = new TextField("", 16); 
      svrField[2] = new TextField("", 16); 
 

      // get Wizard state data if DNS severs found.

      nameServers = (String[]) wizardManager.callServerObjectMethod(getRoute(),
					"getData",
					 new String[] { "java.lang.String.class" },
					 new Object[] { "DNSnameServers" } );
      
      if (! nameServers[0].equals(""))
	svrField[0] = new TextField(nameServers[0], 16);
      if (! nameServers[1].equals(""))
	svrField[1] = new TextField(nameServers[1], 16);
      if (! nameServers[2].equals(""))
	svrField[2] = new TextField(nameServers[2], 16);

      Panel p = new Panel();
      p.add(new FlowLabel("<bold>" + MsgCat.getString("com.sun.sysid.Sysid", 
				       "DNSsvrIPAddr")));
      mainPanel.add(p,"2");
      p = new Panel(new FlowLayout(FlowLayout.LEFT));
      p.add(svrField[0]);
      p.add(new Label(MsgCat.getString("com.sun.sysid.Sysid","req")));
      mainPanel.add(p,"3");
      
     
      p = new Panel(new FlowLayout(FlowLayout.LEFT));
      p.add(svrField[1]);
      p.add(new Label(MsgCat.getString("com.sun.sysid.Sysid","opt")));
      mainPanel.add(p, "4");

      p = new Panel(new FlowLayout(FlowLayout.LEFT));
      p.add(svrField[2]);
      p.add(new Label(MsgCat.getString("com.sun.sysid.Sysid","opt")));      
      mainPanel.add(p, "5");
      add(mainPanel,"Center");
      svrField[0].requestFocus();
    }

  public void beginDisplay()
    {
      super.beginDisplay();
      if (svrField[0] != null)
	svrField[0].requestFocus();
    }
  
  public void consoleInteraction()
     {
       int i = 0;
       String result = "";
       
       String nval = MsgCat.getString("com.sun.sysid.Sysid", "n");
       String eval = MsgCat.getString("com.sun.sysid.Sysid", "e");
 
       do {
         Object[] arg = new Object[] {  
             MsgCat.getString("com.sun.sysid.Sysid",  "cli_count" + i)
         };
         
         String text = MsgCat.getString("com.sun.sysid.Sysid", "cli_dnsPrompt1", arg);
         
         if( i == 0 ) {
           text += MsgCat.getString("com.sun.sysid.Sysid", "cli_dnsPrompt2" );
         } else {
           text += MsgCat.getString("com.sun.sysid.Sysid", "cli_dnsPrompt3" );
         }
         
         dnsNameServerString = MsgCat.getString("com.sun.sysid.Sysid", "cli_dnsString");
         
         result = TTYDisplay.queryValue( text, svrField[i].getText(), 
                                         dnsNameServerString, this, String.valueOf(i) );
         
         if( ( i > 0 ) && result.equalsIgnoreCase(nval)) 
           svrField[i].setText("");
         else if( ( i > 0 ) && result.equalsIgnoreCase(eval) ){ 
           for(int k = i; k < 3; k++) {
             svrField[k].setText("");
           }
           return;
         } else {
           svrField[i].setText(result);
         }
       } while( ++i < 3  ); 
     }
 
   public boolean callback( String arg, String id ) 
     {
       if( !id.equals("0") && 
           ( arg.equalsIgnoreCase("n") || arg.equalsIgnoreCase("e") ) ) return true;
       
       PanelServices panelsvc = new PanelServices();
       
       return panelsvc.validIPAddress(arg, this);
     }


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


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

      PanelServices panelsvc = new PanelServices();
      for (int i = 0; i<maxEnt;++i)
	nameServers[i] = "";
      if(!panelsvc.validIPAddress(svrField[0].getText(), this))
	return(false);
      
      nameServers[0] = svrField[0].getText();
      String tmp = new String(svrField[1].getText());
      if (!tmp.equals("")) {
	if (!panelsvc.validIPAddress(tmp, this)){
	  return(false);
	} else {
	  nameServers[1] = tmp;
	}
      }
      tmp = svrField[2].getText();
      if (!tmp.equals("")) {
	if (!panelsvc.validIPAddress(tmp, this)){
	  return(false);
	} else {
	  nameServers[2] = tmp;
	}
       }
      if (Def.DEBUG) {
	for (int i=0; i<maxEnt;++i) {
	  System.out.println("DNS Server " + (i+1) + " " + "is " + nameServers[i]);
	}
      }
      
      wizardManager.callServerObjectMethod(getRoute(), "setData",
					     new String[] { "java.lang.String.class", 
								"java.lang.Object.class" },
					     new Object[] { "DNSnameServers", nameServers });
      return(true);
      
    }
  
  
  public void addRuntimeResources(Vector resourceVector)
    {
      super.addRuntimeResources(resourceVector);
      resourceVector.addElement(new String[] {null, 
                                "com.sun.sysid.panels.DnsNameServerPanel"});
      resourceVector.addElement(new String[] {null, 
					  "com.sun.sysid.core.PanelServices"});
      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.Separator"});
    }
}
