/*
 * SimpleRotate.java
 *
 * Created on July 18, 2000, 2:11 PM
 */
 
package net.dutchie.desktop.world.behavior;

/** 
 *
 * @author  Administrator
 * @version 
 */

import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.*;

public class SimpleRotate extends Behavior {

  private boolean debug = false;
  
  private TransformGroup tg;
  private Bounds bounds;
  
  private WakeupOnActivation activation = new WakeupOnActivation();
  private WakeupOnDeactivation deactivation = new WakeupOnDeactivation();
  private WakeupOnElapsedTime time = new WakeupOnElapsedTime(100);  


  /** Creates new SimpleRotate */
  public SimpleRotate(TransformGroup tg, Bounds bounds) {
    super();
        if (debug == true) System.out.println("SimpleRotate being initialized");
    this.tg = tg;
    this.bounds = bounds;
    tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 
    tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    setSchedulingBounds(bounds);    
  }
  
  public SimpleRotate(TransformGroup tg) {     
    super();
    if (debug == true) System.out.println("SimpleRotate being initialized");
    this.tg = tg;
    tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 
    tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1.0);    
    setSchedulingBounds(bounds);    
  }
  
  public void initialize(){
    if (debug == true) System.out.println("Behavior initializing");
    waitForActivation();    
  }
  public void processStimulus(Enumeration e){
    if (debug == true) System.out.println("Behavior processing stimulus");
    
    
    for (int i=0; e.hasMoreElements(); ){
      if (debug == true)System.out.println(i);
      Object o = e.nextElement();      
      if (debug == true)System.out.println(o);
      if (o instanceof WakeupOnDeactivation){
        waitForActivation();
      }
      else if (o instanceof WakeupOnElapsedTime){
        executeBehavior();        
      }
      else if (o instanceof WakeupOnActivation){
        executeBehavior();
      }
    }       
  }
  
  private void executeBehavior(){
    if (debug == true)System.out.println("executing behavior");
    Transform3D originalTransform = new Transform3D();
    Transform3D newTransform = new Transform3D();
    newTransform.rotY(0.05);
    
    tg.getTransform(originalTransform);  
    
    newTransform.mul(originalTransform);
    
    tg.setTransform(newTransform);
    
    waitForTimeOrDeactivation();
    
  }
  
  private void waitForActivation(){
    if (debug == true)System.out.println("Behavior waiting for activation");
    wakeupOn(activation);
  }  
  private void waitForDeactivation(){
    if (debug == true)System.out.println("Behavior waiting for deactivation");
    wakeupOn(deactivation);
  }
  private void waitForTimeOrDeactivation(){
    if (debug == true)System.out.println("Behavior wait for time/deactivation");
    WakeupCriterion [] criteria = {time, deactivation};
    WakeupOr w = new WakeupOr(criteria);
    wakeupOn(w);    
  }    


 public Node cloneNode(boolean forceDuplicate){         
    SimpleRotate newNode = new SimpleRotate((TransformGroup) tg.cloneNode(true), (Bounds) bounds.clone());
    System.out.println("SimpleRotate being cloned1");
    newNode.duplicateNode(this, forceDuplicate);    
    System.out.println("SimpleRotate being cloned2");
         
    return newNode;   
  }  
  public void updateNodeReferences(NodeReferenceTable referenceTable) {
        super.updateNodeReferences(referenceTable);
        try{
         TransformGroup newTransformGroup = (TransformGroup)
                    referenceTable.getNewObjectReference(tg);
        }
        catch (DanglingReferenceException e){
            System.out.println(e + " in SimpleRotate");
        }         
  }

}