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> <artifactId>gstreamer-java</artifactId>
<version>1.5</version> <version>1.5</version>
</dependency> </dependency>
<dependency>
<groupId>com.intellij</groupId>
<artifactId>forms_rt</artifactId>
<version>7.0.3</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <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; package musicplayer;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import musicplayer.callbacks.LibraryCallbackInterface; import musicplayer.callbacks.LibraryCallbackInterface;
import musicplayer.callbacks.PlayerCallbackInterface; import musicplayer.callbacks.PlayerCallbackInterface;
import musicplayer.db.DatabaseManager; import musicplayer.db.DatabaseManager;
@ -14,48 +12,28 @@ import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode; import javax.swing.tree.TreeNode;
import java.awt.*; 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.*;
import java.util.List; import java.util.List;
public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterface { public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterface {
public JPanel mainPanel;
private JTree libraryView; private JTree libraryView;
private JTable playList; private JTable playList = new JTable();
private JPanel mainPanel;
private JButton playButton;
private JButton stopButton;
private JButton nextButton;
private JSlider seekBar; private JSlider seekBar;
private JSlider volumeSlider; private JComboBox libraryDisplayType = new JComboBox();
private JMenuBar menuBar;
private JComboBox libraryDisplayType;
private JLabel volumeValue;
private JScrollPane playlistScroll;
private Player player = new Player(this); private Player player = new Player(this);
private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>()); private PlaylistTableModel playlistTableModel = new PlaylistTableModel(new ArrayList<>());
private Map<String, Runnable> libraryDisplayVariants = createDisplayVariantMap(); 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() { public PlayerGUI() {
createUI();
DatabaseManager.init(); DatabaseManager.init();
libraryView.setRootVisible(false);
libraryView.setToggleClickCount(1);
libraryView.setCellRenderer(new LibraryTreeCellRenderer());
playList.setModel(playlistTableModel);
resetTree(); resetTree();
libraryDisplayVariants.keySet().forEach(libraryDisplayType::addItem); libraryDisplayVariants.keySet().forEach(libraryDisplayType::addItem);
volumeSlider.setValue(player.getVolume());
setVolumeValue(player.getVolume());
Thread seekBarUpdater = new Thread(() -> { Thread seekBarUpdater = new Thread(() -> {
boolean running = true; boolean running = true;
while (running) { while (running) {
@ -68,34 +46,19 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
} }
}); });
seekBarUpdater.start(); seekBarUpdater.start();
createMenuBar();
//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();
refreshLibrary(); 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. * Set the value of seekBar.
* *
@ -224,120 +187,159 @@ public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterf
@Override @Override
public void libraryUpdated(boolean successful) { public void libraryUpdated(boolean successful) {
if (successful) if (successful)
populateLibrary(new TreeMap<>(Gateway.listAllSongsGroupedByAlbum().get())); libraryDisplayVariants.get(libraryDisplayType.getSelectedItem().toString()).run();
} }
{ private Map<String, Runnable> createDisplayVariantMap() {
// GUI initializer generated by IntelliJ IDEA GUI Designer Map<String, Runnable> value = new HashMap<>();
// >>> IMPORTANT!! <<< value.put("Song", () -> Gateway.listAllSongs().ifPresent(this::populateLibrary));
// DO NOT EDIT OR ADD ANY CODE HERE! value.put("Album/Song", () -> Gateway.listAllSongsGroupedByAlbum().ifPresent(this::populateLibrary));
$$$setupUI$$$();
return value;
} }
/** /*
* Method generated by IntelliJ IDEA GUI Designer mainPanel
* >>> IMPORTANT!! <<< +--------------------+
* DO NOT edit this method OR call it in your code! |menuBar |
* +---------+----------+
* @noinspection ALL |comboBox |playlist |
+---------+ |
|library | |
| | |
+---------+----------+
|controlPane |
+--------------------+
*/ */
private void $$$setupUI$$$() { private void createUI() {
mainPanel = new JPanel(); mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout()); mainPanel.setLayout(new BorderLayout(0, 0));
final JSplitPane splitPane1 = new JSplitPane(); final JSplitPane libraryAndPlaylistPane = new JSplitPane();
splitPane1.setDividerLocation(240); libraryAndPlaylistPane.setDividerLocation(240);
GridBagConstraints gbc; libraryAndPlaylistPane.setRightComponent(createPlaylistArea());
gbc = new GridBagConstraints(); mainPanel.add(libraryAndPlaylistPane, BorderLayout.CENTER);
gbc.gridx = 0; final JPanel libraryAndComboboxPane = new JPanel();
gbc.gridy = 1; libraryAndComboboxPane.setLayout(new BorderLayout(0, 0));
gbc.weightx = 1.0; libraryAndPlaylistPane.setLeftComponent(libraryAndComboboxPane);
gbc.weighty = 1.0; libraryDisplayType.addItemListener(e -> {
gbc.fill = GridBagConstraints.BOTH; if (e.getStateChange() == ItemEvent.SELECTED)
mainPanel.add(splitPane1, gbc); libraryDisplayVariants.get(libraryDisplayType.getSelectedItem().toString()).run();
playlistScroll = new JScrollPane(); });
splitPane1.setRightComponent(playlistScroll); libraryAndComboboxPane.add(libraryDisplayType, BorderLayout.NORTH);
playList = new JTable(); final JScrollPane libraryPane = new JScrollPane();
playList.setRowSelectionAllowed(true); libraryAndComboboxPane.add(libraryPane, BorderLayout.CENTER);
playlistScroll.setViewportView(playList); libraryPane.setViewportView(createLibraryArea());
final JPanel panel1 = new JPanel(); final JPanel controlPane = new JPanel();
panel1.setLayout(new BorderLayout(0, 0)); controlPane.setLayout(new BorderLayout(0, 0));
splitPane1.setLeftComponent(panel1); mainPanel.add(controlPane, BorderLayout.SOUTH);
libraryDisplayType = new JComboBox(); seekBar = new JSlider();
panel1.add(libraryDisplayType, BorderLayout.NORTH); seekBar.setMinorTickSpacing(1);
final JScrollPane scrollPane1 = new JScrollPane(); seekBar.setValue(0);
panel1.add(scrollPane1, BorderLayout.CENTER); controlPane.add(seekBar, BorderLayout.NORTH);
controlPane.add(createControlButtons(), BorderLayout.SOUTH);
mainPanel.add(createMenuBar(), BorderLayout.NORTH);
}
private JTree createLibraryArea() {
libraryView = new JTree(); libraryView = new JTree();
libraryView.setRootVisible(true); libraryView.setRootVisible(false);
libraryView.setShowsRootHandles(false); libraryView.setToggleClickCount(1);
scrollPane1.setViewportView(libraryView); libraryView.setCellRenderer(new LibraryTreeCellRenderer());
final JToolBar toolBar1 = new JToolBar(); libraryView.addMouseListener(new LibraryMouseAdapter());
toolBar1.setFloatable(false); return libraryView;
gbc = new GridBagConstraints(); }
gbc.gridx = 0;
gbc.gridy = 3; private JScrollPane createPlaylistArea() {
gbc.weightx = 1.0; JScrollPane playlistScroll = new JScrollPane();
gbc.fill = GridBagConstraints.HORIZONTAL; playList.setRowSelectionAllowed(true);
mainPanel.add(toolBar1, gbc); playList.setModel(playlistTableModel);
playButton = new JButton(); JPopupMenu popupMenu = new JPopupMenu();
playButton.setLabel("Play"); 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.setText("Play");
playButton.setMnemonic('P'); playButton.setMnemonic('P');
playButton.setDisplayedMnemonicIndex(0); playButton.setDisplayedMnemonicIndex(0);
toolBar1.add(playButton); playButton.addActionListener(e -> {
stopButton = new JButton(); if (playList.getRowCount() > 0) {
stopButton.setLabel("Stop"); 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.setText("Stop");
stopButton.setMnemonic('S'); stopButton.setMnemonic('S');
stopButton.setDisplayedMnemonicIndex(0); stopButton.setDisplayedMnemonicIndex(0);
toolBar1.add(stopButton); stopButton.addActionListener(e -> player.stop());
nextButton = new JButton(); toolBar.add(stopButton);
JButton nextButton = new JButton();
nextButton.setText("Next"); nextButton.setText("Next");
nextButton.setMnemonic('N'); nextButton.setMnemonic('N');
nextButton.setDisplayedMnemonicIndex(0); nextButton.setDisplayedMnemonicIndex(0);
toolBar1.add(nextButton); nextButton.addActionListener(e -> playNextSong());
final JPanel panel2 = new JPanel(); toolBar.add(nextButton);
panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1)); toolBar.add(createVolumeControls());
toolBar1.add(panel2); return toolBar;
volumeSlider = new JSlider(); }
private JPanel createVolumeControls() {
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JSlider volumeSlider = new JSlider();
volumeSlider.setMajorTickSpacing(20); volumeSlider.setMajorTickSpacing(20);
volumeSlider.setMinorTickSpacing(10); volumeSlider.setMinorTickSpacing(10);
volumeSlider.setPaintTicks(true); volumeSlider.setPaintTicks(true);
volumeSlider.setValue(100); volumeSlider.setValue(player.getVolume());
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)); panel.add(volumeSlider);
volumeValue = new JLabel(); JLabel volumeValue = new JLabel();
volumeValue.setText("100%"); volumeValue.setText(String.format("%d%%", player.getVolume()));
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)); volumeSlider.addChangeListener(e -> {
menuBar = new JMenuBar(); player.setVolume(((JSlider) e.getSource()).getValue());
gbc = new GridBagConstraints(); volumeValue.setText(String.format("%d%%", player.getVolume()));
gbc.gridx = 0; });
gbc.gridy = 0; panel.add(volumeValue);
gbc.weightx = 1.0; return panel;
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);
} }
/** /**
* @noinspection ALL * Populate the top menu bar with items.
*/ */
public JComponent $$$getRootComponent$$$() { private JMenuBar createMenuBar() {
return mainPanel; 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 @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
int selRow = libraryView.getRowForLocation(e.getX(), e.getY()); 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));
}
} }