From 55f879fedd5609d778fcb65d1278f11b94168325 Mon Sep 17 00:00:00 2001 From: Markil3 <75867393+Markil3@users.noreply.github.com> Date: Tue, 14 Sep 2021 17:02:18 -0600 Subject: [PATCH] Almost completely separates the GUI from the logic. This will hopefully make debugging easier as well as make a CLI possible. --- interface/build.gradle | 2 +- .../universeplayer/PlayerEnvironment.java | 115 ++++++++++++++++++ .../regis/universeplayer/gui/Interface.java | 32 +---- .../universeplayer/gui/PlayerControls.java | 75 ++---------- 4 files changed, 129 insertions(+), 95 deletions(-) create mode 100644 interface/src/main/java/edu/regis/universeplayer/PlayerEnvironment.java diff --git a/interface/build.gradle b/interface/build.gradle index 3687b87..ea3cca4 100644 --- a/interface/build.gradle +++ b/interface/build.gradle @@ -44,5 +44,5 @@ sourceCompatibility = JavaVersion.VERSION_16 targetCompatibility = JavaVersion.VERSION_16 // Define the main class for the application -mainClassName = defaultPackage + '.gui.Interface' +mainClassName = defaultPackage + '.PlayerEnvironment' //mainClassName = defaultPackage + '.localPlayer.Player' diff --git a/interface/src/main/java/edu/regis/universeplayer/PlayerEnvironment.java b/interface/src/main/java/edu/regis/universeplayer/PlayerEnvironment.java new file mode 100644 index 0000000..bf8609a --- /dev/null +++ b/interface/src/main/java/edu/regis/universeplayer/PlayerEnvironment.java @@ -0,0 +1,115 @@ +package edu.regis.universeplayer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Locale; +import java.util.ResourceBundle; +import java.util.concurrent.ExecutionException; + +import javax.swing.JOptionPane; + +import edu.regis.universeplayer.browserCommands.QueryFuture; +import edu.regis.universeplayer.data.Queue; +import edu.regis.universeplayer.data.SongProvider; +import edu.regis.universeplayer.gui.Interface; +import edu.regis.universeplayer.player.PlayerManager; + +/** + * A centralized spot to link up all of the components. + * + * @author William Hubbard + * @version 0.1 + */ +public class PlayerEnvironment +{ + private static final Logger logger = LoggerFactory + .getLogger(PlayerEnvironment.class); + private static final ResourceBundle langs = ResourceBundle + .getBundle("lang.interface", Locale.getDefault()); + + /** + * Initializes the various components, setting up listeners as needed. + */ + public static void main(String[] args) + { + /* + * Add this just in case of a crash or something. It won't work if the + * program is forcibly terminated by the OS, but it could be helpful + * otherwise. + */ + logger.info("Starting application"); + + Queue queue = Queue.getInstance(); + PlayerManager playback = PlayerManager.getPlayers(); + queue.addSongChangeListener(queue1 -> { + QueryFuture command = PlayerManager.getPlayers() + .stopSong(); + try + { + if (command != null && !command.getConfirmation() + .wasSuccessful()) + { + if (Interface.getInstance() != null) + { + JOptionPane + .showMessageDialog(Interface.getInstance(), + command.getConfirmation() + .getError(), + command.getConfirmation() + .getMessage(), + JOptionPane.ERROR_MESSAGE); + } + logger.error("Could not run command", + command.getConfirmation().getError()); + } + else + { + command = + PlayerManager.getPlayers() + .playSong(queue1.getCurrentSong()); + if (!command.getConfirmation().wasSuccessful()) + { + if (Interface.getInstance() != null) + { + JOptionPane.showMessageDialog(Interface + .getInstance(), + command.getConfirmation().getError(), + command.getConfirmation().getMessage(), + JOptionPane.ERROR_MESSAGE); + } + logger.error("Could not run command", + command.getConfirmation().getError()); + } + else + { + } + } + } + catch (ExecutionException | InterruptedException e) + { + logger.error("Could not get current playback status", e); + if (Interface.getInstance() != null) + { + JOptionPane + .showMessageDialog(Interface.getInstance(), e + .getMessage() + , langs + .getString( + "error.command"), JOptionPane.ERROR_MESSAGE); + } + } + }); + playback.addPlaybackListener(status -> { + switch (status.getInfo().getStatus()) + { + case FINISHED -> Queue.getInstance().skipNext(); + } + }); + + Interface inter = new Interface(); + inter.setSize(700, 500); + SongProvider.INSTANCE.addUpdateListener(inter); + inter.setVisible(true); + } +} diff --git a/interface/src/main/java/edu/regis/universeplayer/gui/Interface.java b/interface/src/main/java/edu/regis/universeplayer/gui/Interface.java index 9cbbc3b..7ccaa8e 100644 --- a/interface/src/main/java/edu/regis/universeplayer/gui/Interface.java +++ b/interface/src/main/java/edu/regis/universeplayer/gui/Interface.java @@ -82,34 +82,6 @@ public class Interface extends JFrame implements SongDisplayListener, ComponentL return INSTANCE; } - public static void main(String[] args) - { - Interface inter = null; - PlayerManager.getPlayers(); - try - { - /* - * Add this just in case of a crash or something. It won't work if the - * program is forcibly terminated by the OS, but it could be helpful - * otherwise. - */ - logger.info("Starting application"); - - inter = new Interface(); - inter.setSize(700, 500); - SongProvider.INSTANCE.addUpdateListener(inter); - inter.setVisible(true); - } - catch (Throwable e) - { - logger.error("Could not open browser background", e); - JOptionPane - .showMessageDialog(inter != null && inter - .isVisible() ? inter : null, e, langs - .getString("error.generic"), JOptionPane.ERROR_MESSAGE); - } - } - /** * Creates an interface */ @@ -469,7 +441,7 @@ public class Interface extends JFrame implements SongDisplayListener, ComponentL { if (this.isEnabled()) { - controls.previousSong(); + Queue.getInstance().skipPrev(); } } }); @@ -482,7 +454,7 @@ public class Interface extends JFrame implements SongDisplayListener, ComponentL { if (this.isEnabled()) { - controls.nextSong(); + Queue.getInstance().skipNext(); } } }); diff --git a/interface/src/main/java/edu/regis/universeplayer/gui/PlayerControls.java b/interface/src/main/java/edu/regis/universeplayer/gui/PlayerControls.java index 0da82f2..85736d8 100644 --- a/interface/src/main/java/edu/regis/universeplayer/gui/PlayerControls.java +++ b/interface/src/main/java/edu/regis/universeplayer/gui/PlayerControls.java @@ -37,7 +37,7 @@ import java.util.concurrent.ForkJoinPool; * @author William Hubbard * @version 0.1 */ -public class PlayerControls extends JPanel implements Queue.SongChangeListener, PlaybackListener +public class PlayerControls extends JPanel implements PlaybackListener { private static final Logger logger = LoggerFactory .getLogger(PlayerControls.class); @@ -175,7 +175,6 @@ public class PlayerControls extends JPanel implements Queue.SongChangeListener, layout.putConstraint(SpringLayout.EAST, this, 5, SpringLayout.EAST, this.updateProgress); layout.putConstraint(SpringLayout.SOUTH, this, 5, SpringLayout.SOUTH, this.updateProgress); - Queue.getInstance().addSongChangeListener(this); PlayerManager.getPlayers().addPlaybackListener(this); } @@ -436,70 +435,18 @@ public class PlayerControls extends JPanel implements Queue.SongChangeListener, } } - @Override - public void onSongChange(Queue queue) - { - this.service.execute(() -> { - QueryFuture command = PlayerManager.getPlayers().stopSong(); - try - { - if (command != null && !command.getConfirmation().wasSuccessful()) - { - JOptionPane.showMessageDialog(this, - command.getConfirmation().getError(), - command.getConfirmation().getMessage(), - JOptionPane.ERROR_MESSAGE); - logger.error("Could not run command", - command.getConfirmation().getError()); - } - else - { - command = - PlayerManager.getPlayers() - .playSong(queue.getCurrentSong()); - if (!command.getConfirmation().wasSuccessful()) - { - JOptionPane.showMessageDialog(this, - command.getConfirmation().getError(), - command.getConfirmation().getMessage(), - JOptionPane.ERROR_MESSAGE); - logger.error("Could not run command", - command.getConfirmation().getError()); - } - else - { - SwingUtilities.invokeLater(() -> { - this.playButton.setIcon(PLAY_ICON); - this.progress.setMaximum((int) (PlayerManager - .getPlayers() - .getCurrentSong().duration / 1000)); - }); - - } - } - } - catch (ExecutionException | InterruptedException e) - { - logger.error("Could not get current playback status", e); - JOptionPane - .showMessageDialog(this, e.getMessage(), langs - .getString( - "error.command"), JOptionPane.ERROR_MESSAGE); - } - }); - } - @Override public void onPlaybackChanged(PlaybackEvent status) { - switch (status.getInfo().getStatus()) - { - case PLAYING -> this.playButton.setIcon(PAUSE_ICON); - case FINISHED -> Queue.getInstance().skipNext(); - case PAUSED, STOPPED, EMPTY -> this.playButton.setIcon(PLAY_ICON); - } - this.progress.setValue((int) status.getInfo().getPlayTime()); - this.progress.setMaximum((int) (status.getInfo() - .getSong().duration / 1000)); + SwingUtilities.invokeLater(() -> { + switch (status.getInfo().getStatus()) + { + case PLAYING -> this.playButton.setIcon(PAUSE_ICON); + case PAUSED, STOPPED, EMPTY -> this.playButton.setIcon(PLAY_ICON); + } + this.progress.setValue((int) status.getInfo().getPlayTime()); + this.progress.setMaximum((int) (status.getInfo() + .getSong().duration / 1000)); + }); } }