Rename dummy filter to basic filter.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / gui / ComponentInfoPanel.java
1 /*
2  * Sonitus - ComponentInfoPanel.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 java.awt.Dimension;
21 import java.awt.Font;
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;
29
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;
36
37 import com.google.common.base.Optional;
38
39 /**
40  * Panel that shows information about a {@link ControlledComponent}.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class ComponentInfoPanel extends JPanel {
45
46         /** The name of the component. */
47         private final JLabel headerLabel = new JLabel();
48
49         /** The number of received input bytes. */
50         private final JLabel inputLabel = new JLabel();
51
52         /** The number of sent output bytes. */
53         private final JLabel outputLabel = new JLabel();
54
55         /** The current format metadata. */
56         private final JLabel formatLabel = new JLabel();
57
58         /**
59          * Creates a new component info panel.
60          *
61          * @param controlledComponent
62          *              The component to display
63          */
64         public ComponentInfoPanel(ControlledComponent controlledComponent) {
65                 super(new GridBagLayout());
66
67                 setPreferredSize(new Dimension(300, 0));
68                 createPanel(controlledComponent);
69         }
70
71         //
72         // ACTIONS
73         //
74
75         /**
76          * Sets the number of received input bytes.
77          *
78          * @param input
79          *              The number of received input bytes
80          * @return This panel
81          */
82         public ComponentInfoPanel input(Optional<Long> input) {
83                 if (input.isPresent()) {
84                         inputLabel.setText(format(input.get()));
85                 } else {
86                         inputLabel.setText("");
87                 }
88                 return this;
89         }
90
91         /**
92          * Sets the number of sent output bytes.
93          *
94          * @param output
95          *              The number of sent output input bytes
96          * @return This panel
97          */
98         public ComponentInfoPanel output(Optional<Long> output) {
99                 if (output.isPresent()) {
100                         outputLabel.setText(format(output.get()));
101                 } else {
102                         outputLabel.setText("");
103                 }
104                 return this;
105         }
106
107         /**
108          * Sets the current format metadata.
109          *
110          * @param metadata
111          *              The format metadata
112          * @return This panel
113          */
114         public ComponentInfoPanel format(Optional<FormatMetadata> metadata) {
115                 if (metadata.isPresent()) {
116                         formatLabel.setText(metadata.get().toString());
117                 } else {
118                         formatLabel.setText("");
119                 }
120                 return this;
121         }
122
123         //
124         // PRIVATE METHODS
125         //
126
127         /**
128          * Creates the panel for the given controlled component.
129          *
130          * @param controlledComponent
131          *              The controlled component
132          */
133         private void createPanel(ControlledComponent controlledComponent) {
134                 setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
135
136                 headerLabel.setText(controlledComponent.name());
137                 headerLabel.setFont(headerLabel.getFont().deriveFont(Font.BOLD));
138
139                 int line = 0;
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));
147
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));
157                         }
158                 }
159
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));
161         }
162
163         /**
164          * Formats the number using SI prefixes so that a maximum of 3 digits are
165          * shown.
166          *
167          * @param number
168          *              The number to format
169          * @return The formatted number
170          */
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);
177                         }
178                         shortenedNumber /= 1024;
179                 }
180                 return String.format("%.1e B", (double) number);
181         }
182
183 }