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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/actions/measure/RemoveMeasureAction.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.measure;
 
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.Group;
 
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.tab.Caret;
 
24
import org.herac.tuxguitar.gui.editors.tab.TGMeasureImpl;
 
25
import org.herac.tuxguitar.gui.editors.tab.TGTrackImpl;
 
26
import org.herac.tuxguitar.gui.undo.undoables.measure.UndoableRemoveMeasure;
 
27
import org.herac.tuxguitar.gui.util.DialogUtils;
 
28
import org.herac.tuxguitar.song.models.TGMeasure;
 
29
import org.herac.tuxguitar.song.models.TGTrack;
 
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 RemoveMeasureAction extends Action{
 
38
        public static final String NAME = "action.measure.remove";
 
39
        
 
40
        public RemoveMeasureAction() {
 
41
                super(NAME, AUTO_LOCK | AUTO_UNLOCK | AUTO_UPDATE | DISABLE_ON_PLAYING | KEY_BINDING_AVAILABLE);
 
42
        }
 
43
        
 
44
        protected int execute(TypedEvent e){
 
45
                showDialog(getEditor().getTablature().getShell()/*,e*/);
 
46
                return 0;
 
47
        }
 
48
        
 
49
        public void showDialog(Shell shell/*,final TypedEvent event*/) {
 
50
                TGTrackImpl track = getEditor().getTablature().getCaret().getTrack();
 
51
                TGMeasureImpl measure = getEditor().getTablature().getCaret().getMeasure();
 
52
                if (measure != null) {
 
53
                        final Shell dialog = DialogUtils.newDialog(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
 
54
                        dialog.setLayout(new GridLayout());
 
55
                        dialog.setText(TuxGuitar.getProperty("edit.delete"));
 
56
                        
 
57
                        //----------------------------------------------------------------------
 
58
                        Group range = new Group(dialog,SWT.SHADOW_ETCHED_IN);
 
59
                        range.setLayout(new GridLayout(2,false));
 
60
                        range.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
61
                        range.setText(TuxGuitar.getProperty("edit.delete"));
 
62
                        
 
63
                        int measureCount = getSongManager().getSong().countMeasureHeaders();
 
64
                        
 
65
                        Label fromLabel = new Label(range, SWT.NULL);
 
66
                        fromLabel.setText(TuxGuitar.getProperty("edit.from"));
 
67
                        final Spinner fromSpinner = new Spinner(range, SWT.BORDER);
 
68
                        fromSpinner.setLayoutData(getSpinnerData());
 
69
                        fromSpinner.setMinimum(1);
 
70
                        fromSpinner.setMaximum(measureCount);
 
71
                        fromSpinner.setSelection(measure.getNumber());
 
72
                        
 
73
                        Label toLabel = new Label(range, SWT.NULL);
 
74
                        toLabel.setText(TuxGuitar.getProperty("edit.to"));
 
75
                        final Spinner toSpinner = new Spinner(range, SWT.BORDER);
 
76
                        toSpinner.setLayoutData(getSpinnerData());
 
77
                        toSpinner.setMinimum(1);
 
78
                        toSpinner.setMaximum(measureCount);
 
79
                        toSpinner.setSelection(measure.getNumber());
 
80
                        
 
81
                        final int minSelection = 1;
 
82
                        final int maxSelection = track.countMeasures();
 
83
                        
 
84
                        fromSpinner.addSelectionListener(new SelectionAdapter() {
 
85
                                public void widgetSelected(SelectionEvent e) {
 
86
                                        int fromSelection = fromSpinner.getSelection();
 
87
                                        int toSelection = toSpinner.getSelection();
 
88
                                        
 
89
                                        if(fromSelection < minSelection){
 
90
                                                fromSpinner.setSelection(minSelection);
 
91
                                        }else if(fromSelection > toSelection){
 
92
                                                fromSpinner.setSelection(toSelection);
 
93
                                        }
 
94
                                }
 
95
                        });
 
96
                        toSpinner.addSelectionListener(new SelectionAdapter() {
 
97
                                public void widgetSelected(SelectionEvent e) {
 
98
                                        int toSelection = toSpinner.getSelection();
 
99
                                        int fromSelection = fromSpinner.getSelection();
 
100
                                        if(toSelection < fromSelection){
 
101
                                                toSpinner.setSelection(fromSelection);
 
102
                                        }else if(toSelection > maxSelection){
 
103
                                                toSpinner.setSelection(maxSelection);
 
104
                                        }
 
105
                                }
 
106
                        });
 
107
                        //------------------BUTTONS--------------------------
 
108
                        Composite buttons = new Composite(dialog, SWT.NONE);
 
109
                        buttons.setLayout(new GridLayout(2,false));
 
110
                        buttons.setLayoutData(new GridData(SWT.END,SWT.FILL,true,true));
 
111
                        
 
112
                        final Button buttonOK = new Button(buttons, SWT.PUSH);
 
113
                        buttonOK.setText(TuxGuitar.getProperty("ok"));
 
114
                        buttonOK.setLayoutData(getButtonData());
 
115
                        buttonOK.addSelectionListener(new SelectionAdapter() {
 
116
                                public void widgetSelected(SelectionEvent arg0) {
 
117
                                        removeMeasures(fromSpinner.getSelection(),toSpinner.getSelection()/*,event*/);
 
118
                                        dialog.dispose();
 
119
                                }
 
120
                        });
 
121
                        
 
122
                        Button buttonCancel = new Button(buttons, SWT.PUSH);
 
123
                        buttonCancel.setText(TuxGuitar.getProperty("cancel"));
 
124
                        buttonCancel.setLayoutData(getButtonData());
 
125
                        buttonCancel.addSelectionListener(new SelectionAdapter() {
 
126
                                public void widgetSelected(SelectionEvent arg0) {
 
127
                                        dialog.dispose();
 
128
                                }
 
129
                        });
 
130
                        
 
131
                        dialog.setDefaultButton( buttonOK );
 
132
                        
 
133
                        DialogUtils.openDialog(dialog,DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK | DialogUtils.OPEN_STYLE_WAIT);
 
134
                }
 
135
        }
 
136
        
 
137
        private GridData getButtonData(){
 
138
                GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
 
139
                data.minimumWidth = 80;
 
140
                data.minimumHeight = 25;
 
141
                return data;
 
142
        }
 
143
        
 
144
        protected GridData getSpinnerData(){
 
145
                GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
 
146
                data.minimumWidth = 180;
 
147
                return data;
 
148
        }
 
149
        
 
150
        protected void removeMeasures(int m1,int m2/*,TypedEvent event*/){
 
151
                if(m1 > 0 && m1 <= m2 && m2 <= getSongManager().getSong().countMeasureHeaders()){
 
152
                        Caret caret = getEditor().getTablature().getCaret();
 
153
                        
 
154
                        if(m1 == 1 && m2 == getSongManager().getSong().countMeasureHeaders()){
 
155
                                //TuxGuitar.instance().getAction(NewFileAction.NAME).process(event);
 
156
                                TuxGuitar.instance().newSong();
 
157
                                return;
 
158
                        }
 
159
                        //comienza el undoable
 
160
                        UndoableRemoveMeasure undoable = new UndoableRemoveMeasure(m1,m2);
 
161
                        TuxGuitar.instance().getFileHistory().setUnsavedFile();
 
162
                        
 
163
                        //borro los compases
 
164
                        getSongManager().removeMeasureHeaders(m1,m2);
 
165
                        
 
166
                        updateTablature();
 
167
                        
 
168
                        int measureCount = getSongManager().getSong().countMeasureHeaders();
 
169
                        if(caret.getMeasure().getNumber() > measureCount){
 
170
                                TGTrack track = getSongManager().getTrack(caret.getTrack().getNumber());
 
171
                                TGMeasure measure = getSongManager().getTrackManager().getMeasure(track,measureCount);
 
172
                                caret.update(track.getNumber(),measure.getStart(),1);
 
173
                        }
 
174
                        
 
175
                        //termia el undoable
 
176
                        addUndoableEdit(undoable.endUndo());
 
177
                }
 
178
        }
 
179
}