2 * Sonitus - ControlledTab.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sonitus.gui;
20 import static javax.swing.BorderFactory.createEtchedBorder;
21 import static javax.swing.BorderFactory.createTitledBorder;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Insets;
27 import javax.swing.BorderFactory;
28 import javax.swing.Box;
29 import javax.swing.JComponent;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
33 import net.pterodactylus.sonitus.data.Controlled;
34 import net.pterodactylus.sonitus.data.Controller;
35 import net.pterodactylus.sonitus.data.Metadata;
36 import net.pterodactylus.sonitus.data.controller.Fader;
37 import net.pterodactylus.sonitus.data.controller.Knob;
38 import net.pterodactylus.sonitus.data.controller.Switch;
39 import net.pterodactylus.sonitus.data.event.MetadataUpdated;
41 import com.google.common.eventbus.EventBus;
42 import com.google.common.eventbus.Subscribe;
45 * Panel that displays all {@link Controller}s of a {@link Controlled}
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 public class ControlledPane extends JPanel {
53 * Creates a new controlled pane.
58 * The controlled whose controllers to display
60 public ControlledPane(EventBus eventBus, Controlled controlled) {
61 super(new GridBagLayout());
62 setBorder(BorderFactory.createEmptyBorder(6, 12, 12, 12));
64 MetadataPanel metadataPanel = new MetadataPanel(controlled);
65 eventBus.register(metadataPanel);
66 add(metadataPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
67 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));
68 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));
76 * Creates the controller panel for the given component.
79 * The component whose controllers to display
80 * @return The created controller panel
82 private JComponent createControllerPanel(Controlled controlled) {
83 JPanel controllerPanel = new JPanel(new GridBagLayout());
84 controllerPanel.setBorder(createTitledBorder(createEtchedBorder(), "Controller"));
86 int controllerIndex = 0;
87 for (Controller controller : controlled.controllers()) {
88 controllerPanel.add(new JLabel(controller.name()), new GridBagConstraints(0, controllerIndex, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 0, 0, 6), 0, 0));
89 if (controller instanceof Fader) {
90 controllerPanel.add(new FaderPanel((Fader) controller), new GridBagConstraints(1, controllerIndex, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
91 } else if (controller instanceof Switch) {
92 controllerPanel.add(new SwitchPanel((Switch) controller), new GridBagConstraints(1, controllerIndex, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
93 } else if (controller instanceof Knob) {
94 controllerPanel.add(new KnobPanel((Knob) controller), new GridBagConstraints(1, controllerIndex, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
99 return controllerPanel;
103 * A {@link JPanel} that displays {@link Metadata} information about a {@link
104 * Controlled} component.
106 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
108 private static class MetadataPanel extends JPanel {
110 /** The controlled component. */
111 private final Controlled controlled;
113 /** The format label. */
114 private JLabel format;
116 /** The title label. */
117 private JLabel title;
120 * Creates a new metadata panel.
123 * The controlled component
125 public MetadataPanel(Controlled controlled) {
126 this.controlled = controlled;
128 setBorder(createTitledBorder(createEtchedBorder(), "Metadata"));
129 setLayout(new GridBagLayout());
131 format = new JLabel();
132 title = new JLabel();
134 JLabel formatLabel = new JLabel("Format");
135 formatLabel.setFont(formatLabel.getFont().deriveFont(Font.BOLD));
136 JLabel titleLabel = new JLabel("Title");
137 titleLabel.setFont(formatLabel.getFont().deriveFont(Font.BOLD));
139 add(formatLabel, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
140 add(format, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
141 add(titleLabel, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
142 add(title, new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
144 metadataUpdated(new MetadataUpdated(controlled, controlled.metadata()));
148 * Called by the {@link EventBus} when a {@link Controlled} component updates
151 * @param metadataUpdated
152 * The metadata updated event
155 public void metadataUpdated(MetadataUpdated metadataUpdated) {
156 /* do we care about the event’s component? */
157 if (!controlled.equals(metadataUpdated.controlled())) {
161 /* did we actually get any metadata? */
162 Metadata metadata = metadataUpdated.metadata();
163 if (metadata == null) {
170 format.setText(String.format("%s kHz, %d channel%s, %s", metadata.frequency() / 1000.0, metadata.channels(), metadata.channels() != 1 ? "s" : "", metadata.encoding()));
171 title.setText(metadata.title());