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.*;
import java.awt.event.FocusAdapter; import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent; import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import java.util.Formatter; import java.util.Formatter;
public class QueueList extends JPanel implements Queue.SongChangeListener, Queue.QueueChangeListener 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; c.gridwidth = 1;
header.add(new JLabel(" ".repeat(20) + "Queue" + " ".repeat(20)), c); header.add(new JLabel(" ".repeat(20) + "Queue" + " ".repeat(20)), c);
this.clearButton = new JButton("Clear"); this.clearButton = new JButton("Clear");
this.clearButton.addActionListener(e -> { this.clearButton.addActionListener(e -> Queue.getInstance().clear());
Queue.getInstance().clear();
});
c.gridy = 1; c.gridy = 1;
c.gridwidth = 1; c.gridwidth = 1;
header.add(clearButton, c); header.add(clearButton, c);
@@ -163,14 +162,14 @@ public class QueueList extends JPanel implements Queue.SongChangeListener, Queue
public void onQueueChange(Queue queue) public void onQueueChange(Queue queue)
{ {
Formatter dateForm; Formatter dateForm;
JButton songLabel;
JLabel durationLabel; JLabel durationLabel;
GridBagConstraints c; GridBagConstraints c;
this.songList.removeAll(); this.songList.removeAll();
int i = 0; int i = 0;
for (Song song : queue) 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.setFocusPainted(true);
songLabel.setMargin(new Insets(0, 0, 0, 0)); songLabel.setMargin(new Insets(0, 0, 0, 0));
songLabel.setContentAreaFilled(false); songLabel.setContentAreaFilled(false);
@@ -179,9 +178,16 @@ public class QueueList extends JPanel implements Queue.SongChangeListener, Queue
songLabel.setHorizontalAlignment(JLabel.LEFT); songLabel.setHorizontalAlignment(JLabel.LEFT);
int songIndex = i; int songIndex = i;
songLabel.addMouseListener((ClickListener) e -> { songLabel.addMouseListener((ClickListener) e -> {
if (e.getClickCount() == 2) if (e.getButton() == MouseEvent.BUTTON1)
{ {
queue.skipToSong(songIndex); if (e.getClickCount() == 2)
{
queue.skipToSong(songIndex);
}
}
else if (e.getButton() == MouseEvent.BUTTON3)
{
menu.show(songLabel, e.getX(), e.getY());
} }
}); });
dateForm = new Formatter(); dateForm = new Formatter();
@@ -221,4 +227,19 @@ public class QueueList extends JPanel implements Queue.SongChangeListener, Queue
} }
this.validate(); 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);
}
}
} }