Rewrote UI generation code.

This commit is contained in:
neviyn 2016-02-24 22:52:09 +00:00
parent 69c9e442fb
commit 35649b5d60
3 changed files with 145 additions and 351 deletions

View File

@ -80,11 +80,6 @@
<artifactId>gstreamer-java</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>com.intellij</groupId>
<artifactId>forms_rt</artifactId>
<version>7.0.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -1,150 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="musicplayer.PlayerGUI">
<grid id="27dc6" binding="mainPanel" layout-manager="GridBagLayout">
<constraints>
<xy x="20" y="20" width="537" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<splitpane id="79ccb">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="760" height="500"/>
</grid>
<gridbag weightx="1.0" weighty="1.0"/>
</constraints>
<properties>
<dividerLocation value="240"/>
</properties>
<border type="none"/>
<children>
<scrollpane id="359a1" binding="playlistScroll">
<constraints>
<splitpane position="right"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="c10f" class="javax.swing.JTable" binding="playList">
<constraints/>
<properties>
<rowSelectionAllowed value="true"/>
</properties>
</component>
</children>
</scrollpane>
<grid id="1f7af" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<splitpane position="left"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="c14a2" class="javax.swing.JComboBox" binding="libraryDisplayType">
<constraints border-constraint="North"/>
<properties/>
</component>
<scrollpane id="2b209">
<constraints border-constraint="Center"/>
<properties/>
<border type="none"/>
<children>
<component id="6b9fd" class="javax.swing.JTree" binding="libraryView">
<constraints/>
<properties>
<rootVisible value="true"/>
<showsRootHandles value="false"/>
</properties>
</component>
</children>
</scrollpane>
</children>
</grid>
</children>
</splitpane>
<toolbar id="56df">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="20"/>
</grid>
<gridbag weightx="1.0" weighty="0.0"/>
</constraints>
<properties>
<floatable value="false"/>
</properties>
<border type="none"/>
<children>
<component id="a01e9" class="javax.swing.JButton" binding="playButton">
<constraints/>
<properties>
<label value="Play"/>
<text value="&amp;Play"/>
</properties>
</component>
<component id="c0e85" class="javax.swing.JButton" binding="stopButton" default-binding="true">
<constraints/>
<properties>
<label value="Stop"/>
<text value="&amp;Stop"/>
</properties>
</component>
<component id="5cfc3" class="javax.swing.JButton" binding="nextButton">
<constraints/>
<properties>
<text value="&amp;Next"/>
</properties>
</component>
<grid id="9f75c" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints/>
<properties/>
<border type="none"/>
<children>
<component id="c8ecb" class="javax.swing.JSlider" binding="volumeSlider">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<majorTickSpacing value="20"/>
<minorTickSpacing value="10"/>
<paintTicks value="true"/>
<value value="100"/>
</properties>
</component>
<component id="9424d" class="javax.swing.JLabel" binding="volumeValue">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="100%"/>
</properties>
</component>
</children>
</grid>
</children>
</toolbar>
<component id="e1626" class="javax.swing.JMenuBar" binding="menuBar">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="0.0"/>
</constraints>
<properties/>
</component>
<component id="98c55" class="javax.swing.JSlider" binding="seekBar">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
<gridbag weightx="1.0" weighty="0.0"/>
</constraints>
<properties>
<inverted value="false"/>
<minorTickSpacing value="1"/>
<paintTicks value="false"/>
<snapToTicks value="false"/>
<value value="0"/>
<valueIsAdjusting value="false"/>
</properties>
</component>
</children>
</grid>
</form>

View File

@ -1,7 +1,5 @@
package musicplayer;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import musicplayer.callbacks.LibraryCallbackInterface;
import musicplayer.callbacks.PlayerCallbackInterface;
import musicplayer.db.DatabaseManager;
@ -14,48 +12,28 @@ import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.List;
public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterface {
public JPanel mainPanel;
private JTree libraryView;
private JTable playList;
private JPanel mainPanel;
private JButton playButton;
private JButton stopButton;
private JButton nextButton;
private JTable playList = new JTable();
private JSlider seekBar;
private JSlider volumeSlider;
private JMenuBar menuBar;
private JComboBox libraryDisplayType;
private JLabel volumeValue;
private JScrollPane playlistScroll;
private JComboBox libraryDisplayType = new JComboBox();
private Player player = new Player(this);
private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>());
private Map<String, Runnable> libraryDisplayVariants = createDisplayVariantMap();
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setContentPane(new PlayerGUI().mainPanel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public PlayerGUI() {
createUI();
DatabaseManager.init();
libraryView.setRootVisible(false);
libraryView.setToggleClickCount(1);
libraryView.setCellRenderer(new LibraryTreeCellRenderer());
playList.setModel(playlistTableModel);
resetTree();
libraryDisplayVariants.keySet().forEach(libraryDisplayType::addItem);
volumeSlider.setValue(player.getVolume());
setVolumeValue(player.getVolume());
Thread seekBarUpdater = new Thread(() -> {
boolean running = true;
while (running) {
@ -68,34 +46,19 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
}
});
seekBarUpdater.start();
//Action Listeners
libraryDisplayType.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED)
libraryDisplayVariants.get(libraryDisplayType.getSelectedItem().toString()).run();
});
playButton.addActionListener(e -> {
if (playList.getRowCount() > 0) {
if (playList.getSelectedRowCount() > 0)
player.playSong(playlistTableModel.getSong(playList.getSelectedRow()));
else
player.playSong(playlistTableModel.getFirst());
}
});
MouseListener mouseListener = new libraryMouseAdapter();
libraryView.addMouseListener(mouseListener);
stopButton.addActionListener(e -> player.stop());
nextButton.addActionListener(e -> playNextSong());
volumeSlider.addChangeListener(e -> {
player.setVolume(((JSlider) e.getSource()).getValue());
setVolumeValue(player.getVolume());
});
playList.setComponentPopupMenu(createPlaylistPopup());
playlistScroll.setComponentPopupMenu(createPlaylistEmptyAreaPopup());
populateMenuBar();
createMenuBar();
refreshLibrary();
}
public static void main(String[] args) {
PlayerGUI playerGUI = new PlayerGUI();
JFrame frame = new JFrame();
frame.setContentPane(playerGUI.mainPanel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
/**
* Set the value of seekBar.
*
@ -224,120 +187,159 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
@Override
public void libraryUpdated(boolean successful) {
if (successful)
populateLibrary(new TreeMap<>(Gateway.listAllSongsGroupedByAlbum().get()));
libraryDisplayVariants.get(libraryDisplayType.getSelectedItem().toString()).run();
}
{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
$$$setupUI$$$();
private Map<String, Runnable> createDisplayVariantMap() {
Map<String, Runnable> value = new HashMap<>();
value.put("Song", () -> Gateway.listAllSongs().ifPresent(this::populateLibrary));
value.put("Album/Song", () -> Gateway.listAllSongsGroupedByAlbum().ifPresent(this::populateLibrary));
return value;
}
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* @noinspection ALL
/*
mainPanel
+--------------------+
|menuBar |
+---------+----------+
|comboBox |playlist |
+---------+ |
|library | |
| | |
+---------+----------+
|controlPane |
+--------------------+
*/
private void $$$setupUI$$$() {
private void createUI() {
mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
final JSplitPane splitPane1 = new JSplitPane();
splitPane1.setDividerLocation(240);
GridBagConstraints gbc;
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(splitPane1, gbc);
playlistScroll = new JScrollPane();
splitPane1.setRightComponent(playlistScroll);
playList = new JTable();
playList.setRowSelectionAllowed(true);
playlistScroll.setViewportView(playList);
final JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout(0, 0));
splitPane1.setLeftComponent(panel1);
libraryDisplayType = new JComboBox();
panel1.add(libraryDisplayType, BorderLayout.NORTH);
final JScrollPane scrollPane1 = new JScrollPane();
panel1.add(scrollPane1, BorderLayout.CENTER);
mainPanel.setLayout(new BorderLayout(0, 0));
final JSplitPane libraryAndPlaylistPane = new JSplitPane();
libraryAndPlaylistPane.setDividerLocation(240);
libraryAndPlaylistPane.setRightComponent(createPlaylistArea());
mainPanel.add(libraryAndPlaylistPane, BorderLayout.CENTER);
final JPanel libraryAndComboboxPane = new JPanel();
libraryAndComboboxPane.setLayout(new BorderLayout(0, 0));
libraryAndPlaylistPane.setLeftComponent(libraryAndComboboxPane);
libraryDisplayType.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED)
libraryDisplayVariants.get(libraryDisplayType.getSelectedItem().toString()).run();
});
libraryAndComboboxPane.add(libraryDisplayType, BorderLayout.NORTH);
final JScrollPane libraryPane = new JScrollPane();
libraryAndComboboxPane.add(libraryPane, BorderLayout.CENTER);
libraryPane.setViewportView(createLibraryArea());
final JPanel controlPane = new JPanel();
controlPane.setLayout(new BorderLayout(0, 0));
mainPanel.add(controlPane, BorderLayout.SOUTH);
seekBar = new JSlider();
seekBar.setMinorTickSpacing(1);
seekBar.setValue(0);
controlPane.add(seekBar, BorderLayout.NORTH);
controlPane.add(createControlButtons(), BorderLayout.SOUTH);
mainPanel.add(createMenuBar(), BorderLayout.NORTH);
}
private JTree createLibraryArea() {
libraryView = new JTree();
libraryView.setRootVisible(true);
libraryView.setShowsRootHandles(false);
scrollPane1.setViewportView(libraryView);
final JToolBar toolBar1 = new JToolBar();
toolBar1.setFloatable(false);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 3;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(toolBar1, gbc);
playButton = new JButton();
playButton.setLabel("Play");
libraryView.setRootVisible(false);
libraryView.setToggleClickCount(1);
libraryView.setCellRenderer(new LibraryTreeCellRenderer());
libraryView.addMouseListener(new LibraryMouseAdapter());
return libraryView;
}
private JScrollPane createPlaylistArea() {
JScrollPane playlistScroll = new JScrollPane();
playList.setRowSelectionAllowed(true);
playList.setModel(playlistTableModel);
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Remove");
menuItem.addActionListener((e) -> playlistTableModel.removeSong(playList.getSelectedRows()));
popupMenu.add(menuItem);
playList.setComponentPopupMenu(popupMenu);
popupMenu = new JPopupMenu();
menuItem = new JMenuItem("Clear all");
menuItem.addActionListener((e) -> playlistTableModel.removeAll());
popupMenu.add(menuItem);
playlistScroll.setComponentPopupMenu(popupMenu);
playlistScroll.setViewportView(playList);
return playlistScroll;
}
private JToolBar createControlButtons() {
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
JButton playButton = new JButton();
playButton.setText("Play");
playButton.setMnemonic('P');
playButton.setDisplayedMnemonicIndex(0);
toolBar1.add(playButton);
stopButton = new JButton();
stopButton.setLabel("Stop");
playButton.addActionListener(e -> {
if (playList.getRowCount() > 0) {
if (playList.getSelectedRowCount() > 0)
player.playSong(playlistTableModel.getSong(playList.getSelectedRow()));
else
player.playSong(playlistTableModel.getFirst());
}
});
toolBar.add(playButton);
JButton stopButton = new JButton();
stopButton.setText("Stop");
stopButton.setMnemonic('S');
stopButton.setDisplayedMnemonicIndex(0);
toolBar1.add(stopButton);
nextButton = new JButton();
stopButton.addActionListener(e -> player.stop());
toolBar.add(stopButton);
JButton nextButton = new JButton();
nextButton.setText("Next");
nextButton.setMnemonic('N');
nextButton.setDisplayedMnemonicIndex(0);
toolBar1.add(nextButton);
final JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
toolBar1.add(panel2);
volumeSlider = new JSlider();
nextButton.addActionListener(e -> playNextSong());
toolBar.add(nextButton);
toolBar.add(createVolumeControls());
return toolBar;
}
private JPanel createVolumeControls() {
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JSlider volumeSlider = new JSlider();
volumeSlider.setMajorTickSpacing(20);
volumeSlider.setMinorTickSpacing(10);
volumeSlider.setPaintTicks(true);
volumeSlider.setValue(100);
panel2.add(volumeSlider, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
volumeValue = new JLabel();
volumeValue.setText("100%");
panel2.add(volumeValue, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
menuBar = new JMenuBar();
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.BOTH;
mainPanel.add(menuBar, gbc);
seekBar = new JSlider();
seekBar.setInverted(false);
seekBar.setMinorTickSpacing(1);
seekBar.setPaintTicks(false);
seekBar.setSnapToTicks(false);
seekBar.setValue(0);
seekBar.setValueIsAdjusting(false);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weightx = 1.0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(seekBar, gbc);
volumeSlider.setValue(player.getVolume());
panel.add(volumeSlider);
JLabel volumeValue = new JLabel();
volumeValue.setText(String.format("%d%%", player.getVolume()));
volumeSlider.addChangeListener(e -> {
player.setVolume(((JSlider) e.getSource()).getValue());
volumeValue.setText(String.format("%d%%", player.getVolume()));
});
panel.add(volumeValue);
return panel;
}
/**
* @noinspection ALL
* Populate the top menu bar with items.
*/
public JComponent $$$getRootComponent$$$() {
return mainPanel;
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
// Tools menu
JMenu tools = new JMenu("Tools");
JMenuItem menuItem = new JMenuItem("Refresh Library");
menuItem.addActionListener(e -> refreshLibrary());
tools.add(menuItem);
menuItem = new JMenuItem("Update Library");
menuItem.setToolTipText("Reindex and refresh library.");
tools.add(menuItem);
// Add everything to the menu bar itself
menuBar.add(tools);
return menuBar;
}
private class libraryMouseAdapter extends MouseAdapter {
private class LibraryMouseAdapter extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
int selRow = libraryView.getRowForLocation(e.getX(), e.getY());
@ -358,57 +360,4 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
}
}
}
/**
* Populate the top menu bar with items.
*/
private void populateMenuBar() {
// Tools menu
JMenu tools = new JMenu("Tools");
JMenuItem menuItem = new JMenuItem("Refresh Library");
menuItem.addActionListener(e -> refreshLibrary());
tools.add(menuItem);
menuItem = new JMenuItem("Update Library");
menuItem.setToolTipText("Reindex and refresh library.");
tools.add(menuItem);
// Add everything to the menu bar itself
menuBar.add(tools);
}
private JPopupMenu createPlaylistPopup() {
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Remove");
menuItem.addActionListener((e) -> playlistTableModel.removeSong(playList.getSelectedRows()));
popupMenu.add(menuItem);
return popupMenu;
}
private JPopupMenu createPlaylistEmptyAreaPopup() {
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Clear all");
menuItem.addActionListener((e) -> playlistTableModel.removeAll());
popupMenu.add(menuItem);
return popupMenu;
}
private Map<String, Runnable> createDisplayVariantMap() {
Map<String, Runnable> value = new HashMap<>();
value.put("Song", () -> Gateway.listAllSongs().ifPresent(this::populateLibrary));
value.put("Album/Song", () -> Gateway.listAllSongsGroupedByAlbum().ifPresent(this::populateLibrary));
return value;
}
private void setVolumeValue(int value) {
volumeValue.setText(String.format("%d%%", value));
}
}