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

package cz.vutbr.feec.utko.fojtoval.krypto.windows.data;

import cz.vutbr.feec.utko.fojtoval.krypto.funkce.Validator;
import cz.vutbr.feec.utko.fojtoval.krypto.windows.NavrhViewEvents;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import org.openide.util.Exceptions;

/**
 * Trida pro obsluhu udalosti v TextDoTabulky.
 * @author fojtoval
 */
public class TextDoTabulkyEvents extends TextDoTabulky {

    private String outs;

    /**
     *
     * @param navrhH
     * @param pPFina
     * @param x
     * @param y
     */
    public TextDoTabulkyEvents(NavrhViewEvents navrhH, String pPFina, int x, int y) {

        super(navrhH, pPFina, x, y);
        this.navrhV = navrhH;
        this.pFina = pPFina;
        this.x = x;
        this.y = y;

        delejTab();
        this.initEvents();
    }

    /**
     * InitEvents.
     */
    public void initEvents(){

        this.getZamenaSloupcu().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonZamenaSloupcu(evt);
            }
        });

        this.getZamenaRadku().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonZamenaRadku(evt);
            }
        });

        this.getUlozdookna().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonUlozdookna(evt);
            }
        });
     }

    private void buttonZamenaSloupcu(java.awt.event.ActionEvent evt) {
    
        int parametr1 = -1;
        try {
            parametr1 = Validator.validint("Zadejte 1. sloupec (1-" + x + "):", 1, x);
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }

        if (parametr1 > 0) {
            int parametr2 = -1;
            try {
                parametr2 = Validator.validint("Zadejte 2. sloupec (1-" + x + "):", 1, x);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            //int parametr2 = Integer.parseInt(JOptionPane.showInputDialog(jButton1, "Zadejte 2. sloupec:"));
            if ((parametr2 > 0) && (parametr1 != parametr2)) {
                parametr1--;
                parametr2--;
                Object sloupec;
                for (int i = 0; i < y; i++) {
                    sloupec = this.getjTable1().getValueAt(i, parametr1);
                    this.getjTable1().setValueAt(this.getjTable1().getValueAt(i, parametr2), i, parametr1);
                    this.getjTable1().setValueAt(sloupec, i, parametr2);
                }
            }
        }

    }

    private void buttonZamenaRadku(java.awt.event.ActionEvent evt) {

        int parametr1 = -1;

        try {
            parametr1 = Validator.validint("Zadejte 1. sloupec (1-" + x + "):", 1, x);
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }

        if (parametr1 > 0) {
            int parametr2 = -1;
            try {
                parametr2 = Validator.validint("Zadejte 2. sloupec (1-" + x + "):", 1, x);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            //int parametr2 = Integer.parseInt(JOptionPane.showInputDialog(jButton1, "Zadejte 2. sloupec:"));
            if ((parametr2 > 0) && (parametr1 != parametr2)) {
                parametr1--;
                parametr2--;
                Object sloupec;
                for (int i = 0; i < y; i++) {
                    sloupec = this.getjTable1().getValueAt(i, parametr1);
                    this.getjTable1().setValueAt(this.getjTable1().getValueAt(i, parametr2), i, parametr1);
                    this.getjTable1().setValueAt(sloupec, i, parametr2);
                }
            }
        }

    }
    
//    @Override
    /**
     * Vytvoreni tabulky.
     */
    public void delejTab() {

      DefaultTableCellRenderer mujdtcr = new DefaultTableCellRenderer();
      mujdtcr.setHorizontalAlignment(SwingConstants.CENTER);

      //povoluje pretahovani sloupcu
      //jTable1.getTableHeader().setReorderingAllowed(false);
      //jTable1.setRowSelectionAllowed(false);
      //jTable1.setFillsViewportHeight(true);

      int p=0;
      DefaultTableModel tableModel =
        new DefaultTableModel (new Object[][] {{null}},
                               new String [] {""}) {
          @Override
          public boolean isCellEditable (int row, int col) {
            return false;
          }
      };

      this.getjTable1().setModel(tableModel);

      ((DefaultTableModel)this.getjTable1().getModel()).setColumnCount(0);
      ((DefaultTableModel)this.getjTable1().getModel()).removeRow(0);

      for (int i=0;i<x;i++) {
        ((DefaultTableModel)this.getjTable1().getModel()).addColumn(new Object[]{""});
      }

      for (int i=0;i<y;i++) {
        ((DefaultTableModel)this.getjTable1().getModel()).addRow(new Object[]{""});
      }

      for (int j=0; j<x; j++) {

        this.getjTable1().getColumnModel().getColumn(j).setCellRenderer(mujdtcr);
        this.getjTable1().getColumnModel().getColumn(j).setResizable(false);
        this.getjTable1().getColumnModel().getColumn(j).setHeaderValue(Integer.toString(j+1));

        for (int i=0; i<y; i++) {
          if (p==pFina.length()) {
            continue;
          } else {
            this.getjTable1().setValueAt(pFina.charAt(p), i, j);
            p++;
          }
        }

      }

    }

    private void buttonUlozdookna(java.awt.event.ActionEvent evt) {
        outs = "";
        int pcounter = 0;
        for (int i = 0; i < this.getjTable1().getRowCount(); i++) {
            for (int j = 0; j < this.getjTable1().getColumnCount(); j++) {
                if (this.getjTable1().getValueAt(i, j) != null) {
                    pcounter++;
                    outs += this.getjTable1().getValueAt(i, j);
                    if (pcounter%5 == 0){
                        outs += " ";
                    }
                }
            }
        }
        navrhV.getjTextPane2().setText(outs);
        
    }
    



}
