/*
 *  Copyright (C) 2006  Takashi Kasuya <kasuya@sfc.keio.ac.jp>
 *
 * This library is free software; you can redistribute it and/or
 *　modify it under the terms of the GNU Lesser General Public
 *　License as published by the Free Software Foundation; either
 *　version 2.1 of the License, or (at your option) any later version.
 *　This library is distributed in the hope that it will be useful,
 *　but WITHOUT ANY WARRANTY; without even the implied warranty of
 *　MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *　Lesser General Public License for more details.
 *
 *　You should have received a copy of the GNU Lesser General Public
 *　License along with this library; if not, write to the Free Software
 *　Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package jp.ac.naka.ec.media;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Player;

/**
 * JMFの同期のために使用される。
 * @author Takashi Kasuya
 *
 */
class WaitingListener implements ControllerListener {

	Thread th;

	int state;

	int previous;

	public void waitForEvent(final int state) {
		this.state = state;
		th = new Thread() {
			public void run() {
				try {
					synchronized (this) {
						if (previous < state)
							wait();
					}
				} catch (InterruptedException e) {
					;
				}
			}
		};
		th.start();
		try {
			th.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void controllerUpdate(ControllerEvent evt) {
		Player p = (Player) evt.getSourceController();
		previous = p.getState();
		System.out.println(p.getState());
		if (th == null)
			return;
		if (p.getState() >= state) {
			synchronized (th) {
				th.notifyAll();
			}
		}

	}

}