key0-96/
key0-96/doc/key/
key0-96/doc/key/credits/
key0-96/doc/key/developers/
key0-96/doc/key/developers/resources/
key0-96/setup/caves/
key0-96/setup/help/
key0-96/setup/ruins/
key0-96/src/
key0-96/src/commands/
key0-96/src/events/
key0-96/src/hack/
key0-96/src/sql/
key0-96/src/swing/
key0-96/src/talker/forest/
key0-96/src/talker/objects/
key0-96/src/terminals/
/*
**               j###t  ########## ####   ####
**              j###t   ########## ####   ####
**             j###T               "###L J###"
**          ######P'    ##########  #########
**          ######k,    ##########   T######T
**          ####~###L   ####
**          #### q###L  ##########   .#####
**          ####  \###L ##########   #####"
**
**  $Id$
**
**  Class History
**
**  Date        Name         Description
**  ---------|------------|-----------------------------------------------
**  19Aug98     subtle       start of recorded history
**
*/

package key;

/**
  * Is basically an animated atom ;)
 */
public abstract class AnimatedAtom extends Atom implements Animate
{
	private static final long serialVersionUID = 4049403736574501121L;
	transient Thread animated;
	boolean daemon=false;
	
	public AnimatedAtom()
	{
	}
	
	public AnimatedAtom( boolean isDaemon )
	{
		this();
		
		daemon = isDaemon;
	}
	
	public void interrupt()
	{
		if( animated != null && animated.isAlive() )
			animated.interrupt();
	}
	
	public abstract void run();
	
	public void start()
	{
		if( animated != null && animated.isAlive() )
		{
			animated.stop();
		}
		
		animated = new Animated( this );
		animated.setDaemon( daemon );
		animated.setName( getName() );
		animated.start();
	}
	
	public void stop()
	{
		if( animated != null )
		{
			animated.stop();
			
			try
			{
				animated.join();
			}
			catch( InterruptedException e )
			{
			}
			
			animated = null;
		}
	}
	
	public final boolean isAlive()
	{
		if( animated == null )
			return( false );
		else
			return( animated.isAlive() );
	}
	
	boolean canSwap()
	{
		return( !isAlive() );
	}
	
	public final boolean isRunning()
	{
		return( Thread.currentThread() == animated );
	}
	
	public final void yield()
	{
		animated.yield();
	}
	
	public final boolean isDaemon()
	{
		return( animated.isDaemon() );
	}
	
	public final void setPriority( int pri )
	{
		animated.setPriority( pri );
	}
	
	public final int getPriority()
	{
		return( animated.getPriority() );
	}
	
	void setKey_imp( Object newkey )
	{
		super.setKey_imp( newkey );
		
		if( animated != null && newkey instanceof String )
			animated.setName( (String) newkey );
	}
}