Almost completely separates the GUI from the logic.

This will hopefully make debugging easier as well as make a CLI possible.
This commit is contained in:
Markil3
2021-09-14 17:02:18 -06:00
parent 999f3345cc
commit 55f879fedd
4 changed files with 129 additions and 95 deletions

View File

@@ -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'

View File

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

View File

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

View File

@@ -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<Void> 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)
{
SwingUtilities.invokeLater(() -> {
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));
});
}
}