package pddemo;

import com.ibm.aglet.*;
import com.ibm.aglet.event.*;

import java.net.URL;

public class RetractionChild extends Aglet {

   boolean _reverted = false;
   boolean _dispatched = false;

   public void onCreation(Object o) {
   	addMobilityListener(
   	   new MobilityAdapter() {
	      public void onReverting(MobilityEvent e) {
                 _reverted = true;
                 print("\'onReverting()\' is starting...");
                 pause();
                 print("\'onReverting()\' is finishing.");  	
              }
              public void onArrival(MobilityEvent e) {
                 _dispatched = true; 
                 if (_reverted) {
                    print("\'onArrival()\' is starting...");
                    pause();
                    print("\'onArrival()\' is finishing.");
                 }              
              }
	   }
        );	
   }   

   public void run() {
      if (_dispatched) {
         print("\'run()\' is starting...");
         pause(); pause();
         print("\'run()\' is finishing.");
      }
   }

   void print(String s) { System.out.println("(child): " + s); }
   private static long SLEEP = 3000;
   void pause() { try { Thread.sleep(SLEEP); } catch (InterruptedException ie) { } }
}

