-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlPanel.java
More file actions
89 lines (82 loc) · 2.52 KB
/
ControlPanel.java
File metadata and controls
89 lines (82 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.midi.*;
public class ControlPanel extends JPanel {
private JTextField _ipTextField = null;
private JRadioButton _remoteButton = null, _localButton = null;
Receiver _receiver;
/**
* Returns the IP address currently entered in the text box.
* @return the IP address currently entered in the text box.
*/
public String getIPAddress () {
return _ipTextField.getText();
}
/**
* Adds the specified ActionListener to the radio button.
* @param listener the ActionListener to add.
*/
public void addActionListener (ActionListener listener) {
_localButton.addActionListener(listener);
_remoteButton.addActionListener(listener);
}
/**
* Returns whether or not the radio button indicates the
* receiver should be remote (i.e., network).
* @return whether or not the receiver should be remote.
*/
public boolean isRemote () {
return _remoteButton.isSelected();
}
/**
* Sets the local receiver to the specified value.
* @param the local receiver
*/
public void setReceiver (Receiver receiver) {
_receiver = receiver;
}
/**
* @param the local receiver.
*/
public ControlPanel (Receiver receiver) {
_receiver = receiver;
Instrument[] instruments;
Map<String, Patch> patches = new HashMap<>();
try {
instruments = MidiSystem.getSynthesizer().getAvailableInstruments();
} catch (MidiUnavailableException mue) {
return;
}
String[] strings = new String[instruments.length];
for (int i = 0; i < strings.length; i++) {
strings[i] = instruments[i].getName();
patches.put(strings[i], instruments[i].getPatch());
}
JComboBox comboBox = new JComboBox(strings);
comboBox.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
Patch patch = patches.get((String) comboBox.getSelectedItem());
try {
ShortMessage myMsg = new ShortMessage();
myMsg.setMessage(ShortMessage.PROGRAM_CHANGE, patch.getBank(), patch.getProgram(), 0);
_receiver.send(myMsg, -1);
} catch (InvalidMidiDataException imde) {
System.out.println("Could not change instrument!");
}
}
});
ButtonGroup buttonGroup = new ButtonGroup();
_localButton = new JRadioButton("Local");
_localButton.setSelected(true);
_remoteButton = new JRadioButton("Remote: ");
_ipTextField = new JTextField("255.255.255.255");
buttonGroup.add(_localButton);
buttonGroup.add(_remoteButton);
add(comboBox);
add(_localButton);
add(_remoteButton);
add(_ipTextField);
}
}