Revision 15396

View differences:

import/ext3D/branches/ext3D_v1.1/libAnimation/src/com/iver/cit/gvsig/animation/timer/AnimationTimer.java
1 1
package com.iver.cit.gvsig.animation.timer;
2 2

  
3
import java.util.Date;
4
import java.util.Observable;
5
import java.util.Observer;
6
import java.util.Timer;
7
import java.util.TimerTask;
3
import java.util.ArrayList;
4
import java.util.List;
8 5

  
9 6

  
10 7

  
11 8

  
9

  
12 10
// this class is only an example. it isn?t used in librarys
13
public class AnimationTimer extends Observable {
11
public class AnimationTimer  implements Runnable {
12
	double milis;
13
	private boolean finish;
14
	private List callBackList;
15
	private Thread thread;
16
	
17
	
14 18
    /**
15 19
     * Lanza un timer cada segundo.
16 20
     */
17 21
    public AnimationTimer()
18 22
    {
19
        Timer timer = new Timer();
20
        timer.scheduleAtFixedRate(timerTask, 0, 1000);
23
    	
21 24
    }
22 25

  
23
    /**
24
     * main de prueba de esta clase.
25
     * No necesita una ventana para funcionar.
26
     */
27
    public static void main (String [] args)
28
    {
29
    	AnimationTimer modelo = new AnimationTimer();
30
        modelo.addObserver (new Observer()
31
        {
32
            public void update (Observable unObservable, Object dato)
33
            {
34
                System.out.println (dato);
35
            }
36
        });
37
        
38
    }
39
    
40
    /**
41
     * Clase que se mete en Timer, para que se le avise cada segundo.
42
     */
43
    TimerTask timerTask = new TimerTask()
44
    {
45
        /**
46
         * M?todo al que Timer llamar? cada segundo. Se encarga de avisar
47
         * a los observadores de este modelo.
48
         */
49
        public void run() 
50
        {
51
        	
52
            setChanged();
53
            notifyObservers(new Date());
54
        }
55
    };
26
	public void run() {
27
		while (true) {
28
			try {
29
				Thread.sleep((long) milis);
30
				synchronized (this) {
31
					if (finish) {
32
						break;
33
					}
34
				}
35
			} catch (InterruptedException e) {
36

  
37
				e.printStackTrace();
38
			}
39
			//Updating 
40
			Update();
41
		}		
42
	}
43
	
44
	public synchronized void end() {
45
		finish = true;
46
		thread.stop();
47
	}
48
	
49
	
50
	private void Update() {
51
		// TODO Auto-generated method stub
52
		for (int i = 0; i < callBackList.size(); i++) {
53
			IUpdateCallBack element = (IUpdateCallBack) callBackList.get(i);
54
			element.update();		
55
		}
56
		
57
	}
58

  
59
	public void start(double milis) {
60
		this.milis = milis;
61

  
62
		// Create the thread supplying it with the runnable object
63
		thread = new Thread(this);
64

  
65
		// Start the thread
66
		thread.start();
67
	}
68
	
69
	public void addCallBackObject(IUpdateCallBack object) {
70
		if (callBackList == null)
71
			callBackList =  new ArrayList(); 
72
		callBackList.add(object);
73
	}
74

  
75
  
56 76
}

Also available in: Unified diff