Adds a context menu to the songs in the queue.

This allows for finer control over song order (in that it lets us remove songs).
This commit is contained in:
Markil3
2021-08-09 21:36:58 -06:00
parent 52262b5668
commit d76105d7bf

View File

@@ -17,6 +17,7 @@ import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import java.util.Formatter;
public class QueueList extends JPanel implements Queue.SongChangeListener, Queue.QueueChangeListener
@@ -46,9 +47,7 @@ public class QueueList extends JPanel implements Queue.SongChangeListener, Queue
c.gridwidth = 1;
header.add(new JLabel(" ".repeat(20) + "Queue" + " ".repeat(20)), c);
this.clearButton = new JButton("Clear");
this.clearButton.addActionListener(e -> {
Queue.getInstance().clear();
});
this.clearButton.addActionListener(e -> Queue.getInstance().clear());
c.gridy = 1;
c.gridwidth = 1;
header.add(clearButton, c);
@@ -163,14 +162,14 @@ public class QueueList extends JPanel implements Queue.SongChangeListener, Queue
public void onQueueChange(Queue queue)
{
Formatter dateForm;
JButton songLabel;
JLabel durationLabel;
GridBagConstraints c;
this.songList.removeAll();
int i = 0;
for (Song song : queue)
{
songLabel = new JButton(song.title);
SongMenu menu = new SongMenu(song, queue);
JButton songLabel = new JButton(song.title);
songLabel.setFocusPainted(true);
songLabel.setMargin(new Insets(0, 0, 0, 0));
songLabel.setContentAreaFilled(false);
@@ -179,10 +178,17 @@ public class QueueList extends JPanel implements Queue.SongChangeListener, Queue
songLabel.setHorizontalAlignment(JLabel.LEFT);
int songIndex = i;
songLabel.addMouseListener((ClickListener) e -> {
if (e.getButton() == MouseEvent.BUTTON1)
{
if (e.getClickCount() == 2)
{
queue.skipToSong(songIndex);
}
}
else if (e.getButton() == MouseEvent.BUTTON3)
{
menu.show(songLabel, e.getX(), e.getY());
}
});
dateForm = new Formatter();
durationLabel = new JLabel(dateForm.format("%1$tM:%1$tS", song.duration).toString());
@@ -221,4 +227,19 @@ public class QueueList extends JPanel implements Queue.SongChangeListener, Queue
}
this.validate();
}
private class SongMenu extends JPopupMenu
{
public SongMenu(Song song, Queue queue)
{
final JMenuItem play = new JMenuItem("Play");
final JMenuItem remove = new JMenuItem("Remove");
play.addActionListener(e -> queue.skipToSong(queue.indexOf(song)));
remove.addActionListener(e -> queue.remove(song));
this.add(play);
this.add(remove);
}
}
}