From 9545afc554b68737754e309bd51ed8bf8339cc77 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 27 May 2013 09:44:19 +0200 Subject: [PATCH] Show metadata in main window. --- .../pterodactylus/sonitus/gui/ControlledPane.java | 91 +++++++++++++++++++++- .../net/pterodactylus/sonitus/gui/MainWindow.java | 20 +++-- 2 files changed, 102 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/pterodactylus/sonitus/gui/ControlledPane.java b/src/main/java/net/pterodactylus/sonitus/gui/ControlledPane.java index 9a679f1..eb44bc7 100644 --- a/src/main/java/net/pterodactylus/sonitus/gui/ControlledPane.java +++ b/src/main/java/net/pterodactylus/sonitus/gui/ControlledPane.java @@ -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 David ‘Bombe’ Roden + */ + 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()); + } + + } + } diff --git a/src/main/java/net/pterodactylus/sonitus/gui/MainWindow.java b/src/main/java/net/pterodactylus/sonitus/gui/MainWindow.java index e828fcd..842ce80 100644 --- a/src/main/java/net/pterodactylus/sonitus/gui/MainWindow.java +++ b/src/main/java/net/pterodactylus/sonitus/gui/MainWindow.java @@ -29,6 +29,8 @@ import net.pterodactylus.sonitus.data.Controlled; import net.pterodactylus.sonitus.data.Controller; import net.pterodactylus.sonitus.main.Version; +import com.google.common.eventbus.EventBus; + /** * Sonitus main window. * @@ -36,12 +38,21 @@ import net.pterodactylus.sonitus.main.Version; */ public class MainWindow extends JFrame { + /** The event bus. */ + private final EventBus eventBus; + /** The tabbed pane displaying all controlled components. */ private final JTabbedPane tabbedPane = new JTabbedPane(); - /** Creates a new main window. */ - public MainWindow() { + /** + * Creates a new main window. + * + * @param eventBus + * The event bus + */ + public MainWindow(EventBus eventBus) { super(String.format("Sonitus %s", Version.version())); + this.eventBus = eventBus; tabbedPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); getContentPane().add(tabbedPane, BorderLayout.CENTER); setSize(new Dimension(800, 450)); @@ -62,10 +73,7 @@ public class MainWindow extends JFrame { */ public void addControllers(Controlled controlled) { List> controllers = controlled.controllers(); - if (controllers.isEmpty()) { - return; - } - ControlledPane controlledPane = new ControlledPane(controlled); + ControlledPane controlledPane = new ControlledPane(eventBus, controlled); tabbedPane.addTab(controlled.name(), controlledPane); } -- 2.7.4