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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/actions/player/PlaySongAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2007-02-04 01:41:23 UTC
  • Revision ID: james.westby@ubuntu.com-20070204014123-9pv7okph0iaiqkvw
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

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.player;
 
8
 
 
9
import org.eclipse.swt.SWT;
 
10
import org.eclipse.swt.events.TypedEvent;
 
11
import org.eclipse.swt.widgets.Display;
 
12
import org.herac.tuxguitar.gui.TuxGuitar;
 
13
import org.herac.tuxguitar.gui.actions.Action;
 
14
import org.herac.tuxguitar.gui.editors.TablatureEditor;
 
15
import org.herac.tuxguitar.gui.helper.SyncThread;
 
16
import org.herac.tuxguitar.gui.util.MessageDialog;
 
17
import org.herac.tuxguitar.player.base.MidiPlayer;
 
18
import org.herac.tuxguitar.song.models.Measure;
 
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 PlaySongAction extends Action {
 
26
    public static final String NAME = "action.player.play";
 
27
 
 
28
    public PlaySongAction(TablatureEditor tablatureEditor) {
 
29
        super(NAME,true, tablatureEditor);
 
30
    }
 
31
 
 
32
    public boolean doAction(TypedEvent e) {
 
33
        MidiPlayer player = TuxGuitar.instance().getPlayer();
 
34
        if(!player.isRunning()){
 
35
                if(checkSoundbank(player)){
 
36
                        player.play();
 
37
                        redrawTablaturePlayMode();
 
38
                }
 
39
        }else{
 
40
                Measure measure = getSongManager().getTrackManager().getMeasureAt(getSongManager().getFirstTrack(),player.getTickPosition());               
 
41
                player.pause();
 
42
                if(measure != null){
 
43
                        player.setTickPosition(measure.getStartWidthRepetitions(),measure.getStartWidthRepetitions() - measure.getStart());
 
44
                }
 
45
                getEditor().getTablature().getCaret().goToTickPosition();
 
46
            redrawTablatureStopMode();
 
47
        }
 
48
        return true;
 
49
    }   
 
50
    
 
51
    private boolean checkSoundbank(MidiPlayer player){
 
52
        if(player.getSoundbank() == null){
 
53
                String title = TuxGuitar.getProperty("soundbank.error");        
 
54
                String message = TuxGuitar.getProperty("soundbank.error.unavailable");
 
55
                new MessageDialog(title,message,SWT.ICON_ERROR | SWT.OK).show(getEditor().getTablature().getShell());
 
56
                return false;
 
57
        }
 
58
        return true;
 
59
    }
 
60
    
 
61
    protected void redrawTablaturePlayMode() {
 
62
        final Display display = TuxGuitar.instance().getDisplay();
 
63
        final Runnable playing = new Runnable() {
 
64
            public void run() {
 
65
                if(TuxGuitar.instance().getPlayer().isRunning()){                       
 
66
                        TuxGuitar.instance().redrawPayingMode();
 
67
                }
 
68
            }
 
69
        };
 
70
        final Runnable finish = new Runnable() {
 
71
            public void run() {
 
72
                //update();
 
73
                TuxGuitar.instance().updateCache(true);
 
74
                redraw();
 
75
            }
 
76
        };
 
77
        new Thread(new Runnable() {
 
78
            public void run() {
 
79
                while (TuxGuitar.instance().getPlayer().isRunning()) {
 
80
                    if(!display.isDisposed()){
 
81
                        display.syncExec(playing);    
 
82
                        sleep(80);
 
83
                    }
 
84
                }
 
85
                if(!display.isDisposed()){
 
86
                    display.syncExec(finish);
 
87
                }
 
88
            }
 
89
        }).start();
 
90
    }
 
91
 
 
92
    private void redrawTablatureStopMode(){
 
93
        new SyncThread(new Runnable() {
 
94
            public void run() {
 
95
                if(!TuxGuitar.isDisposed()){
 
96
                redraw();
 
97
                }
 
98
            }
 
99
        }).start();   
 
100
    }    
 
101
    
 
102
    private void sleep(long time){
 
103
        try {
 
104
                        Thread.sleep(time);
 
105
                } catch (InterruptedException e) {
 
106
                        e.printStackTrace();
 
107
                }
 
108
    }
 
109
    
 
110
}
 
 
b'\\ No newline at end of file'