/*
* 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.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
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.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;
import org.jampa.Activator;
import org.jampa.controllers.Controller;
import org.jampa.controllers.events.EventConstants;
import org.jampa.gui.actions.ChangePlaymodeAction;
import org.jampa.gui.actions.ChangePositionAction;
import org.jampa.gui.actions.PauseAction;
import org.jampa.gui.actions.PlayNextAction;
import org.jampa.gui.actions.PlayPreviousAction;
import org.jampa.gui.actions.SeekAction;
import org.jampa.gui.actions.SetVolumeAction;
import org.jampa.gui.actions.StopAction;
import org.jampa.gui.actions.VolumeMuteAction;
import org.jampa.gui.components.sliders.SliderWrapper;
import org.jampa.gui.translations.Messages;
import org.jampa.model.IAudioItem;
import org.jampa.preferences.PreferenceConstants;
import org.jampa.utils.Constants;
import org.jampa.utils.Utils;
public class PlayerView extends ViewPart implements PropertyChangeListener {
public static final String ID = "Jampa.PlayerView"; //$NON-NLS-1$
private int _totalTime = 0;
private Button btnPause;
private Button btnPrev;
private Button btnNext;
private Button btnStop;
private Button btnVolumeDown;
private Button btnVolumeUp;
private Button btnVolumeMute;
private Button btnSeekPlus;
private Button btnSeekMinus;
private SliderWrapper scSlider;
private SliderWrapper scVolumeSlider;
private Action _normalPlayModeAction;
private Action _repeatPlayModeAction;
private Action _randomPlayModeAction;
private Image imgPause;
private Image imgPrev;
private Image imgNext;
private Image imgStop;
private Image imgVolumeDown;
private Image imgVolumeUp;
private Image imgVolumeMute;
private Image imgSeekPlus;
private Image imgSeekMinus;
private Label laEllapsedTime;
private Label laTotalTime;
private Label laVolumeLabel;
private boolean _boSliderMove = false;
private boolean _boVolumeSliderMove = false;
public PlayerView() {
Activator.getDefault();
ImageDescriptor imageDescriptor;
imageDescriptor = Activator.getImageDescriptor("/icons/pause_16.png"); //$NON-NLS-1$
imgPause = imageDescriptor.createImage();
imageDescriptor = Activator.getImageDescriptor("/icons/previous_16.png"); //$NON-NLS-1$
imgPrev = imageDescriptor.createImage();
imageDescriptor = Activator.getImageDescriptor("/icons/next_16.png"); //$NON-NLS-1$
imgNext = imageDescriptor.createImage();
imageDescriptor = Activator.getImageDescriptor("/icons/stop_16.png"); //$NON-NLS-1$
imgStop = imageDescriptor.createImage();
imageDescriptor = Activator.getImageDescriptor("/icons/volumedown_16.png"); //$NON-NLS-1$
imgVolumeDown = imageDescriptor.createImage();
imageDescriptor = Activator.getImageDescriptor("/icons/volumeup_16.png"); //$NON-NLS-1$
imgVolumeUp = imageDescriptor.createImage();
imageDescriptor = Activator.getImageDescriptor("/icons/volumemute_16.png"); //$NON-NLS-1$
imgVolumeMute = imageDescriptor.createImage();
imageDescriptor = Activator.getImageDescriptor("/icons/seekplus_16.png"); //$NON-NLS-1$
imgSeekPlus = imageDescriptor.createImage();
imageDescriptor = Activator.getImageDescriptor("/icons/seekminus_16.png"); //$NON-NLS-1$
imgSeekMinus = imageDescriptor.createImage();
Controller.getInstance().getEventController().addGeneralEventsListener(this);
Controller.getInstance().getEventController().addAudioItemChangeListener(this);
}
@Override
public void createPartControl(Composite parent) {
this.setPartName(Messages.getString("PlayerView.ViewTitle")); //$NON-NLS-1$
parent.setLayout(new GridLayout(5, false));
Composite btnGroup1 = new Composite(parent, SWT.NONE);
btnGroup1.setLayout(new FillLayout());
btnGroup1.setLayoutData(new GridData(SWT.NONE, SWT.TOP, false, false));
btnStop = new Button(btnGroup1, SWT.PUSH);
btnStop.setImage(imgStop);
btnStop.setToolTipText(Messages.getString("PlayerView.BtnStop")); //$NON-NLS-1$
btnPause = new Button(btnGroup1, SWT.PUSH);
btnPause.setImage(imgPause);
btnPause.setToolTipText(Messages.getString("PlayerView.BtnPause")); //$NON-NLS-1$
Composite btnGroup2 = new Composite(parent, SWT.NONE);
btnGroup2.setLayout(new FillLayout());
btnGroup2.setLayoutData(new GridData(SWT.NONE, SWT.TOP, false, false));
int seekStep = Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAYERVIEW_SEEK_STEP);
btnSeekMinus = new Button(btnGroup2, SWT.PUSH);
btnSeekMinus.setImage(imgSeekMinus);
btnSeekMinus.setToolTipText("-" + Integer.toString(seekStep) + " " + Messages.getString("PlayerView.Seconds")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
btnPrev = new Button(btnGroup2, SWT.PUSH);
btnPrev.setImage(imgPrev);
btnPrev.setToolTipText(Messages.getString("PlayerView.BtnPrevious")); //$NON-NLS-1$
btnNext = new Button(btnGroup2, SWT.PUSH);
btnNext.setImage(imgNext);
btnNext.setToolTipText(Messages.getString("PlayerView.BtnNext")); //$NON-NLS-1$
btnSeekPlus = new Button(btnGroup2, SWT.PUSH);
btnSeekPlus.setImage(imgSeekPlus);
btnSeekPlus.setToolTipText("+" + Integer.toString(seekStep) + " " + Messages.getString("PlayerView.Seconds")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
Composite btnGroup3 = new Composite(parent, SWT.NONE);
btnGroup3.setLayout(new GridLayout(3, false));
btnGroup3.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
laEllapsedTime = new Label(btnGroup3, SWT.NONE);
laEllapsedTime.setText(Messages.getString("PlayerView.DefaultTimeInfo")); //$NON-NLS-1$
GridData sliderGD = new GridData();
sliderGD.grabExcessHorizontalSpace = true;
sliderGD.horizontalAlignment = SWT.FILL;
sliderGD.verticalAlignment = SWT.CENTER;
scSlider = new SliderWrapper(btnGroup3, SWT.HORIZONTAL);
scSlider.setLayoutData(sliderGD);
laTotalTime = new Label(btnGroup3, SWT.NONE);
laTotalTime.setText(Messages.getString("PlayerView.DefaultTimeInfo")); //$NON-NLS-1$
Composite btnGroup4 = new Composite(parent, SWT.NONE);
btnGroup4.setLayout(new FillLayout());
btnGroup4.setLayoutData(new GridData(SWT.NONE, SWT.TOP, false, false));
btnVolumeMute = new Button(btnGroup4, SWT.PUSH);
btnVolumeMute.setImage(imgVolumeMute);
btnVolumeMute.setToolTipText(Messages.getString("PlayerView.VolumeMute")); //$NON-NLS-1$
btnVolumeDown = new Button(btnGroup4, SWT.PUSH);
btnVolumeDown.setImage(imgVolumeDown);
btnVolumeDown.setToolTipText(Messages.getString("PlayerView.VolumeDown")); //$NON-NLS-1$
btnVolumeUp = new Button(btnGroup4, SWT.PUSH);
btnVolumeUp.setImage(imgVolumeUp);
btnVolumeUp.setToolTipText(Messages.getString("PlayerView.VolumeUp")); //$NON-NLS-1$
Composite btnGroup5 = new Composite(parent, SWT.NONE);
btnGroup5.setLayout(new GridLayout(2, false));
btnGroup5.setLayoutData(new GridData(SWT.NONE, SWT.TOP, false, false));
GridData volumeSliderGD = new GridData();
volumeSliderGD.verticalAlignment = SWT.CENTER;
scVolumeSlider = new SliderWrapper(btnGroup5, SWT.HORIZONTAL);
scVolumeSlider.setLayoutData(volumeSliderGD);
scVolumeSlider.setMinimum(0);
scVolumeSlider.setMaximum(100);
laVolumeLabel = new Label(btnGroup5, SWT.NONE);
laVolumeLabel.setText("100%"); //$NON-NLS-1$
GridData volumeLabelGD = new GridData(SWT.FILL, SWT.CENTER, false, true);
volumeLabelGD.minimumWidth = 30;
laVolumeLabel.setLayoutData(volumeLabelGD);
setVolumeSliderValue(Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAYBACK_VOLUME));
buildActions();
connectListenners();
enableComponents(false);
if (Controller.getInstance().getEngine().isPlaying()) {
updateInformations(Controller.getInstance().getPlaylistController().getCurrentAudioItem());
}
}
private void changePlayMode(Action action) {
if (action == _normalPlayModeAction) {
if (_normalPlayModeAction.isChecked()) {
_repeatPlayModeAction.setChecked(false);
_randomPlayModeAction.setChecked(false);
new ChangePlaymodeAction(0).run();
} else {
_repeatPlayModeAction.setChecked(true);
_randomPlayModeAction.setChecked(false);
new ChangePlaymodeAction(1).run();
}
} else if (action == _repeatPlayModeAction) {
if (_repeatPlayModeAction.isChecked()) {
_normalPlayModeAction.setChecked(false);
_randomPlayModeAction.setChecked(false);
new ChangePlaymodeAction(1).run();
} else {
_normalPlayModeAction.setChecked(false);
_randomPlayModeAction.setChecked(true);
new ChangePlaymodeAction(2).run();
}
} else if (action == _randomPlayModeAction) {
if (_randomPlayModeAction.isChecked()) {
_normalPlayModeAction.setChecked(false);
_repeatPlayModeAction.setChecked(false);
new ChangePlaymodeAction(2).run();
} else {
_normalPlayModeAction.setChecked(true);
_repeatPlayModeAction.setChecked(false);
new ChangePlaymodeAction(0).run();
}
}
}
private void setPlaymodeUI() {
switch (Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAY_MODE)) {
case 0 : _normalPlayModeAction.setChecked(true); _repeatPlayModeAction.setChecked(false); _randomPlayModeAction.setChecked(false); break;
case 1 : _normalPlayModeAction.setChecked(false); _repeatPlayModeAction.setChecked(true); _randomPlayModeAction.setChecked(false); break;
case 2 : _normalPlayModeAction.setChecked(false); _repeatPlayModeAction.setChecked(false); _randomPlayModeAction.setChecked(true); break;
default : _normalPlayModeAction.setChecked(true); _repeatPlayModeAction.setChecked(false); _randomPlayModeAction.setChecked(false); new ChangePlaymodeAction(0).run();
}
}
private void buildActions() {
_normalPlayModeAction = new Action(Messages.getString("PlayerView.PlayModeNormal")) { //$NON-NLS-1$
public void run() {
changePlayMode(_normalPlayModeAction);
}
};
_normalPlayModeAction.setImageDescriptor(Activator.getImageDescriptor("/icons/normal_16.png")); //$NON-NLS-1$
_repeatPlayModeAction = new Action(Messages.getString("PlayerView.PlayModeRepeat")) { //$NON-NLS-1$
public void run() {
changePlayMode(_repeatPlayModeAction);
}
};
_repeatPlayModeAction.setImageDescriptor(Activator.getImageDescriptor("/icons/repeat_16.png")); //$NON-NLS-1$
_randomPlayModeAction = new Action(Messages.getString("PlayerView.PlayModeRandom")) { //$NON-NLS-1$
public void run() {
changePlayMode(_randomPlayModeAction);
}
};
_randomPlayModeAction.setImageDescriptor(Activator.getImageDescriptor("/icons/random_16.png")); //$NON-NLS-1$
setPlaymodeUI();
IToolBarManager rootMenuManager = getViewSite().getActionBars().getToolBarManager();
rootMenuManager.removeAll();
rootMenuManager.add(_normalPlayModeAction);
rootMenuManager.add(_repeatPlayModeAction);
rootMenuManager.add(_randomPlayModeAction);
}
private void connectListenners() {
btnPause.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
if (Controller.getInstance().getEngine().isPlaying()) {
new PauseAction().run();
}
}
});
btnStop.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
new StopAction().run();
}
});
btnNext.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
new PlayNextAction().run();
}
});
btnPrev.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
new PlayPreviousAction().run();
}
});
btnSeekMinus.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
new SeekAction(-Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAYERVIEW_SEEK_STEP)).run();
}
});
btnSeekPlus.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
new SeekAction(Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAYERVIEW_SEEK_STEP)).run();
}
});
scSlider.addMouseListener(new MouseListener() {
public void mouseDown(MouseEvent e) {
_boSliderMove = true;
}
public void mouseUp(MouseEvent e) {
_boSliderMove = false;
new ChangePositionAction(scSlider.getSelection()).run();
}
public void mouseDoubleClick(MouseEvent e) {
}
});
scSlider.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent e) {
if (_boSliderMove) {
laEllapsedTime.setText(Utils.milliseconds2String(scSlider.getSelection()));
laTotalTime.setText(Utils.seconds2String(_totalTime));
}
}
});
scVolumeSlider.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent e) {
if (_boVolumeSliderMove) {
new SetVolumeAction(scVolumeSlider.getSelection()).run();
}
laVolumeLabel.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();
}
});
btnVolumeDown.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new SetVolumeAction(scVolumeSlider.getSelection() - 10).run();
}
});
btnVolumeUp.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new SetVolumeAction(scVolumeSlider.getSelection() + 10).run();
}
});
btnVolumeMute.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new VolumeMuteAction().run();
}
});
}
private void setVolumeSliderValue(int value) {
if (!_boVolumeSliderMove) {
laVolumeLabel.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$
}
}
private void enableComponents(boolean value) {
btnPrev.setEnabled(value);
btnStop.setEnabled(value);
btnPause.setEnabled(value);
btnNext.setEnabled(value);
scSlider.setEnabled(value);
laEllapsedTime.setEnabled(value);
laTotalTime.setEnabled(value);
btnVolumeMute.setEnabled(value);
btnSeekMinus.setEnabled(value);
btnSeekPlus.setEnabled(value);
}
private void enableComponentsForPauseMode(boolean value) {
scSlider.setEnabled(value);
btnSeekMinus.setEnabled(value);
btnSeekPlus.setEnabled(value);
}
private void resetInformations() {
if (!Controller.getInstance().isAplicationStopping()) {
enableComponents(false);
_totalTime = 0;
scSlider.setMinimum(0);
scSlider.setMaximum(0);
scSlider.setSelection(0);
}
}
private void setTime(int time) {
if (!Controller.getInstance().isAplicationStopping()) {
if (!_boSliderMove) {
laEllapsedTime.setText(Utils.milliseconds2String(time));
laTotalTime.setText(Utils.seconds2String(_totalTime));
scSlider.setSelection(time);
}
}
}
private void setTotalTime(int time) {
if (!Controller.getInstance().isAplicationStopping()) {
if (!_boSliderMove) {
_totalTime = time / 1000;
scSlider.setMaximum(time);
}
}
}
private void updateInformations(IAudioItem item) {
if (!Controller.getInstance().isAplicationStopping()) {
enableComponents(true);
//_totalTime = 0;
scSlider.setMinimum(0);
//scSlider.setMaximum(100);
scSlider.setSelection(0);
}
}
@Override
public void propertyChange(PropertyChangeEvent arg0) {
if (arg0.getPropertyName().equals(EventConstants.EVT_PLAY_NEW_AUDIO_ITEM)) {
class updateInformations implements Runnable {
IAudioItem _newItem;
public updateInformations(IAudioItem newItem) {
_newItem = newItem;
}
public void run() {
updateInformations(_newItem);
}
}
Display.getDefault().asyncExec(new updateInformations((IAudioItem) arg0.getNewValue()));
}
if (arg0.getPropertyName().equals(EventConstants.EVT_ELAPSED_TIME_CHANGE)) {
class setTime implements Runnable {
int newTime = 0;
public setTime(int newTime){
this.newTime = newTime;
}
public void run() {
setTime(newTime);
}
}
Display.getDefault().asyncExec(new setTime(((Integer) arg0.getNewValue()).intValue()));
}
if (arg0.getPropertyName().equals(EventConstants.EVT_TOTAL_TIME_CHANGE)) {
class setTotalTime implements Runnable {
int newTime = 0;
public setTotalTime(int newTime) {
this.newTime = newTime;
}
public void run() {
setTotalTime(newTime);
}
}
Display.getDefault().asyncExec(new setTotalTime(((Integer) arg0.getNewValue()).intValue()));
}
if (arg0.getPropertyName().equals(EventConstants.EVT_PAUSE_PLAYBACK)) {
class setPauseMode implements Runnable {
public void run() {
enableComponentsForPauseMode(!Controller.getInstance().getEngine().isPaused());
}
}
Display.getDefault().asyncExec(new setPauseMode());
}
if (arg0.getPropertyName().equals(EventConstants.EVT_STOP_PLAYBACK)) {
class resetInformations implements Runnable {
public void run() {
resetInformations();
}
}
Display.getDefault().asyncExec(new resetInformations());
}
if (arg0.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());
}
if (arg0.getPropertyName().equals(Constants.EVT_PLAY_MODE_CHANGE)) {
class onPlaymodeChange implements Runnable {
public void run() {
setPlaymodeUI();
}
}
Display.getDefault().asyncExec(new onPlaymodeChange());
}
}
public void dispose() {
imgPause.dispose();
imgPrev.dispose();
imgNext.dispose();
imgStop.dispose();
imgVolumeDown.dispose();
imgVolumeUp.dispose();
imgVolumeMute.dispose();
imgSeekPlus.dispose();
imgSeekMinus.dispose();
Controller.getInstance().getEventController().removeGeneralEventsListener(this);
Controller.getInstance().getEventController().removeAudioItemChangeListener(this);
super.dispose();
}
@Override
public void setFocus() {
// TODO Auto-generated method stub
}
}