/* For License see bottom */
package jonkoshare.gui;
import static jonkoshare.util.ResourceKeys.BUTTON_TEST;
import static jonkoshare.util.ResourceKeys.CHECKBOX_TRANSMIT_PCC;
import static jonkoshare.util.ResourceKeys.LABEL_DEVICE_METRONOME;
import static jonkoshare.util.ResourceKeys.LABEL_DEVICE_OUT;
import static jonkoshare.util.ResourceKeys.LABEL_PROGRAM;
import static jonkoshare.util.ResourceKeys.MESSAGE_CHOOSE_OUTPUT_FIRST;
import static jonkoshare.util.ResourceKeys.MESSAGE_TRANSMIT_PCC;
import static jonkoshare.util.ResourceKeys.RADIO_MELODY;
import static jonkoshare.util.ResourceKeys.RADIO_TEST_CHORD;
import static jonkoshare.util.ResourceKeys.RADIO_TEST_DRUMS;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Logger;
import javax.sound.midi.Instrument;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import jonkoshare.midi.MidiThread;
import jonkoshare.util.ResourceFactory;
import jonkoshare.util.ResourceKeys;
/**
* Panel for settings to test midi capabilities.
*
* @since 1.0
* @author Onkobu Tanaake
*/
public class TestSettings extends JPanel {
private static final Logger log=Logger.getLogger(TestSettings.class.getName());
//TODO provide an implementation so option panes
// show up
private final ErrorHandler handler;
public TestSettings() {
this(null);
}
/**
* Creates new form TestSettings_1
*/
public TestSettings(MidiChooser mc) {
initComponents();
initValues();
midiChooser=mc;
handler=null;
}
public void setMetronomeEnabled(boolean flag) {
metronomeButton.setEnabled(flag);
if (!flag && metronomeButton.isSelected()) {
metronomeButton.setSelected(false);
outButton.setSelected(true);
}
}
public boolean isMetronomeEnabled() {
return metronomeButton.isEnabled();
}
protected void initValues() {
programBox.removeAllItems();
String msg=ResourceFactory.getString(LABEL_PROGRAM);
for (int i=0;i<50; i++) {
programBox.addItem(msg+" #"+i);
}
programBox.setSelectedIndex(0);
ButtonGroup bg=new ButtonGroup();
bg.add(this.melodyButton);
bg.add(this.drumButton);
bg.add(this.chordButton);
melodyButton.setSelected(true);
ButtonGroup bg2=new ButtonGroup();
bg2.add(outButton);
bg2.add(metronomeButton);
outButton.setSelected(true);
}
/**
* This method is called from within the constructor to
* initialize the form.
*/
private void initComponents() {
GridBagConstraints gbc=new GridBagConstraints();
gbc.insets = new Insets(2, 2, 2, 2);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
chordButton = new JRadioButton(ResourceFactory.getString(RADIO_TEST_CHORD));
drumButton = new JRadioButton(ResourceFactory.getString(RADIO_TEST_DRUMS));
melodyButton = new JRadioButton(ResourceFactory.getString(RADIO_MELODY));
programBox = new JComboBox();
programCheck = new JCheckBox();
testButton = new JButton();
metronomeButton=new JRadioButton(ResourceFactory.getString(LABEL_DEVICE_METRONOME));
outButton=new JRadioButton(ResourceFactory.getString(LABEL_DEVICE_OUT));
setLayout(new GridBagLayout());
add(outButton, gbc);
gbc.gridx++;
add(metronomeButton, gbc);
gbc.gridx=0;
gbc.gridy++;
gbc.weightx = 0.2;
add(chordButton, gbc);
gbc.gridy++;
add(drumButton, gbc);
gbc.gridy++;
add(melodyButton, gbc);
programBox.setEnabled(false);
gbc.gridx = 1;
gbc.gridy++;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.NORTH;
add(programBox, gbc);
programCheck.setText(ResourceFactory.getString(CHECKBOX_TRANSMIT_PCC));
programCheck.setToolTipText(ResourceFactory.getString(MESSAGE_TRANSMIT_PCC));
programCheck.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent evt) {
programCheckStateChanged(evt);
}
});
gbc.gridx = 0;
add(programCheck, gbc);
testButton.setText(ResourceFactory.getString(BUTTON_TEST));
testButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
testButtonActionPerformed(evt);
}
});
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridheight = 2;
gbc.anchor = GridBagConstraints.EAST;
add(testButton, gbc);
}
private void testButtonActionPerformed(ActionEvent evt) {
if (midiChooser==null) {
log.severe("unable to perform test on null midi chooser, NPE will follow");
}
try {
MidiDevice.Info info=midiChooser.getSelectedOutputDevice();
Instrument instrument=midiChooser.getSelectedInstrument();
if (info==null && instrument==null) {
handleMessage(MESSAGE_CHOOSE_OUTPUT_FIRST);
return;
}
if (info==null) {
testOnInstrument(instrument);
} else {
testOnDevice(info);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void testOnInstrument(Instrument instrument) throws Exception {
MidiThread.TEST_MODES tm=MidiThread.TEST_MODES.TEST_MELODY;
if (drumButton.isSelected()) {
tm=MidiThread.TEST_MODES.TEST_DRUMS;
} else if (chordButton.isSelected()) {
tm=MidiThread.TEST_MODES.TEST_CHORD;
}
MidiThread mt=MidiThread.getTestInstance(midiChooser.getSelectedSynthesizer(),
instrument,
tm);
// nur ein Durchlauf
mt.setRunOnce(true);
mt.start();
}
private void testOnDevice(MidiDevice.Info info) throws Exception {
MidiDevice dev=MidiSystem.getMidiDevice(info);
int chan=midiChooser.getSelectedChannel();
MidiThread.TEST_MODES tm=MidiThread.TEST_MODES.TEST_MELODY;
if (drumButton.isSelected()) {
tm=MidiThread.TEST_MODES.TEST_DRUMS;
} else if (chordButton.isSelected()) {
tm=MidiThread.TEST_MODES.TEST_CHORD;
}
MidiThread mt=MidiThread.getTestInstance(dev, chan,
tm, programCheck.isSelected()?programBox.getSelectedIndex():-1);
// nur ein Durchlauf
mt.setRunOnce(true);
mt.start();
}
private void programCheckStateChanged(ChangeEvent evt) {
programBox.setEnabled(programCheck.isSelected());
}
public void setMidiChooser(MidiChooser chooser) {
midiChooser=chooser;
}
public MidiChooser getSettingsParent() {
return midiChooser;
}
@Override
public void setEnabled(boolean state) {
super.setEnabled(state);
chordButton.setEnabled(state);
drumButton.setEnabled(state);
melodyButton.setEnabled(state);
metronomeButton.setEnabled(state);
outButton.setEnabled(state);
programCheck.setEnabled(state);
testButton.setEnabled(state);
}
private void handleMessage(ResourceKeys msg) {
if (handler==null) {
return;
}
handler.handleMessage(msg.getProperty());
}
private MidiChooser midiChooser;
private JRadioButton chordButton;
private JRadioButton drumButton;
private JRadioButton melodyButton;
private JRadioButton metronomeButton;
private JRadioButton outButton;
private JComboBox programBox;
private JCheckBox programCheck;
private JButton testButton;
}
/*
Copyright (C) 2008 Onkobu Tanaake
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (gplv3.txt).
*/