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.Composite;
17 import org.eclipse.swt.widgets.Group;
18 import org.eclipse.swt.widgets.Shell;
19 import org.herac.tuxguitar.gui.TuxGuitar;
20 import org.herac.tuxguitar.gui.actions.Action;
21 import org.herac.tuxguitar.gui.actions.ActionLock;
22 import org.herac.tuxguitar.gui.editors.tab.Caret;
23 import org.herac.tuxguitar.gui.editors.tab.TGMeasureImpl;
24 import org.herac.tuxguitar.gui.undo.undoables.custom.UndoableChangeTripletFeel;
25 import org.herac.tuxguitar.gui.util.DialogUtils;
26 import org.herac.tuxguitar.gui.util.MessageDialog;
27 import org.herac.tuxguitar.song.models.TGMeasureHeader;
28 import org.herac.tuxguitar.util.TGSynchronizer;
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 ChangeTripletFeelAction extends Action{
37 	public static final String NAME = "action.composition.change-triplet-feel";
38 
ChangeTripletFeelAction()39 	public ChangeTripletFeelAction() {
40 		super(NAME, AUTO_LOCK | AUTO_UNLOCK | AUTO_UPDATE | DISABLE_ON_PLAYING | KEY_BINDING_AVAILABLE);
41 	}
42 
execute(TypedEvent e)43 	protected int execute(TypedEvent e){
44 		showDialog(getEditor().getTablature().getShell());
45 		return 0;
46 	}
47 
showDialog(Shell shell)48 	public void showDialog(Shell shell) {
49 		TGMeasureImpl measure = getEditor().getTablature().getCaret().getMeasure();
50 		if (measure != null) {
51 			final Shell dialog = DialogUtils.newDialog(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
52 
53 			dialog.setLayout(new GridLayout());
54 			dialog.setText(TuxGuitar.getProperty("composition.tripletfeel"));
55 			dialog.setMinimumSize(300,0);
56 
57 			//-------------TIME SIGNATURE-----------------------------------------------
58 			Group tripletFeel = new Group(dialog,SWT.SHADOW_ETCHED_IN);
59 			tripletFeel.setLayout(new GridLayout());
60 			tripletFeel.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
61 			tripletFeel.setText(TuxGuitar.getProperty("composition.tripletfeel"));
62 
63 			//none
64 			final Button tripletFeelNone = new Button(tripletFeel, SWT.RADIO);
65 			tripletFeelNone.setText(TuxGuitar.getProperty("composition.tripletfeel.none"));
66 			tripletFeelNone.setSelection(measure.getTripletFeel() == TGMeasureHeader.TRIPLET_FEEL_NONE);
67 
68 			final Button tripletFeelEighth = new Button(tripletFeel, SWT.RADIO);
69 			tripletFeelEighth.setText(TuxGuitar.getProperty("composition.tripletfeel.eighth"));
70 			tripletFeelEighth.setSelection(measure.getTripletFeel() == TGMeasureHeader.TRIPLET_FEEL_EIGHTH);
71 
72 			final Button tripletFeelSixteenth = new Button(tripletFeel, SWT.RADIO);
73 			tripletFeelSixteenth.setText(TuxGuitar.getProperty("composition.tripletfeel.sixteenth"));
74 			tripletFeelSixteenth.setSelection(measure.getTripletFeel() == TGMeasureHeader.TRIPLET_FEEL_SIXTEENTH);
75 
76 			//--------------------To End Checkbox-------------------------------
77 			Group check = new Group(dialog,SWT.SHADOW_ETCHED_IN);
78 			check.setLayout(new GridLayout());
79 			check.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
80 			check.setText(TuxGuitar.getProperty("options"));
81 
82 			final Button toEnd = new Button(check, SWT.CHECK);
83 			toEnd.setText(TuxGuitar.getProperty("composition.tripletfeel.to-the-end"));
84 			toEnd.setSelection(true);
85 			//------------------BUTTONS--------------------------
86 			Composite buttons = new Composite(dialog, SWT.NONE);
87 			buttons.setLayout(new GridLayout(2,false));
88 			buttons.setLayoutData(new GridData(SWT.END,SWT.FILL,true,true));
89 
90 			final Button buttonOk = new Button(buttons, SWT.PUSH);
91 			buttonOk.setText(TuxGuitar.getProperty("ok"));
92 			buttonOk.setLayoutData(getButtonData());
93 			buttonOk.addSelectionListener(new SelectionAdapter() {
94 				public void widgetSelected(SelectionEvent arg0) {
95 					final boolean toEndValue = toEnd.getSelection();
96 					final int tripletFeel = getSelectedTripletFeel(tripletFeelNone, tripletFeelEighth, tripletFeelSixteenth);
97 
98 					dialog.dispose();
99 					try {
100 						TGSynchronizer.instance().runLater(new TGSynchronizer.TGRunnable() {
101 							public void run() throws Throwable {
102 								ActionLock.lock();
103 								TuxGuitar.instance().loadCursor(SWT.CURSOR_WAIT);
104 								setTripletFeel(tripletFeel,toEndValue);
105 								TuxGuitar.instance().updateCache( true );
106 								TuxGuitar.instance().loadCursor(SWT.CURSOR_ARROW);
107 								ActionLock.unlock();
108 							}
109 						});
110 					} catch (Throwable throwable) {
111 						MessageDialog.errorMessage(throwable);
112 					}
113 				}
114 			});
115 
116 			Button buttonCancel = new Button(buttons, SWT.PUSH);
117 			buttonCancel.setLayoutData(getButtonData());
118 			buttonCancel.setText(TuxGuitar.getProperty("cancel"));
119 			buttonCancel.addSelectionListener(new SelectionAdapter() {
120 				public void widgetSelected(SelectionEvent arg0) {
121 					dialog.dispose();
122 				}
123 			});
124 
125 			dialog.setDefaultButton( buttonOk );
126 
127 			DialogUtils.openDialog(dialog,DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK | DialogUtils.OPEN_STYLE_WAIT);
128 		}
129 	}
130 
getButtonData()131 	private GridData getButtonData(){
132 		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
133 		data.minimumWidth = 80;
134 		data.minimumHeight = 25;
135 		return data;
136 	}
137 
getSelectedTripletFeel(Button tripletFeelNone,Button tripletFeelEighth, Button tripletFeelSixteenth)138 	protected int getSelectedTripletFeel(Button tripletFeelNone,Button tripletFeelEighth, Button tripletFeelSixteenth){
139 		if(tripletFeelNone.getSelection()){
140 			return TGMeasureHeader.TRIPLET_FEEL_NONE;
141 		}else if(tripletFeelEighth.getSelection()){
142 			return TGMeasureHeader.TRIPLET_FEEL_EIGHTH;
143 		}else if(tripletFeelSixteenth.getSelection()){
144 			return TGMeasureHeader.TRIPLET_FEEL_SIXTEENTH;
145 		}
146 		return TGMeasureHeader.TRIPLET_FEEL_NONE;
147 	}
148 
setTripletFeel(int tripletFeel,boolean toEnd)149 	protected void setTripletFeel(int tripletFeel,boolean toEnd){
150 		//comienza el undoable
151 		UndoableChangeTripletFeel undoable = UndoableChangeTripletFeel.startUndo();
152 
153 		Caret caret = getEditor().getTablature().getCaret();
154 		TGMeasureImpl measure = caret.getMeasure();
155 
156 		getSongManager().changeTripletFeel(measure.getStart(),tripletFeel,toEnd);
157 
158 		TuxGuitar.instance().getFileHistory().setUnsavedFile();
159 
160 		//actualizo la tablatura
161 		updateTablature();
162 
163 		//termia el undoable
164 		addUndoableEdit(undoable.endUndo(tripletFeel,toEnd));
165 	}
166 }
167