/*
* Jampa
* Copyright (C) 2008 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.controllers.ui;
import java.beans.PropertyChangeListener;
import java.util.Iterator;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.PlatformUI;
import org.jampa.Activator;
import org.jampa.controllers.Controller;
import org.jampa.controllers.events.EventConstants;
import org.jampa.gui.actions.SetVolumeAction;
import org.jampa.gui.actions.VolumeMuteAction;
import org.jampa.gui.actions.playlist.PlayPlaylistAction;
import org.jampa.gui.actions.podcast.PlayPodcastAction;
import org.jampa.gui.actions.radio.PlayRadioAction;
import org.jampa.gui.translations.Messages;
import org.jampa.logging.Log;
import org.jampa.model.playlists.Playlist;
import org.jampa.model.podcasts.Podcast;
import org.jampa.model.radio.IRadioItem;
import org.jampa.model.radio.RadioItem;
import org.jampa.preferences.PreferenceConstants;
import org.jampa.utils.ApplicationUtils;
import org.jampa.utils.Constants;
public class TrayController implements PropertyChangeListener {
private boolean _appHidden = false;
private boolean _trayShown = false;
private boolean _prevShown = false;
private boolean _stopShown = false;
private boolean _pauseShown = false;
private boolean _nextShown = false;
private Image _appImage;
private Image _prevImage;
private Image _stopImage;
private Image _pauseImage;
private Image _nextImage;
private Image _volumeDownImage;
private Image _volumeUpImage;
private Image _volumeMuteImage;
private TrayItem _appItem;
private TrayItem _prevItem;
private TrayItem _stopItem;
private TrayItem _pauseItem;
private TrayItem _nextItem;
private Menu trayMenu = null;
private MenuItem showItem;
private Shell _shell;
private boolean _onlyShowFavoritesRadio;
public TrayController() {
_onlyShowFavoritesRadio = Controller.getInstance().getPreferenceStore().getBoolean(PreferenceConstants.TRAY_RADIO_SHOW_FAVORITES);
Activator.getDefault();
ImageDescriptor descriptor = Activator.getImageDescriptor("/icons/jampa.png"); //$NON-NLS-1$
_appImage = descriptor.createImage();
descriptor = Activator.getImageDescriptor("/icons/previous_16.png"); //$NON-NLS-1$
_prevImage = descriptor.createImage();
descriptor = Activator.getImageDescriptor("/icons/stop_16.png"); //$NON-NLS-1$
_stopImage = descriptor.createImage();
descriptor = Activator.getImageDescriptor("/icons/pause_16.png"); //$NON-NLS-1$
_pauseImage = descriptor.createImage();
descriptor = Activator.getImageDescriptor("/icons/next_16.png"); //$NON-NLS-1$
_nextImage = descriptor.createImage();
descriptor = Activator.getImageDescriptor("/icons/volumedown_16.png"); //$NON-NLS-1$
_volumeDownImage = descriptor.createImage();
descriptor = Activator.getImageDescriptor("/icons/volumeup_16.png"); //$NON-NLS-1$
_volumeUpImage = descriptor.createImage();
descriptor = Activator.getImageDescriptor("/icons/volumemute_16.png"); //$NON-NLS-1$
_volumeMuteImage = descriptor.createImage();
Controller.getInstance().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
private boolean isTrayProperty(PropertyChangeEvent event) {
String property = event.getProperty();
return (property.equals(PreferenceConstants.TRAY_SHOW_IN_TRAY)) ||
(property.equals(PreferenceConstants.TRAY_SHOW_REVERSED)) ||
(property.equals(PreferenceConstants.TRAY_SHOW_NEXT_BUTTON)) ||
(property.equals(PreferenceConstants.TRAY_SHOW_PAUSE_BUTTON)) ||
(property.equals(PreferenceConstants.TRAY_SHOW_PREV_BUTTON)) ||
(property.equals(PreferenceConstants.TRAY_SHOW_STOP_BUTTON));
}
public void propertyChange(PropertyChangeEvent event)
{
if (isTrayProperty(event)) {
if (_trayShown) {
hideTrayItems();
}
if (Controller.getInstance().getPreferenceStore().getBoolean(PreferenceConstants.TRAY_SHOW_IN_TRAY)) {
showTrayItems();
}
}
}
});
Controller.getInstance().getEventController().addPlaylistChangeListener(this);
Controller.getInstance().getEventController().addRadioChangeListeners(this);
Log.getInstance(TrayController.class).debug("TrayController initialized."); //$NON-NLS-1$
}
public void setShell(Shell shell) {
_shell = shell;
}
private TrayItem createTrayItem(String toolTip, Image image) {
TrayItem result = new TrayItem(Display.getDefault().getSystemTray(), SWT.NONE);
result.setToolTipText(toolTip);
result.setImage(image);
return result;
}
public void showTrayItems() {
IPreferenceStore store = Controller.getInstance().getPreferenceStore();
if (!store.getBoolean(PreferenceConstants.TRAY_SHOW_REVERSED)) {
if (store.getBoolean(PreferenceConstants.TRAY_SHOW_NEXT_BUTTON)) {
_nextItem = createTrayItem(Messages.getString("TrayController.Next"), _nextImage); //$NON-NLS-1$
_nextItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Controller.getInstance().getPlaylistController().playNextInPlaylist(false); //$NON-NLS-1$
}
});
_nextShown = true;
}
if (store.getBoolean(PreferenceConstants.TRAY_SHOW_PAUSE_BUTTON)) {
_pauseItem = createTrayItem(Messages.getString("TrayController.Pause"), _pauseImage); //$NON-NLS-1$
_pauseItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Controller.getInstance().getPlaylistController().pausePlayback();
}
});
_pauseShown = true;
}
if (store.getBoolean(PreferenceConstants.TRAY_SHOW_STOP_BUTTON)) {
_stopItem = createTrayItem(Messages.getString("TrayController.Stop"), _stopImage); //$NON-NLS-1$
_stopItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Controller.getInstance().getPlaylistController().stopPlayback();
}
});
_stopShown = true;
}
if (store.getBoolean(PreferenceConstants.TRAY_SHOW_PREV_BUTTON)) {
_prevItem = createTrayItem(Messages.getString("TrayController.Previous"), _prevImage); //$NON-NLS-1$
_prevItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Controller.getInstance().getPlaylistController().playPreviousInPlaylist();
}
});
_prevShown = true;
}
_appItem = createTrayItem(Constants.APP_NAME + " - " + Messages.getString("Controller.TooltipStopped"), _appImage); //$NON-NLS-1$ //$NON-NLS-2$
_appItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (_appHidden) {
showApplication();
} else {
hideApplication();
}
}
});
} else {
_appItem = createTrayItem(Constants.APP_NAME + " - " + Messages.getString("Controller.TooltipStopped"), _appImage); //$NON-NLS-1$ //$NON-NLS-2$
_appItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (_appHidden) {
showApplication();
} else {
hideApplication();
}
}
});
if (store.getBoolean(PreferenceConstants.TRAY_SHOW_PREV_BUTTON)) {
_prevItem = createTrayItem(Messages.getString("TrayController.Previous"), _prevImage); //$NON-NLS-1$
_prevItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Controller.getInstance().getPlaylistController().playPreviousInPlaylist();
}
});
_prevShown = true;
}
if (store.getBoolean(PreferenceConstants.TRAY_SHOW_STOP_BUTTON)) {
_stopItem = createTrayItem(Messages.getString("TrayController.Stop"), _stopImage); //$NON-NLS-1$
_stopItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Controller.getInstance().getPlaylistController().stopPlayback();
}
});
_stopShown = true;
}
if (store.getBoolean(PreferenceConstants.TRAY_SHOW_PAUSE_BUTTON)) {
_pauseItem = createTrayItem(Messages.getString("TrayController.Pause"), _pauseImage); //$NON-NLS-1$
_pauseItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Controller.getInstance().getPlaylistController().pausePlayback();
}
});
_pauseShown = true;
}
if (store.getBoolean(PreferenceConstants.TRAY_SHOW_NEXT_BUTTON)) {
_nextItem = createTrayItem(Messages.getString("TrayController.Next"), _nextImage); //$NON-NLS-1$
_nextItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Controller.getInstance().getPlaylistController().playNextInPlaylist(false); //$NON-NLS-1$
}
});
_nextShown = true;
}
}
buildMenus();
_trayShown = true;
}
private void buildMenus() {
if (trayMenu != null)
trayMenu.dispose();
trayMenu = new Menu(_shell, SWT.POP_UP);
showItem = new MenuItem(trayMenu, SWT.PUSH);
showItem.setText(Messages.getString("TrayController.Hide")); //$NON-NLS-1$
showItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if (_appHidden) {
showApplication();
} else {
hideApplication();
}
}
});
new MenuItem(trayMenu, SWT.SEPARATOR);
MenuItem playItem = new MenuItem(trayMenu, SWT.CASCADE);
playItem.setText(Messages.getString("TrayController.MenuPlayPlaylist")); //$NON-NLS-1$
Menu playPlaylistMenu = new Menu(trayMenu);
playItem.setMenu(playPlaylistMenu);
MenuItem newPlayPlaylistItem = new MenuItem(playPlaylistMenu, SWT.PUSH);
newPlayPlaylistItem.setText(Messages.getString("TrayController.MenuDefaultPlaylist")); //$NON-NLS-1$
newPlayPlaylistItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new PlayPlaylistAction(Constants.DEFAULT_PLAYLIST_ID).run();
}
});
new MenuItem(playPlaylistMenu, SWT.SEPARATOR);
Object[] playlistList = Controller.getInstance().getPlaylistController().getPlaylistList();
for (int i = 0; i < playlistList.length; i++) {
final MenuItem playlistItem = new MenuItem(playPlaylistMenu, SWT.PUSH);
playlistItem.setText(((Playlist) playlistList[i]).getName());
playlistItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new PlayPlaylistAction(playlistItem.getText()).run();
}
});
}
if (ApplicationUtils.doesEngineSupportPodcasts()) {
MenuItem playPodcastItem = new MenuItem(trayMenu, SWT.CASCADE);
playPodcastItem.setText(Messages.getString("TrayController.MenuPlayPodcast")); //$NON-NLS-1$
Menu playPodcastMenu = new Menu(trayMenu);
playPodcastItem.setMenu(playPodcastMenu);
Iterator<Podcast> podcastIter = Controller.getInstance().getPlaylistController().getPodcastListAsList().iterator();
while (podcastIter.hasNext()) {
Podcast podcastItem = podcastIter.next();
final MenuItem podcastMenuItem = new MenuItem(playPodcastMenu, SWT.PUSH);
podcastMenuItem.setText(podcastItem.getName() + " - " + podcastItem.getTitle());
podcastMenuItem.setData(podcastItem);
podcastMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new PlayPodcastAction(((Podcast) podcastMenuItem.getData()).getName()).run();
}
});
}
}
if (ApplicationUtils.doesEngineSupportRadio()) {
MenuItem playRadioItem = new MenuItem(trayMenu, SWT.CASCADE);
playRadioItem.setText(Messages.getString("TrayController.MenuPlayRadio")); //$NON-NLS-1$
Menu playRadioMenu = new Menu(trayMenu);
playRadioItem.setMenu(playRadioMenu);
final MenuItem onlyShowFavorite = new MenuItem(playRadioMenu, SWT.CHECK);
onlyShowFavorite.setText(Messages.getString("TrayController.OnlyShowFavorites")); //$NON-NLS-1$
onlyShowFavorite.setSelection(_onlyShowFavoritesRadio);
onlyShowFavorite.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
_onlyShowFavoritesRadio = !_onlyShowFavoritesRadio;
onlyShowFavorite.setSelection(_onlyShowFavoritesRadio);
Controller.getInstance().getPreferenceStore().setValue(PreferenceConstants.TRAY_RADIO_SHOW_FAVORITES, _onlyShowFavoritesRadio);
Display.getDefault().asyncExec(new MenuRefresher());
}
});
new MenuItem(playRadioMenu, SWT.SEPARATOR);
Iterator<IRadioItem> categoryIter = Controller.getInstance().getRadioController().getRootCategory().getChildren(_onlyShowFavoritesRadio).iterator();
while (categoryIter.hasNext()) {
IRadioItem categoryItem = categoryIter.next();
MenuItem categoryMenuItem = new MenuItem(playRadioMenu, SWT.CASCADE);
categoryMenuItem.setText(categoryItem.getName());
Menu categoryMenu = new Menu(trayMenu);
categoryMenuItem.setMenu(categoryMenu);
Iterator<IRadioItem> radioIter = categoryItem.getChildren(_onlyShowFavoritesRadio).iterator();
while (radioIter.hasNext()) {
IRadioItem radioItem = radioIter.next();
final MenuItem radioMenuItem = new MenuItem(categoryMenu, SWT.PUSH);
radioMenuItem.setText(radioItem.getName());
radioMenuItem.setData(radioItem);
radioMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new PlayRadioAction((RadioItem) radioMenuItem.getData()).run();
}
});
}
}
}
new MenuItem(trayMenu, SWT.SEPARATOR);
MenuItem volumeItem = new MenuItem(trayMenu, SWT.CASCADE);
volumeItem.setText(Messages.getString("TrayController.Volume")); //$NON-NLS-1$
Menu volumeItemMenu = new Menu(trayMenu);
volumeItem.setMenu(volumeItemMenu);
MenuItem volumeDownItem = new MenuItem(volumeItemMenu, SWT.PUSH);
volumeDownItem.setText(Messages.getString("TrayController.VolumeDown")); //$NON-NLS-1$
volumeDownItem.setImage(_volumeDownImage);
volumeDownItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new SetVolumeAction(Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAYBACK_VOLUME) - 10).run();
}
});
MenuItem volumeUpItem = new MenuItem(volumeItemMenu, SWT.PUSH);
volumeUpItem.setText(Messages.getString("TrayController.VolumeUp")); //$NON-NLS-1$
volumeUpItem.setImage(_volumeUpImage);
volumeUpItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new SetVolumeAction(Controller.getInstance().getPreferenceStore().getInt(PreferenceConstants.PLAYBACK_VOLUME) + 10).run();
}
});
MenuItem volumeMuteItem = new MenuItem(volumeItemMenu, SWT.PUSH);
volumeMuteItem.setText(Messages.getString("TrayController.VolumeMute")); //$NON-NLS-1$
volumeMuteItem.setImage(_volumeMuteImage);
volumeMuteItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new VolumeMuteAction().run();
}
});
new MenuItem(volumeItemMenu, SWT.SEPARATOR);
MenuItem volume100Item = new MenuItem(volumeItemMenu, SWT.PUSH);
volume100Item.setText(Messages.getString("TrayController.Volume100")); //$NON-NLS-1$
volume100Item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new SetVolumeAction(100).run();
}
});
MenuItem volume75Item = new MenuItem(volumeItemMenu, SWT.PUSH);
volume75Item.setText(Messages.getString("TrayController.Volume75")); //$NON-NLS-1$
volume75Item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new SetVolumeAction(75).run();
}
});
MenuItem volume50Item = new MenuItem(volumeItemMenu, SWT.PUSH);
volume50Item.setText(Messages.getString("TrayController.Volume50")); //$NON-NLS-1$
volume50Item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new SetVolumeAction(50).run();
}
});
MenuItem volume25Item = new MenuItem(volumeItemMenu, SWT.PUSH);
volume25Item.setText(Messages.getString("TrayController.Volume25")); //$NON-NLS-1$
volume25Item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new SetVolumeAction(25).run();
}
});
new MenuItem(trayMenu, SWT.SEPARATOR);
MenuItem prevItem = new MenuItem(trayMenu, SWT.PUSH);
prevItem.setText(Messages.getString("TrayController.Previous")); //$NON-NLS-1$
prevItem.setImage(_prevImage);
prevItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Controller.getInstance().getPlaylistController().playPreviousInPlaylist();
}
});
MenuItem stopItem = new MenuItem(trayMenu, SWT.PUSH);
stopItem.setText(Messages.getString("TrayController.Stop")); //$NON-NLS-1$
stopItem.setImage(_stopImage);
stopItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Controller.getInstance().getPlaylistController().stopPlayback();
}
});
MenuItem pauseItem = new MenuItem(trayMenu, SWT.PUSH);
pauseItem.setText(Messages.getString("TrayController.Pause")); //$NON-NLS-1$
pauseItem.setImage(_pauseImage);
pauseItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Controller.getInstance().getPlaylistController().pausePlayback();
}
});
MenuItem nextItem = new MenuItem(trayMenu, SWT.PUSH);
nextItem.setText(Messages.getString("TrayController.Next")); //$NON-NLS-1$
nextItem.setImage(_nextImage);
nextItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Controller.getInstance().getPlaylistController().playNextInPlaylist(false);
}
});
new MenuItem(trayMenu, SWT.SEPARATOR);
MenuItem closeItem = new MenuItem(trayMenu, SWT.PUSH);
closeItem.setText(Messages.getString("TrayController.Exit")); //$NON-NLS-1$
closeItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
PlatformUI.getWorkbench().close();
}
});
_appItem.addListener(SWT.MenuDetect, new Listener() {
public void handleEvent(Event event) {
trayMenu.setVisible(true);
}
});
}
public void showApplication() {
_shell.setVisible(true);
_shell.setActive();
_shell.setFocus();
_shell.setMinimized(false);
_appHidden = false;
showItem.setText(Messages.getString("TrayController.Hide")); //$NON-NLS-1$
}
public void hideApplication() {
_shell.setVisible(false);
_appHidden = true;
showItem.setText(Messages.getString("TrayController.Show")); //$NON-NLS-1$
}
public void hideTrayItems() {
_appItem.dispose();
if (_prevShown) {
_prevItem.dispose();
_prevShown = false;
}
if (_stopShown) {
_stopItem.dispose();
_stopShown = false;
}
if (_pauseShown) {
_pauseItem.dispose();
_pauseShown = false;
}
if (_nextShown) {
_nextItem.dispose();
_nextShown = false;
}
_trayShown = false;
}
public void setToolTipText(String text) {
if (_trayShown) {
_appItem.setToolTipText(text);
}
}
public boolean isApplicationMinimized() {
return _appHidden;
}
class MenuRefresher implements Runnable {
public void run() {
buildMenus();
}
}
@Override
public void propertyChange(java.beans.PropertyChangeEvent arg0) {
if ((arg0.getPropertyName().equals(EventConstants.EVT_ADD_PLAYLIST)) ||
(arg0.getPropertyName().equals(EventConstants.EVT_REMOVE_PLAYLIST)) ||
(arg0.getPropertyName().equals(EventConstants.EVT_RADIO_ADD)) ||
(arg0.getPropertyName().equals(EventConstants.EVT_RADIO_CHANGE)) ||
(arg0.getPropertyName().equals(EventConstants.EVT_RADIO_REMOVE)) ||
(arg0.getPropertyName().equals(EventConstants.EVT_RADIO_CHANGE_FAVORITES))) {
Display.getDefault().asyncExec(new MenuRefresher());
}
}
public void dispose() {
_appImage.dispose();
_prevImage.dispose();
_stopImage.dispose();
_pauseImage.dispose();
_nextImage.dispose();
_volumeDownImage.dispose();
_volumeUpImage.dispose();
_volumeMuteImage.dispose();
Controller.getInstance().getEventController().removePlaylistChangeListener(this);
Controller.getInstance().getEventController().removeRadioChangeListeners(this);
}
}