001    /*
002     * PlayerThread.java
003     *
004     *
005     *  The Salamander Project - 2D and 3D graphics libraries in Java
006     *  Copyright (C) 2004 Mark McKay
007     *
008     *  This library is free software; you can redistribute it and/or
009     *  modify it under the terms of the GNU Lesser General Public
010     *  License as published by the Free Software Foundation; either
011     *  version 2.1 of the License, or (at your option) any later version.
012     *
013     *  This library is distributed in the hope that it will be useful,
014     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
015     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016     *  Lesser General Public License for more details.
017     *
018     *  You should have received a copy of the GNU Lesser General Public
019     *  License along with this library; if not, write to the Free Software
020     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021     *
022     *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
023     *  projects can be found at http://www.kitfox.com
024     *
025     * Created on September 28, 2004, 10:07 PM
026     */
027    
028    
029    package com.kitfox.svg.app;
030    
031    import java.util.*;
032    
033    /**
034     *
035     * @author  kitfox
036     */
037    public class PlayerThread implements Runnable
038    {
039        HashSet listeners = new HashSet();
040        
041        double curTime = 0;
042        double timeStep = .2;
043        
044        public static final int PS_STOP = 0;
045        public static final int PS_PLAY_FWD = 1;
046        public static final int PS_PLAY_BACK = 2;
047        
048        int playState = PS_STOP;
049        
050        Thread thread;
051        
052        /** Creates a new instance of PlayerThread */
053        public PlayerThread()
054        {
055            thread = new Thread(this);
056            thread.start();
057        }
058        
059        public void run()
060        {
061            while (thread != null)
062            {
063                synchronized (this)
064                {
065                    switch (playState)
066                    {
067                        case PS_PLAY_FWD:
068                            curTime += timeStep;
069                            break;
070                        case PS_PLAY_BACK:
071                            curTime -= timeStep;
072                            if (curTime < 0) curTime = 0;
073                            break;
074                        default:
075                        case PS_STOP:
076                            break;
077                    }
078                    
079                    fireTimeUpdateEvent();
080                }
081                
082                try
083                {
084                    Thread.sleep((long)(timeStep * 1000));
085                }
086                catch (Exception e) 
087                { 
088                    throw new RuntimeException(e); 
089                }
090            }
091        }
092        
093        public void exit() { thread = null; }
094        public synchronized void addListener(PlayerThreadListener listener) 
095        {
096            listeners.add(listener); 
097        }
098        
099        public synchronized double getCurTime() { return curTime; }
100        
101        public synchronized void setCurTime(double time)
102        {
103            curTime = time;
104        }
105        
106        public synchronized double getTimeStep() { return timeStep; }
107        
108        public synchronized void setTimeStep(double time)
109        {
110            timeStep = time;
111            if (timeStep < .01) timeStep = .01;
112        }
113        
114        public synchronized int getPlayState() { return playState; }
115        
116        public synchronized void setPlayState(int playState)
117        {
118            this.playState = playState;
119        }
120        
121        private void fireTimeUpdateEvent()
122        {
123            for (Iterator it = listeners.iterator(); it.hasNext();)
124            {
125                PlayerThreadListener listener = (PlayerThreadListener)it.next();
126                listener.updateTime(curTime, timeStep, playState);
127            }
128        }
129    }