package util;

import visad.*;
import java.rmi.RemoteException;

public class ScalarMapSyncher implements ScalarMapListener
{
    boolean got_auto_event = false;
    ScalarMap scalarMap    = null;

    public ScalarMapSyncher(ScalarMap scalarMap) {
      this.scalarMap = scalarMap;
      this.scalarMap.addScalarMapListener(this);
    }

    public void waitForEvent() {
      if (got_auto_event == false) {
        try {
          synchronized (this) {
            this.wait();
          }
        }
        catch (InterruptedException e) {
        }
      }
      else {
        scalarMap.removeScalarMapListener(this);
      }
    }

    public void mapChanged(ScalarMapEvent e)
           throws VisADException, RemoteException {
      if (e.getId() == ScalarMapEvent.AUTO_SCALE) {
        got_auto_event = true;
        synchronized (this) {
          this.notify();
        }
        scalarMap.removeScalarMapListener(this);
      }
    }

    public void controlChanged(ScalarMapControlEvent e) {
    }
}
