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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/actions/transport/TransportModeAction.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 java.util.ArrayList;
 
10
import java.util.Iterator;
 
11
import java.util.List;
 
12
 
 
13
import org.eclipse.swt.SWT;
 
14
import org.eclipse.swt.events.SelectionAdapter;
 
15
import org.eclipse.swt.events.SelectionEvent;
 
16
import org.eclipse.swt.events.TypedEvent;
 
17
import org.eclipse.swt.layout.GridData;
 
18
import org.eclipse.swt.layout.GridLayout;
 
19
import org.eclipse.swt.widgets.Button;
 
20
import org.eclipse.swt.widgets.Combo;
 
21
import org.eclipse.swt.widgets.Composite;
 
22
import org.eclipse.swt.widgets.Control;
 
23
import org.eclipse.swt.widgets.Group;
 
24
import org.eclipse.swt.widgets.Label;
 
25
import org.eclipse.swt.widgets.Shell;
 
26
import org.eclipse.swt.widgets.Spinner;
 
27
import org.herac.tuxguitar.gui.TuxGuitar;
 
28
import org.herac.tuxguitar.gui.actions.Action;
 
29
import org.herac.tuxguitar.gui.util.DialogUtils;
 
30
import org.herac.tuxguitar.player.base.MidiPlayerMode;
 
31
 
 
32
/**
 
33
 * @author julian
 
34
 * 
 
35
 * TODO To change the template for this generated type comment go to Window -
 
36
 * Preferences - Java - Code Style - Code Templates
 
37
 */
 
38
public class TransportModeAction extends Action {
 
39
        public static final String NAME = "action.transport.mode";
 
40
        
 
41
        protected static final int MIN_SELECTION = 1;
 
42
        protected static final int MAX_SELECTION = 500;
 
43
        protected static final int[] DEFAULT_PERCENTS = new int[]{25,50,75,100,125,150,175,200};
 
44
        
 
45
        protected Button single;
 
46
        protected Button singleLoop;
 
47
        protected Combo singlePercent;
 
48
        
 
49
        protected Button custom;
 
50
        protected Spinner customFrom;
 
51
        protected Spinner customTo;
 
52
        protected Spinner customIncrement;
 
53
        
 
54
        public TransportModeAction() {
 
55
                super(NAME, AUTO_LOCK | AUTO_UNLOCK | AUTO_UPDATE | KEY_BINDING_AVAILABLE);
 
56
        }
 
57
        
 
58
        protected int execute(TypedEvent e){
 
59
                this.showDialog(e.widget.getDisplay().getActiveShell(), TuxGuitar.instance().getPlayer().getMode());
 
60
                return 0;
 
61
        }
 
62
        
 
63
        public void showDialog(final Shell parent,final MidiPlayerMode mode) {
 
64
                final Shell dialog = DialogUtils.newDialog(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
 
65
                dialog.setLayout(new GridLayout());
 
66
                dialog.setText(TuxGuitar.getProperty("transport.mode"));
 
67
                
 
68
                // ----------------------------------------------------------------------
 
69
                
 
70
                Composite radios = new Composite(dialog, SWT.NONE);
 
71
                radios.setLayout(new GridLayout());
 
72
                radios.setLayoutData(getMainData());
 
73
                
 
74
                //---Single---
 
75
                this.single = new Button(radios, SWT.RADIO);
 
76
                this.single.setText(TuxGuitar.getProperty("transport.mode.simple"));
 
77
                this.single.setSelection(mode.getType() == MidiPlayerMode.TYPE_SINGLE);
 
78
                RadioSelectionAdapter singleAdapter = new RadioSelectionAdapter(this.single);
 
79
                
 
80
                Group singleGroup = new Group(radios, SWT.SHADOW_ETCHED_IN);
 
81
                singleGroup.setLayout(new GridLayout(2,false));
 
82
                singleGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 
83
                singleGroup.setText(TuxGuitar.getProperty("transport.mode.simple"));
 
84
                singleAdapter.addControl(singleGroup);
 
85
                
 
86
                singleAdapter.addControl(makeLabel(singleGroup, TuxGuitar.getProperty("transport.mode.simple.tempo-percent"),SWT.LEFT,1));
 
87
                this.singlePercent = new Combo(singleGroup, SWT.DROP_DOWN | SWT.READ_ONLY);
 
88
                this.singlePercent.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
89
                for(int i = 0; i < DEFAULT_PERCENTS.length; i ++){
 
90
                        this.singlePercent.add(Integer.toString(DEFAULT_PERCENTS[i]) + "%",i);
 
91
                        if(mode.getSinglePercent() == DEFAULT_PERCENTS[i]){
 
92
                                this.singlePercent.select(i);
 
93
                        }
 
94
                }
 
95
                singleAdapter.addControl(this.singlePercent);
 
96
                
 
97
                this.singleLoop = new Button(singleGroup, SWT.CHECK);
 
98
                this.singleLoop.setText(TuxGuitar.getProperty("transport.mode.simple.loop"));
 
99
                this.singleLoop.setSelection(mode.isLoop());
 
100
                singleAdapter.addControl(this.singleLoop);
 
101
                
 
102
                GridData loopedData = new GridData(SWT.FILL,SWT.FILL,true,true);
 
103
                loopedData.horizontalSpan = 2;
 
104
                this.singleLoop.setLayoutData(loopedData);
 
105
                
 
106
                //---Trainer---
 
107
                this.custom = new Button(radios, SWT.RADIO);
 
108
                this.custom.setText(TuxGuitar.getProperty("transport.mode.trainer"));
 
109
                this.custom.setSelection(mode.getType() == MidiPlayerMode.TYPE_CUSTOM);
 
110
                RadioSelectionAdapter customAdapter = new RadioSelectionAdapter(this.custom);
 
111
                
 
112
                Group trainerGroup = new Group(radios, SWT.SHADOW_ETCHED_IN);
 
113
                trainerGroup.setLayout(new GridLayout(6,false));
 
114
                trainerGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 
115
                trainerGroup.setText(TuxGuitar.getProperty("transport.mode.trainer"));
 
116
                customAdapter.addControl(trainerGroup);
 
117
                
 
118
                customAdapter.addControl(makeLabel(trainerGroup, TuxGuitar.getProperty("composition.tempo"),SWT.LEFT,1));
 
119
                this.customFrom = new Spinner(trainerGroup,SWT.BORDER);
 
120
                this.customFrom.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 
121
                this.customFrom.setMinimum(MIN_SELECTION);
 
122
                this.customFrom.setMaximum(MAX_SELECTION);
 
123
                this.customFrom.setSelection(mode.getCustomPercentFrom());
 
124
                customAdapter.addControl(this.customFrom);
 
125
                customAdapter.addControl(makeLabel(trainerGroup, "%",SWT.LEFT,1));
 
126
                
 
127
                customAdapter.addControl(makeLabel(trainerGroup, TuxGuitar.getProperty("edit.to"),SWT.RIGHT,1));
 
128
                this.customTo = new Spinner(trainerGroup,SWT.BORDER);
 
129
                this.customTo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 
130
                this.customTo.setMinimum(MIN_SELECTION);
 
131
                this.customTo.setMaximum(MAX_SELECTION);
 
132
                this.customTo.setSelection(mode.getCustomPercentTo());
 
133
                customAdapter.addControl(this.customTo);
 
134
                customAdapter.addControl(makeLabel(trainerGroup, "%",SWT.LEFT,1));
 
135
                
 
136
                customAdapter.addControl(makeLabel(trainerGroup, TuxGuitar.getProperty("transport.mode.trainer.increment-description"),SWT.LEFT,4));
 
137
                this.customIncrement = new Spinner(trainerGroup,SWT.BORDER);
 
138
                this.customIncrement.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 
139
                this.customIncrement.setMinimum(MIN_SELECTION);
 
140
                this.customIncrement.setMaximum(MAX_SELECTION);
 
141
                this.customIncrement.setSelection(mode.getCustomPercentIncrement());
 
142
                customAdapter.addControl(this.customIncrement);
 
143
                customAdapter.addControl(makeLabel(trainerGroup, "%",SWT.LEFT,1));
 
144
                
 
145
                SpinnerSelectionAdapter spinnerAdapter = new SpinnerSelectionAdapter(this.customFrom,this.customTo,this.customIncrement);
 
146
                this.customFrom.addSelectionListener(spinnerAdapter);
 
147
                this.customTo.addSelectionListener(spinnerAdapter);
 
148
                this.customIncrement.addSelectionListener(spinnerAdapter);
 
149
                
 
150
                singleAdapter.update();
 
151
                customAdapter.update();
 
152
                // ------------------BUTTONS--------------------------
 
153
                Composite buttons = new Composite(dialog, SWT.NONE);
 
154
                buttons.setLayout(new GridLayout(2, false));
 
155
                buttons.setLayoutData(new GridData(SWT.END, SWT.FILL, true, true));
 
156
                
 
157
                final Button buttonOK = new Button(buttons, SWT.PUSH);
 
158
                buttonOK.setText(TuxGuitar.getProperty("ok"));
 
159
                buttonOK.setLayoutData(getButtonData());
 
160
                buttonOK.addSelectionListener(new SelectionAdapter() {
 
161
                        public void widgetSelected(SelectionEvent arg0) {
 
162
                                updateMode(mode);
 
163
                                dialog.dispose();
 
164
                        }
 
165
                });
 
166
                
 
167
                Button buttonCancel = new Button(buttons, SWT.PUSH);
 
168
                buttonCancel.setText(TuxGuitar.getProperty("cancel"));
 
169
                buttonCancel.setLayoutData(getButtonData());
 
170
                buttonCancel.addSelectionListener(new SelectionAdapter() {
 
171
                        public void widgetSelected(SelectionEvent arg0) {
 
172
                                dialog.dispose();
 
173
                        }
 
174
                });
 
175
                
 
176
                dialog.setDefaultButton( buttonOK );
 
177
                
 
178
                DialogUtils.openDialog(dialog,DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK | DialogUtils.OPEN_STYLE_WAIT);
 
179
        }
 
180
        
 
181
        private GridData getMainData(){
 
182
                GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
 
183
                data.minimumWidth = 350;
 
184
                return data;
 
185
        }
 
186
        
 
187
        private GridData getButtonData(){
 
188
                GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
 
189
                data.minimumWidth = 80;
 
190
                data.minimumHeight = 25;
 
191
                return data;
 
192
        }
 
193
        
 
194
        private Label makeLabel(Composite parent,String text,int aligment,int horizontalSpan){
 
195
                Label label = new Label(parent,SWT.CENTER | aligment);
 
196
                label.setText(text);
 
197
                GridData data = new GridData(SWT.FILL,SWT.CENTER,true,true);
 
198
                data.horizontalSpan = horizontalSpan;
 
199
                label.setLayoutData(data);
 
200
                return label;
 
201
        }
 
202
        
 
203
        protected void updateMode(MidiPlayerMode mode){
 
204
                int type = (this.custom.getSelection())?MidiPlayerMode.TYPE_CUSTOM:MidiPlayerMode.TYPE_SINGLE;
 
205
                mode.setType(type);
 
206
                mode.setLoop( (type == MidiPlayerMode.TYPE_CUSTOM || (type == MidiPlayerMode.TYPE_SINGLE && this.singleLoop.getSelection())));
 
207
                mode.setSinglePercent( this.singlePercent.getSelectionIndex() >= 0?DEFAULT_PERCENTS[this.singlePercent.getSelectionIndex()]:MidiPlayerMode.DEFAULT_TEMPO_PERCENT);
 
208
                mode.setCustomPercentFrom(this.customFrom.getSelection());
 
209
                mode.setCustomPercentTo(this.customTo.getSelection());
 
210
                mode.setCustomPercentIncrement(this.customIncrement.getSelection());
 
211
                mode.reset();
 
212
        }
 
213
        
 
214
        private class RadioSelectionAdapter extends SelectionAdapter{
 
215
                private Button control;
 
216
                private List controls;
 
217
                
 
218
                public RadioSelectionAdapter(Button control) {
 
219
                        this.controls = new ArrayList();
 
220
                        this.control = control;
 
221
                        this.control.addSelectionListener(this);
 
222
                }
 
223
                
 
224
                public void addControl(Control control){
 
225
                        this.controls.add(control);
 
226
                }
 
227
                
 
228
                public void update(){
 
229
                        boolean enabled = this.control.getSelection();
 
230
                        Iterator it = this.controls.iterator();
 
231
                        while(it.hasNext()){
 
232
                                Control control = (Control)it.next();
 
233
                                control.setEnabled(enabled);
 
234
                        }
 
235
                }
 
236
                
 
237
                public void widgetSelected(SelectionEvent e) {
 
238
                        update();
 
239
                }
 
240
                
 
241
        }
 
242
        
 
243
        private class SpinnerSelectionAdapter extends SelectionAdapter{
 
244
                private Spinner to;
 
245
                private Spinner from;
 
246
                private Spinner increment;
 
247
                
 
248
                public SpinnerSelectionAdapter(Spinner from,Spinner to, Spinner increment) {
 
249
                        this.from = from;
 
250
                        this.to = to;
 
251
                        this.increment = increment;
 
252
                }
 
253
                
 
254
                public void widgetSelected(SelectionEvent e) {
 
255
                        if(e.widget.equals(this.from)){
 
256
                                if(this.from.getSelection() < MIN_SELECTION){
 
257
                                        this.from.setSelection(MIN_SELECTION);
 
258
                                }else if(this.from.getSelection() >= this.to.getSelection()){
 
259
                                        this.from.setSelection(this.to.getSelection() - 1);
 
260
                                }
 
261
                        }else if(e.widget.equals(this.to)){
 
262
                                if(this.to.getSelection() <= this.from.getSelection()){
 
263
                                        this.to.setSelection(this.from.getSelection() + 1);
 
264
                                }else if(this.to.getSelection() > MAX_SELECTION){
 
265
                                        this.to.setSelection(MAX_SELECTION);
 
266
                                }
 
267
                        }
 
268
                        if(this.increment.getSelection() > (this.to.getSelection() - this.from.getSelection())){
 
269
                                this.increment.setSelection(this.to.getSelection() - this.from.getSelection());
 
270
                        }
 
271
                }
 
272
        }
 
273
}
 
 
b'\\ No newline at end of file'