Decoupled Player and PlayerGUI, dependency between the two is now injected instead.

This commit is contained in:
neviyn 2016-03-18 17:07:39 +00:00
parent e4e3ed6b6f
commit 95979fff29
5 changed files with 54 additions and 6 deletions

View File

@ -97,6 +97,11 @@
<version>2.0.2</version> <version>2.0.2</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.0</version>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>

View File

@ -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);
}
}

View File

@ -1,5 +1,8 @@
package musicplayer; package musicplayer;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import musicplayer.callbacks.LibraryCallbackInterface; import musicplayer.callbacks.LibraryCallbackInterface;
import musicplayer.callbacks.PlayerCallbackInterface; import musicplayer.callbacks.PlayerCallbackInterface;
import musicplayer.db.DatabaseManager; import musicplayer.db.DatabaseManager;
@ -8,6 +11,7 @@ import musicplayer.model.Album;
import musicplayer.model.Artist; import musicplayer.model.Artist;
import musicplayer.model.HasSongs; import musicplayer.model.HasSongs;
import musicplayer.model.Song; import musicplayer.model.Song;
import musicplayer.player.IPlayer;
import musicplayer.swingmodels.PlaylistTableModel; import musicplayer.swingmodels.PlaylistTableModel;
import musicplayer.util.Icons; import musicplayer.util.Icons;
import musicplayer.util.LibraryUtils; import musicplayer.util.LibraryUtils;
@ -47,7 +51,7 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
private final JTable playList = new JTable(); private final JTable playList = new JTable();
private JSlider seekBar; private JSlider seekBar;
private final JComboBox<String> libraryDisplayType = new JComboBox<>(); private final JComboBox<String> libraryDisplayType = new JComboBox<>();
private final Player player = new Player(this); private final IPlayer player;
private final PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>()); private final PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>());
private static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode(); private static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode();
private boolean libraryUpdating = false; private boolean libraryUpdating = false;
@ -55,7 +59,9 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
private final Map<String, Runnable> libraryDisplayVariants = createDisplayVariantMap(); private final Map<String, Runnable> libraryDisplayVariants = createDisplayVariantMap();
public PlayerGUI() { @Inject
public PlayerGUI(IPlayer player) {
this.player = player;
createUI(); createUI();
resetTree(); resetTree();
Thread seekBarUpdater = new Thread(() -> { Thread seekBarUpdater = new Thread(() -> {
@ -97,7 +103,8 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
public static void main(String[] args) { public static void main(String[] args) {
DatabaseManager.init(); DatabaseManager.init();
PlayerGUI playerGUI = new PlayerGUI(); Injector injector = Guice.createInjector(new BindingsModule());
PlayerGUI playerGUI = injector.getInstance(PlayerGUI.class);
JFrame frame = new JFrame(); JFrame frame = new JFrame();
frame.setMinimumSize(new Dimension(600, 400)); frame.setMinimumSize(new Dimension(600, 400));
frame.setContentPane(playerGUI.mainPanel); frame.setContentPane(playerGUI.mainPanel);

View File

@ -1,5 +1,7 @@
package musicplayer; package musicplayer.player;
import com.google.inject.Inject;
import musicplayer.StartPlayingException;
import musicplayer.callbacks.PlayerCallbackInterface; import musicplayer.callbacks.PlayerCallbackInterface;
import musicplayer.model.Song; import musicplayer.model.Song;
import org.gstreamer.ClockTime; import org.gstreamer.ClockTime;
@ -11,7 +13,7 @@ import org.gstreamer.elements.PlayBin2;
import java.io.File; import java.io.File;
import java.util.Optional; import java.util.Optional;
class Player { public class GStreamerPlayer implements IPlayer{
private final PlayBin2 playBin; private final PlayBin2 playBin;
@ -25,7 +27,8 @@ class Player {
* Manages GStreamer based playback operations. * Manages GStreamer based playback operations.
* @param callbackInterface Interface on which UI updates can be called. * @param callbackInterface Interface on which UI updates can be called.
*/ */
public Player(PlayerCallbackInterface callbackInterface) { @Inject
public GStreamerPlayer(PlayerCallbackInterface callbackInterface) {
this.callbackInterface = callbackInterface; this.callbackInterface = callbackInterface;
Gst.init(); Gst.init();
playBin = new PlayBin2("BusMessages"); playBin = new PlayBin2("BusMessages");

View File

@ -0,0 +1,19 @@
package musicplayer.player;
import musicplayer.StartPlayingException;
import musicplayer.model.Song;
import java.util.Optional;
public interface IPlayer {
void playSong(Optional<Song> 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();
}