/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package id.ac.ums.fkg.panum.form.transaksi;

import id.ac.ums.fkg.panum.form.master.MasterKelompok;
import id.ac.ums.fkg.panum.model.master.MKelompok;
import id.ac.ums.fkg.panum.model.master.MMahasiswa;
import id.ac.ums.fkg.panum.model.transaksi.TrKelompokMahasiswa;
import id.ac.ums.fkg.panum.utility.ModalityInternalFrame;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.observablecollections.ObservableCollections;
import org.jdesktop.swingbinding.JComboBoxBinding;
import org.jdesktop.swingbinding.SwingBindings;

/**
 *
 * @author Dawud
 */
public class TransaksiKelompok extends ModalityInternalFrame {

    private MasterKelompok ortu;
    private List<Short> prdpanum;
    private MKelompok m;
    private BindingGroup bindingGroup;
    private List<MMahasiswa> tobeHidden;

    /**
     * Creates new form KelompokManager
     */
    public TransaksiKelompok(MasterKelompok ortu) {
        super(ortu);
        this.ortu = ortu;
        initComponents();
        jComboBox1.setSelectedIndex(jComboBox1.getItemCount() - 1);
        JTextComponent tcA = (JTextComponent) jComboBox1.getEditor().getEditorComponent();
        tcA.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                Document dc = e.getDocument();
                try {

                    disaring(new Short(dc.getText(0, dc.getLength())));
                    terpilih = Beans.isDesignTime() ? Collections.emptyList() : ObservableCollections.observableList(tobeHidden);
                    jurnals.reloadModel(daftarMhsswa, terpilih);
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(ortu.getOrtu(), "Bukan Angka");
                    try {
                        dc.remove(0, dc.getLength());
                    } catch (BadLocationException ex1) {
                        ex.printStackTrace();
                    }
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                try {
                    Document dc = e.getDocument();
                    if (dc.getLength() > 0) {
                        disaring(new Short(dc.getText(0, dc.getLength())));
                        terpilih = Beans.isDesignTime() ? Collections.emptyList() : ObservableCollections.observableList(tobeHidden);
                        jurnals.reloadModel(daftarMhsswa, terpilih);
                    } else {
                        terpilih = Collections.emptyList();
                        jurnals.reloadModel(daftarMhsswa, terpilih);
                    }
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });
    }

    /**
     * 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        bindingGroup =new BindingGroup();
        jTable1 = ortu.getTblKelompok();
        entityManager1 = Beans.isDesignTime() ? null : ortu.getOrtu().getEmf().createEntityManager();
        kelompokMahasiswa = java.beans.Beans.isDesignTime() ? null : entityManager1.createNamedQuery("trkelompokmahasiswa.all",TrKelompokMahasiswa.class);
        tmk = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(kelompokMahasiswa.getResultList());
        TreeSet<Short> coba = new TreeSet<>();
        for (TrKelompokMahasiswa t : tmk) {
            coba.add(t.getPeriodePanum());
        }
        Short last = (coba.isEmpty()) ? new Short("0") : coba.last();
        disaring(last);
        prdpanum = Beans.isDesignTime() ? Collections.emptyList() : ObservableCollections.observableList(new ArrayList<Short>(coba));
        mhsswa = java.beans.Beans.isDesignTime() ? null : entityManager1.createNamedQuery("mmahasiswa.all",MMahasiswa.class);
        daftarMhsswa = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(mhsswa.getResultList());
        terpilih = Beans.isDesignTime() ? Collections.emptyList() : ObservableCollections.observableList(tobeHidden);
        jTable2 = ortu.getDetailTable();
        jButton1 = new javax.swing.JButton();
        jurnals = new id.ac.ums.fkg.panum.utility.JFilteredCheckList<>(daftarMhsswa, terpilih);
        jPanel1 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox<Short>();
        jLabel1 = new javax.swing.JLabel();

        int indekse =  jTable1.getSelectedRow();
        m = ortu.getList1().get(jTable1.convertRowIndexToModel(indekse));
        jTable1.clearSelection();
        jTable1.setRowSelectionInterval(indekse, indekse);

        setClosable(true);
        setTitle("Form Penambahan Kelompok");
        setMinimumSize(new java.awt.Dimension(450, 384));
        setPreferredSize(new java.awt.Dimension(450, 384));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jButton1.setText("Simpan");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 12, 8);
        getContentPane().add(jButton1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(3, 3, 4, 8);
        getContentPane().add(jurnals, gridBagConstraints);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        JComboBoxBinding jComboBoxBinding = SwingBindings.createJComboBoxBinding(AutoBinding.UpdateStrategy.READ_WRITE, prdpanum, jComboBox1);
        jComboBoxBinding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(jComboBoxBinding);
        jComboBox1.setEditable(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jComboBox1, gridBagConstraints);

        jLabel1.setText("Periode Panum: ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jLabel1, gridBagConstraints);
        bindingGroup.bind();

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(12, 2, 2, 8);
        getContentPane().add(jPanel1, gridBagConstraints);

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (jComboBox1.getSelectedItem() != null) {
            List<MMahasiswa> mk = jurnals.getSelectionCache();
            for (MMahasiswa jk : mk) {
                TrKelompokMahasiswa baru = new TrKelompokMahasiswa();
                entityManager1.persist(baru);
                baru.setMahasiswa(jk);
                baru.setKelompok(m);
                baru.setPeriodePanum(new Short(jComboBox1.getSelectedItem() + ""));
                ortu.getAnggotas().add(baru);
                int row = m.getMahasiswaList().size() - 1;
                ortu.getDetailTable().setRowSelectionInterval(row, row);
                ortu.getDetailTable().scrollRectToVisible(ortu.getDetailTable().getCellRect(row, 0, true));
            }
            ortu.save();
            hide();
        } else {
            JOptionPane.showMessageDialog(ortu.getOrtu(), "Periode Panum kosong!");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void disaring(Short filternya) {
        tobeHidden = new ArrayList<>();
        for (TrKelompokMahasiswa k : tmk) {
            if (k.getPeriodePanum().equals(filternya)) {
                tobeHidden.add(k.getMahasiswa());
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<MMahasiswa> daftarMhsswa;
    private javax.persistence.EntityManager entityManager1;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox<Short> jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private id.ac.ums.fkg.panum.utility.JFilteredCheckList<MMahasiswa> jurnals;
    private javax.persistence.Query kelompokMahasiswa;
    private javax.persistence.Query mhsswa;
    private java.util.List terpilih;
    private java.util.List<TrKelompokMahasiswa> tmk;
    // End of variables declaration//GEN-END:variables
}
