org.clipsmonitor.gui.MapGeneratorTopComponent.java Source code

Java tutorial

Introduction

Here is the source code for org.clipsmonitor.gui.MapGeneratorTopComponent.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 org.clipsmonitor.gui;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.filechooser.FileFilter;
import org.clipsmonitor.core.MonitorImages;
import org.clipsmonitor.core.ProjectDirectory;
import org.clipsmonitor.monitor2015.RescueGenMap;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.json.JSONException;
import org.json.simple.parser.ParseException;
import org.openide.util.Exceptions;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.clipsmonitor.gui//MapGenerator//EN", autostore = false)
@TopComponent.Description(preferredID = "MapGeneratorTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE",
        persistenceType = TopComponent.PERSISTENCE_ALWAYS)

@TopComponent.Registration(mode = "mapGenerator", openAtStartup = true)
@ActionID(category = "Window", id = "org.clipsmonitor.gui.MapGeneratorTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_MapGeneratorAction", preferredID = "MapGeneratorTopComponent")
@Messages({ "CTL_MapGeneratorAction=MapGenerator", "CTL_MapGeneratorTopComponent=MapGenerator Window",
        "HINT_MapGeneratorTopComponent=This is a MapGenerator window" })

@SuppressWarnings({ "rawtypes", "unchecked" })
public final class MapGeneratorTopComponent extends TopComponent {

    // Var Declaration
    private RescueGenMap model;
    private MonitorImages img;
    private JFileChooser fc;
    private JFileChooser save;
    private JFileChooser json;
    private ProjectDirectory directory;
    private String state;
    private int[] actualPosClicked;
    private String actualPath;

    public MapGeneratorTopComponent() {
        initComponents();
        setName(Bundle.CTL_MapGeneratorTopComponent());
        setToolTipText(Bundle.HINT_MapGeneratorTopComponent());
        model = RescueGenMap.getInstance();
        img = MonitorImages.getInstance();
        int x = Integer.parseInt(XButton.getText());
        int y = Integer.parseInt(YButton.getText());
        actualPosClicked = new int[2];
        actualPosClicked[0] = (x / 2) + 1;
        actualPosClicked[1] = (y / 2) + 1;
        actualPath = "empty";
        WaitTime.setText("0");
        LogArea.setText(model.getLog());
        initComboBox();
        MakePersonList();
        MakeStepList(-1);
        MakeMoveList(-1, -1, "all");
        MakePathList(-1);
        state = InsertionOptionComboBox.getSelectedItem().toString();
        model.initModelMap(x, y, PreviewMap.getWidth(), PreviewMap.getHeight());
        directory = ProjectDirectory.getIstance(new File("./"));
        fc = new JFileChooser();
        save = new JFileChooser();
        json = new JFileChooser();
        fc.setCurrentDirectory(directory.getProjectDirectory());
        fc.setFileFilter(new JSONFilter());
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        save.setCurrentDirectory(directory.getProjectDirectory());
        save.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        json.setCurrentDirectory(directory.getProjectDirectory());
        json.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        setButtons(true);
        setListEnable();
        updateLogArea();
    }

    /**
     * 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        InsertionOptionComboBox = new javax.swing.JComboBox<String>();
        SaveButton = new javax.swing.JButton();
        LoadButton = new javax.swing.JButton();
        XButton = new javax.swing.JTextField();
        YButton = new javax.swing.JTextField();
        DimensionLabel = new javax.swing.JLabel();
        RefreshButton = new javax.swing.JButton();
        XLabel = new javax.swing.JLabel();
        YLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        MaxDur = new javax.swing.JTextField();
        MaxDuration = new javax.swing.JLabel();
        Icons = new javax.swing.JLabel();
        MapButton = new javax.swing.JRadioButton();
        MoveButton = new javax.swing.JRadioButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        AddPersonButton = new javax.swing.JButton();
        DeletePersonButton = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        PersonPathList = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        StepList = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        MovementList = new javax.swing.JList();
        AddPathButton = new javax.swing.JButton();
        RemovePathButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        PersonsList = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        WaitTime = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        PreviewMap = new javax.swing.JPanel() {

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g); // call superclass's paintComponent

                Graphics2D g2 = (Graphics2D) g; // cast g to Graphics2D

                if (model != null) {
                    model.drawScene(g2, PreviewMap.getWidth(), PreviewMap.getHeight());
                }
            }

        };
        jScrollPane6 = new javax.swing.JScrollPane();
        LogArea = new javax.swing.JTextArea();
        ClearButton = new javax.swing.JButton();
        JsonCreate = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 100, Short.MAX_VALUE));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 100, Short.MAX_VALUE));

        jTextField1.setText(org.openide.util.NbBundle.getMessage(MapGeneratorTopComponent.class,
                "MapGeneratorTopComponent.jTextField1.text")); // NOI18N

        setAttentionHighlight(true);

        InsertionOptionComboBox.setModel(new javax.swing.DefaultComboBoxModel<String>(
                new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        InsertionOptionComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InsertionOptionComboBoxActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(SaveButton, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.SaveButton.text")); // NOI18N
        SaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(LoadButton, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.LoadButton.text")); // NOI18N
        LoadButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LoadButtonMouseClicked(evt);
            }
        });
        LoadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoadButtonActionPerformed(evt);
            }
        });

        XButton.setText(org.openide.util.NbBundle.getMessage(MapGeneratorTopComponent.class,
                "MapGeneratorTopComponent.XButton.text")); // NOI18N

        YButton.setText(org.openide.util.NbBundle.getMessage(MapGeneratorTopComponent.class,
                "MapGeneratorTopComponent.YButton.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(DimensionLabel, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.DimensionLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(RefreshButton, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.RefreshButton.text")); // NOI18N
        RefreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RefreshButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(XLabel, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.XLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(YLabel, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.YLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.jLabel1.text")); // NOI18N

        MaxDur.setText(org.openide.util.NbBundle.getMessage(MapGeneratorTopComponent.class,
                "MapGeneratorTopComponent.MaxDur.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(MaxDuration, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.MaxDuration.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(Icons, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.Icons.text")); // NOI18N

        MapButton.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(MapButton, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.MapButton.text")); // NOI18N
        MapButton.setContentAreaFilled(false);
        MapButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MapButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(MoveButton, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.MoveButton.text")); // NOI18N
        MoveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MoveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(InsertionOptionComboBox, javax.swing.GroupLayout.Alignment.LEADING,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 162,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout
                                        .createSequentialGroup()
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(MaxDuration).addComponent(
                                                        SaveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(6, 6, 6)
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(MaxDur, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(LoadButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(MoveButton)))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                        jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(DimensionLabel,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 76,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                        .addGroup(jPanel1Layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(XButton,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 39,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                                        .addGap(12, 12, 12).addComponent(XLabel,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                10,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                        .addGroup(jPanel1Layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addGroup(jPanel1Layout.createSequentialGroup()
                                                                        .addGap(12, 12, 12).addComponent(YLabel,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                11,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                .addComponent(YButton,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 39,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(RefreshButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        72, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(Icons, javax.swing.GroupLayout.PREFERRED_SIZE, 162,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(6, 6, 6))
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup().addComponent(MapButton)
                                        .addContainerGap())
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        jPanel1Layout.createSequentialGroup()
                                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 108,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(60, 60, 60)))));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(6, 6, 6).addComponent(DimensionLabel)
                        .addGap(0, 0, 0)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(YButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(XButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(RefreshButton))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(XLabel).addComponent(YLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 16,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(InsertionOptionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Icons, javax.swing.GroupLayout.PREFERRED_SIZE, 145,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(MaxDur, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(MaxDuration))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(LoadButton).addComponent(SaveButton))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(MapButton).addComponent(MoveButton))
                        .addContainerGap()));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.jLabel5.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(AddPersonButton, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.AddPersonButton.text")); // NOI18N
        AddPersonButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddPersonButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(DeletePersonButton, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.DeletePersonButton.text")); // NOI18N
        DeletePersonButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeletePersonButtonActionPerformed(evt);
            }
        });

        PersonPathList.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];
            }
        });
        PersonPathList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                PersonPathListValueChanged(evt);
            }
        });
        jScrollPane5.setViewportView(PersonPathList);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.jLabel3.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.jLabel2.text")); // NOI18N

        StepList.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];
            }
        });
        StepList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                StepListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(StepList);

        MovementList.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];
            }
        });
        jScrollPane3.setViewportView(MovementList);

        org.openide.awt.Mnemonics.setLocalizedText(AddPathButton, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.AddPathButton.text")); // NOI18N
        AddPathButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddPathButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(RemovePathButton, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.RemovePathButton.text")); // NOI18N
        RemovePathButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemovePathButtonActionPerformed(evt);
            }
        });

        PersonsList.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];
            }
        });
        PersonsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                PersonsListMouseClicked(evt);
            }
        });
        PersonsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                PersonsListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(PersonsList);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.jLabel4.text")); // NOI18N

        WaitTime.setText(org.openide.util.NbBundle.getMessage(MapGeneratorTopComponent.class,
                "MapGeneratorTopComponent.WaitTime.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.jLabel6.text")); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(jPanel5Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel5Layout.createSequentialGroup().addGap(6, 6, 6)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel5Layout.createSequentialGroup().addGroup(jPanel5Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 89,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(AddPersonButton, javax.swing.GroupLayout.PREFERRED_SIZE, 89,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(DeletePersonButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 89,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel5Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(AddPathButton, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(RemovePathButton,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 97,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 97,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel5Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(WaitTime, javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addComponent(jLabel5).addComponent(jScrollPane3,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 285,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(6, 6, 6)));
        jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel5Layout.createSequentialGroup().addGap(6, 6, 6).addGroup(jPanel5Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4).addComponent(jLabel3)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.Alignment.LEADING,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 140,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 140,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 140,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel5Layout.createSequentialGroup().addGap(14, 14, 14)
                                        .addComponent(jLabel6))
                                .addGroup(jPanel5Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(AddPersonButton).addComponent(AddPathButton)))
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel5Layout.createSequentialGroup().addGap(4, 4, 4).addComponent(
                                        WaitTime, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel5Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(DeletePersonButton).addComponent(RemovePathButton)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 17,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 137,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));

        PreviewMap.setAutoscrolls(true);
        PreviewMap.setEnabled(false);
        PreviewMap.setPreferredSize(new java.awt.Dimension(250, 250));
        PreviewMap.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                PreviewMapMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout PreviewMapLayout = new javax.swing.GroupLayout(PreviewMap);
        PreviewMap.setLayout(PreviewMapLayout);
        PreviewMapLayout.setHorizontalGroup(PreviewMapLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 294, Short.MAX_VALUE));
        PreviewMapLayout.setVerticalGroup(PreviewMapLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 250, Short.MAX_VALUE));

        jScrollPane4.setViewportView(PreviewMap);

        LogArea.setEditable(false);
        LogArea.setColumns(20);
        LogArea.setRows(5);
        jScrollPane6.setViewportView(LogArea);

        org.openide.awt.Mnemonics.setLocalizedText(ClearButton, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.ClearButton.text")); // NOI18N
        ClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(JsonCreate, org.openide.util.NbBundle
                .getMessage(MapGeneratorTopComponent.class, "MapGeneratorTopComponent.JsonCreate.text")); // NOI18N
        JsonCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JsonCreateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(jPanel3Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 297,
                                        Short.MAX_VALUE)
                                .addComponent(jScrollPane6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanel3Layout.createSequentialGroup().addComponent(ClearButton)
                                        .addGap(18, 18, 18).addComponent(JsonCreate)))
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, 0)));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(jPanel3Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(ClearButton).addComponent(JsonCreate)))
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18).addComponent(jScrollPane6)))
                        .addContainerGap())
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        jPanel3Layout.createSequentialGroup().addGap(0, 19, Short.MAX_VALUE).addComponent(jPanel5,
                                javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, 0)));
    }// </editor-fold>//GEN-END:initComponents

    /*
        
    ActionListener Methods
        
    */

    // PreviewMap MouseClick

    private void PreviewMapMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PreviewMapMouseClicked

        try {
            model.setSizeScreen(PreviewMap.getWidth(), PreviewMap.getHeight());
            int x = evt.getX();
            int y = evt.getY();

            actualPosClicked = model.getCellPosition(x, y);
            if (MapButton.isSelected()) {
                ExecUpdateMap();
            } else {
                if (model.findIndexPosByColor(state) != -1) {
                    actualPath = model.getLastPathOfPerson(state);
                    ExecUpdateMove();
                } else {
                    String[][] move;
                    move = model.getTmpMoveMap(actualPosClicked[0], actualPosClicked[1], state);
                    model.ApplyUpdateOnMoveMap(move);
                    model.CopyToActive(model.getMove());
                    PreviewMap.repaint();
                }
            }

        } catch (NullPointerException err) {
            model.error(err.getMessage());
        }
    }//GEN-LAST:event_PreviewMapMouseClicked

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

        try {
            int numX = Integer.parseInt(XButton.getText());
            int numY = Integer.parseInt(YButton.getText());
            int maxDur = Integer.parseInt(MaxDur.getText());
            if (numX >= 5 && numY >= 5 && maxDur > 0) {
                if (model.getNumx() != numX || model.getNumy() != numY) {

                    String message = "La modifica della griglia comporta l'annullamento di tutte le modifiche fatte. Continuare?";
                    if (JOptionPane.showConfirmDialog(DimensionLabel, message, "Resize",
                            JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
                        ;
                    {
                        updateMap(numX, numY, maxDur);
                        if (!model.getEmptyPerson()) {
                            model.RemoveStepAfterResize();
                        }
                    }

                    PreviewMap.repaint();
                    MakePersonList();
                    MakeStepList(-1);
                    MakeMoveList(-1, -1, "all");
                    setListEnable();
                }
            } else {
                model.error("Input non valido: Verificare che la griglia sia almeno di dimensione 5x5 ");
            }
        } catch (NumberFormatException err) {
            model.error("Input non valido: Verificare che i valori siano interi \n");
        }

        updateLogArea();
    }//GEN-LAST:event_RefreshButtonActionPerformed

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

        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                model.setSizeScreen(PreviewMap.getWidth(), PreviewMap.getHeight());
                File file = fc.getSelectedFile();
                model.LoadFiles(file);
                if (!MoveButton.isEnabled()) {
                    InitColorComboBox();
                    if (getActiveColorMap()) {
                        InsertionOptionComboBox.enable(true);
                        Icons.enable(true);
                        Icons.repaint();
                        model.CopySceneToMove();
                        actualPath = model.getLastPathOfPerson(state);
                        String[][] move = model.getMoveCellMap(actualPath, -1);
                        model.ApplyUpdateOnMoveMap(move);
                        model.CopyToActive(model.getMove());
                    } else {
                        InsertionOptionComboBox.enable(true);
                        Icons.enable(true);
                        model.CopySceneToMove();
                        model.CopyToActive(model.getMove());
                    }

                }
                PreviewMap.repaint();
                MakePersonList();
                MakeStepList(-1);
                MakeMoveList(-1, -1, "all");
                setListEnable();

            } catch (ParseException ex) {
                model.error(ex.getMessage());
            }

        }
        updateLogArea();
    }//GEN-LAST:event_LoadButtonActionPerformed

    private void LoadButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LoadButtonMouseClicked

        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                File file = fc.getSelectedFile();
                model.LoadFiles(file);
                if (!MoveButton.isEnabled()) {
                    InitColorComboBox();
                    if (getActiveColorMap()) {
                        InsertionOptionComboBox.enable(true);
                        Icons.enable(true);
                        Icons.repaint();
                        model.CopySceneToMove();
                        actualPath = model.getLastPathOfPerson(state);
                        String[][] move = model.getMoveCellMap(actualPath, -1);
                        model.ApplyUpdateOnMoveMap(move);
                        model.CopyToActive(model.getMove());
                    } else {
                        InsertionOptionComboBox.enable(true);
                        Icons.enable(true);
                        model.CopySceneToMove();
                        model.CopyToActive(model.getMove());
                    }

                }
                PreviewMap.repaint();
                MakePersonList();
                MakeStepList(-1);
                MakeMoveList(-1, -1, "all");
                setListEnable();

            } catch (ParseException ex) {
                model.error(ex.getMessage());
            }

        }
        updateLogArea();
    }//GEN-LAST:event_LoadButtonMouseClicked

    private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveButtonActionPerformed
        int retrival = save.showSaveDialog(this);
        if (retrival == JFileChooser.APPROVE_OPTION) {
            try {
                String output = model.SaveFiles(save.getSelectedFile());
                model.log(output);
            } catch (JSONException ex) {
                Exceptions.printStackTrace(ex);
                model.error(ex.getMessage());
            }
        }
        updateLogArea();
    }//GEN-LAST:event_SaveButtonActionPerformed

    private void InsertionOptionComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InsertionOptionComboBoxActionPerformed
        boolean checkButton = MapButton.isSelected();
        if (!checkButton) {
            setState(InsertionOptionComboBox.getSelectedItem().toString());
            updateLabel(state);
            actualPath = model.getLastPathOfPerson(state);

            if (!actualPath.equals("empty")) {
                int pos = model.findIndexPosByColor(state);
                MakeMoveList(pos, -1, "all");
            } else {
                MakeMoveList(-1, -1, "all");
            }

            setListEnable();
            MakePersonList();
            MakeStepList(-1);
            String[][] move = model.getMoveCellMap(actualPath, -1);
            model.ApplyUpdateOnMoveMap(move);
            model.CopyToActive(model.getMove());
            PreviewMap.repaint();
        } else {
            setState(InsertionOptionComboBox.getSelectedItem().toString());
            updateLabel(state);
        }
        Icons.repaint();
        updateLogArea();
    }//GEN-LAST:event_InsertionOptionComboBoxActionPerformed

    private void AddPersonButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddPersonButtonActionPerformed
        ExecAddPerson();

    }//GEN-LAST:event_AddPersonButtonActionPerformed

    private void DeletePersonButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeletePersonButtonActionPerformed
        ExecRemove();
    }//GEN-LAST:event_DeletePersonButtonActionPerformed

    private void MapButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MapButtonActionPerformed
        model.setMode("scene");
        setButtons(true);
        InitMapComboBox();
        InsertionOptionComboBox.enable(true);
        Icons.enable(true);
        Icons.repaint();
        model.CopyToActive(model.getScene());
        MakePersonList();
        MakeStepList(-1);
        MakeMoveList(-1, -1, "all");
        MakePathList(-1);
        PreviewMap.repaint();

    }//GEN-LAST:event_MapButtonActionPerformed

    private void MoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MoveButtonActionPerformed
        model.setMode("move");
        setButtons(false);

        InitColorComboBox();
        if (getActiveColorMap()) {
            InsertionOptionComboBox.enable(true);
            Icons.enable(true);
            Icons.repaint();
            model.CopySceneToMove();
            actualPath = model.getLastPathOfPerson(state);
            String[][] move = model.getMoveCellMap(actualPath, -1);
            model.ApplyUpdateOnMoveMap(move);
            model.CopyToActive(model.getMove());
        } else {
            InsertionOptionComboBox.enable(true);
            Icons.enable(true);
            model.CopySceneToMove();
            model.CopyToActive(model.getMove());
        }

        PreviewMap.repaint();
        MakePersonList();
        MakeStepList(-1);
        MakePathList(-1);
        MakeMoveList(-1, -1, "all");
        setListEnable();
    }//GEN-LAST:event_MoveButtonActionPerformed

    private void PersonsListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_PersonsListValueChanged

        String[] persons = model.getListPerson();
        if (evt.getFirstIndex() != persons.length - 1) {
            String val = persons[evt.getFirstIndex()];
            String[] split = val.split("_");
            MakePersonList();
            int pos = model.findIndexPosByColor(split[1]);
            MakeStepList(-1);
            MakeMoveList(pos, -1, "all");
            MakePathList(pos);
            setListEnable();
            setState(split[1]);
            updateLabel(state);
            actualPath = model.getLastPathOfPerson(state);
            if (!actualPath.equals("empty")) {
                String[][] move = model.getMoveCellMap(actualPath, -1);
                model.ApplyUpdateOnMoveMap(move);
            }

            model.CopyToActive(model.getMove());
            PreviewMap.repaint();
        } else {
            MakePersonList();
            MakeStepList(-1);
            MakeMoveList(-1, -1, "all");
            MakePathList(-1);
        }
    }//GEN-LAST:event_PersonsListValueChanged

    private void StepListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_StepListValueChanged

        String value = (String) StepList.getModel().getElementAt(evt.getFirstIndex());
        String intSubStr = value.substring(5); // "Step " length
        int step = Integer.parseInt(intSubStr);
        MakeStepList(-1);
        MakePathList(-1);
        MakeMoveList(-1, step, "all");
        String[][] move = model.getMoveCellMap("none", step);
        model.ApplyUpdateOnMoveMap(move);
        model.CopyToActive(model.getMove());
        PreviewMap.repaint();
        setListEnable();
    }//GEN-LAST:event_StepListValueChanged

    private void AddPathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddPathButtonActionPerformed
        ExecAddPath();

    }//GEN-LAST:event_AddPathButtonActionPerformed

    private void RemovePathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemovePathButtonActionPerformed
        ExecRemovePath();
    }//GEN-LAST:event_RemovePathButtonActionPerformed

    private void PersonPathListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_PersonPathListValueChanged
        ListModel<String> listmodel = PersonPathList.getModel();
        if (PersonPathList.getSelectedIndex() >= 0) {
            String pathname = listmodel.getElementAt(PersonPathList.getSelectedIndex());
            String[] split = pathname.split("_");
            state = split[0];
            setState(state);
            updateLabel(state);
            actualPath = model.getLastPathOfPerson(state);
            MakeMoveList(-1, -1, pathname);
            String[][] move = model.getMoveCellMap(pathname, -1);
            model.ApplyUpdateOnMoveMap(move);
            model.CopyToActive(model.getMove());
            PreviewMap.repaint();
            setListEnable();
        }
    }//GEN-LAST:event_PersonPathListValueChanged

    private void PersonsListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PersonsListMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_PersonsListMouseClicked

    private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed
        String message = "La mappa e i movimenti dell'agente sar ripristinata allo situazione di default. Continuare ?";
        JLabel DimensionLabel = new javax.swing.JLabel();
        if (JOptionPane.showConfirmDialog(DimensionLabel, message, "Clear scene",
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {

            model.clear();
            model = RescueGenMap.getInstance();
            int x = Integer.parseInt(XButton.getText());
            int y = Integer.parseInt(YButton.getText());
            model.setSizeScreen(PreviewMap.getWidth(), PreviewMap.getHeight());
            model.initModelMap(x, y, PreviewMap.getWidth(), PreviewMap.getHeight());
            model.setMaxDuration(model.getMaxDuration());
            model.CopyToActive(model.getScene());
            PreviewMap.repaint();
            updateLogArea();
        }
    }//GEN-LAST:event_ClearButtonActionPerformed

    private void JsonCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JsonCreateActionPerformed
        int jc = json.showOpenDialog(this);
        if (jc == JFileChooser.APPROVE_OPTION) {
            File dir = json.getSelectedFile();
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.getName().contains("RealMap")) {
                    model.createJsonScene(file);
                }
            }

            File jsonMap = new File("./");
            File history = new File("./");
            files = dir.listFiles();

            for (File file : files) {
                if (file.getName().contains("history")) {
                    history = file;
                }
                if (file.getName().contains("infoMap")) {
                    jsonMap = file;
                }
            }

            model.createJsonHistory(history, jsonMap);
            updateLogArea();
        }
    }//GEN-LAST:event_JsonCreateActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddPathButton;
    private javax.swing.JButton AddPersonButton;
    private javax.swing.JButton ClearButton;
    private javax.swing.JButton DeletePersonButton;
    private javax.swing.JLabel DimensionLabel;
    private javax.swing.JLabel Icons;
    private javax.swing.JComboBox InsertionOptionComboBox;
    private javax.swing.JButton JsonCreate;
    private javax.swing.JButton LoadButton;
    private javax.swing.JTextArea LogArea;
    private javax.swing.JRadioButton MapButton;
    private javax.swing.JTextField MaxDur;
    private javax.swing.JLabel MaxDuration;
    private javax.swing.JRadioButton MoveButton;
    private javax.swing.JList MovementList;
    private javax.swing.JList PersonPathList;
    private javax.swing.JList PersonsList;
    private javax.swing.JPanel PreviewMap;
    private javax.swing.JButton RefreshButton;
    private javax.swing.JButton RemovePathButton;
    private javax.swing.JButton SaveButton;
    private javax.swing.JList StepList;
    private javax.swing.JTextField WaitTime;
    private javax.swing.JTextField XButton;
    private javax.swing.JLabel XLabel;
    private javax.swing.JTextField YButton;
    private javax.swing.JLabel YLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTextField jTextField1;

    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    private void InitMapComboBox() {

        ComboBoxRenderer IconsComboBox = new ComboBoxRenderer(model.getSetKey(), InsertionOptionComboBox, Icons);
        updateLabel(InsertionOptionComboBox.getSelectedItem().toString());
    }

    private void InitColorComboBox() {

        ComboBoxRenderer IconsComboBox = new ComboBoxRenderer(model.getSetKeyColor(), InsertionOptionComboBox,
                Icons);
        updateLabel(InsertionOptionComboBox.getSelectedItem().toString());
    }

    /*
    * Genera l'immagine di preview della combobox
    * @param name : chiave dell'hash map
    * @param map : hashmap da cui prelevare le immagini
    */

    protected void updateLabel(String name) {

        try {
            ImageIcon icon = new ImageIcon(img.getImage(name));
            Image image = icon.getImage(); // transform it
            Image newimg = image.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
            icon = new ImageIcon(newimg); // transform it back
            Icons.setIcon(icon);
            Icons.setToolTipText("A drawing of a " + name.toLowerCase());
            Icons.setText(null);
        } catch (NullPointerException e) {

            if (!img.getMapImg().isEmpty()) {
                Icons.setText("Image not found");
                model.error(e.getLocalizedMessage());
            }
        }
        updateLogArea();
    }

    private boolean getActiveColorMap() {

        String[] colors = model.getListColorActive();
        return !(colors.length == 1 && colors[0].equals(""));
    }

    private void MakePersonList() {

        String[] list = model.getListPerson();
        if (list == null) {
            list = new String[1];
            list[0] = "";
        }
        ListRenderer render = new ListRenderer(PersonsList, list);

    }

    private void MakeStepList(int paramStep) {

        String[] list = model.getListStep(paramStep);
        if (list == null) {
            list = new String[1];
            list[0] = "";
        }
        ListRenderer render = new ListRenderer(StepList, list);

    }

    private void MakeMoveList(int paramPerson, int paramStep, String paramPath) {

        String[] list = model.getListMove(paramPerson, paramStep, paramPath);
        if (list == null) {
            list = new String[1];
            list[0] = "";
        }
        ListRenderer render = new ListRenderer(MovementList, list);

    }

    private void MakePathList(int paramPerson) {

        String[] list = model.getStringPaths(paramPerson);
        if (list == null) {
            list = new String[1];
            list[0] = "";
        }
        ListRenderer render = new ListRenderer(PersonPathList, list);

    }

    /*
      Aggiorna la combo e l'icona corrisponendentemente scelta
    */

    private void initComboBox() {

        ComboBoxRenderer IconsComboBox = new ComboBoxRenderer(model.getSetKey(), InsertionOptionComboBox, Icons);
        updateLabel(InsertionOptionComboBox.getSelectedItem().toString());

    }

    private void setButtons(boolean val) {

        MapButton.setSelected(val);
        MoveButton.setSelected(!val);
        MapButton.setEnabled(!val);
        MoveButton.setEnabled(val);
        XButton.setEditable(val);
        YButton.setEditable(val);
        MaxDur.setEditable(val);
        AddPathButton.setEnabled(!val);
        AddPersonButton.setEnabled(!val);
        DeletePersonButton.setEnabled(!val);
        RemovePathButton.setEnabled(!val);
        StepList.setEnabled(!val);
        PersonPathList.setEnabled(!val);
        PersonsList.setEnabled(!val);

    }

    private void setListEnable() {

        boolean personTest = !(PersonsList.getModel().getSize() <= 1);
        boolean personPathTest = !(PersonPathList.getModel().getSize() < 1);
        boolean stepTest = !(StepList.getModel().getSize() < 1);

        PersonsList.setEnabled(personTest);
        PersonPathList.setEnabled(personPathTest);
        StepList.setEnabled(stepTest);

    }

    private void ExecRemove() {

        boolean result = model.Remove(state);
        if (result) {
            model.log(state + " rimosso correttamente ");
            String[][] move = model.getMoveCellMap("none", 0);
            model.ApplyUpdateOnMoveMap(move);
            model.CopyToActive(model.getMove());
            PreviewMap.repaint();
            MakePersonList();
            MakeStepList(-1);
            MakeMoveList(-1, -1, "all");
            MakePathList(-1);
            setListEnable();
        } else {
            model.error(state + " non presente nello scenario");
        }
        updateLogArea();
    }

    /*
    * Inserisce una nuova persona nello scenario
    */

    private void ExecAddPerson() {

        final int Success = 0;
        final int IllegalPosition = 1;
        final int keyColorEmpty = 2;
        final int keyColorFull = 3;
        final int IllegalRobotPosition = 4;
        final int IllegalAgentPosition = 5;
        final int PersonOverride = 6;

        int wait = Integer.parseInt(WaitTime.getText());

        int result = model.AddNewPerson(actualPosClicked[0], actualPosClicked[1], state, wait);
        switch (result) {
        case Success:
            model.log("Modifica della mappa eseguita con successo");
            actualPath = model.getLastPathOfPerson(state);
            String[][] move = model.getMoveCellMap("none", model.getLastStepofPerson(state));
            model.ApplyUpdateOnMoveMap(move);
            model.CopyToActive(model.getMove());
            PreviewMap.repaint();
            MakePersonList();
            MakeStepList(-1);
            MakeMoveList(-1, -1, "all");
            MakePathList(-1);
            setListEnable();
            break;

        case IllegalPosition:
            model.error("Posizione del cursore illegale: Nessuna cella disponibile \n");
            break;
        case keyColorEmpty:
            model.error("Color set vuoto");
            break;
        case keyColorFull:
            model.error("Color set completo: Impossibile aggiungere ulteriore persona alla scena");
            break;
        case IllegalRobotPosition:
            model.error("Posizione del robot non valida");
            break;
        case IllegalAgentPosition:
            model.error("Posizione dell'agente non valida");
            break;

        case PersonOverride:
            model.error("La cella  gi occupata da un altro agente");
            break;
        default:

            break;

        }
        updateLogArea();
    }

    private void ExecAddPath() {

        final int Success = 0;
        final int IllegalStartCell = 1;
        final int IllegalPerson = 2;
        final int PersonOverride = 3;

        try {
            int wait = Integer.parseInt(WaitTime.getText());
            if (wait < 0) {
                throw new NumberFormatException();
            }

            int result = model.AddNewPathToPerson(state, wait);
            switch (result) {

            case Success:
                model.log("Path aggiunto correttamente");
                actualPath = model.getLastPathOfPerson(state);
                String[][] move = model.getMoveCellMap(actualPath, -1);
                model.ApplyUpdateOnMoveMap(move);
                model.CopyToActive(model.getMove());
                PreviewMap.repaint();
                MakePersonList();
                MakeStepList(-1);
                MakeMoveList(-1, -1, "all");
                MakePathList(-1);
                setListEnable();
                break;
            case IllegalStartCell:
                model.error("Illegal start cell");
                break;
            case IllegalPerson:
                model.error("Illegal Person");
                break;
            case PersonOverride:
                model.error("Person ovveride");

                break;

            }
        } catch (NumberFormatException er) {
            model.error("Valore di Waitstep non valido: Input deve essere necessariamente un intero positivo");
        }

        updateLogArea();
    }

    public void ExecRemovePath() {

        final int Success = 0;
        final int PersonNotFound = 1;
        final int FirstPathRemove = 2;

        int result = model.RemoveLastPath(state);
        switch (result) {

        case Success:
            model.log("Rimozione eseguita correttamente");
            actualPath = model.getLastPathOfPerson(state);
            String[][] move = model.getMoveCellMap("none", model.getLastStepofPerson(state));
            model.ApplyUpdateOnMoveMap(move);
            model.CopyToActive(model.getMove());
            PreviewMap.repaint();
            MakePersonList();
            MakeStepList(-1);
            MakeMoveList(-1, -1, "all");
            MakePathList(-1);
            setListEnable();
            break;

        case PersonNotFound:
            model.error("Persona attualmente non attiva");
            break;

        case FirstPathRemove:
            model.error("Impossibile rimuovere il primo path");
            break;

        }

        updateLogArea();
    }

    /*
    * Esegue una richiesta di aggiornamento dei movimenti di una certa persona
    * su una posizione specificata
    *
    */

    private void ExecUpdateMove() {
        final int Success = 0;
        final int IllegalPosition = 1;
        final int UnavaiblePosition = 2;
        final int PersonOverride = 3;
        final int LastMoveRemove = 4;
        actualPath = model.getLastPathOfPerson(state);
        String[][] move;
        int result = model.UpdateMoveCell(actualPosClicked[0], actualPosClicked[1], actualPath);
        switch (result) {
        case Success:
            model.log("Movimento agente aggiunto con successo");
            move = model.getMoveCellMap("none", model.getLastStepofPerson(state));
            model.ApplyUpdateOnMoveMap(move);
            model.CopyToActive(model.getMove());
            PreviewMap.repaint();
            MakePersonList();
            MakeStepList(-1);
            MakeMoveList(-1, -1, actualPath);
            MakePathList(-1);
            setListEnable();
            break;

        case IllegalPosition:
            model.error("Posizione del cursore illegale: Nessuna cella disponibile \n");
            break;
        case UnavaiblePosition:

            model.error("Movimento non disponibile ");
            break;
        case PersonOverride:
            model.error("La cella  occupata da un altro agente");
            break;
        case LastMoveRemove:
            model.log("Movimento agente rimosso con successo");

            move = model.getMoveCellMap("none", model.getLastStepofPerson(state));
            model.ApplyUpdateOnMoveMap(move);
            model.CopyToActive(model.getMove());
            PreviewMap.repaint();
            MakePersonList();
            MakeStepList(-1);
            MakeMoveList(-1, -1, actualPath);
            MakePathList(-1);
            setListEnable();
            break;
        default:

            break;
        }

        updateLogArea();
    }

    /*
     Ridisegna la mappa di generazione in base alla nuova dimensione della griglia e imposta la nuova
     max duration
    */

    private void ExecUpdateMap() {

        final int Success = 0;
        final int IllegalPosition = 1;
        final int keyColorEmpty = 2;
        final int keyColorFull = 3;
        final int IllegalRobotPosition = 4;
        final int IllegalAgentPosition = 5;
        final int PersonOverride = 6;

        int result = model.UpdateCell(actualPosClicked[0], actualPosClicked[1], state);
        switch (result) {
        case Success:
            model.log("Modifica della mappa eseguita con successo");
            model.CopyToActive(model.getScene());
            PreviewMap.repaint();
            MakePersonList();
            MakeStepList(-1);
            MakeMoveList(-1, -1, "all");
            MakePathList(-1);
            setListEnable();
            break;

        case IllegalPosition:
            model.error("Posizione del cursore illegale: Nessuna cella disponibile \n");
            break;
        case keyColorEmpty:
            model.error("Color set vuoto");
            break;
        case keyColorFull:
            model.error("Color set completo: Impossibile aggiungere ulteriore persona alla scena");
            break;
        case IllegalRobotPosition:
            model.error("Posizione del robot non valida");
            break;
        case IllegalAgentPosition:
            model.error("Posizione dell'agente non valida");
            break;

        case PersonOverride:
            model.error("La cella  gi occupata da un altro agente");
            break;
        default:

            break;
        }
        updateLogArea();
    }

    /*
     * Ridisegna la mappa in base alla nuova dimensione della griglia richiesta e
     * in base alla nuova durata del tempo prestabilita
    */

    void updateMap(int x, int y, int max) {
        model.setSizeScreen(PreviewMap.getWidth(), PreviewMap.getHeight());
        model.SetSizeCells();
        model.initModelMap(x, y, PreviewMap.getWidth(), PreviewMap.getHeight());
        model.setMaxDuration(max);
    }

    /***********************************************
     *  LOG AREA FUNCTIONS
     ***********************************************/

    void updateLogArea() {
        String log = model.getLog();
        LogArea.setText(log);
    }

    String getState() {
        return state;
    }

    void setState(String value) {
        state = value;
    }

    void errorMsg(String error) {
        model.error(error);

    }

    void printMsg(String Msg) {
        model.log(Msg);
    }

    private static class CLIPSFilter extends FileFilter {

        @Override
        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(".clp") || f.isDirectory();
        }

        @Override
        public String getDescription() {
            return "CLIPS files (*.clp)";
        }
    }

    private class JSONFilter extends FileFilter {

        @Override
        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(".json") || f.isDirectory();
        }

        @Override
        public String getDescription() {
            return "JSON files (*.json)";
        }

    }

}