/*
* 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.controlcontributions;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
import org.jampa.controllers.Controller;
import org.jampa.controllers.events.EventConstants;
import org.jampa.gui.actions.SetVolumeAction;
import org.jampa.gui.components.sliders.SliderWrapper;
import org.jampa.gui.translations.Messages;
import org.jampa.preferences.PreferenceConstants;
public class VolumeBarToolbarContribution extends WorkbenchWindowControlContribution implements PropertyChangeListener {
private Label volumeLabel;
private SliderWrapper scVolumeSlider;
private boolean _boVolumeSliderMove = false;
public VolumeBarToolbarContribution() {
super();
initialize();
}
public VolumeBarToolbarContribution(String id) {
super(id);
initialize();
}
private void initialize() {
Controller.getInstance().getEventController().addAudioItemChangeListener(this);
}
@Override
protected Control createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
GridLayout containerGL = new GridLayout(3, false);
containerGL.verticalSpacing = 0;
containerGL.marginTop = -5;
containerGL.marginBottom = -5;
container.setLayout(containerGL);
GridData containerGD = new GridData(SWT.NONE, SWT.CENTER, false, true);
container.setLayoutData(containerGD);
GridData volumeSliderGD = new GridData();
volumeSliderGD.verticalAlignment = SWT.CENTER;
scVolumeSlider = new SliderWrapper(container, SWT.HORIZONTAL);
scVolumeSlider.setLayoutData(volumeSliderGD);
scVolumeSlider.setMinimum(0);
scVolumeSlider.setMaximum(100);
scVolumeSlider.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent e) {
if (_boVolumeSliderMove) {
new SetVolumeAction(scVolumeSlider.getSelection()).run();
}
volumeLabel.setText(Integer.toString(scVolumeSlider.getSelection()) + "%"); //$NON-NLS-1$
}
});
scVolumeSlider.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
_boVolumeSliderMove = true;
}
public void mouseUp(MouseEvent e) {
_boVolumeSliderMove = false;
new SetVolumeAction(scVolumeSlider.getSelection()).run();
}
});
volumeLabel = new Label(container, SWT.NONE);
volumeLabel.setText("100%"); //$NON-NLS-1$
GridData volumeLabelGD = new GridData(SWT.FILL, SWT.CENTER, true, true);
volumeLabelGD.minimumWidth = 30;
volumeLabel.setLayoutData(volumeLabelGD);
setVolumeSliderValue(Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAYBACK_VOLUME));
return container;
}
public int getSelection() {
return scVolumeSlider.getSelection();
}
private void setVolumeSliderValue(int value) {
if (!_boVolumeSliderMove) {
volumeLabel.setText(Integer.toString(value) + "%"); //$NON-NLS-1$
scVolumeSlider.setSelection(value);
scVolumeSlider.setToolTipText(Messages.getString("PlayerView.VolumeSliderTooltip") + " " + Integer.toString(value)); //$NON-NLS-1$ //$NON-NLS-2$
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(EventConstants.EVT_VOLUME_CHANGE)) {
class updateVolume implements Runnable {
public void run() {
setVolumeSliderValue(Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAYBACK_VOLUME));
}
}
Display.getDefault().asyncExec(new updateVolume());
}
}
public void dispose() {
Controller.getInstance().getEventController().removeAudioItemChangeListener(this);
super.dispose();
}
}