Add GUI that can display faders.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / gui / FaderPanel.java
1 /*
2  * Sonitus - FaderPanel.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.GridBagConstraints;
21 import java.awt.GridBagLayout;
22 import java.awt.Insets;
23 import javax.swing.BorderFactory;
24 import javax.swing.DefaultBoundedRangeModel;
25 import javax.swing.JLabel;
26 import javax.swing.JPanel;
27 import javax.swing.JSlider;
28 import javax.swing.event.ChangeEvent;
29 import javax.swing.event.ChangeListener;
30
31 import net.pterodactylus.sonitus.data.controller.Fader;
32
33 /**
34  * Displays a {@link Fader}.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class FaderPanel extends JPanel {
39
40         /**
41          * Creates a new fader panel.
42          *
43          * @param fader
44          *              The fader being controlled
45          */
46         public FaderPanel(final Fader fader) {
47                 super(new GridBagLayout());
48                 setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
49
50                 /* create label. */
51                 JLabel label = new JLabel("Volume");
52                 add(label, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
53
54                 /* create fader labels. */
55                 add(new JLabel("-∞"), new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0, 0));
56                 add(new JLabel("0"), new GridBagConstraints(3, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0, 0));
57
58                 /* create fader. */
59                 JSlider slider = new JSlider(new DefaultBoundedRangeModel(fader.value(), 0, fader.minimum(), fader.maximum()));
60                 slider.addChangeListener(new ChangeListener() {
61
62                         @Override
63                         public void stateChanged(ChangeEvent changeEvent) {
64                                 fader.value(((JSlider) changeEvent.getSource()).getValue());
65                         }
66                 });
67                 add(slider, new GridBagConstraints(2, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
68         }
69
70 }