]> ruin.nu Git - moosique.git/blobdiff - MooInstrumentList.java
no message
[moosique.git] / MooInstrumentList.java
index 3e538d489d9b7edc672984addd0f5e077839d8aa..91f8eea239733139899db97a5401ed8b585f978b 100644 (file)
@@ -1,27 +1,58 @@
 import javax.sound.midi.*;
 import javax.swing.*;
+import java.awt.*;
 import java.awt.event.*;
 
-/*
+/**
  * A Combo Box where the instrument of the currently active channel can be selected.
  * 
- * @author  Einar Pehrson
+ * @author  Einar Pehrson, Michael Andreen
  */
  
 public class MooInstrumentList extends JComboBox implements ActionListener {
 
-       public MooInstrumentList() {
-               super(instruments);
+       private int channel;
+       private ShortMessage programChangeMessage;
+       public static final int INSTRUMENTS = 0, DRUMS = 1;
+       
+       /**
+        * Creates the instrument list.
+        * @param chan the channel it will operate on.
+        * @param type one of the constants: INSTRUMENTS and DRUMS
+        * @param chan the MIDI message assigning the initial program change
+        */
+       public MooInstrumentList(int chan, int listType, ShortMessage programMsg) {
+               super(instruments[listType]);
+               programChangeMessage = programMsg;
+               if (programChangeMessage != null) setSelectedIndex(programChangeMessage.getData1());
+               setChannel(chan);       
+               setFont(Moosique.getGUI().FONT);
                addActionListener(this);
        }
 
+       /**
+        * Sets the channel that it will operate on.
+        */
+       public void setChannel(int chan) {
+               channel = chan;
+               try {programChangeMessage.setMessage(programChangeMessage.getCommand(), chan, programChangeMessage.getData1(), 0);}
+               catch (Exception e) {}
+       }
+
+       /**
+        * Sets the instrument on a channel when it's changed in the combobox.
+        */
        public void actionPerformed(ActionEvent e) {
-               JComboBox box = (JComboBox)e.getSource();
-               int instrument = box.getSelectedIndex();
-               Moosique.getActiveChannel().programChange(instrument);
+               int instrument = ((JComboBox)e.getSource()).getSelectedIndex();
+               Moosique.getChannel(channel).programChange(instrument);
+               try {programChangeMessage.setMessage(programChangeMessage.getCommand(), programChangeMessage.getChannel(), instrument, 0);}
+               catch (InvalidMidiDataException ex) {}
        }
     
-       public static final String[] instruments = {
+       /**
+        * The list with standard MIDI instruments.
+        */
+       public static final String[][] instruments = {{
                " 0 Acoustic Grand Piano",
                " 1 Bright Acoustic Piano",
                " 2 Electric Grand Piano",
@@ -150,5 +181,11 @@ public class MooInstrumentList extends JComboBox implements ActionListener {
                " 125 Helicopter",
                " 126 Applause",
                " 127 Gunshot"
-       };
-}
\ No newline at end of file
+       }, new String[128]};
+       
+       static {
+               for (int i = 0; i < 128; i++) {
+                       instruments[1][i] = " Program Change " + i;
+               }       
+       }
+}