kolacer.Kolacer.java Source code

Java tutorial

Introduction

Here is the source code for kolacer.Kolacer.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 kolacer;

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import javax.swing.JButton;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.RectangleInsets;

/**
 *
 * @author K
 */
public class Kolacer extends javax.swing.JFrame {

    static ArrayList<Udaj> udaje;
    static Color barvaGradA = Color.GRAY, barvaGradB = Color.ORANGE;
    static Font popisekGrafuFont;
    static Font podekovaniAutoroviFont;
    static final boolean useGraphics = false;

    static final String[] oznaceni = new String[] { "Autor", "Autorka", "Autoi", "Autorky" };
    int oznIndex = -1;

    /**
     * Creates new form Kolacer
     */
    public Kolacer() {
        initVariables();
        initComponents();
        int2Components();

        final Kolacer k = this;
        if (nactiNastaveni())
            System.out.println("Na?tn probhlo spn");
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

            }
        });

        //nactiData();
    }

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

        radButGroup_barvy = new javax.swing.ButtonGroup();
        jT_format = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jT_obdobiOd = new javax.swing.JTextField();
        jT_obdobiDo = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jT_nadpisGrafu = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jBut_chColor = new javax.swing.JButton();
        jBut_sort = new javax.swing.JButton();
        jBut_moveUp = new javax.swing.JButton();
        jBut_moveDown = new javax.swing.JButton();
        jBut_nahled = new javax.swing.JButton();
        jBut_novyUdaj = new javax.swing.JButton();
        jBut_upravUdaj = new javax.swing.JButton();
        jBut_odstranUdaj = new javax.swing.JButton();
        jCHB_popisky = new javax.swing.JCheckBox();
        jCHB_nuloveVyskyty = new javax.swing.JCheckBox();
        jBut_dataZClipboardu = new javax.swing.JButton();
        jCmb_dny = new javax.swing.JComboBox();
        jT_poradi = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jText_thankiez = new javax.swing.JTextField();
        jLab_thankies = new javax.swing.JLabel();
        jRad_barvy_auto = new javax.swing.JRadioButton();
        jLabel7 = new javax.swing.JLabel();
        jRad_barvy_gradient = new javax.swing.JRadioButton();
        jRad_barvy_manual = new javax.swing.JRadioButton();
        jSeparator2 = new javax.swing.JSeparator();
        jBut_barva_grad_A = new javax.swing.JButton();
        jBut_barva_grad_B = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMI_dataText = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMI_exit = new javax.swing.JMenuItem();

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

        jT_format.setText("Hlasovalo [poc] tvorov v dnoch od [od] do [do]");

        jLabel5.setText("Formt popisku o hlasovn");

        jLabel3.setText("Od");

        jLabel4.setText("Do");

        jLabel2.setText("Obdob");

        jT_nadpisGrafu.setText("Nadpis grafu");

        jLabel1.setText("Nadpis grafu");

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jList1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jList1PropertyChange(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jBut_chColor.setText("Upravit barvu");
        jBut_chColor.setEnabled(false);
        jBut_chColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_chColorActionPerformed(evt);
            }
        });

        jBut_sort.setText("Seadit");
        jBut_sort.setEnabled(false);
        jBut_sort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_sortActionPerformed(evt);
            }
        });

        jBut_moveUp.setText("");
        jBut_moveUp.setEnabled(false);
        jBut_moveUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_moveUpActionPerformed(evt);
            }
        });

        jBut_moveDown.setText("");
        jBut_moveDown.setEnabled(false);
        jBut_moveDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_moveDownActionPerformed(evt);
            }
        });

        jBut_nahled.setText("Sestav nhled");
        jBut_nahled.setEnabled(false);
        jBut_nahled.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_nahledActionPerformed(evt);
            }
        });

        jBut_novyUdaj.setText("Nov daj");
        jBut_novyUdaj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_novyUdajActionPerformed(evt);
            }
        });

        jBut_upravUdaj.setText("Uprav daj");
        jBut_upravUdaj.setEnabled(false);
        jBut_upravUdaj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_upravUdajActionPerformed(evt);
            }
        });

        jBut_odstranUdaj.setText("Odstra daj");
        jBut_odstranUdaj.setEnabled(false);
        jBut_odstranUdaj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_odstranUdajActionPerformed(evt);
            }
        });

        jCHB_popisky.setSelected(true);
        jCHB_popisky.setText("Popisky");

        jCHB_nuloveVyskyty.setText("Zahrnout nulov vskyty");

        jBut_dataZClipboardu.setText("daje ze schrnky");
        jBut_dataZClipboardu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_dataZClipboarduActionPerformed(evt);
            }
        });

        jCmb_dny.setModel(new javax.swing.DefaultComboBoxModel(
                new String[] { "Pondelok", "Utorok", "Streda", "tvrtok", "Piatok", "Sobota", "Nedea" }));
        jCmb_dny.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCmb_dnyItemStateChanged(evt);
            }
        });

        jLabel6.setText("Poad");

        jText_thankiez.setText("Autor ankety: ");
        jText_thankiez.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jText_thankiezMouseReleased(evt);
            }
        });

        jLab_thankies.setText("Thankiezzzs");

        radButGroup_barvy.add(jRad_barvy_auto);
        jRad_barvy_auto.setSelected(true);
        jRad_barvy_auto.setText("Automaticky");

        jLabel7.setText("Zpsob vbru barev");

        radButGroup_barvy.add(jRad_barvy_gradient);
        jRad_barvy_gradient.setText("Gradientn");

        radButGroup_barvy.add(jRad_barvy_manual);
        jRad_barvy_manual.setText("Ru?n");

        jBut_barva_grad_A.setText("Grad A");
        jBut_barva_grad_A.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_barva_grad_ActionPerformed(evt);
            }
        });

        jBut_barva_grad_B.setText("Grad B");
        jBut_barva_grad_B.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBut_barva_grad_ActionPerformed(evt);
            }
        });

        jMenu1.setText("Soubor");

        jMI_dataText.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q,
                java.awt.event.InputEvent.CTRL_MASK));
        jMI_dataText.setText("Data z textu");
        jMI_dataText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMI_dataTextActionPerformed(evt);
            }
        });
        jMenu1.add(jMI_dataText);
        jMenu1.add(jSeparator1);

        jMI_exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4,
                java.awt.event.InputEvent.ALT_MASK));
        jMI_exit.setText("Konec");
        jMI_exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMI_exitActionPerformed(evt);
            }
        });
        jMenu1.add(jMI_exit);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addContainerGap()
                                .addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jLabel1)
                                        .addGroup(layout
                                                .createSequentialGroup().addComponent(jLabel2)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(
                                                        jCmb_dny, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(jT_format)
                                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup().addComponent(jLabel3)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jT_obdobiOd,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 125,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addComponent(jLabel5)).addGap(18, 18, 18).addComponent(jLabel4)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jT_obdobiDo, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        128, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout
                                                .createSequentialGroup()
                                                .addComponent(jBut_nahled, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(54, 54, 54)
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(jBut_novyUdaj,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 102,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jBut_upravUdaj,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 102,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(
                                                                jBut_odstranUdaj,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 102,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addComponent(jT_nadpisGrafu, javax.swing.GroupLayout.PREFERRED_SIZE, 306,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout
                                                .createSequentialGroup()
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jCHB_popisky)
                                                        .addComponent(jCHB_nuloveVyskyty))
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(jBut_chColor,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 102,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jLab_thankies))))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        230, Short.MAX_VALUE)
                                                .addGroup(layout.createSequentialGroup()
                                                        .addComponent(jBut_sort,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 101,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE)
                                                        .addComponent(jBut_moveUp,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 42,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                        .addComponent(jBut_moveDown,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 42,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addComponent(
                                                        jBut_dataZClipboardu, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(layout.createSequentialGroup().addGap(18, 18, 18)
                                                                .addGroup(layout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addGroup(layout
                                                                                .createSequentialGroup()
                                                                                .addGroup(layout
                                                                                        .createParallelGroup(
                                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                        .addComponent(
                                                                                                jRad_barvy_auto)
                                                                                        .addComponent(
                                                                                                jLabel7)
                                                                                        .addComponent(
                                                                                                jRad_barvy_gradient)
                                                                                        .addComponent(
                                                                                                jRad_barvy_manual))
                                                                                .addGap(0, 0, Short.MAX_VALUE))
                                                                        .addGroup(
                                                                                javax.swing.GroupLayout.Alignment.TRAILING,
                                                                                layout.createSequentialGroup()
                                                                                        .addGap(0, 0,
                                                                                                Short.MAX_VALUE)
                                                                                        .addGroup(layout
                                                                                                .createParallelGroup(
                                                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                                                .addComponent(
                                                                                                        jT_poradi,
                                                                                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                        90,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                .addComponent(
                                                                                                        jLabel6,
                                                                                                        javax.swing.GroupLayout.Alignment.TRAILING)))))
                                                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addGroup(layout.createSequentialGroup()
                                                                        .addGap(8, 8, 8)
                                                                        .addComponent(jSeparator2,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                279,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                .addGroup(layout.createSequentialGroup()
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(jBut_barva_grad_A,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                112,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addGap(18, 18, 18)
                                                                        .addComponent(jBut_barva_grad_B,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                112,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                                .addGap(0, 0, Short.MAX_VALUE))))
                                        .addComponent(jText_thankiez))
                                .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jT_nadpisGrafu, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2).addComponent(jCmb_dny,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(3, 3, 3)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jT_obdobiOd, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel3).addComponent(jLabel4).addComponent(jT_obdobiDo,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(20, 20, 20).addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jT_format, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jBut_nahled, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jBut_chColor).addComponent(jCHB_popisky)))
                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addGroup(layout.createSequentialGroup().addGap(176, 176, 176)
                                                        .addComponent(jBut_novyUdaj).addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(jBut_upravUdaj))
                                                .addComponent(jScrollPane1))
                                .addGroup(layout.createSequentialGroup().addGap(11, 11, 11).addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jRad_barvy_auto)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jRad_barvy_gradient)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jRad_barvy_manual)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jBut_barva_grad_A,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 60,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jBut_barva_grad_B,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 60,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jBut_odstranUdaj).addComponent(jBut_sort)
                                                .addComponent(jBut_moveUp).addComponent(jBut_moveDown))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jBut_dataZClipboardu))
                                        .addGroup(layout.createSequentialGroup().addGap(9, 9, 9)
                                                .addComponent(jLabel6)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jT_poradi, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jCHB_nuloveVyskyty).addContainerGap(19, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12,
                                                Short.MAX_VALUE)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jText_thankiez,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLab_thankies))
                                        .addContainerGap()))));

        jRad_barvy_auto.getAccessibleContext().setAccessibleName("jRad_barvy_random");

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

    private void jMI_dataTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMI_dataTextActionPerformed
        nactiData(null);
    }//GEN-LAST:event_jMI_dataTextActionPerformed

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

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        int selI = jList1.getSelectedIndex();
        boolean z = selI >= 0;
        jBut_moveUp.setEnabled(z);
        jBut_moveDown.setEnabled(selI < jList1.getModel().getSize() - 1 && selI != -1);
        jBut_chColor.setEnabled(z);
        jBut_upravUdaj.setEnabled(z);
        jBut_odstranUdaj.setEnabled(z);

        if (selI < udaje.size()) {
            jBut_chColor.setBackground((selI >= 0 ? udaje.get(selI).barva : Color.white));
        }
    }//GEN-LAST:event_jList1ValueChanged

    private void jBut_chColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut_chColorActionPerformed
        Color bar = ziskejBarvu(false);
        if (bar == null)
            return;
        udaje.get(jList1.getSelectedIndex()).barva = bar;
        jBut_chColor.setBackground(bar);
    }//GEN-LAST:event_jBut_chColorActionPerformed

    private void jBut_moveUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut_moveUpActionPerformed
        int selI = jList1.getSelectedIndex();
        Udaj u = udaje.get(selI - 1);
        udaje.set(selI - 1, udaje.get(selI));
        udaje.set(selI, u);
        synchronizujSeznam();
        jList1.setSelectedIndex(selI - 1);
    }//GEN-LAST:event_jBut_moveUpActionPerformed

    private void jBut_moveDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut_moveDownActionPerformed
        int selI = jList1.getSelectedIndex();
        Udaj u = udaje.get(selI + 1);
        udaje.set(selI + 1, udaje.get(selI));
        udaje.set(selI, u);
        synchronizujSeznam();
        jList1.setSelectedIndex(selI + 1);
    }//GEN-LAST:event_jBut_moveDownActionPerformed

    private void jBut_sortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut_sortActionPerformed
        seradPrvky();
        synchronizujSeznam();
    }//GEN-LAST:event_jBut_sortActionPerformed

    private void jList1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jList1PropertyChange
        jBut_sort.setEnabled(jList1.getModel().getSize() > 0);
        jBut_nahled.setEnabled(jList1.getModel().getSize() > 0);
    }//GEN-LAST:event_jList1PropertyChange

    private void jBut_novyUdajActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut_novyUdajActionPerformed
        Udaj u = ziskejUdaj(null);
        if (u == null)
            return;
        int selI = jList1.getSelectedIndex();
        ArrayList<Udaj> novy = new ArrayList<>();
        for (int i = 0; i < selI; i++)
            novy.add(udaje.get(i));
        novy.add(u);
        for (int i = (selI == -1) ? selI + 1 : selI; i < udaje.size(); i++)
            novy.add(udaje.get(i));

        udaje = novy;
        synchronizujSeznam();
        jList1.setSelectedIndex(selI);
    }//GEN-LAST:event_jBut_novyUdajActionPerformed

    private void jBut_nahledActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut_nahledActionPerformed
        sestavAzobrazGraf();
    }//GEN-LAST:event_jBut_nahledActionPerformed

    private void jBut_upravUdajActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut_upravUdajActionPerformed
        int selI = jList1.getSelectedIndex();
        Udaj u = ziskejUdaj(udaje.get(selI));
        if (u == null)
            return;
        udaje.set(selI, u);
        synchronizujSeznam();

    }//GEN-LAST:event_jBut_upravUdajActionPerformed

    private void jBut_odstranUdajActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut_odstranUdajActionPerformed
        int selI = jList1.getSelectedIndex();
        ArrayList<Udaj> novy = new ArrayList<>();
        for (int i = 0; i < selI; i++)
            novy.add(udaje.get(i));
        for (int i = selI + 1; i < udaje.size(); i++)
            novy.add(udaje.get(i));
        udaje = novy;
        synchronizujSeznam();
        jList1.setSelectedIndex(-1);
    }//GEN-LAST:event_jBut_odstranUdajActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if (ulozNastaveni())
            System.out.println("Ukldn probhlo spn");
        else
            System.err.println("Pri ukldn nastaven nastala chyba");
    }//GEN-LAST:event_formWindowClosing

    private void jBut_dataZClipboarduActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut_dataZClipboarduActionPerformed
        nactiData(stringZeSchranky());
    }//GEN-LAST:event_jBut_dataZClipboarduActionPerformed

    private void jCmb_dnyItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCmb_dnyItemStateChanged
        nastavDatumy();
    }//GEN-LAST:event_jCmb_dnyItemStateChanged

    private void jText_thankiezMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jText_thankiezMouseReleased
        System.out.format("%d%n", evt.getButton());
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            nastavOznaceni();
        }
    }//GEN-LAST:event_jText_thankiezMouseReleased

    private void jBut_barva_grad_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBut_barva_grad_ActionPerformed
        JButton sauc = (JButton) evt.getSource();
        short gradID = -1;
        int saucHash = sauc.hashCode();

        if (saucHash == jBut_barva_grad_A.hashCode()) {
            gradID = 0;
        } else if (saucHash == jBut_barva_grad_B.hashCode()) {
            gradID = 1;
        } else {
            System.err.println("Invailt gradient color change from: " + sauc.getName());
            return;
        }

        Color bar = ziskejBarvu(false);
        if (bar == null)
            return;
        switch (gradID) {
        case 0:
            jBut_barva_grad_A.setBackground(barvaGradA = bar);
            break;
        case 1:
            jBut_barva_grad_B.setBackground(barvaGradB = bar);
            break;
        }

    }//GEN-LAST:event_jBut_barva_grad_ActionPerformed

    /**
     * @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 
         */
        if (useGraphics)
            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 | InstantiationException | IllegalAccessException
                    | javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Kolacer.class.getName()).log(java.util.logging.Level.SEVERE,
                        null, ex);
            }
        //</editor-fold>*/

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                Kolacer frame = new Kolacer();
                frame.setTitle("Kolacovac ultimate");
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.setResizable(false);

                frame.setLocationRelativeTo(null); //vycentruje okno 
                frame.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JButton jBut_barva_grad_A;
    javax.swing.JButton jBut_barva_grad_B;
    javax.swing.JButton jBut_chColor;
    javax.swing.JButton jBut_dataZClipboardu;
    javax.swing.JButton jBut_moveDown;
    javax.swing.JButton jBut_moveUp;
    javax.swing.JButton jBut_nahled;
    javax.swing.JButton jBut_novyUdaj;
    javax.swing.JButton jBut_odstranUdaj;
    javax.swing.JButton jBut_sort;
    javax.swing.JButton jBut_upravUdaj;
    javax.swing.JCheckBox jCHB_nuloveVyskyty;
    javax.swing.JCheckBox jCHB_popisky;
    javax.swing.JComboBox jCmb_dny;
    javax.swing.JLabel jLab_thankies;
    javax.swing.JLabel jLabel1;
    javax.swing.JLabel jLabel2;
    javax.swing.JLabel jLabel3;
    javax.swing.JLabel jLabel4;
    javax.swing.JLabel jLabel5;
    javax.swing.JLabel jLabel6;
    javax.swing.JLabel jLabel7;
    javax.swing.JList jList1;
    javax.swing.JMenuItem jMI_dataText;
    javax.swing.JMenuItem jMI_exit;
    javax.swing.JMenu jMenu1;
    javax.swing.JMenuBar jMenuBar1;
    javax.swing.JRadioButton jRad_barvy_auto;
    javax.swing.JRadioButton jRad_barvy_gradient;
    javax.swing.JRadioButton jRad_barvy_manual;
    javax.swing.JScrollPane jScrollPane1;
    javax.swing.JPopupMenu.Separator jSeparator1;
    javax.swing.JSeparator jSeparator2;
    javax.swing.JTextField jT_format;
    javax.swing.JTextField jT_nadpisGrafu;
    javax.swing.JTextField jT_obdobiDo;
    javax.swing.JTextField jT_obdobiOd;
    javax.swing.JTextField jT_poradi;
    javax.swing.JTextField jText_thankiez;
    javax.swing.ButtonGroup radButGroup_barvy;
    // End of variables declaration//GEN-END:variables

    private void sestavAzobrazGraf() {
        String infoPopisek = sestavInfoPopisek(udaje);
        String diky = jText_thankiez.getText();
        JFreeChart graf = makePieChart(jT_nadpisGrafu.getText());
        NahledGrafu nh = new NahledGrafu(graf, infoPopisek, this, diky);
        nh.setVisible(true);
    }

    private void nactiData(String textPrevod) {
        NacitacDat o = new NacitacDat(this, true, "", ":");
        boolean result = o.showDialog(textPrevod);
        if (!result)
            return;
        udaje = o.udaje;
        jT_nadpisGrafu.setText(o.nadpis);
        synchronizujSeznam();
    }

    private void synchronizujSeznam() {
        int selI = jList1.getSelectedIndex();
        String[] pole = new String[udaje.size()];
        for (int i = 0; i < udaje.size(); i++) {
            pole[i] = udaje.get(i).toString();
        }
        jList1.setListData(pole);
        jList1.setSelectedIndex(selI);
    }

    private Color ziskejBarvu(boolean povolPruhl) {
        VyberBarvy v = new VyberBarvy(this, true, povolPruhl);
        return v.showDialog();
    }

    private Udaj ziskejUdaj(Udaj u) {
        NovyUdaj o = new NovyUdaj(this, true, u);
        return o.showDialog();
    }

    private void int2Components() {
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setListData(new String[] {});

        jBut_barva_grad_A.setBackground(barvaGradA);
        jBut_barva_grad_B.setBackground(barvaGradB);

        nastavDatumy();
        nastavOznaceni();
    }

    private void nastavDatumy() {
        String[] datumy = ziskejDatumyPodleDne(jCmb_dny.getSelectedItem().toString());
        jT_obdobiOd.setText(datumy[0]);
        jT_obdobiDo.setText(datumy[1]);
    }

    private void seradPrvky() {
        int lenD = udaje.size();
        int j;
        Udaj tmp;
        for (int i = 0; i < lenD; i++) {
            j = i;
            for (int k = i; k < lenD; k++) {
                if (udaje.get(j).pocet < udaje.get(k).pocet) {
                    j = k;
                }
            }
            tmp = udaje.get(i);
            udaje.set(i, udaje.get(j));
            udaje.set(j, tmp);
        }
    }

    private String[] ziskejDatumyPodleDne(String den) {
        String[] nedele = new String[2];

        Date date = new Date();
        int style = DateFormat.LONG;
        DateFormat df = DateFormat.getDateInstance(style, Locale.forLanguageTag("sk"));
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        int pocitadlo = 0;
        while (!den.equals(df.format(date).split(",")[0])) {
            date.setTime(date.getTime() - 1000 * 60 * 60 * 24);
            pocitadlo++;
            if (pocitadlo == 7)
                break;
        }
        nedele[1] = sdf.format(date);
        date.setTime(date.getTime() - 14000 * 60 * 60 * 24);
        nedele[0] = sdf.format(date);
        return nedele;
    }

    public JFreeChart makePieChart(String nazevGrafu) {
        JFreeChart graf;
        DefaultPieDataset ds = new DefaultPieDataset();
        for (Udaj u : udaje) {
            if (jCHB_nuloveVyskyty.isSelected() || u.pocet > 0)
                ds.setValue(u.hodnota, u.pocet);
        }
        graf = ChartFactory.createPieChart(nazevGrafu, (PieDataset) ds, false, false, false); // legends, tooltips, urls
        PiePlot plot = (PiePlot) graf.getPlot();
        upravPlot(plot);

        graf.setBackgroundPaint(Barvy.barvaPozadi);
        graf.setBorderVisible(false);
        return graf;
    }

    private void upravPlot(PiePlot plot) {
        plot.setSectionOutlinesVisible(false);
        plot.setOutlinePaint(null);

        if (jRad_barvy_manual.isSelected()) {
            for (Udaj u : udaje) {
                plot.setSectionPaint(u.hodnota, u.barva);
            }
        } else if (jRad_barvy_auto.isSelected()) {
            Barvy.nastavBarvy(plot);
        } else if (jRad_barvy_gradient.isSelected()) {
            Barvy.nastavBarvy(plot, barvaGradA, barvaGradB);
        }

        if (!jCHB_popisky.isSelected())
            plot.setLabelGenerator(null);
        else {
            plot.setLabelBackgroundPaint(new Color(0, 0, 0, 0));
            plot.setLabelOutlinePaint(null);
            plot.setLabelShadowPaint(null);
            plot.setSimpleLabels(true);
            plot.setInsets(new RectangleInsets(5, 5, 5, 400));
            plot.setLabelFont(popisekGrafuFont);
            //plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{1} hl. ({2})"));
            plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}"));
        }
        plot.setBackgroundPaint(null);
        plot.setShadowPaint(null);
        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{1} ({2}) - {0}"));
    }

    private String sestavInfoPopisek(ArrayList<Udaj> hodnoty) {
        String format = jT_format.getText();
        int suma = 0;
        for (Udaj u : hodnoty)
            suma += u.pocet;
        format = format.replace("[poc]", "" + suma);
        format = format.replace("[od]", jT_obdobiOd.getText());
        format = format.replace("[do]", jT_obdobiDo.getText());
        return format;
    }

    private void initVariables() {

        /*String fonts[] = 
        java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
            
        for ( int i = 0; i < fonts.length; i++ )
        {
          System.out.println(fonts[i]);
        }*/

        udaje = new ArrayList<>();
        popisekGrafuFont = new Font("Impact", Font.BOLD, 20);
        podekovaniAutoroviFont = new Font("Impact", Font.PLAIN, 16);
        barvaGradA = Color.lightGray;
        barvaGradB = Color.ORANGE;
    }

    private boolean nactiNastaveni() {
        File f = new File("nastaveniKolaceru.txt");
        if (!f.exists()) {
            System.err.println("Soubor " + f.getName() + " nebyl nalezen");
            return false;
        }
        boolean vPoho = true;
        try (BufferedReader b = new BufferedReader(new FileReader(f))) {
            String radek = b.readLine();
            while (radek != null) {
                vPoho = vPoho && nastavHodnotu(radek);
                radek = b.readLine();
            }
        } catch (IOException e) {
            System.err.println(e);
            return false;
        }
        return vPoho;
    }

    private boolean nastavHodnotu(String r) {
        String[] rd = r.split("=");
        if (rd.length != 2) {
            System.err.println(r + " neni platny udaj o hodnote");
            return false;
        }
        Color c;
        boolean b;
        switch (rd[0]) {
        default:
            System.err.println("Nebyla nalezena nastavovana hodnota " + rd[0]);
            return false;
        case "vykresleniPopisku":
            b = "true".equals(rd[1]);
            jCHB_popisky.setSelected(b);
            break;
        case "zahrnutiNulPrvku":
            b = "true".equals(rd[1]);
            jCHB_nuloveVyskyty.setSelected(b);
            break;
        }
        return true;
    }

    private Color radekNaBarvu(String s) {
        String[] pb = s.split(",");
        if (pb.length != 4) {
            System.err.println(s + " nebosahuje ?tyi prvky barvy");
            return null;
        }
        int r, g, b, a;
        try {
            r = Integer.parseInt(pb[0]);
            g = Integer.parseInt(pb[1]);
            b = Integer.parseInt(pb[2]);
            a = Integer.parseInt(pb[3]);
            if (r > 255 || r < 0 || g > 255 || g < 0 || b > 255 || b < 0 || a > 255 || a < 0)
                return null;
            return new Color(r, g, b, a);
        } catch (NumberFormatException e) {
            System.err.println(e);
            return null;
        }
    }

    private boolean ulozNastaveni() {
        File f = new File("nastaveniKolaceru.txt");

        try (BufferedWriter b = new BufferedWriter(new FileWriter(f))) {
            b.write("vykresleniPopisku=" + jCHB_popisky.isSelected());
            b.newLine();
            b.write("zahrnutiNulPrvku=" + jCHB_nuloveVyskyty.isSelected());
            b.newLine();
        } catch (IOException e) {
            System.err.println(e);
            return false;
        }
        return true;
    }

    private String stringZeSchranky() {
        Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable t = c.getContents(this);
        if (t == null)
            return null;
        try {
            return ((String) t.getTransferData(DataFlavor.stringFlavor));
        } catch (UnsupportedFlavorException | IOException e) {
            System.err.println(e.getMessage());
            return "Chyba pi ?ten ze schrnky";
        } //try
    }//onPaste

    private void nastavOznaceni() {
        oznIndex++;
        if (oznIndex >= oznaceni.length)
            oznIndex = 0;
        String[] casti = jText_thankiez.getText().split(":");
        this.jText_thankiez.setText(oznaceni[oznIndex] + " ankety: " + ((casti.length > 1) ? casti[1] : "").trim());
    }
}