/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package org.jampa.gui.views;
import java.text.DecimalFormat;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.part.ViewPart;
import org.jampa.controllers.Controller;
import org.jampa.gui.translations.Messages;
import org.jampa.model.validators.PresetNameValidator;
import org.jampa.utils.ApplicationUtils;
public class EqualizerView extends ViewPart {
public static final String ID = "Jampa.EqualizerView"; //$NON-NLS-1$
private Scale scBand0;
private Scale scBand1;
private Scale scBand2;
private Scale scBand3;
private Scale scBand4;
private Scale scBand5;
private Scale scBand6;
private Scale scBand7;
private Scale scBand8;
private Scale scBand9;
private Button ckActivated;
private Button btnResetToZero;
private Button btnReset;
private Button btnApply;
private Button btnLoadPreset;
private Button btnSavePreset;
private Button btnDeletePreset;
private boolean _boModified = false;
private Shell _shell;
public EqualizerView() {
}
@Override
public void createPartControl(Composite parent) {
_shell = parent.getShell();
this.setPartName(Messages.getString("EqualizerView.ViewTitle")); //$NON-NLS-1$
if (ApplicationUtils.doesEngineSupportEqualizer()) {
parent.setLayout(new GridLayout(3, false));
Group equalizerGroup = new Group(parent, SWT.NONE);
equalizerGroup.setLayout(new GridLayout(11, false));
equalizerGroup.setText(Messages.getString("EqualizerView.EqualizerGroupTitle")); //$NON-NLS-1$
GridData equalizerGroupGD = new GridData();
equalizerGroupGD.horizontalSpan = 2;
equalizerGroupGD.grabExcessHorizontalSpace = true;
equalizerGroupGD.horizontalAlignment = SWT.FILL;
equalizerGroupGD.grabExcessVerticalSpace = true;
equalizerGroupGD.verticalAlignment = SWT.FILL;
equalizerGroup.setLayoutData(equalizerGroupGD);
scBand0 = new Scale(equalizerGroup, SWT.VERTICAL);
scBand0.setMaximum(100);
scBand0.setMinimum(0);
GridData band0GD = new GridData(SWT.CENTER, SWT.FILL, true, true);
scBand0.setLayoutData(band0GD);
scBand1 = new Scale(equalizerGroup, SWT.VERTICAL);
scBand1.setMaximum(100);
scBand1.setMinimum(0);
GridData band1GD = new GridData(SWT.CENTER, SWT.FILL, true, true);
scBand1.setLayoutData(band1GD);
scBand2 = new Scale(equalizerGroup, SWT.VERTICAL);
scBand2.setMaximum(100);
scBand2.setMinimum(0);
GridData band2GD = new GridData(SWT.CENTER, SWT.FILL, true, true);
scBand2.setLayoutData(band2GD);
scBand3 = new Scale(equalizerGroup, SWT.VERTICAL);
scBand3.setMaximum(100);
scBand3.setMinimum(0);
GridData band3GD = new GridData(SWT.CENTER, SWT.FILL, true, true);
scBand3.setLayoutData(band3GD);
scBand4 = new Scale(equalizerGroup, SWT.VERTICAL);
scBand4.setMaximum(100);
scBand4.setMinimum(0);
GridData band4GD = new GridData(SWT.CENTER, SWT.FILL, true, true);
scBand4.setLayoutData(band4GD);
scBand5 = new Scale(equalizerGroup, SWT.VERTICAL);
scBand5.setMaximum(100);
scBand5.setMinimum(0);
GridData band5GD = new GridData(SWT.CENTER, SWT.FILL, true, true);
scBand5.setLayoutData(band5GD);
scBand6 = new Scale(equalizerGroup, SWT.VERTICAL);
scBand6.setMaximum(100);
scBand6.setMinimum(0);
GridData band6GD = new GridData(SWT.CENTER, SWT.FILL, true, true);
scBand6.setLayoutData(band6GD);
scBand7 = new Scale(equalizerGroup, SWT.VERTICAL);
scBand7.setMaximum(100);
scBand7.setMinimum(0);
GridData band7GD = new GridData(SWT.CENTER, SWT.FILL, true, true);
scBand7.setLayoutData(band7GD);
scBand8 = new Scale(equalizerGroup, SWT.VERTICAL);
scBand8.setMaximum(100);
scBand8.setMinimum(0);
GridData band8GD = new GridData(SWT.CENTER, SWT.FILL, true, true);
scBand8.setLayoutData(band8GD);
scBand9 = new Scale(equalizerGroup, SWT.VERTICAL);
scBand9.setMaximum(100);
scBand9.setMinimum(0);
GridData band9GD = new GridData(SWT.CENTER, SWT.FILL, true, true);
scBand9.setLayoutData(band9GD);
Composite equalizerLabelGroup = new Composite(equalizerGroup, SWT.NONE);
equalizerLabelGroup.setLayout(new GridLayout(1, false));
GridData equalizerLabelGroupGD = new GridData();
equalizerLabelGroupGD.grabExcessVerticalSpace = true;
equalizerLabelGroupGD.verticalAlignment = SWT.FILL;
equalizerLabelGroup.setLayoutData(equalizerLabelGroupGD);
GridData maxValueGD = new GridData();
maxValueGD.verticalAlignment = SWT.TOP;
maxValueGD.grabExcessVerticalSpace = true;
Label maxValue = new Label(equalizerLabelGroup, SWT.NONE);
maxValue.setText("+4db"); //$NON-NLS-1$
maxValue.setLayoutData(maxValueGD);
GridData middleValueGD = new GridData();
middleValueGD.verticalAlignment = SWT.CENTER;
middleValueGD.grabExcessVerticalSpace = true;
Label middleValue = new Label(equalizerLabelGroup, SWT.NONE);
middleValue.setText("0db"); //$NON-NLS-1$
middleValue.setLayoutData(middleValueGD);
GridData minValueGD = new GridData();
minValueGD.verticalAlignment = SWT.BOTTOM;
minValueGD.grabExcessVerticalSpace = true;
Label minValue = new Label(equalizerLabelGroup, SWT.NONE);
minValue.setText("-4db"); //$NON-NLS-1$
minValue.setLayoutData(minValueGD);
Label laBand0 = new Label(equalizerGroup, SWT.NONE);
laBand0.setText("31.25Hz"); //$NON-NLS-1$
laBand0.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
Label laBand1 = new Label(equalizerGroup, SWT.NONE);
laBand1.setText("62Hz"); //$NON-NLS-1$
laBand1.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
Label laBand2 = new Label(equalizerGroup, SWT.NONE);
laBand2.setText("125Hz"); //$NON-NLS-1$
laBand2.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
Label laBand3 = new Label(equalizerGroup, SWT.NONE);
laBand3.setText("250Hz"); //$NON-NLS-1$
laBand3.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
Label laBand4 = new Label(equalizerGroup, SWT.NONE);
laBand4.setText("500Hz"); //$NON-NLS-1$
laBand4.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
Label laBand5 = new Label(equalizerGroup, SWT.NONE);
laBand5.setText("1kHz"); //$NON-NLS-1$
laBand5.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
Label laBand6 = new Label(equalizerGroup, SWT.NONE);
laBand6.setText("2kHz"); //$NON-NLS-1$
laBand6.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
Label laBand7 = new Label(equalizerGroup, SWT.NONE);
laBand7.setText("4kHz"); //$NON-NLS-1$
laBand7.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
Label laBand8 = new Label(equalizerGroup, SWT.NONE);
laBand8.setText("8kHz"); //$NON-NLS-1$
laBand8.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
Label laBand9 = new Label(equalizerGroup, SWT.NONE);
laBand9.setText("16kHz"); //$NON-NLS-1$
laBand9.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false));
Group controlGroup = new Group(parent, SWT.NONE);
controlGroup.setLayout(new GridLayout(2, true));
controlGroup.setText(Messages.getString("EqualizerView.ControlGroupTitle")); //$NON-NLS-1$
GridData controlGroupGD = new GridData();
controlGroupGD.horizontalSpan = 1;
controlGroupGD.grabExcessVerticalSpace = true;
controlGroupGD.verticalAlignment = SWT.FILL;
controlGroup.setLayoutData(controlGroupGD);
GridData ckActivatedGD = new GridData();
ckActivatedGD.horizontalSpan = 2;
ckActivated = new Button(controlGroup, SWT.CHECK);
ckActivated.setText(Messages.getString("EqualizerView.EqualizerActivated")); //$NON-NLS-1$
ckActivated.setLayoutData(ckActivatedGD);
GridData btnResetToZeroGD = new GridData();
btnResetToZeroGD.grabExcessHorizontalSpace = true;
btnResetToZeroGD.horizontalAlignment = SWT.FILL;
btnResetToZero = new Button(controlGroup, SWT.NONE);
btnResetToZero.setLayoutData(btnResetToZeroGD);
btnResetToZero.setText(Messages.getString("EqualizerView.ResetEqualizerToZero")); //$NON-NLS-1$
GridData btnLoadPresetGD = new GridData();
btnLoadPresetGD.grabExcessHorizontalSpace = true;
btnLoadPresetGD.horizontalAlignment = SWT.FILL;
btnLoadPreset = new Button(controlGroup, SWT.NONE);
btnLoadPreset.setLayoutData(btnLoadPresetGD);
btnLoadPreset.setText(Messages.getString("EqualizerView.LoadPreset")); //$NON-NLS-1$
GridData btnResetGD = new GridData();
btnResetGD.grabExcessHorizontalSpace = true;
btnResetGD.horizontalAlignment = SWT.FILL;
btnReset = new Button(controlGroup, SWT.NONE);
btnReset.setLayoutData(btnResetGD);
btnReset.setText(Messages.getString("EqualizerView.ResetEqualizer")); //$NON-NLS-1$
GridData btnSavePresetGD = new GridData();
btnSavePresetGD.grabExcessHorizontalSpace = true;
btnSavePresetGD.horizontalAlignment = SWT.FILL;
btnSavePreset = new Button(controlGroup, SWT.NONE);
btnSavePreset.setLayoutData(btnSavePresetGD);
btnSavePreset.setText(Messages.getString("EqualizerView.SavePreset")); //$NON-NLS-1$
GridData btnApplyGD = new GridData();
btnApplyGD.grabExcessHorizontalSpace = true;
btnApplyGD.horizontalAlignment = SWT.FILL;
btnApply = new Button(controlGroup, SWT.NONE);
btnApply.setLayoutData(btnApplyGD);
btnApply.setText(Messages.getString("EqualizerView.ApplyEqualizer")); //$NON-NLS-1$
GridData btnDeletePresetGD = new GridData();
btnDeletePresetGD.grabExcessHorizontalSpace = true;
btnDeletePresetGD.horizontalAlignment = SWT.FILL;
btnDeletePreset = new Button(controlGroup, SWT.NONE);
btnDeletePreset.setLayoutData(btnDeletePresetGD);
btnDeletePreset.setText(Messages.getString("EqualizerView.DeletePreset")); //$NON-NLS-1$
GridData laInfoGD = new GridData();
laInfoGD.horizontalSpan = 2;
Label laInfo = new Label(controlGroup, SWT.WRAP);
laInfo.setText(Messages.getString("EqualizerView.Information")); //$NON-NLS-1$
laInfo.setLayoutData(laInfoGD);
connectListeners();
ckActivated.setSelection(Controller.getInstance().getEqualizerController().isEqualizerActivated());
setBandEnabled(ckActivated.getSelection());
setEqualizerToCurrentValues();
setButtonsEnabled(false);
setDefaultButtonsEnabled(ckActivated.getSelection());
setScaleToolTips();
} else {
this.setContentDescription(String.format(Messages.getString("EqualizerView.EqualizerNotSupported"), //$NON-NLS-1$
Controller.getInstance().getEngine().getEngineType()));
}
}
private void setDefaultButtonsEnabled(boolean value) {
btnResetToZero.setEnabled(value);
btnLoadPreset.setEnabled(value);
btnSavePreset.setEnabled(value);
btnDeletePreset.setEnabled(value);
}
private void setButtonsEnabled(boolean value) {
btnReset.setEnabled(value);
btnApply.setEnabled(value);
}
private void setBandEnabled(boolean value) {
scBand0.setEnabled(value);
scBand1.setEnabled(value);
scBand2.setEnabled(value);
scBand3.setEnabled(value);
scBand4.setEnabled(value);
scBand5.setEnabled(value);
scBand6.setEnabled(value);
scBand7.setEnabled(value);
scBand8.setEnabled(value);
scBand9.setEnabled(value);
}
private void setEqualizerToZero() {
scBand0.setSelection(dbToValue(0));
scBand1.setSelection(dbToValue(0));
scBand2.setSelection(dbToValue(0));
scBand3.setSelection(dbToValue(0));
scBand4.setSelection(dbToValue(0));
scBand5.setSelection(dbToValue(0));
scBand6.setSelection(dbToValue(0));
scBand7.setSelection(dbToValue(0));
scBand8.setSelection(dbToValue(0));
scBand9.setSelection(dbToValue(0));
}
private void setEqualizerToCurrentValues() {
scBand0.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getBand0()));
scBand1.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getBand1()));
scBand2.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getBand2()));
scBand3.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getBand3()));
scBand4.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getBand4()));
scBand5.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getBand5()));
scBand6.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getBand6()));
scBand7.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getBand7()));
scBand8.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getBand8()));
scBand9.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getBand9()));
}
private void setCurrentValuesToEqualizer() {
Controller.getInstance().getEqualizerController().setBand0(valueToDb(scBand0.getSelection()));
Controller.getInstance().getEqualizerController().setBand1(valueToDb(scBand1.getSelection()));
Controller.getInstance().getEqualizerController().setBand2(valueToDb(scBand2.getSelection()));
Controller.getInstance().getEqualizerController().setBand3(valueToDb(scBand3.getSelection()));
Controller.getInstance().getEqualizerController().setBand4(valueToDb(scBand4.getSelection()));
Controller.getInstance().getEqualizerController().setBand5(valueToDb(scBand5.getSelection()));
Controller.getInstance().getEqualizerController().setBand6(valueToDb(scBand6.getSelection()));
Controller.getInstance().getEqualizerController().setBand7(valueToDb(scBand7.getSelection()));
Controller.getInstance().getEqualizerController().setBand8(valueToDb(scBand8.getSelection()));
Controller.getInstance().getEqualizerController().setBand9(valueToDb(scBand9.getSelection()));
}
private void setScaleToolTips() {
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(0);
df.setDecimalSeparatorAlwaysShown(false);
scBand0.setToolTipText(df.format(valueToDb(scBand0.getSelection())) + "db"); //$NON-NLS-1$
scBand1.setToolTipText(df.format(valueToDb(scBand1.getSelection())) + "db"); //$NON-NLS-1$
scBand2.setToolTipText(df.format(valueToDb(scBand2.getSelection())) + "db"); //$NON-NLS-1$
scBand3.setToolTipText(df.format(valueToDb(scBand3.getSelection())) + "db"); //$NON-NLS-1$
scBand4.setToolTipText(df.format(valueToDb(scBand4.getSelection())) + "db"); //$NON-NLS-1$
scBand5.setToolTipText(df.format(valueToDb(scBand5.getSelection())) + "db"); //$NON-NLS-1$
scBand6.setToolTipText(df.format(valueToDb(scBand6.getSelection())) + "db"); //$NON-NLS-1$
scBand7.setToolTipText(df.format(valueToDb(scBand7.getSelection())) + "db"); //$NON-NLS-1$
scBand8.setToolTipText(df.format(valueToDb(scBand8.getSelection())) + "db"); //$NON-NLS-1$
scBand9.setToolTipText(df.format(valueToDb(scBand9.getSelection())) + "db"); //$NON-NLS-1$
}
private void connectListeners() {
ckActivated.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (ckActivated.getSelection()) {
setBandEnabled(true);
setButtonsEnabled(_boModified);
setDefaultButtonsEnabled(true);
Controller.getInstance().getEqualizerController().setEqualizerActivated(true);
} else {
setBandEnabled(false);
setButtonsEnabled(false);
setDefaultButtonsEnabled(false);
Controller.getInstance().getEqualizerController().setEqualizerActivated(false);
}
}
});
btnResetToZero.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
setEqualizerToZero();
setButtonsEnabled(true);
_boModified = true;
}
});
btnReset.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
setEqualizerToCurrentValues();
setButtonsEnabled(false);
_boModified = false;
}
});
btnApply.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
setCurrentValuesToEqualizer();
setButtonsEnabled(false);
_boModified = false;
}
});
btnLoadPreset.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
loadPreset();
}
});
btnSavePreset.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
savePreset();
}
});
btnDeletePreset.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
deletePreset();
}
});
SelectionAdapter bandAdapter = new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
setScaleToolTips();
setButtonsEnabled(true);
_boModified = true;
}
};
scBand0.addSelectionListener(bandAdapter);
scBand1.addSelectionListener(bandAdapter);
scBand2.addSelectionListener(bandAdapter);
scBand3.addSelectionListener(bandAdapter);
scBand4.addSelectionListener(bandAdapter);
scBand5.addSelectionListener(bandAdapter);
scBand6.addSelectionListener(bandAdapter);
scBand7.addSelectionListener(bandAdapter);
scBand8.addSelectionListener(bandAdapter);
scBand9.addSelectionListener(bandAdapter);
}
private String getSelectedPreset() {
ElementListSelectionDialog dialog = new ElementListSelectionDialog(_shell, new LabelProvider());
dialog.setTitle(Messages.getString("EqualizerView.PresetSelectTitle")); //$NON-NLS-1$
dialog.setMessage(Messages.getString("EqualizerView.PresetSelectMessage")); //$NON-NLS-1$
dialog.setMultipleSelection(false);
dialog.setElements(Controller.getInstance().getEqualizerController().getPresetsList());
int result = dialog.open();
if (result == Dialog.OK) {
return (String) dialog.getFirstResult();
} else {
return null;
}
}
private void loadPreset() {
String presetName = getSelectedPreset();
if ((presetName != null) &&
(Controller.getInstance().getEqualizerController().doesPresetExists(presetName))){
scBand0.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getPreset(presetName).getBand0()));
scBand1.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getPreset(presetName).getBand1()));
scBand2.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getPreset(presetName).getBand2()));
scBand3.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getPreset(presetName).getBand3()));
scBand4.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getPreset(presetName).getBand4()));
scBand5.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getPreset(presetName).getBand5()));
scBand6.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getPreset(presetName).getBand6()));
scBand7.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getPreset(presetName).getBand7()));
scBand8.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getPreset(presetName).getBand8()));
scBand9.setSelection(dbToValue(Controller.getInstance().getEqualizerController().getPreset(presetName).getBand9()));
setScaleToolTips();
setButtonsEnabled(true);
_boModified = true;
}
}
private void deletePreset() {
String presetName = getSelectedPreset();
if ((presetName != null) &&
(Controller.getInstance().getEqualizerController().doesPresetExists(presetName))){
Controller.getInstance().getEqualizerController().deletePreset(presetName);
}
}
private void savePreset() {
InputDialog dialog = new InputDialog(_shell,
Messages.getString("EqualizerView.PresetTitle"), //$NON-NLS-1$
Messages.getString("EqualizerView.PresetQuestion"), //$NON-NLS-1$
Messages.getString("EqualizerView.PresetDefaultName"), //$NON-NLS-1$
new PresetNameValidator()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
int result = dialog.open();
if (result == InputDialog.OK) {
Controller.getInstance().getEqualizerController().addPreset(dialog.getValue(),
valueToDb(scBand0.getSelection()),
valueToDb(scBand1.getSelection()),
valueToDb(scBand2.getSelection()),
valueToDb(scBand3.getSelection()),
valueToDb(scBand4.getSelection()),
valueToDb(scBand5.getSelection()),
valueToDb(scBand6.getSelection()),
valueToDb(scBand7.getSelection()),
valueToDb(scBand8.getSelection()),
valueToDb(scBand9.getSelection()));
}
}
private int dbToValue(float db) {
//float result = 100 - ((float)((db + 12) * 100) / (float) 24);
// 100 is substract to value because Scale component is top to bottom
// (i.e. 0 is on the top, and 100 on the bottom).
float result = 100 - ((float)((db + 4) * 100) / (float) 8);
return Math.round(result);
}
private float valueToDb(int value) {
//return ((float)((100 - value) * 24) / (float) 100) - 12;
// 100 is substract to value because Scale component is top to bottom
// (i.e. 0 is on the top, and 100 on the bottom).
return ((float)((100 - value) * 8) / (float) 100) - 4;
}
@Override
public void setFocus() {
// TODO Auto-generated method stub
}
}