Show metadata in main window.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / gui / ControlledPane.java
1 /*
2  * Sonitus - ControlledTab.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sonitus.gui;
19
20 import static javax.swing.BorderFactory.createEtchedBorder;
21 import static javax.swing.BorderFactory.createTitledBorder;
22
23 import java.awt.Font;
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;
32
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;
40
41 import com.google.common.eventbus.EventBus;
42 import com.google.common.eventbus.Subscribe;
43
44 /**
45  * Panel that displays all {@link Controller}s of a {@link Controlled}
46  * component.
47  *
48  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49  */
50 public class ControlledPane extends JPanel {
51
52         /**
53          * Creates a new controlled pane.
54          *
55          * @param eventBus
56          *              The event bus
57          * @param controlled
58          *              The controlled whose controllers to display
59          */
60         public ControlledPane(EventBus eventBus, Controlled controlled) {
61                 super(new GridBagLayout());
62                 setBorder(BorderFactory.createEmptyBorder(6, 12, 12, 12));
63
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));
69         }
70
71         //
72         // PRIVATE METHODS
73         //
74
75         /**
76          * Creates the controller panel for the given component.
77          *
78          * @param controlled
79          *              The component whose controllers to display
80          * @return The created controller panel
81          */
82         private JComponent createControllerPanel(Controlled controlled) {
83                 JPanel controllerPanel = new JPanel(new GridBagLayout());
84                 controllerPanel.setBorder(createTitledBorder(createEtchedBorder(), "Controller"));
85
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));
95                         }
96                         ++controllerIndex;
97                 }
98
99                 return controllerPanel;
100         }
101
102         /**
103          * A {@link JPanel} that displays {@link Metadata} information about a {@link
104          * Controlled} component.
105          *
106          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
107          */
108         private static class MetadataPanel extends JPanel {
109
110                 /** The controlled component. */
111                 private final Controlled controlled;
112
113                 /** The format label. */
114                 private JLabel format;
115
116                 /** The title label. */
117                 private JLabel title;
118
119                 /**
120                  * Creates a new metadata panel.
121                  *
122                  * @param controlled
123                  *              The controlled component
124                  */
125                 public MetadataPanel(Controlled controlled) {
126                         this.controlled = controlled;
127
128                         setBorder(createTitledBorder(createEtchedBorder(), "Metadata"));
129                         setLayout(new GridBagLayout());
130
131                         format = new JLabel();
132                         title = new JLabel();
133
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));
138
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));
143
144                         metadataUpdated(new MetadataUpdated(controlled, controlled.metadata()));
145                 }
146
147                 /**
148                  * Called by the {@link EventBus} when a {@link Controlled} component updates
149                  * its metadata.
150                  *
151                  * @param metadataUpdated
152                  *              The metadata updated event
153                  */
154                 @Subscribe
155                 public void metadataUpdated(MetadataUpdated metadataUpdated) {
156                         /* do we care about the event’s component? */
157                         if (!controlled.equals(metadataUpdated.controlled())) {
158                                 return;
159                         }
160
161                         /* did we actually get any metadata? */
162                         Metadata metadata = metadataUpdated.metadata();
163                         if (metadata == null) {
164                                 format.setText("");
165                                 title.setText("");
166                                 return;
167                         }
168
169                         /* set the texts. */
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());
172                 }
173
174         }
175
176 }