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

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

import cz.vutbr.feec.utko.fojtoval.krypto.doc.Napoveda;
import cz.vutbr.feec.utko.fojtoval.krypto.windows.data.TextDoTabulky;
import cz.vutbr.feec.utko.fojtoval.krypto.sifry.AfinniSifra;
import cz.vutbr.feec.utko.fojtoval.krypto.funkce.Eic;
import cz.vutbr.feec.utko.fojtoval.krypto.funkce.Euclides;
import cz.vutbr.feec.utko.fojtoval.krypto.funkce.Fa;
import cz.vutbr.feec.utko.fojtoval.krypto.funkce.FactorizeRec;
import cz.vutbr.feec.utko.fojtoval.krypto.funkce.FileMy;
import cz.vutbr.feec.utko.fojtoval.krypto.windows.data.Graf;
import cz.vutbr.feec.utko.fojtoval.krypto.funkce.Ic;
import cz.vutbr.feec.utko.fojtoval.krypto.sifry.JednoduchaTranspozice;
import cz.vutbr.feec.utko.fojtoval.krypto.sifry.JednoduchyPosun;
import cz.vutbr.feec.utko.fojtoval.krypto.sifry.NahodnaAbeceda;
import cz.vutbr.feec.utko.fojtoval.krypto.sifry.Sifra;
import cz.vutbr.feec.utko.fojtoval.krypto.sifry.SubstituceKlic;
import cz.vutbr.feec.utko.fojtoval.krypto.windows.data.SubstituceZadaniAbecedyEvents;
import cz.vutbr.feec.utko.fojtoval.krypto.windows.data.TextDoTabulkyEvents;
import cz.vutbr.feec.utko.fojtoval.krypto.funkce.UpravaTextu;
import cz.vutbr.feec.utko.fojtoval.krypto.sifry.Utils;
import cz.vutbr.feec.utko.fojtoval.krypto.sifry.Vigenere;
import cz.vutbr.feec.utko.fojtoval.krypto.sifry.VigenereAutoklic;
import cz.vutbr.feec.utko.fojtoval.krypto.funkce.VymenaPismen;
import cz.vutbr.feec.utko.fojtoval.krypto.funkce.Validator;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;
import javax.help.CSH;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.jdesktop.application.SingleFrameApplication;
import org.openide.util.Exceptions;

/**
 * Obsluha udalosti hlavniho okna NavrhView a metody pro obsluh techto udalosti.
 * @author fojtoval
 */
public class NavrhViewEvents extends NavrhView {

    //DefaultTableCellRenderer render = new DefaultTableCellRenderer();
    private javax.swing.JTextPane mytextpane;
    private ZobrazeniIc novyic = new ZobrazeniIc();
    private ZobrazeniFa novygraf = new ZobrazeniFa();
    private Ntice ntice = new Ntice();
    private Dvojice bitri = new Dvojice();
    private IndexyAbecedy ia = new IndexyAbecedy();

    public NavrhViewEvents(SingleFrameApplication app) {
        super(app);
        this.initEvents();
        this.getjLabel3().setText(" a b c d e f g h i j k l m n o p q r s t u v w x y z ");
        this.getjLabel4().setText(" a b c d e f g h i j k l m n o p q r s t u v w x y z ");
    }


    public void initEvents(){

        this.getjTextPane1().getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent evt) {
                tchange = true;
                labelPocetZnakuVstup(evt);
            }
            public void removeUpdate(DocumentEvent evt) {
                tchange = true;
                labelPocetZnakuVstup(evt);
            }
            public void insertUpdate(DocumentEvent evt) {
                tchange = true;
                labelPocetZnakuVstup(evt);
            }
        });

        this.getjTextPane2().getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent evt) {
                labelPocetZnakuVystup(evt);
            }
            public void removeUpdate(DocumentEvent evt) {
                labelPocetZnakuVystup(evt);
            }
            public void insertUpdate(DocumentEvent evt) {
                labelPocetZnakuVystup(evt);
            }
        });


        this.getjTextPane1().addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextPane1FocusGained(evt);
            }
        });
        
        this.getjTextPane2().addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextPane2FocusGained(evt);
            }
        });

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        this.getNapoveda().addActionListener(new CSH.DisplayHelpFromSource(Napoveda.showHelp()));

    }

    private void menuSifrujJednoduchyPosun(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
       //     this.getjTextPane1().setText(upravenytext);
            int parametr = -1;
            try {
                parametr = Validator.validint("Zadejte parametr posunu v rozsahu 0 - 25:", 0, 25);
            } catch (Exception ex) {Exceptions.printStackTrace(ex);          }
            if (parametr>-1) {
                Sifra monoalfa = new JednoduchyPosun();
		        monoalfa.setParametr (new Integer (parametr));
                this.getjTextPane2().setText(monoalfa.sifruj(this.getjTextPane1().getText()));
                this.getjLabel4().setText (Utils.abecedaMezery(monoalfa.abecedaSifToString()));
                this.getjLabel5().setText(Integer.toString(parametr));
            }
        }
    }

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

        if (!checkChange(this.getMainPanel())){
            int parametr1=-1;
                try {
                    parametr1 = Validator.validint("Zadejte parametr a v rozsahu 1 - 25:", 1, 25);
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
            if (parametr1>0) {
                if ( Euclides.gcd(parametr1, 26) == 1 ) {
                    int parametr2=-1;
                        try {
                            parametr2 = Validator.validint("Zadejte parametr b v rozsahu 0 - 25:", 0, 25);
                        } catch (Exception ex) {
                            Exceptions.printStackTrace(ex);
                        }
                    if (parametr2>-1) {
                        Sifra afinni = new AfinniSifra();
                        List <Integer> params = new LinkedList <Integer> ();
                        params.add(new Integer(parametr1));
                        params.add(new Integer(parametr2));
                        afinni.setParametr(params);
                        this.getjTextPane2().setText(afinni.sifruj(this.getjTextPane1().getText()));
                        this.getjLabel4().setText (Utils.abecedaMezery(afinni.abecedaSifToString()));
                        this.getjLabel5().setText("a = " + Integer.toString(parametr1) + ", b = " +  Integer.toString(parametr2));
                    }
                } else {
                    JOptionPane.showMessageDialog(this.getMainPanel(), "Nelze jednoznačně zašifrovat!", "Chyba", 0);
                }
            }
        }
    }

    private void menuSifrujZadaniAbecedy(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            SubstituceZadaniAbecedyEvents jsubs = new SubstituceZadaniAbecedyEvents(this, this.getjTextPane1().getText(),true);
            //jsubs.setAlwaysOnTop(true);
            jsubs.setVisible(true);
           this.getjLabel5().setText("-");
        }
    }

    private void menuVymenaVse(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            SubstituceZadaniAbecedyEvents jsubs = new SubstituceZadaniAbecedyEvents(this, this.getjTextPane1().getText(),false);
            jsubs.setVisible(true);
            this.getjLabel5().setText("-");
        }
    }

    private void menuVymenaJedenZnak(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            String parametr1 = "";
                try {
                    parametr1 = Validator.validstring("Zadejte znak, ktery se ma nahradit:", 1, 1);
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
                if (!UpravaTextu.isEmptyOrBlank(parametr1)) {
                    String parametr2 = "";
                    try {
                        parametr2 = Validator.validstring("Zadejte znak, kterym se ma nahradit:", 1, 1);
                    } catch (Exception ex) {
                        Exceptions.printStackTrace(ex);
                    }
                    if (!UpravaTextu.isEmptyOrBlank(parametr2)) {
                        this.setjTextPane2(VymenaPismen.vymenaPismen(parametr1, parametr2, this.getjTextPane1().getText(), this.getjTextPane2()));
                        this.getjLabel4().setText(Utils.abecedaMezery(VymenaPismen.nastavLabel()));
                        this.getjLabel5().setText("-");

                    }
                }
        }
    }

    private void menuSifrujNahodnaAbeceda(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            Sifra nahodna = new NahodnaAbeceda();
            this.getjTextPane2().setText(nahodna.sifruj (this.getjTextPane1().getText()));
		    this.getjLabel4().setText(Utils.abecedaMezery(nahodna.abecedaSifToString()));
            this.getjLabel5().setText("-");
        }
    }

    private void menuSifrujZadaniKlice(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            String key = "";
                try {
                    key = Validator.validstring("Zadejte klíč (slovo o délce 1-26 znaků):", 1, 26);
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
                if (!UpravaTextu.isEmptyOrBlank(key)) {
                    Sifra klic = new SubstituceKlic();
		            klic.setParametr (new String (key));
                    this.getjTextPane2().setText (klic.sifruj (this.getjTextPane1().getText()));
		            this.getjLabel4().setText (Utils.abecedaMezery(klic.abecedaSifToString()));
                    this.getjLabel5().setText(key);
                }
        }
    }

    private void menuSifrujVigenere(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            String key = "";
                try {
                    key = Validator.validstring("Zadejte klíč (slovo o délce 1-30 znaků):", 1, 30);
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
                if (!UpravaTextu.isEmptyOrBlank(key)) {

                    Sifra vigen = new Vigenere();
                    vigen.setParametr (new String (key));
                    this.getjTextPane2().setText (vigen.sifruj (this.getjTextPane1().getText()));
                    this.getjLabel4().setText(" nelze určit ");
                    this.getjLabel5().setText(key);
                }
        }
    }

    private void menuSifrujVigenereAutoklic(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            String key = "";
                try {
                    key = Validator.validstring("Zadejte klíč (jeden znak):", 1, 1);
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
                if (!UpravaTextu.isEmptyOrBlank(key)) {
                    Sifra vigena = new VigenereAutoklic();
                    vigena.setParametr (new String (key));
                    this.getjTextPane2().setText (vigena.sifruj (this.getjTextPane1().getText()));
                    this.getjLabel4().setText(" nelze určit ");
                    this.getjLabel5().setText(key);
                }// else {
                  //  JOptionPane.showMessageDialog(this.getMainPanel(), "Klíčem mu" + this.getjTextPane1().getText().length(), "OK", 1);
               // }
        }
    }

    private void menuSifrujJednoduchaTranspozice(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            String key = "";
                try {
                    key = Validator.validstring("Zadejte klíč (slovo o délce 1-20 znaků, jeho délka určí počet sloupců v tabulce):", 1, 20);
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
                if (!UpravaTextu.isEmptyOrBlank(key)) {
                    Sifra trans = new JednoduchaTranspozice();
                    trans.setParametr (new String (key));
                    this.getjTextPane2().setText (trans.sifruj (this.getjTextPane1().getText()));
                    this.getjLabel4().setText("a b c d e f g h i j k l m n o p q r s t u v w x y z");
                    this.getjLabel5().setText(key);
                }
        }
    }

    private void menuLustiJednoduchyPosun(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            int parametr=-1;
            try {
                parametr = Validator.validint("Zadejte parametr posunu v rozsahu 0 - 25:", 0, 25);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            if (parametr>-1) {
                Sifra jednoposunlus = new JednoduchyPosun();
                jednoposunlus.setParametr (new Integer (parametr));
                this.getjTextPane2().setText (jednoposunlus.lusti (this.getjTextPane1().getText()));
                this.getjLabel4().setText (Utils.abecedaMezery(jednoposunlus.abecedaLusToString()));
                this.getjLabel5().setText(Integer.toString(parametr));
            }
        }
    }

    private void menuLustiAfinniSifra(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            int parametr1=-1;
            try {
                parametr1 = Validator.validint("Zadejte parametr a-1 v rozsahu 1 - 25:", 1, 25);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            if (parametr1>0) {
                int parametr2=-1;
                try {
                    parametr2 = Validator.validint("Zadejte parametr b v rozsahu 0 - 25:", 0, 25);
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
                if (parametr2>-1) {
                    Sifra afinnilus = new AfinniSifra();
                    List <Integer> params = new LinkedList <Integer> ();
                    params.add (new Integer (parametr1));
                    params.add (new Integer (parametr2));
                    afinnilus.setParametr (params);
                    this.getjTextPane2().setText (afinnilus.lusti (this.getjTextPane1().getText()));
                    this.getjLabel4().setText (Utils.abecedaMezery(afinnilus.abecedaLusToString()));
                    this.getjLabel5().setText("a = " + Integer.toString(parametr1) + ", b = " +  Integer.toString(parametr2));
                }
            }
        }
    }

    private void menuLustiZadaniKlice(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            String key = "";
            try {
                key = Validator.validstring("Zadejte klíč (slovo o délce 1-26 znaků):", 1, 26);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            if (!UpravaTextu.isEmptyOrBlank(key)) {
                Sifra klic = new SubstituceKlic();
		        klic.setParametr (new String (key));
                this.getjTextPane2().setText (klic.lusti (this.getjTextPane1().getText()));
		        this.getjLabel4().setText (Utils.abecedaMezery(klic.abecedaLusToString()));
                this.getjLabel5().setText(key);
            }
        }
    }

    private void menuLustiVigenere(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            String key = "";
            try {
                key = Validator.validstring("Zadejte klíč (slovo o délce 1-30 znaků):", 1, 30);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            if (!UpravaTextu.isEmptyOrBlank(key) && key.length()<100) {
                Sifra vigen = new Vigenere();
                vigen.setParametr (new String (key));
                this.getjTextPane2().setText (vigen.lusti (this.getjTextPane1().getText()));
                this.getjLabel4().setText(" nelze určit ");
                this.getjLabel5().setText(key);
            }
        }
    }

    private void menuLustiVigenereAutoklic(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            String key = "";
            try {
                key = Validator.validstring("Zadejte klíč (jeden znak):", 1, 1);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            if (!UpravaTextu.isEmptyOrBlank(key)) {
                Sifra vigena = new VigenereAutoklic();
                vigena.setParametr (new String (key));
                this.getjTextPane2().setText (vigena.lusti (this.getjTextPane1().getText()));
                this.getjLabel4().setText(" nelze určit ");
                this.getjLabel5().setText(key);
            }
        }
    }

    private void menuJednoduchaTranspoziceLusteni(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            String key = "";
            try {
                key = Validator.validstring("Zadejte klíč (delka_textu mod(klic)=0):", 1, 20);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }

            if (!UpravaTextu.isEmptyOrBlank(key) && (this.getjTextPane1().getText().length()%key.length()==0)) {

                Sifra trans = new JednoduchaTranspozice();
                trans.setParametr (new String (key));
                this.getjTextPane2().setText (trans.lusti (this.getjTextPane1().getText()));
                this.getjLabel4().setText("a b c d e f g h i j k l m n o p q r s t u v w x y z");
                this.getjLabel5().setText(key);
                
            } else { JOptionPane.showMessageDialog(this.getMainPanel(), "Chybně zadaný klíč!", "Chyba", 0);

                   }
        }
    }

    private void menuFreqa(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            double[][] d = Fa.naplnprvnitabulku(this.getjTextPane1().getText());
            String[][] e = Fa.naplndruhoutabulku(this.getjTextPane1().getText(),UpravaTextu.isEmptyOrBlank(this.getjTextPane1().getText()));
            System.out.println(e[1][0]);
            System.out.println(e[1][1]);

            DecimalFormat mform = new DecimalFormat("##.###");

            for (int i = 0; i < 26; i++) {
                //novygraf.getjTable1().getColumnModel().getColumn(i).setCellRenderer(render);
                novygraf.getjTable1().setValueAt(mform.format(d[0][i]), 0, i);
                novygraf.getjTable1().setValueAt(mform.format(d[1][i]), 1, i);
                novygraf.getjTable2().setValueAt(e[0][i], 0, i);
                novygraf.getjTable2().setValueAt(e[1][i], 1, i);
            }

            novygraf.getJLabel4().setText("Počet znaků analyzovaného textu je: " + this.getjTextPane1().getText().length());
            BufferedImage picture = Graf.zobrazGraf(Fa.procFa(this.getjTextPane1().getText()));
            novygraf.getJLabel1().setIcon(new ImageIcon(picture));
            novygraf.setVisible(true);
        }
    }

    private void menuIc(java.awt.event.ActionEvent evt) {
        //ZobrazeniIc novyic = new ZobrazeniIc();
        if (!checkChange(this.getMainPanel())){
            novyic.getjLabel2().setText("Počet znaků analyzovaného textu je: " + this.getjTextPane1().getText().length());
            novyic.getjLabel1().setText("Index koincidence textu zadaného v okně A je IC = " + String.format("%2.4f", Ic.ic(this.getjTextPane1().getText())) + ".");
            novyic.setVisible(true);

        }
    }

    private void menuBiTrigramy(java.awt.event.ActionEvent evt) {
            bitri.setVisible(true);
    }

    private void menuOtevriSoubor(java.awt.event.ActionEvent evt) {
        String soubor = "";
        if  (this.getjFileChooser1().showOpenDialog(this.getOpenFile()) == JFileChooser.APPROVE_OPTION) {
            File file = getjFileChooser1().getSelectedFile();
            soubor = file.getAbsolutePath();
            this.getjTextPane1().setText(FileMy.nactiSoubor(soubor));
        }
        
    }

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

        if  (this.getjFileChooser1().showSaveDialog(this.getOpenFile()) == JFileChooser.APPROVE_OPTION) {
            File file = this.getjFileChooser1().getSelectedFile();
            if (!file.getName().toLowerCase().endsWith(".txt")) {
                file = new File(file.getAbsolutePath() + ".txt");
            }
            FileWriter fw = null;
            try {
                String s = mytextpane.getText();
                fw = new FileWriter(file);
                fw.write(s);
            } catch (IOException ex) {
                //Logger.getLogger(NavrhView.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this.getMainPanel(), "Soubor se nepodařilo uložit.", "Chyba", 0);
            } finally {
                try {
                    fw.close();
                } catch (IOException ex) {
                    //Logger.getLogger(NavrhView.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }

    private void menuRozkladPrvocisla(java.awt.event.ActionEvent evt) {
        int parametr = -1;
        try {
            parametr = Validator.validint("Zadejte číslo (1-10000):", 1, 10000);
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }
        if (parametr > 0) {
            FactorizeRec.prvocisla = "";
            FactorizeRec.facRec(true, parametr);
            JOptionPane.showMessageDialog(this.getMainPanel(), "Rozklad čísla " + parametr + " na prvočísla: " + FactorizeRec.prvocisla, "OK", 1);
        }
    }


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

        int vysledek = 0;
        int parametr1 = -1;
        
        try {
            parametr1 = Validator.validint("Zadejte a:", 0, 10000);
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }
        if (parametr1 > 0) {
            int parametr2 = -1;
            try {
                parametr2 = Validator.validint("Zadejte b:", 0, 10000);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            if (parametr2 > 0) {
                vysledek = Euclides.gcd(parametr1, parametr2);
                JOptionPane.showMessageDialog(this.getMainPanel(), "Vysledek je: " + vysledek, "OK", 1);
            }
        }

    }

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

        int parametr1 = -1;
        try {
            parametr1 = Validator.validint("Zadejte Zx (obvykle 26):", 1, 10000);
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }
        if (parametr1  >0) {
            int parametr2 = -1;
            try {
                parametr2 = Validator.validint("Zadejte a (obvykle 1-26):", 1, 10000);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            if (parametr2 > 0)
                JOptionPane.showMessageDialog(this.getMainPanel(), "Multiplikativni inverze cisla " + parametr2 +
                    " v Z(" + parametr1 + ") je: "+ Euclides.euc(parametr1, parametr2), "OK", 1);
        }

    }

    private void menuPrevodTabulka(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            int parametr1 = -1;
            try {
                parametr1 = Validator.validint("Zadejte počet sloupců (1 - " + this.getjTextPane1().getText().length() + "):", 1, this.getjTextPane1().getText().length());
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            if (parametr1 > 0){
                int maxtab = this.getjTextPane1().getText().length()/parametr1;
                if (this.getjTextPane1().getText().length() % parametr1>0) {
                    maxtab++;
                }
                    TextDoTabulky tabulky = new TextDoTabulkyEvents(this, getjTextPane1().getText(), parametr1,maxtab);
                    tabulky.setVisible(true);

            }
        }
    }

    private void menuEditujText(java.awt.event.ActionEvent evt) {
        boolean tmp = editujText();
    }

    private void menuVyhledaniNtic(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            ntice.delejTab(Ntice.delejNtice(this.getjTextPane1().getText()));
            ntice.setVisible(true);
        }
    }

    private void buttonVymazVpravo(java.awt.event.ActionEvent evt) {
        this.getjTextPane2().setText("");
    }

    private void buttonVymazVlevo(java.awt.event.ActionEvent evt) {
        this.getjTextPane1().setText("");
    }

    private void buttonVymaz(java.awt.event.ActionEvent evt) {
        this.getjTextPane1().setText("");
        this.getjTextPane2().setText("");

    }

    private void buttonPresunVlevo(java.awt.event.ActionEvent evt) {
        this.getjTextPane1().setText(this.getjTextPane2().getText());
    }


    public String upravenytext = "";
    public boolean tchange;



    public boolean editujText(){
        String normalized_string = java.text.Normalizer.normalize(this.getjTextPane1().getText(), java.text.Normalizer.Form.NFD);  // odstraneni diakritiky
        String ascii = normalized_string.replaceAll("[^\\p{ASCII}]","");                                  // odstraneni diakritiky
        String mala = ascii.toLowerCase();  // vse na mala pismena
        this.getjTextPane1().setText(mala.replaceAll("[^abcdefghijklmnopqrstuvwxyz]",""));
        return(tchange = false);

   }
/**
 * Slouzi pro kontrolu obsahu vstupni textoveho okna - pokud byl upraven, vynuti jeho editaci (odstra-
 * neni diaktiriky atd.), jinak se pozadovane metody neprovedou !
 * @param p
 * @return
 */

    public boolean checkChange(JPanel p) {
       if (tchange) {
           int temp = JOptionPane.showConfirmDialog(p, "Vstupni text byl editovan, ale nebyl upraven! Upravit?", "Upozornění", JOptionPane.YES_NO_OPTION);
            if (temp == 0){
                boolean tmp = editujText();
            }
       }
       return(tchange);
    }

    private void menuSelect(java.awt.event.ActionEvent evt) {
        mytextpane.selectAll();
    }

    private void menuPaste(java.awt.event.ActionEvent evt) {
        UpravaTextu.Paste(mytextpane);
    }

    private void menuCopy(java.awt.event.ActionEvent evt) {
        UpravaTextu.Copy(mytextpane);
    }

    private void menuDelete(java.awt.event.ActionEvent evt) {
        mytextpane.replaceSelection("");
    }

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

        if (!checkChange(this.getMainPanel()) && !UpravaTextu.isEmptyOrBlank(this.getjTextPane1().getText())){
            int parametr1 = -1;
            int maxdhesla = 30;
            if (this.getjTextPane1().getText().length() < 30) {
                maxdhesla = this.getjTextPane1().getText().length();
            }
            try {
                parametr1 = Validator.validint("Zadejte předpokládanou délku hesla (1 - " + maxdhesla + "):", 1, maxdhesla);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            if (parametr1 > 0){
                double sumaindex = 0;
                for (int i = 0; i < parametr1; i++) {
                    double index = Ic.ic(Utils.Xtice(this.getjTextPane1().getText(), parametr1, i));
                    sumaindex += index;

                }
                JOptionPane.showMessageDialog(this.getMainPanel(), "Pro délku hesla " + parametr1 + " je průměrná hodnota IC skupin subtextů " + sumaindex/parametr1 +  ".", "OK", 1);

            }
        }

    }

    private void menuEic(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel()) && (!UpravaTextu.isEmptyOrBlank(this.getjTextPane1().getText()))){
            int parametr = -1;
            int maxdhesla = 30;
            if (this.getjTextPane1().getText().length() < 30) {
                maxdhesla = this.getjTextPane1().getText().length();
            }
            try {
                parametr = Validator.validint("Zadejte délku klíče u v rozsahu (1 - "+maxdhesla+"): ", 1, maxdhesla);
            } catch (Exception ex) {Exceptions.printStackTrace(ex);          }
            if (parametr > -1) {
                double ic = Ic.ic(this.getjTextPane1().getText());
                double odhadic = Eic.eic(parametr, this.getjTextPane1().getText().length());
                JOptionPane.showMessageDialog(this.getMainPanel(), "Pro délku klíče " + parametr + " je očekáváná hodnota E(IC) = " + odhadic +  ".", "OK", 1);
            }

        }
        
    }

    private void jTextPane1FocusGained(java.awt.event.FocusEvent evt) {
        mytextpane = this.getjTextPane1();
    }

    private void jTextPane2FocusGained(java.awt.event.FocusEvent evt) {
        mytextpane = this.getjTextPane2();
    }

    private void menuVzdalenosti(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            String retezec = "";
            try {
                retezec = Validator.validstring("Zadejte hledaný řetězec (2-10 znaků):", 2, 10);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            if (!UpravaTextu.isEmptyOrBlank(retezec)) {
                String vyskyt = "";
                vyskyt = Utils.hledejVzdalenosti(this.getjTextPane1().getText(), retezec);
                JOptionPane.showMessageDialog(this.getMainPanel(), "Vzdalenost(i) mezi retezci: " + vyskyt, "OK", 1);
            } else {
                  JOptionPane.showMessageDialog(this.getMainPanel(), "Hledany retezec se v textu nevyskytuje!", "OK", 1);
            }

        }
    }

    private void menuSubtext(java.awt.event.ActionEvent evt) {
        if (!checkChange(this.getMainPanel())){
            int parametr1 = -1;
            int maxxtice = 30;
            if (this.getjTextPane1().getText().length()<30) {
                maxxtice = this.getjTextPane1().getText().length();
            }
            try {
                parametr1 = Validator.validint("Zadejte vzdálenost znaků (1 - " + maxxtice + "):", 1, maxxtice);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
            if (parametr1 > 0){
                int parametr2 = -1;
                int maxodsaz = parametr1 - 1;
                try {
                    parametr2 = Validator.validint("Zadejte pozici počátečního znaku (0 - " + maxodsaz + "):", 0, maxodsaz);
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
                if (parametr2 > -1){
                    this.getjTextPane2().setText(Utils.Xtice(this.getjTextPane1().getText(), parametr1, parametr2));
                }
            }
        }
    }

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

            ia.setVisible(true);

    }

    private void buttonVynuluj(java.awt.event.ActionEvent evt) {
        this.getjLabel4().setText(" a b c d e f g h i j k l m n o p q r s t u v w x y z ");
        this.getjLabel5().setText("-");
    }

    private void labelPocetZnakuVstup(DocumentEvent evt) {
        this.getjLabel10().setText(Integer.toString(this.getjTextPane1().getText().length()));
    }

   private void labelPocetZnakuVystup(DocumentEvent evt) {
        this.getjLabel12().setText(Integer.toString(this.getjTextPane2().getText().length()));
    }

}
