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 GetPanel extends WizardLeaf implements Runnable
{
      private boolean initialize = false;
  Panel waitPanel = null;
  Panel spinnerPanel = null;
  Pinwheel pw = null;
  
  public GetPanel()
    {
    }
  
  public GetPanel(WizardState wizardState, String name)
    {
      super(wizardState, name);
    }
  
  public GetPanel(WizardState wizardState)
    {
      super(wizardState, "");
      wizardState.addChildObject("exececuteSysidCmd", 
				 new SysIDServerObject());
      wizardState.addChildObject("addSysStates", 
				 new SysIDServerObject());
    }
  
  public GetPanel(String name, Route route, 
		       WizardTreeManager wizardManager)
    {
      super(name, route, wizardManager);
      
    }
  
  public void createUI()
    {
      WizardTreeManager wizardManager = getTreeManager();
      super.createUI();

      String loadString = null;
      loadString = MsgCat.getString("com.sun.sysid.Sysid",
				    "Loading");
      waitPanel = new Panel (new BorderLayout());
      spinnerPanel = new Panel(new GridBagLayout());
      
      pw = new Pinwheel(6 , 6);
      spinnerPanel.add(pw);
      waitPanel.add(new FlowLabel("<bold>" + loadString + 
				  "</bold>"), "North");
      waitPanel.add(spinnerPanel, "Center");
      
      add(waitPanel, "Center");
      
      Thread t = new Thread(this);
      t.start();
    }

      public void consoleInteraction()
      {
	 String[] spinner = {"/", "-", "\\", "|"};
	 
	 TTYDisplay.showNewline();
	 String waiting = MsgCat.getString("com.sun.sysid.Sysid", "Loading")+"  ";
	 TTYDisplay.print(waiting);
	 
	 int i=0;
	 while(!initialize) 
	 {
	    try
	    { 
	       TTYDisplay.print("\b"+spinner[i++ % spinner.length]);
	       Thread.sleep(800); 
	    }
	    catch(InterruptedException ie)
	    {
	    }
	 }
	 TTYDisplay.print("\b");
	 TTYDisplay.showNewline();
       TTYDisplay.showNewline();
     }
 

  public void run()
    {
      WizardTreeManager wizardManager = getTreeManager();
    
      Route route = 
	getRoute().getChildServerRoute("exececuteSysidCmd");
      wizardManager.callServerObjectMethod(route.copy(),
					   "executeCmd",
			        new String[] {"java.lang.String.class"},
			        new Object[] {"sysidget"});
      route = getRoute().getChildServerRoute("addSysStates");
      wizardManager.callServerObjectMethod(route.copy(), 
					   "addSysStates",
				new String[] {"java.lang.String.class"},
			        new Object[] {"/tmp/preconfig"});
      wizardManager.callServerObjectMethod(route.copy(),
					   "testEStar",
					   null,
					   null);
      remove(waitPanel);

      validate();
      initialize = true;
      wizardManager.displayCorrectButtons();
      if (wizardManager.hasGUI())
      {
	 wizardManager.nextButtonPressed();
      }
    }
  public void beginDisplay()
    {
      super.beginDisplay();
     
    }

  public int getButtonMask()
  {   
      return(0);
  }

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