package com.sun.solarisinstall.panels;

import com.sun.wizards.core.*;
import com.sun.wizards.nodes.*;
import com.sun.wizards.awt.*;
import com.sun.install.products.*;
import com.sun.install.panels.*;
import com.sun.solarisinstall.*;
import com.sun.solarisinstall.core.*;
import java.awt.*;
import java.util.*;

/**
 * The ReinitPanel is a node that kicks off a pfinstall dry run on
 * the server upon traversal and display of the tree.
 */
public class ReinitPanel extends WizardLeaf implements Runnable, ProgressMonitor
{
  Panel waitPanel = null;
      /*
       * how long this panel should take
       */
      public static final int REINIT_TIME = 240;
      private ProgressBar progress;
      private ProgressCounter timer;
      private Cursor defaultCursor;
      private boolean done = false;

  /**
   * Creates an ReinitPanel with a route to the root of
   * the server object tree.
   */
  public ReinitPanel()
    {
    }

  /**
   * Creates an ReinitPanel with the specified name and WizardState.
   *
   * @param	wizardState	The buildtime WizardState.
   * @param	name		The name for this ReinitPanel.
   */
  public ReinitPanel(WizardState wizardState, String name)
    {
      super(wizardState, name);
    }
  
  /**
   * Creates an ReinitPanel with the specified name, the specified
   * route and wizard manager.
   *
   * @param name		The name for this ReinitPanel.
   * @param route		The route to the appropriate server side object.
   * @param wizardManager	The wizardManager responsible for this component.
   */
  public ReinitPanel(String name, Route route, WizardTreeManager wizardManager)
    {
      super(name, route, wizardManager);
    }

   /*
    * Set the progress to the specified values.
    *
    * @param p	An array specifying progress.
    */
      public void setProgress(int p)
  {
     progress.setProgress(p);
  }

  public void beginDisplay()
    {
      super.beginDisplay();

     /*
      * Add the header (Please wait while the system is initializing...)
      */
      String heading = 
          MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
           SolarisInstallResources.MSG_HDR_INITIALIZING);
      add(new HeaderPanel(heading, null, 38), "North");

      String loadString = 
         MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
                          SolarisInstallResources.MSG_PLEASE_WAIT);
      

      waitPanel = new Panel (new GridBagLayout());
      add(waitPanel, "Center");

      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridwidth = GridBagConstraints.REMAINDER;
      gbc.insets = new Insets(5,2,5,2);
      gbc.fill = GridBagConstraints.NONE;
      gbc.anchor = GridBagConstraints.SOUTH;
      waitPanel.add(new FlowLabel("<bold>" + loadString + "</bold>"), gbc);

      gbc.anchor = GridBagConstraints.NORTH;
      waitPanel.add(progress = new ProgressBar());

      /*
       * change cursor on entire frame
       */
      if (getTreeManager().hasGUI()) {
	 Component c = this;
	 while (c!=null && !(c instanceof Frame)) c=c.getParent();
	 if (c != null) {
	    defaultCursor = getCursor();
	    c.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	 }
      }
      done = false;
      Thread t = new Thread(this);
      t.start();
    }

 

      public void consoleInteraction()
      {
	 String[] spinner = {"/", "-", "\\", "|"};
	 
	 TTYDisplay.showNewline();
	 String waiting = MsgCat.getString("com.sun.solarisinstall.SolarisInstall", 
                          SolarisInstallResources.MSG_PLEASE_WAIT);
	 TTYDisplay.print(waiting);
	 
	 int i=0;
	 while(!done)
	 {
	    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();

      /*
       * Start timer
       */
      timer = new ProgressCounter(this, REINIT_TIME*1000, 0, 99);
      new Thread(timer).start();

      /*
       * initialize profile
       */
      Boolean initialInstall = 
	(Boolean) wizardManager.callServerObjectMethod(
                                getRoute(),
                                "getData",
                                new String[]{"com.sun.wizards.core.Route.class",
                                              "java.lang.String.class"},
                                new Object[] {getRoute(),  "InitialInstall" });
     
      if (initialInstall.booleanValue()) {
	 if (Def.DEBUG) System.out.println("running run in reinitpanel");
	wizardManager.callServerObjectMethod(
					   getRoute().getChildServerRoute("profileObject"),
					   "initProfile",
					   null, null);
      }
      wizardManager.callServerObjectMethod(
					   getRoute(),
					   "setData",
					   new String[] {"com.sun.wizards.core.Route.class",
							   "java.lang.String.class",
							   "java.lang.Object.class"},
					   new Object[] {getRoute(), "reinit", Boolean.TRUE});

      Boolean skiponeroot = 
	(Boolean)wizardManager.callServerObjectMethod(
		   getRoute(),
	           "getData",
		   new String[]{"com.sun.wizards.core.Route.class",
			     "java.lang.String.class"},
		   new Object[] {null, "skiponeroot"});
      
      if (skiponeroot.booleanValue()) {
	wizardManager.callServerObjectMethod(getRoute(),
                	"setData",
			new String[] {"com.sun.wizards.core.Route.class",
				       "java.lang.String.class",
                	               "java.lang.Object.class"},
                	new Object[] {null,
                 	               "oneroot", 
               	 	               Boolean.TRUE});
      }
      done = true;
      timer.complete();
      // change cursor on entire frame
      if (getTreeManager().hasGUI()) {
	 Component c = this;
	 while (c!=null && !(c instanceof Frame)) c=c.getParent();
	 if (c != null && defaultCursor != null) 
	    c.setCursor(defaultCursor);
      }
      remove(waitPanel);
      if (wizardManager.hasGUI())
      {
	 wizardManager.nextButtonPressed();
      }
  }
      
  public int getButtonMask()
  {
    return(0);
  }


  /**
   * Get the runtime classes required by this panel.
   */
  public void addRuntimeResources(Vector resourceVector)
  {
    super.addRuntimeResources(resourceVector);
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.panels.ReinitPanel"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.core.ProgressMonitor"});
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.core.ProgressCounter"});
    resourceVector.addElement(new String[] {null, "com.sun.wizards.awt.ProgressBar"});
  }
}

