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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/actions/composition/ChangeTempoAction.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.composition;
8
 
 
9
 
import org.eclipse.swt.SWT;
10
 
import org.eclipse.swt.events.SelectionAdapter;
11
 
import org.eclipse.swt.events.SelectionEvent;
12
 
import org.eclipse.swt.events.TypedEvent;
13
 
import org.eclipse.swt.layout.GridData;
14
 
import org.eclipse.swt.layout.GridLayout;
15
 
import org.eclipse.swt.widgets.Button;
16
 
import org.eclipse.swt.widgets.Combo;
17
 
import org.eclipse.swt.widgets.Composite;
18
 
import org.eclipse.swt.widgets.Label;
19
 
import org.eclipse.swt.widgets.Shell;
20
 
import org.eclipse.swt.widgets.Spinner;
21
 
import org.herac.tuxguitar.gui.TuxGuitar;
22
 
import org.herac.tuxguitar.gui.actions.Action;
23
 
import org.herac.tuxguitar.gui.editors.TablatureEditor;
24
 
import org.herac.tuxguitar.gui.editors.tab.Caret;
25
 
import org.herac.tuxguitar.gui.editors.tab.MeasureCoords;
26
 
import org.herac.tuxguitar.gui.editors.tab.SongCoords;
27
 
import org.herac.tuxguitar.gui.undo.undoables.custom.UndoableChangeTempo;
28
 
import org.herac.tuxguitar.gui.util.MessageDialog;
29
 
import org.herac.tuxguitar.song.models.Tempo;
30
 
 
31
 
/**
32
 
 * @author julian
33
 
 *
34
 
 * TODO To change the template for this generated type comment go to
35
 
 * Window - Preferences - Java - Code Style - Code Templates
36
 
 */
37
 
public class ChangeTempoAction extends Action{
38
 
    public static final String NAME = "action.composition.change-tempo";
39
 
    private static final int MIN_TEMPO = 30;
40
 
    private static final int MAX_TEMPO = 320;
41
 
    
42
 
    private static final int[] DEFAULT_PERCENTS = new int[]{25,50,75,100,125,150,175,200};
43
 
    
44
 
    public ChangeTempoAction(TablatureEditor tablatureEditor) {
45
 
        super(NAME,true,tablatureEditor);       
46
 
    }
47
 
 
48
 
    public boolean doAction(TypedEvent e) {
49
 
        showDialog(getEditor().getTablature().getShell());
50
 
        return true;
51
 
    }
52
 
 
53
 
    
54
 
    public void showDialog(Shell shell) {
55
 
        MeasureCoords measure = getEditor().getTablature().getCaret().getMeasureCoords();
56
 
        if (measure != null) {
57
 
            final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
58
 
 
59
 
            dialog.setLayout(new GridLayout());
60
 
            dialog.setText(TuxGuitar.getProperty("composition.tempo"));
61
 
 
62
 
            //-----------------TEMPO------------------------
63
 
            Composite composite = new Composite(dialog, SWT.NONE);
64
 
            composite.setLayout(new GridLayout(2,false));
65
 
            composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true)); 
66
 
            
67
 
            Tempo currentTempo = measure.getMeasure().getTempo();
68
 
            Label tempoLabel = new Label(composite, SWT.NULL);
69
 
            tempoLabel.setText(TuxGuitar.getProperty("composition.tempo"));
70
 
                        
71
 
            final Spinner tempo = new Spinner(composite, SWT.BORDER);
72
 
            tempo.setLayoutData(getSpinnerData());            
73
 
            tempo.setMinimum(MIN_TEMPO);
74
 
            tempo.setMaximum(MAX_TEMPO);
75
 
            tempo.setSelection(currentTempo.getValue());                                    
76
 
            
77
 
            Label percentLabel = new Label(composite, SWT.NULL);
78
 
            percentLabel.setText(TuxGuitar.getProperty("composition.tempo-percent"));
79
 
            
80
 
            final Combo percent = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
81
 
            percent.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
82
 
            for(int i = 0; i < DEFAULT_PERCENTS.length; i ++){
83
 
                percent.add(Integer.toString(DEFAULT_PERCENTS[i]) + "%",i);
84
 
                if(currentTempo.getPercent() == DEFAULT_PERCENTS[i]){
85
 
                        percent.select(i);
86
 
                }
87
 
            }            
88
 
            //------------------BUTTONS--------------------------            
89
 
            Composite buttons = new Composite(dialog, SWT.NONE);
90
 
            buttons.setLayout(new GridLayout(2,false));
91
 
            buttons.setLayoutData(new GridData(SWT.END,SWT.FILL,true,true));            
92
 
            
93
 
            GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);    
94
 
            data.minimumWidth = 80;
95
 
            data.minimumHeight = 25;     
96
 
            
97
 
            final Button buttonOK = new Button(buttons, SWT.PUSH);
98
 
            buttonOK.setText(TuxGuitar.getProperty("ok"));
99
 
            buttonOK.setLayoutData(data);
100
 
            buttonOK.addSelectionListener(new SelectionAdapter() {
101
 
                public void widgetSelected(SelectionEvent arg0) {   
102
 
                    boolean valid = false;
103
 
                   
104
 
                    int tempoValue = tempo.getSelection();
105
 
                                      
106
 
                    if(tempoValue >= MIN_TEMPO && MAX_TEMPO <= 320){
107
 
                        Tempo tempo = new Tempo(tempoValue,DEFAULT_PERCENTS[ (percent.getSelectionIndex() >= 0)?percent.getSelectionIndex():3 ]);                                       
108
 
                        setTempo(tempo);
109
 
                        valid = true;
110
 
                    }
111
 
               
112
 
                    if(!valid){
113
 
                        showErrorMessage(TuxGuitar.getProperty("composition.tempo.invalid"));
114
 
                    }
115
 
                    
116
 
                    dialog.dispose();
117
 
                }
118
 
            });
119
 
 
120
 
            Button buttonCancel = new Button(buttons, SWT.PUSH);
121
 
            buttonCancel.setText(TuxGuitar.getProperty("cancel"));
122
 
            buttonCancel.setLayoutData(data);
123
 
            buttonCancel.addSelectionListener(new SelectionAdapter() {
124
 
                public void widgetSelected(SelectionEvent arg0) {
125
 
                    dialog.dispose();
126
 
                }
127
 
            });
128
 
 
129
 
            dialog.pack();
130
 
            dialog.open();
131
 
 
132
 
            int x = shell.getBounds().x + (shell.getBounds().width - dialog.getSize().x) / 2;
133
 
            int y = shell.getBounds().y + (shell.getBounds().height - dialog.getSize().y) / 2;
134
 
            dialog.setLocation(x, y);
135
 
            
136
 
                while (!dialog.isDisposed()) {
137
 
                if (!dialog.getDisplay().readAndDispatch()) {
138
 
                        dialog.getDisplay().sleep();
139
 
                }
140
 
            }
141
 
        }
142
 
    }
143
 
    
144
 
    private GridData getSpinnerData(){
145
 
        GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
146
 
        data.minimumWidth = 150;
147
 
        return data;
148
 
    }
149
 
    
150
 
    private void setTempo(Tempo tempo){
151
 
        //comienza el undoable
152
 
        UndoableChangeTempo undoable = UndoableChangeTempo.startUndo(); 
153
 
        
154
 
        Caret caret = getEditor().getTablature().getCaret();
155
 
        SongCoords songCoords = caret.getSongCoords();
156
 
        MeasureCoords measure = caret.getMeasureCoords();
157
 
        
158
 
        getSongManager().changeTempo(measure.getMeasure().getStart(),tempo,true);
159
 
        
160
 
        //actualizo la tablatura
161
 
        updateTablature();
162
 
        redraw();                
163
 
        
164
 
        //termia el undoable
165
 
        getEditor().getUndoManager().addEdit(undoable.endUndo(tempo));
166
 
    }    
167
 
 
168
 
    
169
 
    private void showErrorMessage(String message){            
170
 
        String name = "Error Message";
171
 
        new MessageDialog(name,message,SWT.ICON_ERROR | SWT.OK).show(getEditor().getTablature().getShell());        
172
 
    }    
173
 
}