/*
 * 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 puntocassa;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Marco
 */
public class MyKeyBoard extends javax.swing.JFrame {

    public JTextField txt;
    public JFrame parent;
    Boolean formCerca = false;

    /**
     * Creates new form MyKeyBoard
     */
    public MyKeyBoard(JFrame parent, Boolean ricerca) {
        initComponents();
        creaPulsanti();
        this.parent = parent;
        formCerca = ricerca;
        this.parent.setEnabled(false);
        String startDir = System.getProperty("user.dir");
        File iconaApp = new File(startDir + "/risorse/logo.png");
        if (iconaApp.exists()) {

            ImageIcon icon = new ImageIcon(startDir + "/risorse/logo.png");
            this.setIconImage(icon.getImage());
        }

    }

    private void creaPulsanti() {
        String[] tasti = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "/", "ì"};
        crea(tasti, jPanel1);
        String[] tasti2 = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "è", "é"};
        crea(tasti2, jPanel2);
        String[] tasti3 = {"a", "s", "d", "f", "g", "h", "j", "k", "l", "ò", "à", "ù"};
        crea(tasti3, jPanel3);
        String[] tasti4 = {"z", "x", "c", "v", "b", "n", "m", ",", ".", ":", "-", "'"};
        crea(tasti4, jPanel4);
    }

    private void crea(String[] tasti, JPanel pnl) {
        ActionListener listenerBottoni = new ListenerPulsanti();
        for (int i = 0; i < tasti.length; i++) {
            JButton btn = new JButton(tasti[i]);
            btn.setFont(new java.awt.Font("Tahoma", 1, 12));
            pnl.add(btn);
            btn.addActionListener(listenerBottoni);

        }

    }

    /**
     * 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() {

        jPanel1 = new javax.swing.JPanel();
        jBtnCanc = new javax.swing.JButton();
        jBtnInvio = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jBtnSpazio = new javax.swing.JButton();
        jBtnShift = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        jBtnCanc.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jBtnCanc.setText("Canc");
        jBtnCanc.setToolTipText("");
        jBtnCanc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancActionPerformed(evt);
            }
        });

        jBtnInvio.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jBtnInvio.setText("Invio");
        jBtnInvio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnInvioActionPerformed(evt);
            }
        });

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jPanel4.setLayout(new java.awt.GridLayout(1, 0));

        jPanel3.setLayout(new java.awt.GridLayout(1, 0));

        jBtnSpazio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSpazioActionPerformed(evt);
            }
        });

        jBtnShift.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jBtnShift.setText("Maiuscolo");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(6, 6, 6))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jBtnSpazio, javax.swing.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
                        .addGap(14, 14, 14)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jBtnCanc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnInvio, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                    .addComponent(jBtnShift, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                .addGap(5, 5, 5)
                .addComponent(jBtnSpazio, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jBtnCanc, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBtnInvio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBtnShift, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

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

    private void jBtnCancActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancActionPerformed
        // TODO add your handling code here:
        try {
            int d = txt.getText().length();
            if (d > 0) {
                txt.setText(txt.getText().substring(0, d - 1));

            }

        } catch (Exception e) {
        }
    }//GEN-LAST:event_jBtnCancActionPerformed

    private void jBtnInvioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnInvioActionPerformed

        if (formCerca) {
            FrmCerca frm;
            frm = (FrmCerca) parent;
            frm.Cerca();
        }

        parent.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_jBtnInvioActionPerformed

    private void jBtnSpazioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSpazioActionPerformed
        // TODO add your handling code here:
        txt.setText(txt.getText() + " ");
    }//GEN-LAST:event_jBtnSpazioActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        parent.setEnabled(true);

        this.dispose();
    }//GEN-LAST:event_formWindowClosing
    private class ListenerPulsanti implements ActionListener {

        public void actionPerformed(ActionEvent ev) {
            JButton pressed = (JButton) ev.getSource();
            String c = pressed.getText();
            if (jBtnShift.isSelected()) {
                c = c.toUpperCase();
            }
            txt.setText(txt.getText() + c);

        }
    }

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnCanc;
    private javax.swing.JButton jBtnInvio;
    private javax.swing.JToggleButton jBtnShift;
    private javax.swing.JButton jBtnSpazio;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    // End of variables declaration//GEN-END:variables
}
