/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.nplusc.izc.iZpl.GUI;

import de.nplusc.izc.tools.UiToolz.TableCellListener;
import de.nplusc.izc.tools.baseTools.Tools;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.table.DefaultTableModel;
import org.yaml.snakeyaml.Yaml;

/**
 *
 * @author LH
 */
public class PlayListManagingDefault extends javax.swing.JFrame
{

    /**
     * Creates new form PlayListManagingDefault
     */
    public PlayListManagingDefault()
    {
        initComponents();
        
        Action action = new AbstractAction()
        {
            public void actionPerformed(ActionEvent e)
            {
                if(!rebuilding)
                {
                    TableCellListener tcl = (TableCellListener)e.getSource();
                    System.out.println("Row   : " + tcl.getRow());
                    System.out.println("Value  : " + tcl.getNewValue());
                    PlayListManagingBackend.setTargetPlayCount(tcl.getRow(), (int) tcl.getNewValue());
                }
            }
        };

        TableCellListener tcl = new TableCellListener(tblPLE, action);
        rebuildTable();
    }

    /**
     * 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();
        tblPLE = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        btnForce = new javax.swing.JButton();
        btnReload = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();

        setTitle("Edit PlayList Data");

        tblPLE.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][]
            {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String []
            {
                "Element-Titel", "# to Play", "# Played"
            }
        )
        {
            Class[] types = new Class []
            {
                java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean []
            {
                false, true, false
            };

            public Class getColumnClass(int columnIndex)
            {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex)
            {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblPLE);
        tblPLE.getColumnModel().getColumn(1).setMinWidth(80);
        tblPLE.getColumnModel().getColumn(1).setPreferredWidth(80);
        tblPLE.getColumnModel().getColumn(1).setMaxWidth(80);
        tblPLE.getColumnModel().getColumn(2).setMinWidth(80);
        tblPLE.getColumnModel().getColumn(2).setPreferredWidth(80);
        tblPLE.getColumnModel().getColumn(2).setMaxWidth(80);

        btnForce.setText("Als näxxtes setzen");
        btnForce.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnForceActionPerformed(evt);
            }
        });

        btnReload.setText("Neu Laden");
        btnReload.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnReloadActionPerformed(evt);
            }
        });

        btnClose.setText("Schließen");
        btnClose.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(btnForce, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btnReload, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btnClose, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnForce)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnReload)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnClose))
        );

        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()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 735, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

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

    private void btnReloadActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnReloadActionPerformed
    {//GEN-HEADEREND:event_btnReloadActionPerformed
   
        if((evt.getModifiers()& ActionEvent.SHIFT_MASK)!=0)
        {
            PlayListManagingBackend.reloadList();
        }
        rebuildTable();
    }//GEN-LAST:event_btnReloadActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnCloseActionPerformed
    {//GEN-HEADEREND:event_btnCloseActionPerformed
       setVisible(false);
    }//GEN-LAST:event_btnCloseActionPerformed

    private void btnForceActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnForceActionPerformed
    {//GEN-HEADEREND:event_btnForceActionPerformed
       PlayListManagingBackend.forceElement(tblPLE.getSelectedRow());
    }//GEN-LAST:event_btnForceActionPerformed

    /**
     * @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(PlayListManagingDefault.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(PlayListManagingDefault.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(PlayListManagingDefault.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(PlayListManagingDefault.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new PlayListManagingDefault().setVisible(true);
            }
        });
    }
    
    private boolean rebuilding=false;
    private void rebuildTable()
    {
        rebuilding = true;
        PlayListManagingBackend.refreshKeyList();
        String[] titles = PlayListManagingBackend.getTrackTitles();
        //System.out.println("titles for tblrefresh"+new Yaml().dump(titles));
        for(int i=tblPLE.getRowCount()-1;i>0;i--)
	{
	    ((DefaultTableModel)tblPLE.getModel()).removeRow(i-1);
	}
        tblPLE.setValueAt(titles[0],0,0);
        tblPLE.setValueAt(PlayListManagingBackend.getTargetPlayCount(0),0,1);
        tblPLE.setValueAt(PlayListManagingBackend.getPlayCount(0),0,2);
        
        for (int i = 1; i < titles.length; i++)
        {
            ((DefaultTableModel)tblPLE.getModel()).addRow(new Object[]{null,null,null});
            tblPLE.setValueAt(titles[i],i,0);
            tblPLE.setValueAt(PlayListManagingBackend.getTargetPlayCount(i),i,1);
            tblPLE.setValueAt(PlayListManagingBackend.getPlayCount(i),i,2);
        }
        rebuilding=false;
    }
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnForce;
    private javax.swing.JButton btnReload;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblPLE;
    // End of variables declaration//GEN-END:variables
}
