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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/transport/SongTransport.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 20-mar-2006
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.transport;
8
 
 
9
 
import org.eclipse.swt.SWT;
10
 
import org.eclipse.swt.events.MouseAdapter;
11
 
import org.eclipse.swt.events.MouseEvent;
12
 
import org.eclipse.swt.events.SelectionAdapter;
13
 
import org.eclipse.swt.events.SelectionEvent;
14
 
import org.eclipse.swt.events.TypedEvent;
15
 
import org.eclipse.swt.graphics.Font;
16
 
import org.eclipse.swt.layout.GridData;
17
 
import org.eclipse.swt.layout.GridLayout;
18
 
import org.eclipse.swt.widgets.Composite;
19
 
import org.eclipse.swt.widgets.Label;
20
 
import org.eclipse.swt.widgets.Scale;
21
 
import org.eclipse.swt.widgets.Shell;
22
 
import org.eclipse.swt.widgets.ToolBar;
23
 
import org.eclipse.swt.widgets.ToolItem;
24
 
import org.herac.tuxguitar.gui.SystemImages;
25
 
import org.herac.tuxguitar.gui.TuxGuitar;
26
 
import org.herac.tuxguitar.gui.actions.player.PlaySongAction;
27
 
import org.herac.tuxguitar.gui.actions.player.StopSongAction;
28
 
import org.herac.tuxguitar.player.base.MidiPlayer;
29
 
import org.herac.tuxguitar.song.managers.SongManager;
30
 
import org.herac.tuxguitar.song.models.Duration;
31
 
import org.herac.tuxguitar.song.models.MeasureHeader;
32
 
 
33
 
/**
34
 
 * @author julian
35
 
 * 
36
 
 * TODO To change the template for this generated type comment go to Window -
37
 
 * Preferences - Java - Code Style - Code Templates
38
 
 */
39
 
public class SongTransport {
40
 
        public static final int STATUS_STOPED = 1;
41
 
        public static final int STATUS_PAUSED = 2;
42
 
        public static final int STATUS_RUNNING = 3;
43
 
        
44
 
        private Shell dialog;
45
 
        private Label label;
46
 
        private Scale tickScale;
47
 
        private ToolItem first;
48
 
        private ToolItem last;
49
 
        private ToolItem previous;
50
 
        private ToolItem next;
51
 
        private ToolItem stop;
52
 
        private ToolItem play;  
53
 
        private boolean editingTickScale;
54
 
        private int status;
55
 
        
56
 
        public SongTransport() {
57
 
        }
58
 
        
59
 
        public void show() {
60
 
                Shell shell = TuxGuitar.instance().getTablatureEditor().getTablature().getShell();
61
 
                this.dialog = new Shell(shell, SWT.DIALOG_TRIM);                
62
 
                this.dialog.setLayout(new GridLayout());
63
 
                this.init();
64
 
                this.dialog.pack();
65
 
                this.dialog.open();
66
 
                
67
 
                int x = shell.getBounds().x + (shell.getBounds().width - dialog.getSize().x) / 2;
68
 
                int y = shell.getBounds().y + (shell.getBounds().height - dialog.getSize().y) / 2;
69
 
                this.dialog.setLocation(x, y);
70
 
                                
71
 
                TuxGuitar.instance().updateCache(true);
72
 
                while (!this.dialog.isDisposed()) {
73
 
                        if (!this.dialog.getDisplay().readAndDispatch()) {
74
 
                                this.dialog.getDisplay().sleep();
75
 
                        }
76
 
                }               
77
 
                TuxGuitar.instance().updateCache(true);
78
 
                
79
 
        }
80
 
        
81
 
        private void init(){
82
 
                Composite composite = new Composite(this.dialog,SWT.NONE);
83
 
                composite.setLayout(new GridLayout());
84
 
                composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
85
 
                
86
 
                initLabel(composite);
87
 
                initScale(composite);
88
 
                initButtons(composite);
89
 
                
90
 
                this.updateItems();
91
 
                this.redraw();
92
 
        }
93
 
 
94
 
        private void initLabel(Composite parent){               
95
 
                this.label = new Label(parent,SWT.BORDER | SWT.RIGHT);
96
 
                this.label.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));    
97
 
                this.label.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
98
 
                this.label.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
99
 
                this.label.setFont(new Font(parent.getDisplay(),"Minisystem",36,SWT.NORMAL));
100
 
        }
101
 
        
102
 
        private void initScale(Composite parent){
103
 
                this.tickScale = new Scale(parent, SWT.HORIZONTAL);
104
 
                this.tickScale.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));        
105
 
                this.tickScale.setIncrement(1000);              
106
 
                this.tickScale.setSelection(1000);
107
 
                this.tickScale.addMouseListener(new MouseAdapter() {            
108
 
                        public void mouseDown(MouseEvent e) {
109
 
                                setEditingTickScale(true);
110
 
                        }               
111
 
                        public void mouseUp(MouseEvent e) {
112
 
                                if(tickScale.getSelection() < Duration.QUARTER_TIME){
113
 
                                        tickScale.setSelection((int)Duration.QUARTER_TIME);
114
 
                                }
115
 
                                long position = tickScale.getSelection();                       
116
 
                                gotoMeasure(getSongManager().getMeasureHeaderAt(position),true);
117
 
                                redraw();
118
 
                                setEditingTickScale(false);
119
 
                        }                       
120
 
                });
121
 
        }
122
 
        
123
 
        private void initButtons(Composite parent){
124
 
                ToolBar toolBar = new ToolBar(this.dialog,SWT.FLAT);
125
 
 
126
 
                this.first = new ToolItem(toolBar,SWT.PUSH);
127
 
                this.first.setImage(SystemImages.TRANSPORT_FIRST_1);
128
 
                this.first.addSelectionListener(new SelectionAdapter() {
129
 
                        public void widgetSelected(SelectionEvent e) {
130
 
                                gotoFirst(e);
131
 
                        }               
132
 
                });
133
 
                
134
 
                this.previous = new ToolItem(toolBar,SWT.PUSH);
135
 
                this.previous.setImage(SystemImages.TRANSPORT_PREVIOUS_1);
136
 
                this.previous.addSelectionListener(new SelectionAdapter() {
137
 
                        public void widgetSelected(SelectionEvent e) {
138
 
                                gotoPrevious(e);
139
 
                        }               
140
 
                });
141
 
 
142
 
                this.stop = new ToolItem(toolBar,SWT.PUSH);
143
 
                this.stop.setImage(SystemImages.TRANSPORT_STOP_1);
144
 
                this.stop.addSelectionListener(new SelectionAdapter() {
145
 
                        public void widgetSelected(SelectionEvent e) {
146
 
                                stop(e);
147
 
                        }               
148
 
                });                             
149
 
                
150
 
                this.play = new ToolItem(toolBar,SWT.PUSH);
151
 
                this.play.setImage(SystemImages.TRANSPORT_PLAY_1);      
152
 
                this.play.addSelectionListener(new SelectionAdapter() {
153
 
                        public void widgetSelected(SelectionEvent e) {
154
 
                                play(e);
155
 
                        }               
156
 
                });             
157
 
                
158
 
                this.next = new ToolItem(toolBar,SWT.PUSH);
159
 
                this.next.setImage(SystemImages.TRANSPORT_NEXT_1);
160
 
                this.next.addSelectionListener(new SelectionAdapter() {
161
 
                        public void widgetSelected(SelectionEvent e) {
162
 
                                gotoNext(e);
163
 
                        }               
164
 
                });
165
 
                
166
 
                this.last = new ToolItem(toolBar,SWT.PUSH);
167
 
                this.last.setImage(SystemImages.TRANSPORT_LAST_1);
168
 
                this.last.addSelectionListener(new SelectionAdapter() {
169
 
                        public void widgetSelected(SelectionEvent e) {
170
 
                                gotoLast(e);
171
 
                        }               
172
 
                });
173
 
                                
174
 
                this.setStatus(STATUS_STOPED);          
175
 
        }
176
 
 
177
 
        
178
 
        
179
 
        public int getStatus() {
180
 
                return status;
181
 
        }
182
 
 
183
 
        public void setStatus(int status) {
184
 
                this.status = status;
185
 
        }
186
 
 
187
 
        public void redraw(){           
188
 
                if(!isDisposed()){                      
189
 
                        if(isEditingTickScale()){
190
 
                                this.label.setText(Long.toString(this.tickScale.getSelection()));
191
 
                        }else{
192
 
                                long tickPosition = TuxGuitar.instance().getPlayer().getTickPosition();
193
 
                                this.label.setText(Long.toString(tickPosition));                                
194
 
                                this.tickScale.setSelection((int)tickPosition);
195
 
                        }
196
 
                }
197
 
        }
198
 
        
199
 
        public void updateItems(){
200
 
                if(!isDisposed()){
201
 
                        int lastStatus = getStatus();
202
 
                        
203
 
                        if(TuxGuitar.instance().getPlayer().isRunning()){
204
 
                                setStatus(STATUS_RUNNING);                      
205
 
                        }else if(TuxGuitar.instance().getPlayer().isPaused()){
206
 
                                setStatus(STATUS_PAUSED);
207
 
                        }else{
208
 
                                setStatus(STATUS_STOPED);
209
 
                        }               
210
 
                        
211
 
                        if(lastStatus != getStatus()){
212
 
                                if(getStatus() == STATUS_RUNNING){
213
 
                                        this.first.setImage(SystemImages.TRANSPORT_FIRST_2);
214
 
                                        this.last.setImage(SystemImages.TRANSPORT_LAST_2);
215
 
                                        this.previous.setImage(SystemImages.TRANSPORT_PREVIOUS_2);
216
 
                                        this.next.setImage(SystemImages.TRANSPORT_NEXT_2);
217
 
                                        this.stop.setImage(SystemImages.TRANSPORT_STOP_2);
218
 
                                        this.play.setImage(SystemImages.TRANSPORT_PAUSE);                               
219
 
                                }else if(getStatus() == STATUS_PAUSED){
220
 
                                        this.first.setImage(SystemImages.TRANSPORT_FIRST_2);
221
 
                                        this.last.setImage(SystemImages.TRANSPORT_LAST_2);
222
 
                                        this.previous.setImage(SystemImages.TRANSPORT_PREVIOUS_2);
223
 
                                        this.next.setImage(SystemImages.TRANSPORT_NEXT_2);
224
 
                                        this.stop.setImage(SystemImages.TRANSPORT_STOP_2);
225
 
                                        this.play.setImage(SystemImages.TRANSPORT_PLAY_2);      
226
 
                                        
227
 
                                }else if(getStatus() == STATUS_STOPED){
228
 
                                        this.first.setImage(SystemImages.TRANSPORT_FIRST_1);
229
 
                                        this.last.setImage(SystemImages.TRANSPORT_LAST_1);
230
 
                                        this.previous.setImage(SystemImages.TRANSPORT_PREVIOUS_1);
231
 
                                        this.next.setImage(SystemImages.TRANSPORT_NEXT_1);
232
 
                                        this.stop.setImage(SystemImages.TRANSPORT_STOP_1);
233
 
                                        this.play.setImage(SystemImages.TRANSPORT_PLAY_1);
234
 
                                }       
235
 
                        }                       
236
 
                        MeasureHeader first = getSongManager().getFirstMeasureHeader();
237
 
                        MeasureHeader last = getSongManager().getLastMeasureHeader();
238
 
                        this.tickScale.setMinimum((int)first.getStart());
239
 
                        this.tickScale.setMaximum((int)(last.getStart() + last.getLength()) -1);                        
240
 
                        this.tickScale.setPageIncrement((int) ((last.getStart() + last.getLength() - Duration.QUARTER_TIME) / 10));
241
 
                        this.redraw();
242
 
                }               
243
 
        }
244
 
        
245
 
        
246
 
        
247
 
        public void loadProperties(){
248
 
                if(!isDisposed()){                                              
249
 
                }
250
 
        }       
251
 
        
252
 
        public void dispose() {
253
 
                if(!isDisposed()){
254
 
                        this.dialog.dispose();
255
 
                }
256
 
        }
257
 
 
258
 
        public boolean isDisposed() {
259
 
                return (this.dialog == null || this.dialog.isDisposed());
260
 
        }
261
 
 
262
 
        public boolean isEditingTickScale() {
263
 
                return editingTickScale;
264
 
        }
265
 
 
266
 
        public void setEditingTickScale(boolean editingTickScale) {
267
 
                this.editingTickScale = editingTickScale;
268
 
        }
269
 
 
270
 
        private SongManager getSongManager(){
271
 
                return TuxGuitar.instance().getSongManager();
272
 
        }
273
 
        
274
 
        public void gotoFirst(TypedEvent e){
275
 
                gotoMeasure(getSongManager().getFirstMeasureHeader(),true);
276
 
        }
277
 
        
278
 
        public void gotoLast(TypedEvent e){
279
 
                gotoMeasure(getSongManager().getLastMeasureHeader(),true) ;             
280
 
        }
281
 
        
282
 
        public void gotoNext(TypedEvent e){
283
 
                MidiPlayer player = TuxGuitar.instance().getPlayer();
284
 
                MeasureHeader header = getSongManager().getMeasureHeaderAt(player.getTickPosition());
285
 
                if(header != null){
286
 
                        gotoMeasure(getSongManager().getNextMeasureHeader(header),true);
287
 
                }
288
 
        }
289
 
        
290
 
        public void gotoPrevious(TypedEvent e){
291
 
                MidiPlayer player = TuxGuitar.instance().getPlayer();
292
 
                MeasureHeader header = getSongManager().getMeasureHeaderAt(player.getTickPosition());
293
 
                if(header != null){
294
 
                        gotoMeasure(getSongManager().getPrevMeasureHeader(header),true);
295
 
                }               
296
 
        }       
297
 
        
298
 
        public void gotoMeasure(MeasureHeader header){
299
 
                gotoMeasure(header,false);
300
 
        }
301
 
        
302
 
        private void gotoMeasure(MeasureHeader header,boolean moveCaret){
303
 
                if(header != null && !getSongManager().isAtPosition(header,TuxGuitar.instance().getPlayer().getTickPosition())){                        
304
 
                        TuxGuitar.instance().getPlayer().setTickPosition(header.getStartWidthRepetitions(),header.getStartWidthRepetitions() - header.getStart());
305
 
                        if(moveCaret){
306
 
                                TuxGuitar.instance().getTablatureEditor().getTablature().getCaret().goToTickPosition();
307
 
                                TuxGuitar.instance().redraw();
308
 
                        }
309
 
                        redraw();
310
 
                }               
311
 
        }
312
 
 
313
 
        public void play(TypedEvent e){
314
 
                TuxGuitar.instance().getAction(PlaySongAction.NAME).process(e);
315
 
        }
316
 
        
317
 
        public void stop(TypedEvent e){
318
 
                TuxGuitar.instance().getAction(StopSongAction.NAME).process(e);
319
 
        }       
320
 
        
321
 
}