1 package org.herac.tuxguitar.gui.transport;
2 
3 import org.herac.tuxguitar.gui.TuxGuitar;
4 import org.herac.tuxguitar.player.base.MidiPlayerListener;
5 import org.herac.tuxguitar.util.TGSynchronizer;
6 
7 public class TGTransportListener implements MidiPlayerListener{
8 
9 	protected Object sync;
10 	protected TGSynchronizer.TGRunnable startedRunnable;
11 	protected TGSynchronizer.TGRunnable stoppedRunnable;
12 
TGTransportListener()13 	public TGTransportListener(){
14 		this.sync = new Object();
15 		this.startedRunnable = getStartedRunnable();
16 		this.stoppedRunnable = getStoppedRunnable();
17 	}
18 
notifyStarted()19 	public void notifyStarted() {
20 		new Thread(new Runnable() {
21 			public void run() {
22 				try {
23 					TuxGuitar.instance().updateCache(true);
24 					while (TuxGuitar.instance().getPlayer().isRunning()) {
25 						synchronized( TGTransportListener.this.sync ){
26 							TGSynchronizer.instance().addRunnable( TGTransportListener.this.startedRunnable );
27 							TGTransportListener.this.sync.wait(25);
28 						}
29 					}
30 					TGTransportListener.this.notifyStopped();
31 				} catch (Throwable throwable) {
32 					throwable.printStackTrace();
33 				}
34 			}
35 		}).start();
36 	}
37 
notifyStopped()38 	public void notifyStopped() {
39 		try {
40 			if(!TuxGuitar.instance().getDisplay().isDisposed()){
41 				TGSynchronizer.instance().runLater( TGTransportListener.this.stoppedRunnable );
42 			}
43 		} catch (Throwable throwable) {
44 			throwable.printStackTrace();
45 		}
46 	}
47 
notifyLoop()48 	public void notifyLoop(){
49 		// Not implemented
50 	}
51 
getStartedRunnable()52 	private TGSynchronizer.TGRunnable getStartedRunnable(){
53 		return new TGSynchronizer.TGRunnable() {
54 			public void run() {
55 				if(TuxGuitar.instance().getPlayer().isRunning()){
56 					TuxGuitar.instance().redrawPlayingMode();
57 				}
58 			}
59 		};
60 	}
61 
62 	private TGSynchronizer.TGRunnable getStoppedRunnable(){
63 		return new TGSynchronizer.TGRunnable() {
64 			public void run() {
65 				TuxGuitar.instance().getTransport().gotoPlayerPosition();
66 			}
67 		};
68 	}
69 }
70