1 package org.herac.tuxguitar.gui.tools.custom.tuner; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 6 import org.eclipse.swt.SWT; 7 import org.eclipse.swt.events.DisposeEvent; 8 import org.eclipse.swt.events.DisposeListener; 9 import org.eclipse.swt.events.SelectionAdapter; 10 import org.eclipse.swt.events.SelectionEvent; 11 import org.eclipse.swt.layout.GridData; 12 import org.eclipse.swt.layout.GridLayout; 13 import org.eclipse.swt.widgets.Button; 14 import org.eclipse.swt.widgets.Composite; 15 import org.eclipse.swt.widgets.Group; 16 import org.eclipse.swt.widgets.Shell; 17 import org.eclipse.swt.widgets.Label; 18 import org.herac.tuxguitar.gui.TuxGuitar; 19 import org.herac.tuxguitar.gui.util.DialogUtils; 20 import org.herac.tuxguitar.gui.util.MessageDialog; 21 import org.herac.tuxguitar.util.TGSynchronizer; 22 23 /** 24 * @author Nikola Kolarovic <nikola.kolarovic at gmail.com> 25 * 26 */ 27 public class TGTunerDialog implements TGTunerListener { 28 29 private static final int SHELL_WIDTH = 400; 30 protected TGTuner tuner = null; 31 protected int[] tuning = null; 32 protected Label currentFrequency = null; 33 protected Shell dialog = null; 34 protected TGTunerRoughWidget roughTuner = null; 35 protected ArrayList allStringButtons = null; 36 protected TGTunerFineWidget fineTuner = null; 37 TGTunerDialog(int[] tuning)38 TGTunerDialog(int[] tuning) { 39 this.tuning = tuning; 40 } 41 42 show()43 public void show() { 44 45 this.dialog = DialogUtils.newDialog(TuxGuitar.instance().getShell(),SWT.DIALOG_TRIM | SWT.RESIZE); 46 this.dialog.setLayout(new GridLayout()); 47 this.dialog.setImage(TuxGuitar.instance().getIconManager().getAppIcon()); 48 this.dialog.setText(TuxGuitar.getProperty("tuner.instrument-tuner")); 49 this.dialog.setMinimumSize(SHELL_WIDTH,SWT.DEFAULT); 50 this.dialog.setSize(700, 400); 51 52 Group group = new Group(this.dialog,SWT.SHADOW_ETCHED_IN); 53 group.setLayout(new GridLayout()); 54 group.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true)); 55 group.setText(TuxGuitar.getProperty("tuner.tuner")); 56 57 Composite specialComposite = new Composite(group,SWT.NONE); 58 specialComposite.setLayout(new GridLayout(2,false)); 59 specialComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true)); 60 this.allStringButtons = new ArrayList(this.tuning.length); 61 62 this.fineTuner = new TGTunerFineWidget(specialComposite); 63 64 65 Composite buttonsComposite = new Composite (specialComposite,SWT.NONE); 66 buttonsComposite.setLayout(new GridLayout(1,false)); 67 buttonsComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true)); 68 for (int i=0; i<this.tuning.length; i++) 69 createTuningString(this.tuning[i],buttonsComposite); 70 71 Composite tunComposite = new Composite(group,SWT.NONE); 72 tunComposite.setLayout(new GridLayout(1,false)); 73 tunComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true)); 74 75 this.currentFrequency = new Label(tunComposite,SWT.LEFT); 76 this.currentFrequency.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true)); 77 78 this.roughTuner = new TGTunerRoughWidget(group); 79 80 Composite btnComposite = new Composite(group,SWT.NONE); 81 btnComposite.setLayout(new GridLayout(2,false)); 82 btnComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true)); 83 84 final Button buttonSettings = new Button(btnComposite, SWT.PUSH); 85 buttonSettings.setText(TuxGuitar.getProperty("settings")); 86 buttonSettings.setLayoutData(getGridData(80,25)); 87 buttonSettings.addSelectionListener(new SelectionAdapter() { 88 public void widgetSelected(SelectionEvent arg0) { 89 TGTunerDialog.this.getTuner().pause(); 90 new TGTunerSettingsDialog(TGTunerDialog.this).show(); 91 } 92 }); 93 94 final Button buttonExit = new Button(btnComposite, SWT.PUSH); 95 buttonExit.setText(TuxGuitar.getProperty("close")); 96 buttonExit.setLayoutData(getGridData(80,25)); 97 buttonExit.addSelectionListener(new SelectionAdapter() { 98 public void widgetSelected(SelectionEvent arg0) { 99 TGTunerDialog.this.getTuner().setCanceled(true); 100 TGTunerDialog.this.dialog.dispose(); 101 } 102 }); 103 104 105 // if closed on [X], set this.tuner.setCanceled(true); 106 this.dialog.addDisposeListener(new DisposeListener() { 107 public void widgetDisposed(DisposeEvent arg0) { 108 TGTunerDialog.this.getTuner().setCanceled(true); 109 TGTunerDialog.this.dialog.dispose(); 110 } 111 }); 112 113 DialogUtils.openDialog(this.dialog, DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK); 114 115 // start the tuner thread 116 this.tuner = new TGTuner(this); 117 this.getTuner().start(); 118 119 } 120 getGridData(int minimumWidth, int minimumHeight)121 static GridData getGridData(int minimumWidth, int minimumHeight){ 122 GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); 123 data.minimumWidth = minimumWidth; 124 data.minimumHeight = minimumHeight; 125 return data; 126 } 127 128 fireFrequency(final double freq)129 public void fireFrequency(final double freq) { 130 if (!this.dialog.isDisposed()) { 131 try { 132 TGSynchronizer.instance().addRunnable(new TGSynchronizer.TGRunnable() { 133 public void run() { 134 if (!TGTunerDialog.this.dialog.isDisposed() && !TGTunerDialog.this.roughTuner.isDisposed()) { 135 TGTunerDialog.this.currentFrequency.setText(Math.floor(freq)+" Hz"); 136 TGTunerDialog.this.roughTuner.setCurrentFrequency(freq); 137 } 138 if (!TGTunerDialog.this.dialog.isDisposed() && !TGTunerDialog.this.fineTuner.isDisposed()) 139 TGTunerDialog.this.fineTuner.setCurrentFrequency(freq); 140 } 141 }); 142 } catch (Throwable e) { 143 e.printStackTrace(); 144 } 145 } 146 } 147 148 getTuner()149 public TGTuner getTuner() { 150 return this.tuner; 151 } 152 getTuning()153 public int[] getTuning() { 154 return this.tuning; 155 } 156 157 fireException(final Exception ex)158 public void fireException(final Exception ex) { 159 try { 160 TGSynchronizer.instance().addRunnable(new TGSynchronizer.TGRunnable() { 161 public void run() { 162 if (!TGTunerDialog.this.dialog.isDisposed()) 163 MessageDialog.errorMessage(ex); 164 } 165 }); 166 } catch (Throwable e) { 167 e.printStackTrace(); 168 } 169 } 170 171 fireCurrentString(final int string)172 public void fireCurrentString(final int string) { 173 this.tuner.pause(); 174 if (string == 0) { // TODO: it never happens 175 this.tuner.setWantedRange(); 176 this.fineTuner.setEnabled(false); 177 } 178 else { 179 this.tuner.setWantedNote(string); 180 this.fineTuner.setWantedTone(string); 181 } 182 this.tuner.resumeFromPause(); 183 } 184 185 186 createTuningString(int midiNote, Composite parent)187 protected void createTuningString(int midiNote, Composite parent) { 188 TGTuningString tempString = new TGTuningString(midiNote,parent,this); 189 this.allStringButtons.add(tempString); 190 tempString.getStringButton().addSelectionListener(new SelectionAdapter() { 191 public void widgetSelected(SelectionEvent arg0) { 192 // disable all others 193 TGTunerDialog.this.fineTuner.setCurrentFrequency(-1); 194 Iterator it = TGTunerDialog.this.allStringButtons.iterator(); 195 while (it.hasNext()) { 196 TGTuningString tmp = (TGTuningString)it.next(); 197 tmp.getStringButton().setSelection(false); 198 } 199 } 200 }); 201 tempString.addListener(); 202 203 } 204 205 } 206