package com.sun.launcher.panels;

import java.awt.*;
import java.util.*;
import com.sun.wizards.core.*;
import com.sun.wizards.nodes.*;
import com.sun.launcher.core.*;


/*
 * ParentPanel is the superclass for all the panels in the 
 * launcher LoopNode. It contains methods of common interest
 * to the panels.
 */
public class ParentPanel extends WizardLeaf 
{
  protected boolean usingVOLD = false;

  public ParentPanel()
  {
  }

  public ParentPanel(WizardState wizardState, String name)
  {
    super(wizardState, name);
  }
    
  public ParentPanel(String name, Route route, WizardTreeManager wizardManager) 
  {
    super(name, route, wizardManager);
  }


  /**
   * This method writes text to the logfile
   */
  protected void callWriteToLog(String logtext)
  {
    WizardTreeManager wizardManager = getTreeManager();

    String logname=
           (String)wizardManager.callServerObjectMethod(
            getRoute(),
            "getData",
            new String[] {"java.lang.String.class"},
            new Object[] {LaunchConstants.logName});

    Route launchRoute = getRoute().getChildServerRoute("launchObject");
    wizardManager.callServerObjectMethod(
                       launchRoute.copy(),
                       "writeToLog",
                       new String[] {"java.lang.String.class",
                                     "java.lang.String.class"},
                       new Object[] { logname,
                                      logtext });
  }


  /**
   * Returns the frame object containing this Panel.
   */
  protected Frame getFrame()
    {
      Container parent = this.getParent();
      while(parent != null && !(parent instanceof Frame))
        {
          parent = parent.getParent();
        }
      return((Frame)parent);
  }


  /**
   * Returns the loopnode, or null
   */
  protected LoopNode getLoop()
  {
    WizardComponent parent = this.getParentComponent();
    while(parent != null && !(parent instanceof LoopNode)) {
      parent = parent.getParentComponent();
    }
    return((LoopNode)parent);
  }


  /**
   * Continues to the next iteration of the loop and updates checkpoint
   */
  protected void nextIteration()
  {
    LoopNode myloop = getLoop();
    if (myloop == null) {
      System.out.println("Error: can't get loop.");
    } else {
      if (Def.DEBUG) {
        System.out.println("ParentPanel: nextIteration: CALLING continueLoop");
      }
      updateCheckpoint();
      myloop.continueLoop();
      return;
    }
  }


  /**
   * Restarts the same iteration of the loop
   */
  protected void redoIteration()
  {
    WizardTreeManager wizardManager = getTreeManager();

    LoopNode myloop = getLoop();
    if (myloop == null) {
      System.out.println("Error: can't get loop.");
    } else {
      Integer currIter =
           (Integer)wizardManager.callServerObjectMethod(getRoute(),
            "getData",
            new String[] {"java.lang.String.class"},
            new Object[] {"iterationKey"});


      int prevIteration = currIter.intValue()-1;
      wizardManager.callServerObjectMethod(
                     getRoute(),
                     "setData",
                     new String[] {"java.lang.String.class",
                                   "java.lang.Object.class"},
                     new Object[] {"iterationKey",
                                   new Integer(prevIteration)});
      if (Def.DEBUG) {
        System.out.println("ParentPanel: redoIteration "+(prevIteration+1));
      }
      myloop.continueLoop();
      return;
    }
  }

  /**
   * Updates the checkpoint file
   */
  protected void updateCheckpoint()
  {
    WizardTreeManager wizardManager = getTreeManager();

    Integer iteration =
           (Integer)wizardManager.callServerObjectMethod(getRoute(),
            "getData",
            new String[] {"java.lang.String.class"},
            new Object[] {"iterationKey"});

    Route launchRoute = getRoute().getChildServerRoute("launchObject");
    wizardManager.callServerObjectMethod(
                         launchRoute.copy(),
                         "updateCheckpoint",
                         new String[] {"java.lang.String.class"},
                         new Object[] { iteration.toString() });

    if (Def.DEBUG) {
      System.out.println("ParentPanel: updateCheckpoint "+iteration);
    }
  }

  public void addRuntimeResources(Vector resourceVector)
  {
     super.addRuntimeResources(resourceVector);
     resourceVector.addElement(new String[] {null, "com.sun.launcher.panels.ParentPanel"});
  }
}
