From 95979fff2955db96729e707c1591d924ff0e1447 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Fri, 18 Mar 2016 17:07:39 +0000 Subject: [PATCH] Decoupled Player and PlayerGUI, dependency between the two is now injected instead. --- pom.xml | 5 +++++ src/main/java/musicplayer/BindingsModule.java | 14 ++++++++++++++ src/main/java/musicplayer/PlayerGUI.java | 13 ++++++++++--- .../GStreamerPlayer.java} | 9 ++++++--- src/main/java/musicplayer/player/IPlayer.java | 19 +++++++++++++++++++ 5 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 src/main/java/musicplayer/BindingsModule.java rename src/main/java/musicplayer/{Player.java => player/GStreamerPlayer.java} (95%) create mode 100644 src/main/java/musicplayer/player/IPlayer.java diff --git a/pom.xml b/pom.xml index 514d79e..f2d77fb 100644 --- a/pom.xml +++ b/pom.xml @@ -97,6 +97,11 @@ 2.0.2 compile + + com.google.inject + guice + 4.0 + diff --git a/src/main/java/musicplayer/BindingsModule.java b/src/main/java/musicplayer/BindingsModule.java new file mode 100644 index 0000000..b9a4f48 --- /dev/null +++ b/src/main/java/musicplayer/BindingsModule.java @@ -0,0 +1,14 @@ +package musicplayer; + +import com.google.inject.AbstractModule; +import musicplayer.callbacks.PlayerCallbackInterface; +import musicplayer.player.IPlayer; +import musicplayer.player.GStreamerPlayer; + +public class BindingsModule extends AbstractModule { + @Override + protected void configure() { + bind(IPlayer.class).to(GStreamerPlayer.class); + bind(PlayerCallbackInterface.class).to(PlayerGUI.class); + } +} diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index d5d9043..839102f 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -1,5 +1,8 @@ package musicplayer; +import com.google.inject.Guice; +import com.google.inject.Inject; +import com.google.inject.Injector; import musicplayer.callbacks.LibraryCallbackInterface; import musicplayer.callbacks.PlayerCallbackInterface; import musicplayer.db.DatabaseManager; @@ -8,6 +11,7 @@ import musicplayer.model.Album; import musicplayer.model.Artist; import musicplayer.model.HasSongs; import musicplayer.model.Song; +import musicplayer.player.IPlayer; import musicplayer.swingmodels.PlaylistTableModel; import musicplayer.util.Icons; import musicplayer.util.LibraryUtils; @@ -47,7 +51,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf private final JTable playList = new JTable(); private JSlider seekBar; private final JComboBox libraryDisplayType = new JComboBox<>(); - private final Player player = new Player(this); + private final IPlayer player; private final PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>()); private static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode(); private boolean libraryUpdating = false; @@ -55,7 +59,9 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf private final Map libraryDisplayVariants = createDisplayVariantMap(); - public PlayerGUI() { + @Inject + public PlayerGUI(IPlayer player) { + this.player = player; createUI(); resetTree(); Thread seekBarUpdater = new Thread(() -> { @@ -97,7 +103,8 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf public static void main(String[] args) { DatabaseManager.init(); - PlayerGUI playerGUI = new PlayerGUI(); + Injector injector = Guice.createInjector(new BindingsModule()); + PlayerGUI playerGUI = injector.getInstance(PlayerGUI.class); JFrame frame = new JFrame(); frame.setMinimumSize(new Dimension(600, 400)); frame.setContentPane(playerGUI.mainPanel); diff --git a/src/main/java/musicplayer/Player.java b/src/main/java/musicplayer/player/GStreamerPlayer.java similarity index 95% rename from src/main/java/musicplayer/Player.java rename to src/main/java/musicplayer/player/GStreamerPlayer.java index d4747ba..484012e 100644 --- a/src/main/java/musicplayer/Player.java +++ b/src/main/java/musicplayer/player/GStreamerPlayer.java @@ -1,5 +1,7 @@ -package musicplayer; +package musicplayer.player; +import com.google.inject.Inject; +import musicplayer.StartPlayingException; import musicplayer.callbacks.PlayerCallbackInterface; import musicplayer.model.Song; import org.gstreamer.ClockTime; @@ -11,7 +13,7 @@ import org.gstreamer.elements.PlayBin2; import java.io.File; import java.util.Optional; -class Player { +public class GStreamerPlayer implements IPlayer{ private final PlayBin2 playBin; @@ -25,7 +27,8 @@ class Player { * Manages GStreamer based playback operations. * @param callbackInterface Interface on which UI updates can be called. */ - public Player(PlayerCallbackInterface callbackInterface) { + @Inject + public GStreamerPlayer(PlayerCallbackInterface callbackInterface) { this.callbackInterface = callbackInterface; Gst.init(); playBin = new PlayBin2("BusMessages"); diff --git a/src/main/java/musicplayer/player/IPlayer.java b/src/main/java/musicplayer/player/IPlayer.java new file mode 100644 index 0000000..063a430 --- /dev/null +++ b/src/main/java/musicplayer/player/IPlayer.java @@ -0,0 +1,19 @@ +package musicplayer.player; + +import musicplayer.StartPlayingException; +import musicplayer.model.Song; + +import java.util.Optional; + +public interface IPlayer { + void playSong(Optional inputSong) throws StartPlayingException; + void stop(); + void resume(); + void pause(); + Song getCurrentSong(); + int currentSongPosition(); + void setVolume(int volume); + int getVolume(); + void seek(int position); + boolean isPlaying(); +}