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

import de.nplusc.izc.tools.baseTools.HidableTableColumnModel;
import de.nplusc.izc.tools.baseTools.Tools;
import java.awt.Point;
import javax.swing.table.DefaultTableModel;

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

    /**
     * Creates new form PackagerGUI
     */
    private PackManager p;
    public PackagerGUI()
    {
        initComponents();
        updatePosition();
        lblVID.setText(InstallManager.VERSION_GUI_TXT);
        packStatus.setVisible(false);
        p = new PackManager();
        p.loadPackageData(ModeSel.filePath);
        ((HidableTableColumnModel)(tblSelector.getColumnModel())).setColumnVisible(0, false);
        //^auskommentieren um id-spalte sichtbar zu bekommenTempcode fürs Testen
        int rows=1;
        String[] tablefill = p.getPackList();
        for (int i = 0; i < tablefill.length; i++)
        {
            String GRP = tablefill[i];
            String[] data = GRP.split("\\|");
            if(i>=rows)
                ((DefaultTableModel)tblSelector.getModel()).addRow(new String[3]);
            ((DefaultTableModel)tblSelector.getModel()).setValueAt(data[0], i, 0);
            ((DefaultTableModel)tblSelector.getModel()).setValueAt(data[1], i, 1);
            ((DefaultTableModel)tblSelector.getModel()).setValueAt("Wird installiert", i, 2);
            if(p.packIsInstalled(data[0]))
            {
                ((DefaultTableModel)tblSelector.getModel()).setValueAt("Bereits installiert", i, 2);
            }
        }
    }
    private void updatePosition()
    {
        Point p = new Point(ModeSel.topleft_coords_x, ModeSel.topleft_coords_y);
        setLocation(p);
    }
    /**
     * 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();
        tblSelector = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        packDesc = new javax.swing.JTextArea();
        packTitle = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        packStatus = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblVID = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addComponentListener(new java.awt.event.ComponentAdapter()
        {
            public void componentMoved(java.awt.event.ComponentEvent evt)
            {
                formComponentMoved(evt);
            }
        });

        tblSelector.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][]
            {
                {null, null, null}
            },
            new String []
            {
                "meta:id", "Name", "Status"
            }
        )
        {
            boolean[] canEdit = new boolean []
            {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex)
            {
                return canEdit [columnIndex];
            }
        });
        tblSelector.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                tblSelectorMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblSelector);
        tblSelector.setColumnModel(new HidableTableColumnModel(tblSelector.getColumnModel()));
        tblSelector.getColumnModel().getColumn(0).setResizable(false);
        tblSelector.getColumnModel().getColumn(0).setPreferredWidth(30);
        tblSelector.getColumnModel().getColumn(1).setMinWidth(80);
        tblSelector.getColumnModel().getColumn(1).setPreferredWidth(80);
        tblSelector.getColumnModel().getColumn(1).setMaxWidth(80);
        tblSelector.getColumnModel().getColumn(2).setPreferredWidth(80);

        packDesc.setColumns(20);
        packDesc.setRows(5);
        packDesc.addComponentListener(new java.awt.event.ComponentAdapter()
        {
            public void componentMoved(java.awt.event.ComponentEvent evt)
            {
                packDescComponentMoved(evt);
            }
        });
        jScrollPane2.setViewportView(packDesc);

        packTitle.setText("{PackTitle}");

        jButton1.setText("Installation starten");
        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }
        });

        packStatus.setText("{DepDetails}");

        jLabel1.setText("Doppelklicken um Status zu ändern");

        lblVID.setText("izSetup 1.0.0 (c) 2012 iZc");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(34, 34, 34)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(packStatus)
                                    .addComponent(packTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 429, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 12, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 597, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 584, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(8, 8, 8))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblVID)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 642, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(packStatus))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(packTitle)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblVID, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

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

    private void tblSelectorMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tblSelectorMouseClicked
    {//GEN-HEADEREND:event_tblSelectorMouseClicked
        int x = tblSelector.getSelectedRow();
        String packid=(String)((DefaultTableModel)tblSelector.getModel()).getValueAt(x, 0);
        boolean sel = p.packIsScheduled(packid);
        if(evt.getClickCount()==2)
        {
            if(sel==false&&p.packIsInstallable(packid))
            {
                sel=true;
            }
            else
            {
                sel=false;
            }
            p.setPackInstallState(packid, sel);
        }
        rebuildTable();
        String _packTitle = p.getPackName(packid);
        String packDescr = p.getPackDescription(packid);
        packTitle.setText(_packTitle);
        packDesc.setText(packDescr);
    }//GEN-LAST:event_tblSelectorMouseClicked

    private String[] handoverqueue;
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        p.createQueue();
        handoverqueue = p.getQueue();
        new Thread(new Runnable()
        {
            @Override
            @SuppressWarnings("SleepWhileInLoop")
            public void run()
            {
                new InstallProgressGui(handoverqueue).setVisible(true);
            }
        }).start();
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void packDescComponentMoved(java.awt.event.ComponentEvent evt)//GEN-FIRST:event_packDescComponentMoved
    {//GEN-HEADEREND:event_packDescComponentMoved
        //NOP
    }//GEN-LAST:event_packDescComponentMoved

    private void formComponentMoved(java.awt.event.ComponentEvent evt)//GEN-FIRST:event_formComponentMoved
    {//GEN-HEADEREND:event_formComponentMoved
        ModeSel.topleft_coords_x = getLocation().x;
        ModeSel.topleft_coords_y = getLocation().x;
    }//GEN-LAST:event_formComponentMoved

    private void rebuildTable()
    {
        String[] tablefill = p.getPackList();
        for (int i = 0; i < tablefill.length; i++)
        {
            String GRP = tablefill[i];
            String[] data = GRP.split("\\|");
            String packid = data[0];
            ((DefaultTableModel)tblSelector.getModel()).setValueAt(data[0], i, 0);
            ((DefaultTableModel)tblSelector.getModel()).setValueAt(data[1], i, 1);
            ((DefaultTableModel)tblSelector.getModel()).setValueAt("0xFA11", i, 2);
            if(p.packIsScheduled(packid))
            {
                Tools.DebugHelperPrint(packid+"|sched", true, "iZsetup.enableFinerDebug");
                ((DefaultTableModel)tblSelector.getModel()).setValueAt("Wird installiert", i, 2);
            }
            else
            {
                if(p.packIsInstalled(data[0]))
                {
                    Tools.DebugHelperPrint(packid+"|exis", true, "iZsetup.enableFinerDebug");
                    ((DefaultTableModel)tblSelector.getModel()).setValueAt("Bereits installiert", i, 2);
                }
                else
                {
                    if(!p.packIsInstallable(packid))
                    {
                        String depName = ""; 
                        String depID = p.packDependency(data[0]);
                        try
                        {
                        depName = p.getPackName(depID);
                        }
                        catch(Exception e)
                        {
                        }
                        Tools.DebugHelperPrint(packid+"|reqdep:"+depID, true, "iZsetup.enableFinerDebug");
                        ((DefaultTableModel)tblSelector.getModel()).setValueAt(depName+" benötigt", i, 2);
                    }
                    else
                    {
                        ((DefaultTableModel)tblSelector.getModel()).setValueAt("Abgewählt", i, 2);
                    }
                }
            }
        }
    }
            
    
    /**
     * @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(PackagerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(PackagerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(PackagerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(PackagerGUI.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 PackagerGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JLabel lblVID;
    private javax.swing.JTextArea packDesc;
    private javax.swing.JLabel packStatus;
    private javax.swing.JLabel packTitle;
    private javax.swing.JTable tblSelector;
    // End of variables declaration//GEN-END:variables
}
