Notice: Attachments you submit will be routed for moderation. If you have an account, please log in first.

Ticket #1704: PlayerStateMediator.java

File PlayerStateMediator.java, 11.6 kB (added by goesen, 21 months ago)

The solution

Line 
1
2/*
3 *  Jajuk
4 *  Copyright (C) 2003-2009 The Jajuk Team
5 *  http://jajuk.info
6 *
7 *  This program is free software; you can redistribute it and/or
8 *  modify it under the terms of the GNU General Public License
9 *  as published by the Free Software Foundation; either version 2
10 *  of the License, or any later version.
11 *
12 *  This program is distributed in the hope that it will be useful,
13 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *  GNU General Public License for more details.
16 *
17 *  You should have received a copy of the GNU General Public License
18 *  along with this program; if not, write to the Free Software
19 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *  $Revision$
21 */
22package org.jajuk.ui.helpers;
23
24import static org.jajuk.ui.actions.JajukActions.FINISH_ALBUM;
25import static org.jajuk.ui.actions.JajukActions.FORWARD_TRACK;
26import static org.jajuk.ui.actions.JajukActions.NEXT_ALBUM;
27import static org.jajuk.ui.actions.JajukActions.NEXT_TRACK;
28import static org.jajuk.ui.actions.JajukActions.PAUSE_RESUME_TRACK;
29import static org.jajuk.ui.actions.JajukActions.PREVIOUS_ALBUM;
30import static org.jajuk.ui.actions.JajukActions.PREVIOUS_TRACK;
31import static org.jajuk.ui.actions.JajukActions.REWIND_TRACK;
32import static org.jajuk.ui.actions.JajukActions.STOP_TRACK;
33
34import java.util.HashSet;
35import java.util.Set;
36
37import javax.swing.SwingUtilities;
38
39import org.jajuk.base.File;
40import org.jajuk.base.FileManager;
41import org.jajuk.events.JajukEvent;
42import org.jajuk.events.JajukEvents;
43import org.jajuk.events.ObservationManager;
44import org.jajuk.events.Observer;
45import org.jajuk.services.notification.INotificator;
46import org.jajuk.services.notification.NotificatorFactory;
47import org.jajuk.services.players.Player;
48import org.jajuk.services.players.QueueModel;
49import org.jajuk.services.webradio.WebRadio;
50import org.jajuk.ui.actions.ActionManager;
51import org.jajuk.ui.actions.MuteAction;
52import org.jajuk.util.Conf;
53import org.jajuk.util.Const;
54import org.jajuk.util.IconLoader;
55import org.jajuk.util.JajukIcons;
56import org.jajuk.util.Messages;
57import org.jajuk.util.UtilFeatures;
58import org.jajuk.util.log.Log;
59
60/**
61 * This mediator observes events on player state and change actions (and player
62 * buttons state) accordingly.
63 */
64public class PlayerStateMediator implements Observer {
65
66  /** Singleton */
67  private static PlayerStateMediator self = new PlayerStateMediator();
68
69  // Register this item, do not do this in the constructor as the instance is not yet available
70  static {
71    ObservationManager.register(self);
72    // Update initial status
73    UtilFeatures.updateStatus(self);
74  }
75
76  /**
77   * Instantiates a new player state mediator.
78   */
79  private PlayerStateMediator() {
80  }
81
82  /**
83   * Gets the single instance of PlayerStateMediator.
84   *
85   * @return single instance of PlayerStateMediator
86   */
87  public static PlayerStateMediator getInstance() {
88    return self;
89  }
90
91  /*
92   * (non-Javadoc)
93   *
94   * @see org.jajuk.events.Observer#getRegistrationKeys()
95   */
96  public Set<JajukEvents> getRegistrationKeys() {
97    Set<JajukEvents> eventSubjectSet = new HashSet<JajukEvents>();
98    eventSubjectSet.add(JajukEvents.PLAYER_PLAY);
99    eventSubjectSet.add(JajukEvents.PLAYER_STOP);
100    eventSubjectSet.add(JajukEvents.PLAYER_PAUSE);
101    eventSubjectSet.add(JajukEvents.PLAYER_RESUME);
102    eventSubjectSet.add(JajukEvents.PLAY_OPENING);
103    eventSubjectSet.add(JajukEvents.PLAY_ERROR);
104    eventSubjectSet.add(JajukEvents.ZERO);
105    eventSubjectSet.add(JajukEvents.WEBRADIO_LAUNCHED);
106    eventSubjectSet.add(JajukEvents.VOLUME_CHANGED);
107    eventSubjectSet.add(JajukEvents.MUTE_STATE);
108    eventSubjectSet.add(JajukEvents.SHOW_CURRENTLY_PLAYING);
109
110    // for notification display
111    eventSubjectSet.add(JajukEvents.FILE_LAUNCHED);
112
113    return eventSubjectSet;
114  }
115
116  /*
117   * (non-Javadoc)
118   *
119   * @see org.jajuk.events.Observer#update(org.jajuk.events.Event)
120   */
121  public void update(final JajukEvent event) {
122    SwingUtilities.invokeLater(new Runnable() {
123      public void run() {
124        JajukEvents subject = event.getSubject();
125        if (JajukEvents.PLAYER_STOP.equals(subject)) {
126          ActionManager.getAction(REWIND_TRACK).setEnabled(false);
127          // Enable the play button to allow restarting the queue but disable if
128          // the queue is void
129          boolean bQueueNotVoid = (QueueModel.getQueue().size() > 0);
130          ActionManager.getAction(PAUSE_RESUME_TRACK).setEnabled(bQueueNotVoid);
131          ActionManager.getAction(NEXT_ALBUM).setEnabled(bQueueNotVoid);
132          ActionManager.getAction(PREVIOUS_ALBUM).setEnabled(bQueueNotVoid);
133          ActionManager.getAction(PREVIOUS_TRACK).setEnabled(bQueueNotVoid);
134          ActionManager.getAction(NEXT_TRACK).setEnabled(bQueueNotVoid);
135
136          setToPlay();
137          ActionManager.getAction(STOP_TRACK).setEnabled(false);
138          ActionManager.getAction(FORWARD_TRACK).setEnabled(false);
139          ActionManager.getAction(FINISH_ALBUM).setEnabled(false);
140          // reset startup position
141          Conf.setProperty(Const.CONF_STARTUP_LAST_POSITION, "0");
142        } else if (JajukEvents.ZERO.equals(subject)) {
143          ActionManager.getAction(PREVIOUS_TRACK).setEnabled(false);
144          ActionManager.getAction(NEXT_TRACK).setEnabled(false);
145          ActionManager.getAction(REWIND_TRACK).setEnabled(false);
146          ActionManager.getAction(PAUSE_RESUME_TRACK).setEnabled(false);
147          ActionManager.getAction(STOP_TRACK).setEnabled(false);
148          ActionManager.getAction(FORWARD_TRACK).setEnabled(false);
149          ActionManager.getAction(NEXT_ALBUM).setEnabled(false);
150          ActionManager.getAction(PREVIOUS_ALBUM).setEnabled(false);
151          ActionManager.getAction(FINISH_ALBUM).setEnabled(false);
152          setToPlay();
153          // reset startup position
154          Conf.setProperty(Const.CONF_STARTUP_LAST_POSITION, "0");
155          ActionManager.getAction(FINISH_ALBUM).setEnabled(true);
156        } else if (JajukEvents.PLAYER_PLAY.equals(subject)) {
157          ActionManager.getAction(PREVIOUS_TRACK).setEnabled(true);
158          ActionManager.getAction(NEXT_TRACK).setEnabled(true);
159          ActionManager.getAction(REWIND_TRACK).setEnabled(true);
160          ActionManager.getAction(PAUSE_RESUME_TRACK).setEnabled(true);
161          ActionManager.getAction(STOP_TRACK).setEnabled(true);
162          ActionManager.getAction(FORWARD_TRACK).setEnabled(true);
163          ActionManager.getAction(NEXT_ALBUM).setEnabled(true);
164          ActionManager.getAction(PREVIOUS_ALBUM).setEnabled(true);
165          ActionManager.getAction(FINISH_ALBUM).setEnabled(true);
166          // We need to set the icon here because the event can be
167          // thrown by the information panel, not directly the
168          // PlayPauseAction
169            setToPause();
170        } else if (JajukEvents.PLAY_OPENING.equals(subject)
171            || JajukEvents.PLAY_ERROR.equals(subject)) {
172          ActionManager.getAction(PREVIOUS_TRACK).setEnabled(true);
173          ActionManager.getAction(NEXT_TRACK).setEnabled(true);
174          ActionManager.getAction(REWIND_TRACK).setEnabled(false);
175          ActionManager.getAction(PAUSE_RESUME_TRACK).setEnabled(false);
176          ActionManager.getAction(STOP_TRACK).setEnabled(true);
177          ActionManager.getAction(FORWARD_TRACK).setEnabled(false);
178          ActionManager.getAction(NEXT_ALBUM).setEnabled(true);
179          ActionManager.getAction(PREVIOUS_ALBUM).setEnabled(true);
180          ActionManager.getAction(FINISH_ALBUM).setEnabled(true);
181          setToPlay();
182        } else if (JajukEvents.PLAYER_PAUSE.equals(subject)) {
183          ActionManager.getAction(REWIND_TRACK).setEnabled(false);
184          ActionManager.getAction(FORWARD_TRACK).setEnabled(false);
185          // We need to set the icon here because the event can be
186          // thrown by the information panel, not directly the
187          // PlayPauseAction
188          setToPlay();
189        } else if (JajukEvents.PLAYER_RESUME.equals(subject)) {
190          // Enable the volume when resuming (fix a mplayer issue, see
191          // above)
192          ActionManager.getAction(REWIND_TRACK).setEnabled(true);
193          ActionManager.getAction(FORWARD_TRACK).setEnabled(true);
194          // We need to set the icon here because the event can be
195          // thrown by the information panel, not directly the
196          // PlayPauseAction
197            setToPause();
198        } else if (JajukEvents.WEBRADIO_LAUNCHED.equals(subject)) {
199          ActionManager.getAction(PREVIOUS_TRACK).setEnabled(true);
200          ActionManager.getAction(NEXT_TRACK).setEnabled(true);
201          ActionManager.getAction(PAUSE_RESUME_TRACK).setEnabled(true);
202            setToPause();
203            ActionManager.getAction(STOP_TRACK).setEnabled(true);
204
205          // display a system notification if specified
206          INotificator notifier = NotificatorFactory.getNotificator();
207          if (notifier != null) {
208            WebRadio radio = (WebRadio) (event.getDetails().get(Const.DETAIL_CONTENT));
209            Log.debug("Got update for new webradio launched, item: " + radio);
210            notifier.notify(radio);
211          }
212        } else if (JajukEvents.VOLUME_CHANGED.equals(subject)) {
213          MuteAction.setVolumeIcon(100 * Player.getCurrentVolume());
214        } else if (JajukEvents.MUTE_STATE.equals(subject) &&
215        // Update mute icon look when changing the volume
216            !Player.isMuted()) {
217          MuteAction.setVolumeIcon(Player.getCurrentVolume() * 100);
218        } else if (subject.equals(JajukEvents.FILE_LAUNCHED)) {
219          INotificator notifier = NotificatorFactory.getNotificator();
220          if (notifier != null) {
221            String id = (String) ObservationManager.getDetail(event, Const.DETAIL_CURRENT_FILE_ID);
222            if (id == null) {
223              Log.debug("No id found on FILE_LAUNCHED");
224              return;
225            }
226
227            File file = FileManager.getInstance().getFileByID(id);
228            Log.debug("Got update for new file launched, item: {{" + file + "}}. Sending text: {{"
229                + QueueModel.getCurrentFileTitle() + "}}");
230            notifier.notify(file);
231          }
232        } else if (subject.equals(JajukEvents.SHOW_CURRENTLY_PLAYING)) {
233          INotificator notifier = NotificatorFactory.getNotificator();
234          if (notifier != null) {
235            if (QueueModel.getCurrentRadio() != null) {
236              Log.debug("Got request to notify with current webradio information: {{"
237                  + QueueModel.getCurrentRadio() + "}}");
238              notifier.notify(QueueModel.getCurrentRadio());
239            } else {
240              Log.debug("Got request to notify with current file information: {{"
241                  + QueueModel.getCurrentItem().getFile() + "}}");
242              notifier.notify(QueueModel.getCurrentItem().getFile());
243            }
244          }
245        }
246
247        // For all events except Volume Change/Mute, refresh the queue
248        if (!JajukEvents.VOLUME_CHANGED.equals(subject) && !JajukEvents.MUTE_STATE.equals(subject)
249            && !JajukEvents.FILE_LAUNCHED.equals(subject)
250            && !JajukEvents.SHOW_CURRENTLY_PLAYING.equals(subject)) {
251          ObservationManager.notify(new JajukEvent(JajukEvents.QUEUE_NEED_REFRESH));
252        }
253      }
254    });
255  }
256
257    private void setToPause() {
258        ActionManager.getAction(PAUSE_RESUME_TRACK).setIcon(
259            IconLoader.getIcon(JajukIcons.PLAYER_PAUSE));
260        ActionManager.getAction(PAUSE_RESUME_TRACK).setName(Messages.getString("JajukWindow.10"));
261    }
262
263    private void setToPlay() {
264        ActionManager.getAction(PAUSE_RESUME_TRACK).setIcon(
265            IconLoader.getIcon(JajukIcons.PLAYER_PLAY));
266        ActionManager.getAction(PAUSE_RESUME_TRACK).setName(Messages.getString("JajukWindow.12"));
267    }
268
269}