~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/actions/transport/TransportPlayAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on 17-dic-2005
 
3
 *
 
4
 * TODO To change the template for this generated file go to
 
5
 * Window - Preferences - Java - Code Style - Code Templates
 
6
 */
 
7
package org.herac.tuxguitar.gui.actions.transport;
 
8
 
 
9
import org.eclipse.swt.events.TypedEvent;
 
10
import org.eclipse.swt.widgets.Display;
 
11
import org.herac.tuxguitar.gui.TuxGuitar;
 
12
import org.herac.tuxguitar.gui.actions.Action;
 
13
import org.herac.tuxguitar.gui.util.MessageDialog;
 
14
import org.herac.tuxguitar.gui.util.MidiTickUtil;
 
15
import org.herac.tuxguitar.player.base.MidiPlayer;
 
16
import org.herac.tuxguitar.player.base.MidiPlayerException;
 
17
import org.herac.tuxguitar.song.models.TGMeasureHeader;
 
18
import org.herac.tuxguitar.util.TGSynchronizer;
 
19
 
 
20
/**
 
21
 * @author julian
 
22
 * 
 
23
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
 
24
 */
 
25
public class TransportPlayAction extends Action {
 
26
        public static final String NAME = "action.transport.play";
 
27
        
 
28
        public TransportPlayAction() {
 
29
                super(NAME, AUTO_LOCK | AUTO_UNLOCK | AUTO_UPDATE | KEY_BINDING_AVAILABLE);
 
30
        }
 
31
        
 
32
        protected int execute(TypedEvent e){
 
33
                MidiPlayer player = TuxGuitar.instance().getPlayer();
 
34
                if(!player.isRunning()){
 
35
                        try{
 
36
                                player.getMode().reset();
 
37
                                player.play();
 
38
                                playThread();
 
39
                        }catch(MidiPlayerException exception){
 
40
                                MessageDialog.errorMessage(exception);
 
41
                        }
 
42
                }else{
 
43
                        player.pause();
 
44
                        updateTickPosition();
 
45
                }
 
46
                return 0;
 
47
        }
 
48
        
 
49
        protected void playThread() {
 
50
                final Display display = TuxGuitar.instance().getDisplay();
 
51
                final TGSynchronizer.TGRunnable playing = new TGSynchronizer.TGRunnable() {
 
52
                        public void run() {
 
53
                                if(TuxGuitar.instance().getPlayer().isRunning()){
 
54
                                        TuxGuitar.instance().redrawPayingMode();
 
55
                                }
 
56
                        }
 
57
                };
 
58
                final TGSynchronizer.TGRunnable finish = new TGSynchronizer.TGRunnable() {
 
59
                        public void run() {
 
60
                                updateTickPosition();
 
61
                                TuxGuitar.instance().updateCache(true);
 
62
                        }
 
63
                };
 
64
                new Thread(new Runnable() {
 
65
                        public void run() {
 
66
                                try {
 
67
                                        while (TuxGuitar.instance().getPlayer().isRunning()) {
 
68
                                                synchronized(playing){
 
69
                                                        TGSynchronizer.instance().addRunnable(playing);
 
70
                                                        playing.wait(25);
 
71
                                                }
 
72
                                        }
 
73
                                        if(!display.isDisposed()){
 
74
                                                TGSynchronizer.instance().addRunnable(finish);
 
75
                                        }
 
76
                                } catch (Throwable throwable) {
 
77
                                        throwable.printStackTrace();
 
78
                                }
 
79
                        }
 
80
                }).start();
 
81
        }
 
82
        
 
83
        protected void updateTickPosition(){
 
84
                MidiPlayer player = TuxGuitar.instance().getPlayer();
 
85
                TGMeasureHeader header = getSongManager().getMeasureHeaderAt(MidiTickUtil.getStart(player.getTickPosition()));
 
86
                if(header != null){
 
87
                        player.setTickPosition(MidiTickUtil.getTick(header.getStart()));
 
88
                }
 
89
                getEditor().getTablature().getCaret().goToTickPosition();
 
90
        }
 
91
}
 
 
b'\\ No newline at end of file'