/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import data.InstrumentStatics;
import java.awt.Rectangle;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultListModel;

/**
 *
 * @author Tom
 */
public class InstrumentDialog extends javax.swing.JDialog {

   /**
    * Creates new form InstrumentDialog
    */
   public InstrumentDialog(java.awt.Frame parent, boolean modal, List<Integer> instruments, List<Integer> percussions, String _title) {
      super(parent, modal);
      initComponents();
      
      Rectangle parentBounds = parent.getBounds();
      this.setBounds((int)(parentBounds.getWidth()/2-getBounds().getWidth()/2) + (int)parentBounds.getX(), 
                     (int)(parentBounds.getHeight()/2-getBounds().getHeight()/2) + (int)parentBounds.getY(), 
                     (int)getBounds().getWidth(), (int)getBounds().getHeight());
      
      this.setTitle(_title);
      fillLists(instruments, percussions);
   }

   /**
    * befuellt die Listen mit den Instrumenten und Percussions
    * @param instruments Instrumente
    * @param percussions Percussions
    */
   private void fillLists(List<Integer> instruments, List<Integer> percussions) {
      DefaultListModel<String> dlmi = new DefaultListModel<>();
      DefaultListModel<String> dlmp = new DefaultListModel<>();
      
      for (int i : instruments) {
            dlmi.addElement(getKeyByValue(false, i));
      }
      for (int i : percussions) {
         String s = getKeyByValue(true, i);
         if (s != null) {
            dlmp.addElement(s);
         }
      }
      
      lstInstruments.setModel(dlmi);
      lstPercussions.setModel(dlmp);
   }
   
   /**
     * liefert den Namen des Instruments zum uebergebenen Integer-Wert
     * @param value Nummer des Instruments laut MIDI-Spezifikation
     * @return Name des Instruments
     */
    public String getKeyByValue(boolean Drum, int value) {
       if (!Drum)
       {
         for (Map.Entry <String, Integer> entry : InstrumentStatics.instruments.entrySet()) {
            if (value == entry.getValue())  {
               return entry.getKey();
            }
         }
       }
       else
       {
         for (Map.Entry <String, Integer> entry : InstrumentStatics.drums.entrySet()) {
            if (value == entry.getValue())  {
               return entry.getKey();
            }
         }
       }
      return null;
    }
   
   /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      jScrollPane1 = new javax.swing.JScrollPane();
      lstInstruments = new javax.swing.JList();
      jScrollPane2 = new javax.swing.JScrollPane();
      lstPercussions = new javax.swing.JList();
      jLabel1 = new javax.swing.JLabel();
      jLabel2 = new javax.swing.JLabel();

      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

      lstInstruments.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
      jScrollPane1.setViewportView(lstInstruments);

      lstPercussions.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
      jScrollPane2.setViewportView(lstPercussions);

      jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
      jLabel1.setText("Instruments");

      jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
      jLabel2.setText("Percussion");

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
               .addGroup(layout.createSequentialGroup()
                  .addGap(40, 40, 40)
                  .addComponent(jLabel1)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE)
                  .addComponent(jLabel2)
                  .addGap(55, 55, 55)))
            .addContainerGap())
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel1)
               .addComponent(jLabel2))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
               .addComponent(jScrollPane2)))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

   /**
    * @param args the command line arguments
    */
   public static void main(String args[]) {
      /* Set the Nimbus look and feel */
      //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
       * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
       */
      try {
         for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
               javax.swing.UIManager.setLookAndFeel(info.getClassName());
               break;
            }
         }
      } catch (ClassNotFoundException ex) {
         java.util.logging.Logger.getLogger(InstrumentDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (InstantiationException ex) {
         java.util.logging.Logger.getLogger(InstrumentDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (IllegalAccessException ex) {
         java.util.logging.Logger.getLogger(InstrumentDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (javax.swing.UnsupportedLookAndFeelException ex) {
         java.util.logging.Logger.getLogger(InstrumentDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      }
      //</editor-fold>

      /* Create and display the dialog */
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            InstrumentDialog dialog = new InstrumentDialog(new javax.swing.JFrame(), true, null, null, null);
            dialog.addWindowListener(new java.awt.event.WindowAdapter() {
               @Override
               public void windowClosing(java.awt.event.WindowEvent e) {
                  System.exit(0);
               }
            });
            dialog.setVisible(true);
         }
      });
   }
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JScrollPane jScrollPane2;
   private javax.swing.JList lstInstruments;
   private javax.swing.JList lstPercussions;
   // End of variables declaration//GEN-END:variables
}
