package jpianotrain.gui;
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import jpianotrain.staff.Chord;
import jpianotrain.staff.Note;
import jpianotrain.staff.NoteName;
import jpianotrain.staff.Scale;
import org.apache.log4j.Logger;
/**
* Panel to choose notes to form a chord.
*
* @author onkobu
*/
public class ChordPane extends JPanel
implements ActionListener {
private static final Logger log=Logger.getLogger(ChordPane.class);
public ChordPane() {
listMap=new HashMap<JCheckBox, JComboBox>();
createUI();
}
/**
* Returns the selected chord. This
* may be <code>null</code> if
* nothing is selected.
*
*/
public Chord getChord() {
Chord c=null;
int len=notes.length;
List<Note> noteList=new ArrayList<Note>();
for (int i=0;i<len;i++) {
if (notes[i].isEnabled()) {
noteList.add(new Note((NoteName)notes[i].getSelectedItem()));
}
}
if (noteList.size()>0) {
c=new Chord(noteList);
}
return c;
}
/**
* Returns the selected notes. This
* may be <code>null</code> if
* nothing is selected.
*
*/
public Collection<Note> getNotes() {
List<Note> result=new ArrayList<Note>();
for (JComboBox box:notes) {
if (box.isEnabled()) {
result.add(new Note((NoteName)box.getSelectedItem()));
}
}
return Collections.unmodifiableCollection(result);
}
protected void createUI() {
int len=Scale.MAJOR_SCALES_CLASSIC.length;
setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
setLayout(new GridLayout(2,len));
notes=new JComboBox[len];
JCheckBox cb=null;
JComboBox jl=null;
List<JComboBox> nl=new ArrayList<JComboBox>();
for (int i=0;i<len;i++) {
cb=new JCheckBox();
cb.addActionListener(this);
add(cb);
jl=new JComboBox(NoteName.values());
jl.setEnabled(false);
notes[i]=jl;
listMap.put(cb,jl);
nl.add(jl);
}
for (int i=0;i<len;i++) {
add(notes[i]);
}
}
// ActionListener
@Override
public void actionPerformed(ActionEvent e) {
Object src=e.getSource();
JComboBox lst=listMap.get(src);
if (lst!=null) {
JCheckBox check=(JCheckBox)src;
lst.setEnabled(check.isSelected());
}
}
private HashMap<JCheckBox, JComboBox> listMap;
private JComboBox[] notes;
}