From b36a3af860dc4ef833e86eedc9731ec328503bf1 Mon Sep 17 00:00:00 2001 From: mdb Date: Sun, 3 Mar 2002 17:21:46 +0000 Subject: [PATCH] Use the new callback interface to manage the "playing" UI components. git-svn-id: https://samskivert.googlecode.com/svn/trunk@633 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../src/java/robodj/chooser/ChooserFrame.java | 80 ++++++++++--- .../java/robodj/chooser/PlaylistPanel.java | 107 ++++++++---------- 2 files changed, 112 insertions(+), 75 deletions(-) diff --git a/projects/robodj/src/java/robodj/chooser/ChooserFrame.java b/projects/robodj/src/java/robodj/chooser/ChooserFrame.java index d95207df..f9c1a600 100644 --- a/projects/robodj/src/java/robodj/chooser/ChooserFrame.java +++ b/projects/robodj/src/java/robodj/chooser/ChooserFrame.java @@ -1,5 +1,5 @@ // -// $Id: ChooserFrame.java,v 1.7 2002/03/03 06:32:12 mdb Exp $ +// $Id: ChooserFrame.java,v 1.8 2002/03/03 17:21:46 mdb Exp $ package robodj.chooser; @@ -9,13 +9,17 @@ import java.awt.event.ActionListener; import javax.swing.*; import com.samskivert.swing.*; +import com.samskivert.swing.util.TaskAdapter; +import com.samskivert.swing.util.TaskObserver; +import com.samskivert.swing.util.TaskMaster; +import robodj.Log; import robodj.Version; import robodj.repository.*; +import robodj.util.ServerControl.PlayingListener; -public class ChooserFrame - extends JFrame - implements ActionListener +public class ChooserFrame extends JFrame + implements ActionListener, TaskObserver, PlayingListener { public ChooserFrame () { @@ -54,8 +58,10 @@ public class ChooserFrame JPanel cbar = new JPanel(bgl); // add some fake control buttons for now + cbar.add(createControlButton("Back", "back")); + cbar.add(_stop = createControlButton("Stop", "stop")); cbar.add(_pause = createControlButton("Pause", "pause")); - cbar.add(createControlButton("Stop", "stop")); + cbar.add(createControlButton("Skip", "skip")); cbar.add(createControlButton("Exit", "exit")); // stick it into the frame @@ -64,6 +70,12 @@ public class ChooserFrame // now add our top-level panel (we'd not use this if we could set // a border on the content pane returned by the frame... alas) getContentPane().add(top, BorderLayout.CENTER); + + // add ourselves as a playing listener + Chooser.scontrol.addPlayingListener(this); + + // read our playing state + TaskMaster.invokeMethodTask("refreshPlaying", Chooser.scontrol, this); } protected JButton createControlButton (String label, String action) @@ -80,29 +92,61 @@ public class ChooserFrame if (cmd.equals("exit")) { System.exit(0); + } else if (cmd.equals("skip")) { + TaskMaster.invokeMethodTask("skip", Chooser.scontrol, this); + + } else if (cmd.equals("back")) { + TaskMaster.invokeMethodTask("back", Chooser.scontrol, this); + } else if (cmd.equals("pause")) { - Chooser.scontrol.pause(); - _pause.setLabel("Play"); - _pause.setActionCommand("play"); + TaskMaster.invokeMethodTask("pause", Chooser.scontrol, this); } else if (cmd.equals("play")) { - Chooser.scontrol.play(); - _pause.setLabel("Pause"); - _pause.setActionCommand("Pause"); - - } else if (cmd.equals("clear")) { - Chooser.scontrol.clear(); + TaskMaster.invokeMethodTask("play", Chooser.scontrol, this); } else if (cmd.equals("stop")) { - Chooser.scontrol.stop(); - - } else if (cmd.equals("skip")) { - Chooser.scontrol.skip(); + TaskMaster.invokeMethodTask("stop", Chooser.scontrol, this); } else { System.out.println("Unknown action event: " + cmd); } } + public void taskCompleted (String name, Object result) + { + // nothing to do here + } + + public void taskFailed (String name, Throwable exception) + { + String msg; + if (Exception.class.equals(exception.getClass())) { + msg = exception.getMessage(); + } else { + msg = exception.toString(); + } + JOptionPane.showMessageDialog(this, msg, "Error", + JOptionPane.ERROR_MESSAGE); + Log.logStackTrace(exception); + } + + public void playingUpdated (int songid, boolean paused) + { + if (songid == -1 || paused) { + _pause.setLabel("Play"); + _pause.setActionCommand("play"); + + } else { + _pause.setLabel("Pause"); + _pause.setActionCommand("pause"); + } + + _stop.setEnabled(songid != -1); + } + + /** A reference to the play/pause button. */ protected JButton _pause; + + /** A reference to the stop button. */ + protected JButton _stop; } diff --git a/projects/robodj/src/java/robodj/chooser/PlaylistPanel.java b/projects/robodj/src/java/robodj/chooser/PlaylistPanel.java index 7b2ed757..6cde24a0 100644 --- a/projects/robodj/src/java/robodj/chooser/PlaylistPanel.java +++ b/projects/robodj/src/java/robodj/chooser/PlaylistPanel.java @@ -1,5 +1,5 @@ // -// $Id: PlaylistPanel.java,v 1.11 2002/02/28 16:32:45 mdb Exp $ +// $Id: PlaylistPanel.java,v 1.12 2002/03/03 17:21:46 mdb Exp $ package robodj.chooser; @@ -18,10 +18,10 @@ import com.samskivert.util.StringUtil; import robodj.Log; import robodj.repository.*; +import robodj.util.ServerControl.PlayingListener; -public class PlaylistPanel - extends JPanel - implements TaskObserver, ActionListener +public class PlaylistPanel extends JPanel + implements TaskObserver, ActionListener, PlayingListener { public PlaylistPanel () { @@ -48,13 +48,15 @@ public class PlaylistPanel // add our control buttons cbar.add(_clearbut = createControlButton("Clear", "clear")); - cbar.add(createControlButton("Skip", "skip")); cbar.add(createControlButton("Refresh", "refresh")); add(cbar, GroupLayout.FIXED); // use a special font for our name buttons _nameFont = new Font("Helvetica", Font.PLAIN, 10); + // add ourselves as a playing listener + Chooser.scontrol.addPlayingListener(this); + // load up the playlist refreshPlaylist(); } @@ -71,12 +73,7 @@ public class PlaylistPanel { String cmd = e.getActionCommand(); if (cmd.equals("clear")) { - Chooser.scontrol.clear(); - refreshPlaylist(); - - } else if (cmd.equals("skip")) { - Chooser.scontrol.skip(); - refreshPlaying(); + TaskMaster.invokeMethodTask("clear", Chooser.scontrol, this); } else if (cmd.equals("refresh")) { refreshPlaylist(); @@ -85,14 +82,20 @@ public class PlaylistPanel JButton src = (JButton)e.getSource(); PlaylistEntry entry = (PlaylistEntry)src.getClientProperty("entry"); - Chooser.scontrol.skipto(entry.song.songid); - refreshPlaying(); + + // fire up a task to talk to the music daemon + final int songid = entry.song.songid; + TaskMaster.invokeTask("noop", new TaskAdapter() { + public Object invoke () throws Exception { + Chooser.scontrol.skipto(songid); + return null; + } + }, this); } else if (cmd.equals("remove")) { JButton src = (JButton)e.getSource(); PlaylistEntry entry = (PlaylistEntry)src.getClientProperty("entry"); - Chooser.scontrol.remove(entry.song.songid); // remove the entry UI elements JPanel epanel = (JPanel)entry.label.getParent(); @@ -102,9 +105,14 @@ public class PlaylistPanel // remove the entry from the playlist _plist.remove(entry); - // update the playing indicator because we may have removed - // the playing entry - refreshPlaying(); + // fire up a task to talk to the music daemon + final int songid = entry.song.songid; + TaskMaster.invokeTask("noop", new TaskAdapter() { + public Object invoke () throws Exception { + Chooser.scontrol.remove(songid); + return null; + } + }, this); } else if (cmd.equals("remove_all")) { JButton src = (JButton)e.getSource(); @@ -147,12 +155,16 @@ public class PlaylistPanel revalidate(); // relayout - // now tell the music daemon to remove these entries - Chooser.scontrol.removeGroup(entry.song.songid, count); - - // update the playing indicator because we may have removed - // the playing entry - refreshPlaying(); + // fire up a task to talk to the music daemon, telling it to + // remove those entries + final int songid = entry.song.songid; + final int fcount = count; + TaskMaster.invokeTask("noop", new TaskAdapter() { + public Object invoke () throws Exception { + Chooser.scontrol.removeGroup(songid, fcount); + return null; + } + }, this); } } @@ -170,27 +182,12 @@ public class PlaylistPanel TaskMaster.invokeMethodTask("readPlaylist", this, this); } - protected void refreshPlaying () - { - // unhighlight whoever is playing now - PlaylistEntry pentry = getPlayingEntry(); - if (pentry != null) { - pentry.label.setForeground(Color.black); - } - - // figure out who's playing - TaskMaster.invokeMethodTask("readPlaying", this, this); - } - public void readPlaylist () throws PersistenceException { // clear out any previous playlist _plist.clear(); - // find out what's currently playing - readPlaying(); - // get the playlist from the music daemon String[] plist = Chooser.scontrol.getPlaylist(); @@ -213,30 +210,15 @@ public class PlaylistPanel Log.warning("Unable to load entry [eid=" + eid + "]."); } } - } - public void readPlaying () - { - String playing = Chooser.scontrol.getPlaying(); - playing = StringUtil.split(playing, ":")[1].trim(); - _playid = -1; - if (!playing.equals("")) { - try { - _playid = Integer.parseInt(playing); - } catch (NumberFormatException nfe) { - Log.warning("Unable to parse currently playing id '" + - playing + "'."); - } - } + // now find out what's currently playing + Chooser.scontrol.refreshPlaying(); } public void taskCompleted (String name, Object result) { if (name.equals("readPlaylist")) { populatePlaylist(); - - } else if (name.equals("readPlaying")) { - highlightPlaying(); } } @@ -333,11 +315,22 @@ public class PlaylistPanel return button; } - protected void highlightPlaying () + public void playingUpdated (int songid, boolean paused) { + // unhighlight whoever is playing now + PlaylistEntry pentry = getPlayingEntry(); + if (pentry != null && pentry.label != null) { + pentry.label.setForeground(Color.black); + } + + // grab the new playing song id + _playid = songid; + + // highlight the playing song for (int i = 0; i < _plist.size(); i++) { PlaylistEntry entry = (PlaylistEntry)_plist.get(i); - if (entry.song.songid == _playid) { + if (entry.song.songid == _playid && + entry.label != null) { entry.label.setForeground(Color.red); } }