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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/actions/composition/ChangeTimeSignatureAction.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.herac.tuxguitar.gui.TuxGuitar;
21
 
import org.herac.tuxguitar.gui.actions.Action;
22
 
import org.herac.tuxguitar.gui.editors.TablatureEditor;
23
 
import org.herac.tuxguitar.gui.editors.tab.Caret;
24
 
import org.herac.tuxguitar.gui.editors.tab.MeasureCoords;
25
 
import org.herac.tuxguitar.gui.editors.tab.SongCoords;
26
 
import org.herac.tuxguitar.gui.undo.undoables.custom.UndoableChangeTimeSignature;
27
 
import org.herac.tuxguitar.song.models.Duration;
28
 
import org.herac.tuxguitar.song.models.TimeSignature;
29
 
 
30
 
/**
31
 
 * @author julian
32
 
 *
33
 
 * TODO To change the template for this generated type comment go to
34
 
 * Window - Preferences - Java - Code Style - Code Templates
35
 
 */
36
 
public class ChangeTimeSignatureAction extends Action{
37
 
    public static final String NAME = "action.composition.change-time-signature";
38
 
    
39
 
    public ChangeTimeSignatureAction(TablatureEditor tablatureEditor) {
40
 
        super(NAME,true,tablatureEditor);       
41
 
    }
42
 
 
43
 
    public boolean doAction(TypedEvent e) {
44
 
        showDialog(getEditor().getTablature().getShell()); 
45
 
        return true;
46
 
    }
47
 
 
48
 
    
49
 
    public void showDialog(Shell shell) {
50
 
        MeasureCoords measure = getEditor().getTablature().getCaret().getMeasureCoords();
51
 
        if (measure != null) {
52
 
            final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
53
 
 
54
 
            dialog.setLayout(new GridLayout());
55
 
            dialog.setText(TuxGuitar.getProperty("composition.timesignature"));
56
 
            
57
 
            //-------------TIME SIGNATURE-----------------------------------------------
58
 
            Composite timeSignature = new Composite(dialog, SWT.NONE);
59
 
            timeSignature.setLayout(new GridLayout(2,false));
60
 
            timeSignature.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));   
61
 
            
62
 
            TimeSignature currentTimeSignature = measure.getMeasure().getTimeSignature();
63
 
            //numerator
64
 
            Label numeratorLabel = new Label(timeSignature, SWT.NULL);
65
 
            numeratorLabel.setText(TuxGuitar.getProperty("composition.timesignature.Numerator"));
66
 
            final Combo numerator = new Combo(timeSignature, SWT.DROP_DOWN | SWT.READ_ONLY);
67
 
            for (int i = 1; i <= 32; i++) {
68
 
                numerator.add(Integer.toString(i));
69
 
            }            
70
 
            numerator.setText(Integer.toString(currentTimeSignature.getNumerator()));
71
 
            numerator.setLayoutData(getComboData());
72
 
            //denominator
73
 
            Label denominatorLabel = new Label(timeSignature, SWT.NULL);
74
 
            denominatorLabel.setText(TuxGuitar.getProperty("composition.timesignature.denominator"));
75
 
            final Combo denominator = new Combo(timeSignature, SWT.DROP_DOWN | SWT.READ_ONLY);
76
 
            for (int i = 1; i <= 32; i = i * 2) {
77
 
                denominator.add(Integer.toString(i));
78
 
            }
79
 
            denominator.setText(Integer.toString(currentTimeSignature.getDenominator().getValue()));
80
 
            denominator.setLayoutData(getComboData());
81
 
            //--------------------To End Checkbox-------------------------------
82
 
            Composite check = new Composite(dialog, SWT.NONE);
83
 
            check.setLayout(new GridLayout());
84
 
            check.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));    
85
 
           
86
 
            final Button toEnd = new Button(check, SWT.CHECK);
87
 
            toEnd.setText(TuxGuitar.getProperty("composition.timesignature.to-the-end"));            
88
 
            toEnd.setSelection(true);
89
 
            //------------------BUTTONS--------------------------            
90
 
            Composite buttons = new Composite(dialog, SWT.NONE);
91
 
            buttons.setLayout(new GridLayout(2,false));
92
 
            buttons.setLayoutData(new GridData(SWT.END,SWT.FILL,true,true));            
93
 
            
94
 
            GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);    
95
 
            data.minimumWidth = 80;
96
 
            data.minimumHeight = 25;            
97
 
            
98
 
            
99
 
            final Button buttonOk = new Button(buttons, SWT.PUSH);
100
 
            buttonOk.setText(TuxGuitar.getProperty("ok"));
101
 
            buttonOk.setLayoutData(data);
102
 
            buttonOk.addSelectionListener(new SelectionAdapter() {
103
 
                public void widgetSelected(SelectionEvent arg0) {   
104
 
                    boolean toEndValue = toEnd.getSelection();
105
 
                    int numeratorValue = Integer.parseInt(numerator.getText());
106
 
                    int denominatorValue = Integer.parseInt(denominator.getText());
107
 
                    TimeSignature timeSignature = new TimeSignature(numeratorValue,new Duration(denominatorValue));                    
108
 
                    setTimeSignature(timeSignature,toEndValue);
109
 
                    
110
 
                    dialog.dispose();
111
 
                }
112
 
            });
113
 
 
114
 
            Button buttonCancel = new Button(buttons, SWT.PUSH);
115
 
            buttonCancel.setLayoutData(data);
116
 
            buttonCancel.setText(TuxGuitar.getProperty("cancel"));
117
 
            buttonCancel.addSelectionListener(new SelectionAdapter() {
118
 
                public void widgetSelected(SelectionEvent arg0) {
119
 
                    dialog.dispose();
120
 
                }
121
 
            });
122
 
 
123
 
            dialog.pack();
124
 
            dialog.open();
125
 
 
126
 
            int x = shell.getBounds().x + (shell.getBounds().width - dialog.getSize().x) / 2;
127
 
            int y = shell.getBounds().y + (shell.getBounds().height - dialog.getSize().y) / 2;
128
 
            dialog.setLocation(x, y);
129
 
            
130
 
                while (!dialog.isDisposed()) {
131
 
                if (!dialog.getDisplay().readAndDispatch()) {
132
 
                        dialog.getDisplay().sleep();
133
 
                }
134
 
            }
135
 
        }
136
 
    }
137
 
    
138
 
    private GridData getComboData(){
139
 
        GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
140
 
        data.minimumWidth = 150;
141
 
        return data;
142
 
    }
143
 
    
144
 
    private void setTimeSignature(TimeSignature timeSignature,boolean toEnd){
145
 
        //comienza el undoable
146
 
        UndoableChangeTimeSignature undoable = UndoableChangeTimeSignature.startUndo(); 
147
 
        
148
 
        Caret caret = getEditor().getTablature().getCaret();
149
 
        SongCoords songCoords = caret.getSongCoords();
150
 
        MeasureCoords measure = caret.getMeasureCoords();
151
 
        
152
 
        getSongManager().changeTimeSignature(measure.getMeasure().getStart(),timeSignature,toEnd);
153
 
        
154
 
        //actualizo la tablatura
155
 
        updateTablature();
156
 
        redraw();                
157
 
        
158
 
        //termia el undoable
159
 
        getEditor().getUndoManager().addEdit(undoable.endUndo(timeSignature,toEnd));
160
 
    }    
161
 
 
162
 
}