Show metadata in main window.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / gui / ControlledPane.java
index 9a679f1..eb44bc7 100644 (file)
@@ -20,6 +20,7 @@ package net.pterodactylus.sonitus.gui;
 import static javax.swing.BorderFactory.createEtchedBorder;
 import static javax.swing.BorderFactory.createTitledBorder;
 
+import java.awt.Font;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.awt.Insets;
@@ -31,9 +32,14 @@ import javax.swing.JPanel;
 
 import net.pterodactylus.sonitus.data.Controlled;
 import net.pterodactylus.sonitus.data.Controller;
+import net.pterodactylus.sonitus.data.Metadata;
 import net.pterodactylus.sonitus.data.controller.Fader;
 import net.pterodactylus.sonitus.data.controller.Knob;
 import net.pterodactylus.sonitus.data.controller.Switch;
+import net.pterodactylus.sonitus.data.event.MetadataUpdated;
+
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
 
 /**
  * Panel that displays all {@link Controller}s of a {@link Controlled}
@@ -46,15 +52,20 @@ public class ControlledPane extends JPanel {
        /**
         * Creates a new controlled pane.
         *
+        * @param eventBus
+        *              The event bus
         * @param controlled
         *              The controlled whose controllers to display
         */
-       public ControlledPane(Controlled controlled) {
+       public ControlledPane(EventBus eventBus, Controlled controlled) {
                super(new GridBagLayout());
                setBorder(BorderFactory.createEmptyBorder(6, 12, 12, 12));
 
-               add(createControllerPanel(controlled), new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
-               add(Box.createVerticalGlue(), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(6, 0, 0, 0), 0, 0));
+               MetadataPanel metadataPanel = new MetadataPanel(controlled);
+               eventBus.register(metadataPanel);
+               add(metadataPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
+               add(createControllerPanel(controlled), new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
+               add(Box.createVerticalGlue(), new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(6, 0, 0, 0), 0, 0));
        }
 
        //
@@ -88,4 +99,78 @@ public class ControlledPane extends JPanel {
                return controllerPanel;
        }
 
+       /**
+        * A {@link JPanel} that displays {@link Metadata} information about a {@link
+        * Controlled} component.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       private static class MetadataPanel extends JPanel {
+
+               /** The controlled component. */
+               private final Controlled controlled;
+
+               /** The format label. */
+               private JLabel format;
+
+               /** The title label. */
+               private JLabel title;
+
+               /**
+                * Creates a new metadata panel.
+                *
+                * @param controlled
+                *              The controlled component
+                */
+               public MetadataPanel(Controlled controlled) {
+                       this.controlled = controlled;
+
+                       setBorder(createTitledBorder(createEtchedBorder(), "Metadata"));
+                       setLayout(new GridBagLayout());
+
+                       format = new JLabel();
+                       title = new JLabel();
+
+                       JLabel formatLabel = new JLabel("Format");
+                       formatLabel.setFont(formatLabel.getFont().deriveFont(Font.BOLD));
+                       JLabel titleLabel = new JLabel("Title");
+                       titleLabel.setFont(formatLabel.getFont().deriveFont(Font.BOLD));
+
+                       add(formatLabel, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
+                       add(format, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
+                       add(titleLabel, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
+                       add(title, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
+
+                       metadataUpdated(new MetadataUpdated(controlled, controlled.metadata()));
+               }
+
+               /**
+                * Called by the {@link EventBus} when a {@link Controlled} component updates
+                * its metadata.
+                *
+                * @param metadataUpdated
+                *              The metadata updated event
+                */
+               @Subscribe
+               public void metadataUpdated(MetadataUpdated metadataUpdated) {
+                       /* do we care about the event’s component? */
+                       if (!controlled.equals(metadataUpdated.controlled())) {
+                               return;
+                       }
+
+                       /* did we actually get any metadata? */
+                       Metadata metadata = metadataUpdated.metadata();
+                       if (metadata == null) {
+                               format.setText("");
+                               title.setText("");
+                               return;
+                       }
+
+                       /* set the texts. */
+                       format.setText(String.format("%s kHz, %d channel%s, %s", metadata.frequency() / 1000.0, metadata.channels(), metadata.channels() != 1 ? "s" : "", metadata.encoding()));
+                       title.setText(metadata.title());
+               }
+
+       }
+
 }