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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/actions/insert/CloseRepeatAction.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.insert;
 
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.Composite;
 
17
import org.eclipse.swt.widgets.Label;
 
18
import org.eclipse.swt.widgets.Shell;
 
19
import org.eclipse.swt.widgets.Spinner;
 
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.MeasureCoords;
 
24
import org.herac.tuxguitar.gui.editors.tab.SongCoords;
 
25
import org.herac.tuxguitar.gui.undo.undoables.custom.UndoableChangeCloseRepeat;
 
26
 
 
27
/**
 
28
 * @author julian
 
29
 *
 
30
 * TODO To change the template for this generated type comment go to
 
31
 * Window - Preferences - Java - Code Style - Code Templates
 
32
 */
 
33
public class CloseRepeatAction extends Action{
 
34
    public static final String NAME = "action.insert.close-repeat";
 
35
    
 
36
    public CloseRepeatAction(TablatureEditor tablatureEditor) {
 
37
        super(NAME,true,tablatureEditor);       
 
38
    }
 
39
 
 
40
    public boolean doAction(TypedEvent e) {
 
41
        MeasureCoords measureCoords = getEditor().getTablature().getCaret().getMeasureCoords();
 
42
        SongCoords songCoords = getEditor().getTablature().getCaret().getSongCoords();        
 
43
        showCloseRepeatDialog(getEditor().getTablature().getShell(), measureCoords, songCoords);
 
44
        return true;
 
45
    }
 
46
 
 
47
    protected void updateTablature() {
 
48
        fireUpdate(getEditor().getTablature().getCaret().getMeasureCoords().getMeasure().getNumber());
 
49
        redraw();
 
50
    }        
 
51
    
 
52
    public void showCloseRepeatDialog(Shell shell, final MeasureCoords measureCoords, final SongCoords songCoords) {
 
53
        if (measureCoords != null) {
 
54
            final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
 
55
 
 
56
            dialog.setLayout(new GridLayout());
 
57
            dialog.setText(TuxGuitar.getProperty("repetitions"));
 
58
            
 
59
            
 
60
            int currentNumberOfRepetitions = measureCoords.getMeasure().getNumberOfRepetitions();
 
61
            if (currentNumberOfRepetitions < 1) {
 
62
                currentNumberOfRepetitions = 1;
 
63
            }
 
64
 
 
65
            Composite composite = new Composite(dialog,SWT.NONE);
 
66
            composite.setLayout(new GridLayout(2,false));
 
67
            composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
68
            
 
69
            Label numberOfRepetitionsLabel = new Label(composite, SWT.NULL);
 
70
            numberOfRepetitionsLabel.setText(TuxGuitar.getProperty("repeat.number-of-repetitions"));
 
71
 
 
72
            final Spinner numberOfRepetitions = new Spinner(composite, SWT.BORDER);
 
73
            numberOfRepetitions.setLayoutData(new GridData(50,13));                 
 
74
            numberOfRepetitions.setMinimum(0);
 
75
            numberOfRepetitions.setMaximum(100);
 
76
            numberOfRepetitions.setSelection(currentNumberOfRepetitions);
 
77
            numberOfRepetitions.setLayoutData(new GridData(SWT.END,SWT.FILL,true,true));
 
78
            
 
79
            //----------------------BUTTONS--------------------------------
 
80
            Composite buttons = new Composite(dialog, SWT.NONE);
 
81
            buttons.setLayout(new GridLayout(2,false));
 
82
            buttons.setLayoutData(new GridData(SWT.END,SWT.FILL,true,true));            
 
83
            
 
84
            GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);    
 
85
            data.minimumWidth = 80;
 
86
            data.minimumHeight = 25;            
 
87
            
 
88
            final Button buttonOK = new Button(buttons, SWT.PUSH);            
 
89
            buttonOK.setText(TuxGuitar.getProperty("ok"));
 
90
            buttonOK.setLayoutData(data);
 
91
            buttonOK.addSelectionListener(new SelectionAdapter() {
 
92
                public void widgetSelected(SelectionEvent arg0) {
 
93
                        closeRepeat(measureCoords, songCoords, numberOfRepetitions.getSelection());
 
94
                    dialog.dispose();
 
95
                }
 
96
            });                        
 
97
            Button buttonCancel = new Button(buttons, SWT.PUSH);            
 
98
            buttonCancel.setText(TuxGuitar.getProperty("cancel"));
 
99
            buttonCancel.setLayoutData(data);
 
100
            buttonCancel.addSelectionListener(new SelectionAdapter() {
 
101
                public void widgetSelected(SelectionEvent arg0) {
 
102
                    dialog.dispose();
 
103
                }
 
104
            });
 
105
 
 
106
            dialog.pack();
 
107
            dialog.open();
 
108
 
 
109
            int x = shell.getBounds().x + (shell.getBounds().width - dialog.getSize().x) / 2;
 
110
            int y = shell.getBounds().y + (shell.getBounds().height - dialog.getSize().y) / 2;
 
111
            dialog.setLocation(x, y);
 
112
 
 
113
                while (!dialog.isDisposed()) {
 
114
                if (!dialog.getDisplay().readAndDispatch()) {
 
115
                        dialog.getDisplay().sleep();
 
116
                }
 
117
            }
 
118
        }
 
119
 
 
120
    }
 
121
 
 
122
    private void closeRepeat(MeasureCoords measureCoords, SongCoords songCoords, int numberOfRepetitions) {
 
123
        //comienza el undoable
 
124
        UndoableChangeCloseRepeat undoable = UndoableChangeCloseRepeat.startUndo(); 
 
125
        
 
126
        numberOfRepetitions = Math.abs(numberOfRepetitions);
 
127
        getSongManager().changeCloseRepeat(measureCoords.getMeasure().getStart(), numberOfRepetitions);
 
128
        updateTablature();
 
129
        
 
130
        //termia el undoable
 
131
        getEditor().getUndoManager().addEdit(undoable.endUndo(numberOfRepetitions));
 
132
    }    
 
133
 
 
134
}