tax.MainForm.java Source code

Java tutorial

Introduction

Here is the source code for tax.MainForm.java

Source

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

import com.itextpdf.text.DocumentException;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.commons.io.FileUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

/**
 *
 * @author ninja
 */
public class MainForm extends javax.swing.JFrame {

    private int curMonth = 0;
    private int curYear = 0;
    private String curTable = "";
    private HashMap afmToName = new HashMap();
    private ArrayList<String> AFMList = new ArrayList<String>();
    private boolean isAliasPromptOpen = false;
    private int lastDateTextLength = 0;
    private int lastAFMTextLength = 0;
    private int lastAFMTextSLength = 0;
    private int lastPriceTextLength = 0;
    private ArrayList<JComponent> textList = new ArrayList<JComponent>();
    private Vector<Component> order = new Vector<Component>();

    /**
     * Creates new form form
     */
    public MainForm() {
        initAFMList();
        initComponents();
        loadNamesToAFM();
        String lastUsedTable = "";
        try {
            lastUsedTable = FileUtils.readFileToString(new File("etc/usedLast"), "UTF-8");
            loadTable(lastUsedTable);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            this.setIconImage((BufferedImage) ImageIO.read(Tax.class.getResource("icons/mainIcon.png")));
        } catch (IOException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }

        textList.add(dateText);
        textList.add(priceText);
        textList.add(afmText);
        textList.add(nameText);

        textList.add(afmTextS);
        textList.add(nameTextS);

        dateText.requestFocusInWindow();

        order.add(dateText);
        order.add(nameTextS);
        this.setFocusTraversalPolicy(new TabPolicy(order));
    }

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

        jMenuItem1 = new javax.swing.JMenuItem();
        bgPan = new javax.swing.JPanel();
        monthLab = new javax.swing.JLabel();
        inputRowPan = new javax.swing.JPanel();
        dateLab = new javax.swing.JLabel();
        dateText = new javax.swing.JTextField();
        priceLab = new javax.swing.JLabel();
        priceText = new javax.swing.JTextField();
        afmLab = new javax.swing.JLabel();
        afmText = new javax.swing.JTextField();
        nameLab = new javax.swing.JLabel();
        nameText = new javax.swing.JTextField();
        addRowPan = new javax.swing.JPanel();
        addRowBut = new javax.swing.JButton();
        delRowBut = new javax.swing.JButton();
        namePan = new javax.swing.JPanel();
        nameToAfm_scroll = new javax.swing.JScrollPane();
        nameToAfm_table = new javax.swing.JTable();
        inputNamePan = new javax.swing.JPanel();
        nameLabS = new javax.swing.JLabel();
        nameTextS = new javax.swing.JTextField();
        afmLabS = new javax.swing.JLabel();
        afmTextS = new javax.swing.JTextField();
        addNamePan = new javax.swing.JPanel();
        addNameBut = new javax.swing.JButton();
        delNameBut = new javax.swing.JButton();
        blackLineLab = new javax.swing.JLabel();
        refreshBut = new javax.swing.JButton();
        db_scroll = new javax.swing.JScrollPane();
        db_table = new javax.swing.JTable();
        monthMenu = new javax.swing.JMenuBar();
        createMonth = new javax.swing.JMenu();
        newMonth = new javax.swing.JMenuItem();
        openMonth = new javax.swing.JMenuItem();
        sep1 = new javax.swing.JPopupMenu.Separator();
        excelMenu = new javax.swing.JMenu();
        excel = new javax.swing.JMenuItem();
        excelYear = new javax.swing.JMenuItem();
        sep2 = new javax.swing.JPopupMenu.Separator();
        printMonth = new javax.swing.JMenuItem();
        printAll = new javax.swing.JMenuItem();
        sep3 = new javax.swing.JPopupMenu.Separator();
        exit = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        about = new javax.swing.JMenuItem();
        help = new javax.swing.JMenuItem();

        jMenuItem1.setText("jMenuItem1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("EnTax");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                formKeyTyped(evt);
            }

            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridLayout(2, 0));

        bgPan.setBackground(new java.awt.Color(204, 204, 204));
        bgPan.setLayout(null);

        monthLab.setBackground(new java.awt.Color(255, 153, 0));
        monthLab.setForeground(new java.awt.Color(0, 0, 0));
        monthLab.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        monthLab.setText("");
        monthLab.setOpaque(true);
        bgPan.add(monthLab);
        monthLab.setBounds(-20, 0, 740, 30);

        inputRowPan.setOpaque(false);
        inputRowPan.setLayout(new java.awt.GridLayout(4, 2, 2, 2));

        dateLab.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        dateLab.setText("?");
        inputRowPan.add(dateLab);

        dateText.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        dateText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                dateTextFocusGained(evt);
            }
        });
        dateText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                dateTextKeyTyped(evt);
            }

            public void keyPressed(java.awt.event.KeyEvent evt) {
                dateTextKeyPressed(evt);
            }

            public void keyReleased(java.awt.event.KeyEvent evt) {
                dateTextKeyReleased(evt);
            }
        });
        inputRowPan.add(dateText);

        priceLab.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        priceLab.setText("");
        inputRowPan.add(priceLab);

        priceText.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        priceText.setEnabled(false);
        priceText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                priceTextActionPerformed(evt);
            }
        });
        priceText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                priceTextKeyPressed(evt);
            }

            public void keyReleased(java.awt.event.KeyEvent evt) {
                priceTextKeyReleased(evt);
            }
        });
        inputRowPan.add(priceText);

        afmLab.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        afmLab.setText("");
        inputRowPan.add(afmLab);

        afmText.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        afmText.setEnabled(false);
        afmText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                afmTextKeyPressed(evt);
            }

            public void keyReleased(java.awt.event.KeyEvent evt) {
                afmTextKeyReleased(evt);
            }
        });
        inputRowPan.add(afmText);

        nameLab.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        nameLab.setText("");
        inputRowPan.add(nameLab);

        nameText.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        nameText.setEnabled(false);
        nameText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                nameTextKeyPressed(evt);
            }
        });
        inputRowPan.add(nameText);

        bgPan.add(inputRowPan);
        inputRowPan.setBounds(20, 50, 220, 100);

        addRowPan.setOpaque(false);

        addRowBut.setText("?");
        addRowBut.setEnabled(false);
        addRowBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addRowButActionPerformed(evt);
            }
        });
        addRowPan.add(addRowBut);

        delRowBut.setText("?");
        delRowBut.setEnabled(false);
        delRowBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delRowButActionPerformed(evt);
            }
        });
        addRowPan.add(delRowBut);

        bgPan.add(addRowPan);
        addRowPan.setBounds(20, 160, 250, 40);

        namePan.setOpaque(false);

        nameToAfm_scroll.setPreferredSize(new java.awt.Dimension(170, 130));

        nameToAfm_table.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "", "" }) {
            Class[] types = new Class[] { java.lang.String.class, java.lang.String.class };
            boolean[] canEdit = new boolean[] { false, false };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        nameToAfm_table.getTableHeader().setReorderingAllowed(false);
        nameToAfm_scroll.setViewportView(nameToAfm_table);

        namePan.add(nameToAfm_scroll);

        bgPan.add(namePan);
        namePan.setBounds(310, 47, 170, 140);

        inputNamePan.setOpaque(false);
        inputNamePan.setLayout(new java.awt.GridLayout(2, 2, 2, 2));

        nameLabS.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        nameLabS.setText("");
        inputNamePan.add(nameLabS);

        nameTextS.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        nameTextS.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                nameTextSFocusGained(evt);
            }
        });
        nameTextS.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                nameTextSKeyPressed(evt);
            }
        });
        inputNamePan.add(nameTextS);

        afmLabS.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        afmLabS.setText("");
        inputNamePan.add(afmLabS);

        afmTextS.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        afmTextS.setEnabled(false);
        afmTextS.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                afmTextSKeyPressed(evt);
            }

            public void keyReleased(java.awt.event.KeyEvent evt) {
                afmTextSKeyReleased(evt);
            }
        });
        inputNamePan.add(afmTextS);

        bgPan.add(inputNamePan);
        inputNamePan.setBounds(490, 50, 190, 50);

        addNamePan.setOpaque(false);

        addNameBut.setText("?");
        addNameBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNameButActionPerformed(evt);
            }
        });
        addNamePan.add(addNameBut);

        delNameBut.setText("?");
        delNameBut.setEnabled(false);
        delNameBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delNameButActionPerformed(evt);
            }
        });
        addNamePan.add(delNameBut);

        bgPan.add(addNamePan);
        addNamePan.setBounds(490, 110, 220, 40);

        blackLineLab.setBackground(new java.awt.Color(0, 0, 0));
        blackLineLab.setOpaque(true);
        bgPan.add(blackLineLab);
        blackLineLab.setBounds(-9, 30, 730, 5);

        refreshBut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tax/icons/refresh.png"))); // NOI18N
        refreshBut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButActionPerformed(evt);
            }
        });
        bgPan.add(refreshBut);
        refreshBut.setBounds(524, 145, 153, 40);

        getContentPane().add(bgPan);

        db_table.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "?", "", "", "" }) {
            Class[] types = new Class[] { java.lang.String.class, java.lang.Double.class, java.lang.String.class,
                    java.lang.String.class };
            boolean[] canEdit = new boolean[] { false, false, false, false };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        db_table.getTableHeader().setReorderingAllowed(false);
        db_scroll.setViewportView(db_table);

        getContentPane().add(db_scroll);

        createMonth.setText("");

        newMonth.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tax/icons/newMonth.png"))); // NOI18N
        newMonth.setText("?");
        newMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMonthActionPerformed(evt);
            }
        });
        createMonth.add(newMonth);

        openMonth.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tax/icons/openMonth.png"))); // NOI18N
        openMonth.setText("");
        openMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMonthActionPerformed(evt);
            }
        });
        createMonth.add(openMonth);
        createMonth.add(sep1);

        excelMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tax/icons/export.png"))); // NOI18N
        excelMenu.setText("");

        excel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tax/icons/excel.png"))); // NOI18N
        excel.setText("Excel");
        excel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excelActionPerformed(evt);
            }
        });
        excelMenu.add(excel);

        excelYear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tax/icons/excel.png"))); // NOI18N
        excelYear.setText("Excel ");
        excelYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excelYearActionPerformed(evt);
            }
        });
        excelMenu.add(excelYear);

        createMonth.add(excelMenu);
        createMonth.add(sep2);

        printMonth.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tax/icons/print.png"))); // NOI18N
        printMonth.setText("?");
        printMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printMonthActionPerformed(evt);
            }
        });
        createMonth.add(printMonth);

        printAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tax/icons/print.png"))); // NOI18N
        printAll.setText("? ");
        printAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printAllActionPerformed(evt);
            }
        });
        createMonth.add(printAll);
        createMonth.add(sep3);

        exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tax/icons/exit.png"))); // NOI18N
        exit.setText("");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });
        createMonth.add(exit);

        monthMenu.add(createMonth);

        helpMenu.setText("");

        about.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tax/icons/help.png"))); // NOI18N
        about.setText("");
        about.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutActionPerformed(evt);
            }
        });
        helpMenu.add(about);

        help.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tax/icons/info.png"))); // NOI18N
        help.setText("");
        help.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpActionPerformed(evt);
            }
        });
        helpMenu.add(help);

        monthMenu.add(helpMenu);

        setJMenuBar(monthMenu);

        setSize(new java.awt.Dimension(719, 447));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void newMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMonthActionPerformed
        new NewMonthPrompt(this).setVisible(true);
    }//GEN-LAST:event_newMonthActionPerformed

    private void openMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMonthActionPerformed
        new OpenMonthPrompt(this).setVisible(true);
    }//GEN-LAST:event_openMonthActionPerformed

    private void delRowButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delRowButActionPerformed
        removeLastRow();
        dateText.requestFocus();
    }//GEN-LAST:event_delRowButActionPerformed

    private void addRowButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addRowButActionPerformed
        String afm = afmText.getText();

        if (AFMList.contains(afm) && !afmToName.containsValue(afm) && !isAliasPromptOpen) {
            new AliasPrompt(this).setVisible(true);
            isAliasPromptOpen = true;
        } else {
            if (isAliasPromptOpen)
                isAliasPromptOpen = false;

            addRow();
            dateText.requestFocus();
        }
    }//GEN-LAST:event_addRowButActionPerformed

    private void dateTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dateTextKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            if (!dateText.getText().equals("")) {
                Util.fadeInAndOut(dateText, Util.darkGreen);

                priceText.setEnabled(true);
                priceText.requestFocus();
            } else
                Util.fadeInAndOut(dateText, Util.darkOrange);
        } else if (!evt.isActionKey() && !evt.isAltDown() && !evt.isControlDown() && !evt.isShiftDown()
                && !evt.isMetaDown() && (evt.getKeyCode() != KeyEvent.VK_BACK_SPACE)
                && (evt.getKeyCode() != KeyEvent.VK_DELETE) && (evt.getKeyCode() != KeyEvent.VK_ESCAPE)) {
            EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                    String text = dateText.getText();
                    int dateLength = text.length();

                    while (lastDateTextLength == dateLength) {
                        try {
                            Thread.sleep(100);
                            System.out.println("text: " + text);
                            System.out.println(lastDateTextLength + " " + dateLength);
                            return;
                        } catch (InterruptedException ex) {
                            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        text = dateText.getText();
                        dateLength = text.length();
                    }
                    try {
                        int num = Integer.decode(text);
                        if (num > 31) {
                            if (text.length() > 0)
                                dateText.setText(text.substring(0, dateLength - 1));
                            else
                                dateText.setText("");
                            return;
                        }
                    } catch (Exception e) {
                        if (text.length() > 0)
                            dateText.setText(text.substring(0, dateLength - 1));
                        else
                            dateText.setText("");
                        return;
                    }
                    lastDateTextLength = dateLength;
                }
            });
        }
    }//GEN-LAST:event_dateTextKeyPressed

    private void priceTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_priceTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_priceTextActionPerformed

    private void priceTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_priceTextKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            if (!priceText.getText().equals("")) {
                Util.fadeInAndOut(priceText, Util.darkGreen);

                afmText.setEnabled(true);
                afmText.requestFocus();
            } else
                Util.fadeInAndOut(priceText, Util.darkOrange);
        } else if (!evt.isActionKey() && !evt.isAltDown() && !evt.isControlDown() && !evt.isShiftDown()
                && !evt.isMetaDown() && (evt.getKeyCode() != KeyEvent.VK_BACK_SPACE)
                && (evt.getKeyCode() != KeyEvent.VK_DELETE) && (evt.getKeyCode() != KeyEvent.VK_ESCAPE)) {
            EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                    String text = priceText.getText();
                    int priceLength = text.length();

                    while (lastPriceTextLength == priceLength) {
                        try {
                            Thread.sleep(100);
                            System.out.println("text: " + text);
                            System.out.println(lastPriceTextLength + " " + priceLength);
                            return;
                        } catch (InterruptedException ex) {
                            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        text = priceText.getText();
                        priceLength = text.length();
                    }

                    //                    if (text.charAt(priceLength - 1) == '\u002c')
                    //                        priceText.setText(text.substring(0, priceLength - 1) + ".");

                    try {
                        Double.parseDouble(text);
                    } catch (Exception e) {
                        if (text.length() > 0) {
                            //                            System.out.println(text.charAt(priceLength - 1));
                            priceText.setText(text.substring(0, priceLength - 1));
                        } else
                            priceText.setText("");
                        return;
                    }
                    lastPriceTextLength = priceLength;
                }

            });
        }
    }//GEN-LAST:event_priceTextKeyPressed

    private void addNameButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNameButActionPerformed
        addNameToAfm();
        nameTextS.requestFocus();
    }//GEN-LAST:event_addNameButActionPerformed

    private void delNameButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delNameButActionPerformed
        removeLastNameToAfm();
    }//GEN-LAST:event_delNameButActionPerformed

    private void afmTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_afmTextKeyPressed
        String text = afmText.getText();

        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            if (text.length() == 9) {

                for (Object key : afmToName.keySet()) {
                    if (afmToName.get(key).equals(text))
                        nameText.setText(key.toString());
                }

                //                Util.fadeInAndOut(afmText, Util.darkGreen);
                addRowBut.doClick();
            } else if (text.length() == 0) {
                Util.fadeInAndOut(afmText, Color.white);
                nameText.setEnabled(true);
                nameText.requestFocus();
            } else
                Util.fadeInAndOut(afmText, Util.darkOrange);
        } else if (!evt.isActionKey() && !evt.isAltDown() && !evt.isControlDown() && !evt.isShiftDown()
                && !evt.isMetaDown() && (evt.getKeyCode() != KeyEvent.VK_BACK_SPACE)
                && (evt.getKeyCode() != KeyEvent.VK_DELETE) && (evt.getKeyCode() != KeyEvent.VK_ESCAPE)) {
            EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                    String text = afmText.getText();
                    int afmLength = text.length();

                    while (lastAFMTextLength == afmLength) {
                        try {
                            Thread.sleep(100);
                            System.out.println("text: " + text);
                            System.out.println(lastAFMTextLength + " " + afmLength);
                            return;
                        } catch (InterruptedException ex) {
                            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        text = afmText.getText();
                        afmLength = text.length();
                    }

                    try {
                        int num = Integer.decode(text);
                        if (afmLength > 9) {
                            if (text.length() > 0)
                                afmText.setText(text.substring(0, afmLength - 1));
                            else
                                afmText.setText("");
                            return;
                        }
                    } catch (Exception e) {
                        if (text.length() > 0)
                            afmText.setText(text.substring(0, afmLength - 1));
                        else
                            afmText.setText("");
                        return;
                    }

                    lastAFMTextLength = afmLength;
                }
            });
        }
    }//GEN-LAST:event_afmTextKeyPressed

    private void nameTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameTextKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            String text = nameText.getText();

            if (text.equals("")) {
                Util.fadeInAndOut(nameText, Color.white);
                afmText.requestFocus();
                return;
            }

            if (afmToName.containsKey(text)) {
                //                Util.fadeInAndOut(afmText, Util.darkGreen);
                //                Util.fadeInAndOut(nameText, Util.darkGreen);
                afmText.setText((String) afmToName.get(text));
                addRowBut.doClick();
            } else {
                Util.fadeInAndOut(nameText, Util.darkOrange);
                nameText.setText("");
            }
        }
    }//GEN-LAST:event_nameTextKeyPressed

    private void nameTextSKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameTextSKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            if (!afmToName.containsKey(nameTextS.getText()) && !nameTextS.getText().equals("")) {

                afmTextS.setEnabled(true);
                afmTextS.requestFocus();
                Util.fadeInAndOut(nameTextS, Util.darkGreen);
            } else {
                nameTextS.setText("");
                Util.fadeInAndOut(nameTextS, Util.darkOrange);
            }
        }
    }//GEN-LAST:event_nameTextSKeyPressed

    private void afmTextSKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_afmTextSKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            String text = afmTextS.getText();

            if (text.length() == 9) {
                try {
                    int num = Integer.parseInt(text);
                } catch (Exception e) {
                    afmTextS.setText("");
                    return;
                }

                //                Util.fadeInAndOut(afmTextS, Util.darkGreen);
                addNameBut.doClick();
            } else {
                Util.fadeInAndOut(afmTextS, Util.darkOrange);
            }
        } else if (!evt.isActionKey() && !evt.isAltDown() && !evt.isControlDown() && !evt.isShiftDown()
                && !evt.isMetaDown() && (evt.getKeyCode() != KeyEvent.VK_BACK_SPACE)
                && (evt.getKeyCode() != KeyEvent.VK_DELETE) && (evt.getKeyCode() != KeyEvent.VK_ESCAPE)) {
            EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                    String text = afmTextS.getText();
                    int afmLength = text.length();

                    while (lastAFMTextSLength == afmLength) {
                        try {
                            Thread.sleep(100);
                            System.out.println("text: " + text);
                            System.out.println(lastAFMTextSLength + " " + afmLength);
                            return;
                        } catch (InterruptedException ex) {
                            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        text = afmTextS.getText();
                        afmLength = text.length();
                    }

                    try {
                        int num = Integer.decode(text);
                        if (afmLength > 9) {
                            if (text.length() > 0)
                                afmTextS.setText(text.substring(0, afmLength - 1));
                            else
                                afmTextS.setText("");
                            return;
                        }
                    } catch (Exception e) {
                        if (text.length() > 0)
                            afmTextS.setText(text.substring(0, afmLength - 1));
                        else
                            afmTextS.setText("");
                        return;
                    }

                    lastAFMTextSLength = afmLength;
                }
            });
        }
    }//GEN-LAST:event_afmTextSKeyPressed

    private void printMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printMonthActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(" ");
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String folderName = chooser.getSelectedFile().getAbsolutePath();
            //            System.out.println(folderName);

            try {
                FileUtils.forceMkdir(new File(folderName + "/" + curYear + "/pdf"));
                Printer.createPdf(
                        folderName + "/" + curYear + "/pdf/" + curMonth + ". " + Util.months[curMonth - 1] + ".pdf",
                        curTable);
            } catch (IOException ex) {
                Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            } catch (DocumentException ex) {
                Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            }

            new Completed("<html> ? ?<br> .</html>")
                    .setVisible(true);
        }
    }//GEN-LAST:event_printMonthActionPerformed

    private void printAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printAllActionPerformed
        new YearChooserPrompt(this, Util.PDF).setVisible(true);
    }//GEN-LAST:event_printAllActionPerformed

    private void dateTextKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dateTextKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_dateTextKeyTyped

    private void dateTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dateTextKeyReleased
        int keyCode = evt.getKeyCode();
        if ((keyCode == KeyEvent.VK_BACK_SPACE) || (keyCode == KeyEvent.VK_DELETE)) {
            lastDateTextLength = dateText.getText().length();
        }
    }//GEN-LAST:event_dateTextKeyReleased

    private void afmTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_afmTextKeyReleased
        int keyCode = evt.getKeyCode();
        if ((keyCode == KeyEvent.VK_BACK_SPACE) || (keyCode == KeyEvent.VK_DELETE)) {
            lastDateTextLength = dateText.getText().length();
        }
    }//GEN-LAST:event_afmTextKeyReleased

    private void afmTextSKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_afmTextSKeyReleased
        int keyCode = evt.getKeyCode();
        if ((keyCode == KeyEvent.VK_BACK_SPACE) || (keyCode == KeyEvent.VK_DELETE)) {
            lastDateTextLength = dateText.getText().length();
        }
    }//GEN-LAST:event_afmTextSKeyReleased

    private void priceTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_priceTextKeyReleased
        int keyCode = evt.getKeyCode();
        if ((keyCode == KeyEvent.VK_BACK_SPACE) || (keyCode == KeyEvent.VK_DELETE)) {
            lastDateTextLength = dateText.getText().length();
        }
    }//GEN-LAST:event_priceTextKeyReleased

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        //        final Container cp = this.getContentPane();
        //        final Graphics g = cp.getGraphics();
        //        
        //        EventQueue.invokeLater(new Runnable() {
        //
        //            @Override
        //            public void run() {
        //                for (int i=0; i< 20; i++) {
        //                    try {
        //                        Thread.sleep(10);
        //                        g.clearRect(5, 5, i*3, i*3);
        //                    } catch (InterruptedException ex) {
        //                        Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        //                    }
        //                    try {
        //                        g.drawImage(ImageIO.read(Tax.class.getResource("icons/img/anarchy.png")), 5, 5, i*3, i*3, null);
        //                    } catch (IOException ex) {
        //                        Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        //                    }
        //                    System.out.println(i);
        //                }
        //            }
        //            
        //        });
    }//GEN-LAST:event_formWindowOpened

    private void excelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excelActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(" ");
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String folderName = chooser.getSelectedFile().getAbsolutePath();

            String data[][] = null;
            data = Util.arrayFromTablename(curTable);
            List month = new ArrayList();
            List receipts = new ArrayList();

            month.add(Util.tablenameToDate(curTable));
            Map beans = new HashMap();
            beans.put("month", month);

            for (int i = 0; i < data.length; i++) {
                receipts.add(new Receipt(data[i][0], Float.parseFloat(data[i][1]), data[i][2], data[i][3]));
            }

            beans.put("receipts", receipts);

            XLSTransformer transformer = new XLSTransformer();
            transformer.markAsFixedSizeCollection("month");
            transformer.markAsFixedSizeCollection("receipts");

            try {
                FileUtils.forceMkdir(new File(folderName + "/" + curYear + "/excel"));
                transformer.transformXLS("etc/excel.xls", beans, folderName + "/" + curYear + "/excel/" + curMonth
                        + ". " + Util.months[curMonth - 1] + ".xls");
            } catch (IOException ex) {
                Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParsePropertyException ex) {
                Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvalidFormatException ex) {
                Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            }

            new Completed("<html>  ?<br> .</html>")
                    .setVisible(true);
        }
    }//GEN-LAST:event_excelActionPerformed

    private void excelYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excelYearActionPerformed
        new YearChooserPrompt(this, Util.EXCEL).setVisible(true);
    }//GEN-LAST:event_excelYearActionPerformed

    private void refreshButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButActionPerformed
        this.setCursor(Cursor.WAIT_CURSOR);
        Util.updateDB();
        loadTable(curTable);
        this.setCursor(Cursor.DEFAULT_CURSOR);
    }//GEN-LAST:event_refreshButActionPerformed

    private void aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutActionPerformed
        new HelpForm().setVisible(true);
    }//GEN-LAST:event_aboutActionPerformed

    private void dateTextFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_dateTextFocusGained
        for (int i = 0; i < 6; i++) {
            ((JTextField) textList.get(i)).setText("");
            if (i != 0 && i != 5)
                textList.get(i).setEnabled(false);
            Util.fadeInAndOut(textList.get(i), Color.white);
        }
    }//GEN-LAST:event_dateTextFocusGained

    private void nameTextSFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextSFocusGained
        for (int i = 0; i < 6; i++) {
            ((JTextField) textList.get(i)).setText("");
            if (i != 0 && i != 5)
                textList.get(i).setEnabled(false);
            Util.fadeInAndOut(textList.get(i), Color.white);
        }
    }//GEN-LAST:event_nameTextSFocusGained

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        //        
    }//GEN-LAST:event_formKeyPressed

    private void formKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyTyped
        //        
    }//GEN-LAST:event_formKeyTyped

    private void helpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpActionPerformed
        new AboutForm().setVisible(true);
    }//GEN-LAST:event_helpActionPerformed

    public void loadTable(String table) {
        curTable = table;
        curMonth = Integer.parseInt(table.substring(1, 3));
        curYear = Integer.parseInt(table.substring(4, table.length()));
        monthLab.setText(Util.months[curMonth - 1] + " " + curYear);

        try {
            FileUtils.write(new File("etc/usedLast"), curTable, "UTF-8");
        } catch (IOException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }

        Object data[][] = new Object[][] {};
        int rows = 0;

        Connection c = null;
        Statement stmt = null;
        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:etc/tax.sqlite");
            c.setAutoCommit(false);

            stmt = c.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + table + ";");
            rows = rs.getInt("COUNT(*)");
            if (rows > 0) {
                data = new Object[rs.getInt("COUNT(*)")][4];
                delRowBut.setEnabled(true);
            } else if (delRowBut.isEnabled())
                delRowBut.setEnabled(false);

            System.out.println("rows: " + rows);

            rs = stmt.executeQuery("SELECT * FROM " + table + ";");
            int i = 0;
            while (rs.next()) {
                String date = rs.getString("date");
                Float price = rs.getFloat("price");
                String afm = rs.getString("afm");
                String name = rs.getString("name");
                data[i][0] = date;
                data[i][1] = price;
                data[i][2] = afm;
                data[i][3] = name;

                i++;
                System.out.println(date + " " + price + " " + afm + " " + name);
            }
            rs.close();
            stmt.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }

        db_table.setModel(new javax.swing.table.DefaultTableModel(data,
                new String[] { "?", "", "", "" }) {
            Class[] types = new Class[] { java.lang.String.class, java.lang.Double.class, java.lang.String.class,
                    java.lang.String.class };
            boolean[] canEdit = new boolean[] { false, false, false, false };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        db_table.repaint();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                db_scroll.getVerticalScrollBar().setValue(db_scroll.getVerticalScrollBar().getMaximum());
            }
        });

        if (!addRowBut.isEnabled())
            addRowBut.setEnabled(true);
    }

    public void addRow() {
        String date = "";
        String month = "";
        String day = dateText.getText();
        String afm = afmText.getText();

        Connection c = null;
        Statement stmt = null;
        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:etc/tax.sqlite");
            c.setAutoCommit(false);

            stmt = c.createStatement();

            if (curMonth < 10)
                month = "0" + curMonth;
            else
                month = curMonth + "";

            if (day.length() == 1) {
                day = "0" + day;
            }

            date = day + "/" + month + "/" + curYear;
            stmt.executeUpdate("INSERT INTO " + curTable + " (id, date, price, afm, name) " + "values(NULL, '"
                    + date + "', '" + priceText.getText() + "', '" + afm + "', '" + nameText.getText() + "');");
            c.commit();
            stmt.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }

        if (!AFMList.contains(afm))
            AFMList.add(afm);

        dateText.setText("");
        priceText.setText("");
        afmText.setText("");
        nameText.setText("");

        dateText.requestFocus();
        loadTable(curTable);
    }

    private void removeLastRow() {
        Connection c = null;
        Statement stmt = null;
        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:etc/tax.sqlite");
            c.setAutoCommit(false);

            stmt = c.createStatement();
            stmt.executeUpdate("DELETE FROM " + curTable + " WHERE id = (SELECT MAX(id) FROM " + curTable + ");");
            c.commit();
            stmt.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }

        loadTable(curTable);
    }

    private void loadNamesToAFM() {
        afmToName.clear();

        Object data[][] = new Object[][] {};
        int rows = 0;

        Connection c = null;
        Statement stmt = null;
        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:etc/tax.sqlite");
            c.setAutoCommit(false);

            stmt = c.createStatement();
            stmt.execute("CREATE TABLE IF NOT EXISTS names(id INTEGER PRIMARY KEY, name CHAR(10), afm CHAR(10));");
            c.commit();
            ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM names;");
            rows = rs.getInt("COUNT(*)");
            if (rows > 0) {
                data = new Object[rs.getInt("COUNT(*)")][2];
                delNameBut.setEnabled(true);
            } else if (delNameBut.isEnabled())
                delNameBut.setEnabled(false);

            rs = stmt.executeQuery("SELECT * FROM names;");
            int i = 0;
            while (rs.next()) {
                String name = rs.getString("name");
                String afm = rs.getString("afm");
                data[i][0] = name;
                data[i][1] = afm;
                afmToName.put(name, afm);

                i++;
            }

            stmt.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }

        nameToAfm_table
                .setModel(new javax.swing.table.DefaultTableModel(data, new String[] { "", "" }) {
                    Class[] types = new Class[] { java.lang.String.class, java.lang.String.class };
                    boolean[] canEdit = new boolean[] { false, false };

                    public Class getColumnClass(int columnIndex) {
                        return types[columnIndex];
                    }

                    public boolean isCellEditable(int rowIndex, int columnIndex) {
                        return canEdit[columnIndex];
                    }
                });
        nameToAfm_table.repaint();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                nameToAfm_scroll.getVerticalScrollBar()
                        .setValue(nameToAfm_scroll.getVerticalScrollBar().getMaximum());
            }
        });
    }

    private void addNameToAfm() {
        Connection c = null;
        Statement stmt = null;
        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:etc/tax.sqlite");
            c.setAutoCommit(false);

            stmt = c.createStatement();
            stmt.executeUpdate("INSERT INTO names (id, name, afm) values(NULL, '" + nameTextS.getText() + "', '"
                    + afmTextS.getText() + "');");
            c.commit();
            stmt.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }

        nameTextS.setText("");
        afmTextS.setText("");

        loadNamesToAFM();
    }

    private void removeLastNameToAfm() {
        Connection c = null;
        Statement stmt = null;
        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:etc/tax.sqlite");
            c.setAutoCommit(false);

            stmt = c.createStatement();
            stmt.executeUpdate("DELETE FROM names WHERE id = (SELECT MAX(id) FROM names);");
            c.commit();
            stmt.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }

        loadNamesToAFM();
        AFMList.remove(AFMList.size() - 1);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem about;
    private javax.swing.JButton addNameBut;
    private javax.swing.JPanel addNamePan;
    private javax.swing.JButton addRowBut;
    private javax.swing.JPanel addRowPan;
    private javax.swing.JLabel afmLab;
    private javax.swing.JLabel afmLabS;
    private javax.swing.JTextField afmText;
    private javax.swing.JTextField afmTextS;
    private javax.swing.JPanel bgPan;
    private javax.swing.JLabel blackLineLab;
    private javax.swing.JMenu createMonth;
    private javax.swing.JLabel dateLab;
    private javax.swing.JTextField dateText;
    private javax.swing.JScrollPane db_scroll;
    private javax.swing.JTable db_table;
    private javax.swing.JButton delNameBut;
    private javax.swing.JButton delRowBut;
    private javax.swing.JMenuItem excel;
    private javax.swing.JMenu excelMenu;
    private javax.swing.JMenuItem excelYear;
    private javax.swing.JMenuItem exit;
    private javax.swing.JMenuItem help;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JPanel inputNamePan;
    private javax.swing.JPanel inputRowPan;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JLabel monthLab;
    private javax.swing.JMenuBar monthMenu;
    private javax.swing.JLabel nameLab;
    private javax.swing.JLabel nameLabS;
    private javax.swing.JPanel namePan;
    private javax.swing.JTextField nameText;
    private javax.swing.JTextField nameTextS;
    private javax.swing.JScrollPane nameToAfm_scroll;
    private javax.swing.JTable nameToAfm_table;
    private javax.swing.JMenuItem newMonth;
    private javax.swing.JMenuItem openMonth;
    private javax.swing.JLabel priceLab;
    private javax.swing.JTextField priceText;
    private javax.swing.JMenuItem printAll;
    private javax.swing.JMenuItem printMonth;
    private javax.swing.JButton refreshBut;
    private javax.swing.JPopupMenu.Separator sep1;
    private javax.swing.JPopupMenu.Separator sep2;
    private javax.swing.JPopupMenu.Separator sep3;
    // End of variables declaration//GEN-END:variables

    private void initAFMList() {
        Connection c = null;
        Statement stmt = null;
        try {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:etc/tax.sqlite");
            c.setAutoCommit(false);

            stmt = c.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM main.sqlite_master WHERE type='table';");
            ArrayList<String> tableNames = new ArrayList<String>();
            while (rs.next()) {
                String name = rs.getString("name");
                //             System.out.println("#" + name + "#");
                if (name.startsWith("t"))
                    tableNames.add(name);
                else
                    continue;
            }

            for (int i = 0; i < tableNames.size(); i++) {
                rs = stmt.executeQuery("SELECT afm FROM " + tableNames.get(i) + ";");
                while (rs.next()) {
                    String afm = rs.getString("afm");
                    if (!AFMList.contains(afm)) {
                        AFMList.add(afm);
                        System.out.println(afm);
                    }
                }
            }
            rs.close();
            stmt.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

    public void printYear(int inYear) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(" ");
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String folderName = chooser.getSelectedFile().getAbsolutePath();
            String output = "\u0009\u0009\u0009\n"
                    + "-----------------------------------\n";
            float totalSum = 0;

            Connection c = null;
            Statement stmt = null;
            try {
                Class.forName("org.sqlite.JDBC");
                c = DriverManager.getConnection("jdbc:sqlite:etc/tax.sqlite");
                c.setAutoCommit(false);

                stmt = c.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * FROM main.sqlite_master WHERE type='table';");
                while (rs.next()) {
                    String name = rs.getString("name");
                    if (!name.startsWith("t"))
                        continue;

                    int monthYear[] = new int[2];
                    monthYear = Util.tablenameToMonthAndYear(name);
                    int month = monthYear[0];
                    int year = monthYear[1];

                    float monthlySum = 0;

                    if (inYear != year)
                        continue;

                    String data[][] = null;
                    data = Util.arrayFromTablename(name);

                    for (int i = 0; i < data.length; i++) {
                        Float price = Float.parseFloat(data[i][1]);
                        monthlySum += price;
                    }

                    totalSum += monthlySum;
                    String sumS = Util.formatSum(monthlySum);

                    String tabSpace;
                    if (Util.months[month - 1].length() >= 8)
                        tabSpace = "\u0009\u0009";
                    else
                        tabSpace = "\u0009\u0009\u0009";

                    output += Util.months[month - 1] + tabSpace + sumS + "\n";

                    try {
                        FileUtils.forceMkdir(new File(folderName + "/" + year + "/pdf"));
                        Printer.createPdf(
                                folderName + "/" + year + "/pdf/" + month + ". " + Util.months[month - 1] + ".pdf",
                                name);
                    } catch (IOException ex) {
                        Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (SQLException ex) {
                        Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (DocumentException ex) {
                        Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                output += "-----------------------------------\n" + "?\u0009\u0009\u0009"
                        + Util.formatSum(totalSum) + "\n";

                FileUtils.write(new File(folderName + "/" + inYear + "/?.txt"), output, "UTF-8");

                rs.close();
                stmt.close();
                c.close();
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(0);
            }

            new Completed("<html> ? ?<br> .</html>")
                    .setVisible(true);
        }
    }

    public void exportExcelYear(int inYear) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(" ");
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String folderName = chooser.getSelectedFile().getAbsolutePath();
            String output = "\u0009\u0009\u0009\n"
                    + "-----------------------------------\n";
            float totalSum = 0;

            Connection c = null;
            Statement stmt = null;
            try {
                Class.forName("org.sqlite.JDBC");
                c = DriverManager.getConnection("jdbc:sqlite:etc/tax.sqlite");
                c.setAutoCommit(false);

                stmt = c.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * FROM main.sqlite_master WHERE type='table';");

                while (rs.next()) {
                    String name = rs.getString("name");
                    if (!name.startsWith("t"))
                        continue;

                    int monthYear[] = new int[2];
                    monthYear = Util.tablenameToMonthAndYear(name);
                    int month = monthYear[0];
                    int year = monthYear[1];

                    float monthlySum = 0;

                    if (inYear != year)
                        continue;

                    String data[][] = null;
                    data = Util.arrayFromTablename(name);
                    List monthL = new ArrayList();
                    List receipts = new ArrayList();

                    monthL.add(Util.tablenameToDate(name));
                    Map beans = new HashMap();
                    beans.put("month", monthL);

                    for (int i = 0; i < data.length; i++) {
                        Float price = Float.parseFloat(data[i][1]);
                        receipts.add(new Receipt(data[i][0], price, data[i][2], data[i][3]));
                        monthlySum += price;
                    }

                    totalSum += monthlySum;
                    String sumS = Util.formatSum(monthlySum);

                    String tabSpace;
                    if (Util.months[month - 1].length() >= 8)
                        tabSpace = "\u0009\u0009";
                    else
                        tabSpace = "\u0009\u0009\u0009";

                    output += Util.months[month - 1] + tabSpace + sumS + "\n";

                    beans.put("receipts", receipts);

                    XLSTransformer transformer = new XLSTransformer();
                    transformer.markAsFixedSizeCollection("month");
                    transformer.markAsFixedSizeCollection("receipts");

                    try {
                        FileUtils.forceMkdir(new File(folderName + "/" + year + "/excel"));
                        transformer.transformXLS("etc/excel.xls", beans, folderName + "/" + year + "/excel/" + month
                                + ". " + Util.months[month - 1] + ".xls");
                    } catch (IOException ex) {
                        Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ParsePropertyException ex) {
                        Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InvalidFormatException ex) {
                        Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                output += "-----------------------------------\n" + "?\u0009\u0009\u0009"
                        + Util.formatSum(totalSum) + "\n";

                FileUtils.write(new File(folderName + "/" + inYear + "/?.txt"), output, "UTF-8");

                rs.close();
                stmt.close();
                c.close();
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(0);
            }

            new Completed("<html>  ?<br> .</html>")
                    .setVisible(true);
        }
    }

    public JButton getAddRowBut() {
        return addRowBut;
    }

    public JButton getAddNameBut() {
        return addNameBut;
    }

    public JTextField getAfmTextS() {
        return afmTextS;
    }

    public JTextField getNameTextS() {
        return nameTextS;
    }

    public JTextField getDateText() {
        return dateText;
    }

    public JTextField getAfmText() {
        return afmText;
    }

    public JTextField getNameText() {
        return nameText;
    }

    public HashMap getAfmToName() {
        return afmToName;
    }

}