2 * Sonitus - ComponentInfoPanel.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 java.awt.Dimension;
22 import java.awt.GridBagConstraints;
23 import java.awt.GridBagLayout;
24 import java.awt.Insets;
25 import javax.swing.BorderFactory;
26 import javax.swing.Box;
27 import javax.swing.JLabel;
28 import javax.swing.JPanel;
30 import net.pterodactylus.sonitus.data.ControlledComponent;
31 import net.pterodactylus.sonitus.data.Controller;
32 import net.pterodactylus.sonitus.data.FormatMetadata;
33 import net.pterodactylus.sonitus.data.controller.Fader;
34 import net.pterodactylus.sonitus.data.controller.Knob;
35 import net.pterodactylus.sonitus.data.controller.Switch;
37 import com.google.common.base.Optional;
40 * Panel that shows information about a {@link ControlledComponent}.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class ComponentInfoPanel extends JPanel {
46 /** The name of the component. */
47 private final JLabel headerLabel = new JLabel();
49 /** The number of received input bytes. */
50 private final JLabel inputLabel = new JLabel();
52 /** The number of sent output bytes. */
53 private final JLabel outputLabel = new JLabel();
55 /** The current format metadata. */
56 private final JLabel formatLabel = new JLabel();
59 * Creates a new component info panel.
61 * @param controlledComponent
62 * The component to display
64 public ComponentInfoPanel(ControlledComponent controlledComponent) {
65 super(new GridBagLayout());
67 setPreferredSize(new Dimension(400, 0));
68 createPanel(controlledComponent);
76 * Sets the number of received input bytes.
79 * The number of received input bytes
82 public ComponentInfoPanel input(Optional<Long> input) {
83 if (input.isPresent()) {
84 inputLabel.setText(format(input.get()));
86 inputLabel.setText("");
92 * Sets the number of sent output bytes.
95 * The number of sent output input bytes
98 public ComponentInfoPanel output(Optional<Long> output) {
99 if (output.isPresent()) {
100 outputLabel.setText(format(output.get()));
102 outputLabel.setText("");
108 * Sets the current format metadata.
111 * The format metadata
114 public ComponentInfoPanel format(Optional<FormatMetadata> metadata) {
115 if (metadata.isPresent()) {
116 formatLabel.setText(metadata.get().toString());
118 formatLabel.setText("");
128 * Creates the panel for the given controlled component.
130 * @param controlledComponent
131 * The controlled component
133 private void createPanel(ControlledComponent controlledComponent) {
134 setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
136 headerLabel.setText(controlledComponent.name());
137 headerLabel.setFont(headerLabel.getFont().deriveFont(Font.BOLD));
140 add(headerLabel, new GridBagConstraints(0, line++, 2, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
141 add(new JLabel("Input"), new GridBagConstraints(0, line, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(18, 0, 0, 0), 0, 0));
142 add(inputLabel, new GridBagConstraints(1, line++, 1, 1, 1.0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(18, 6, 0, 0), 0, 0));
143 add(new JLabel("Output"), new GridBagConstraints(0, line, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
144 add(outputLabel, new GridBagConstraints(1, line++, 1, 1, 1.0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
145 add(new JLabel("Format"), new GridBagConstraints(0, line, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 0, 0, 0), 0, 0));
146 add(formatLabel, new GridBagConstraints(1, line++, 1, 1, 1.0, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
148 /* add the controllers. */
149 for (Controller<?> controller : controlledComponent.controllers()) {
150 add(new JLabel(controller.name()), new GridBagConstraints(0, line, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(6, 0, 0, 6), 0, 0));
151 if (controller instanceof Fader) {
152 add(new FaderPanel((Fader) controller), new GridBagConstraints(1, line++, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
153 } else if (controller instanceof Switch) {
154 add(new SwitchPanel((Switch) controller), new GridBagConstraints(1, line++, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
155 } else if (controller instanceof Knob) {
156 add(new KnobPanel((Knob) controller), new GridBagConstraints(1, line++, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0));
160 add(Box.createVerticalGlue(), new GridBagConstraints(1, line++, 2, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(6, 6, 0, 0), 0, 0));
164 * Formats the number using SI prefixes so that a maximum of 3 digits are
168 * The number to format
169 * @return The formatted number
171 private static String format(long number) {
172 String[] prefixes = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei" };
173 double shortenedNumber = number;
174 for (String prefix : prefixes) {
175 if (shortenedNumber < 1000) {
176 return String.format("%.1f %sB", shortenedNumber, prefix);
178 shortenedNumber /= 1024;
180 return String.format("%.1e B", (double) number);