org.scify.talkandplay.gui.users.UserFormPanel.java Source code

Java tutorial

Introduction

Here is the source code for org.scify.talkandplay.gui.users.UserFormPanel.java

Source

/**
* Copyright 2016 SciFY
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scify.talkandplay.gui.users;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.commons.lang3.StringUtils;
import org.scify.talkandplay.gui.MainFrame;
import org.scify.talkandplay.gui.MainPanel;
import org.scify.talkandplay.gui.configuration.ConfigurationPanel;
import org.scify.talkandplay.gui.helpers.GuiHelper;
import org.scify.talkandplay.gui.helpers.UIConstants;
import org.scify.talkandplay.models.User;
import org.scify.talkandplay.models.sensors.KeyboardSensor;
import org.scify.talkandplay.models.sensors.MouseSensor;
import org.scify.talkandplay.models.sensors.Sensor;
import org.scify.talkandplay.services.UserService;

public class UserFormPanel extends javax.swing.JPanel {

    private String userImage;
    private MainFrame parent;
    private UserService userService;
    private GuiHelper guiHelper;
    private User user;
    private Sensor selectionSensor;
    private Sensor navigationSensor;

    public UserFormPanel(MainFrame parent) {
        this.guiHelper = new GuiHelper();
        this.parent = parent;
        this.userService = new UserService();
        this.user = null;
        this.selectionSensor = null;
        this.navigationSensor = null;
        initComponents();
        initCustomComponents();
    }

    public UserFormPanel(MainFrame parent, User user) {
        this.guiHelper = new GuiHelper();
        this.parent = parent;
        this.user = user;
        this.userService = new UserService();
        initComponents();
        initCustomComponents();
    }

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

        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        userPanel = new javax.swing.JPanel();
        nameTextField = new javax.swing.JTextField();
        uploadImageLabel = new javax.swing.JLabel();
        rotationSpeedSlider = new javax.swing.JSlider();
        rotationSpeedLabel = new javax.swing.JLabel();
        saveAndNextButton = new javax.swing.JButton();
        errorLabel = new javax.swing.JLabel();
        saveAndBackButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        nameLabel = new javax.swing.JLabel();
        imageLabel = new javax.swing.JLabel();
        sensorLabel = new javax.swing.JLabel();
        defaultGridSizeLabel = new javax.swing.JLabel();
        columnsTextField = new javax.swing.JTextField();
        xLabel = new javax.swing.JLabel();
        rowsTextField = new javax.swing.JTextField();
        tilesLabel = new javax.swing.JLabel();
        soundCheckBox = new javax.swing.JCheckBox();
        textCheckBox = new javax.swing.JCheckBox();
        imageCheckBox = new javax.swing.JCheckBox();
        selectionSensorTextField1 = new javax.swing.JTextField();
        navigationSensorTextField = new javax.swing.JTextField();
        selectionSensorTextField2 = new javax.swing.JTextField();
        autoScanRadioButton = new javax.swing.JRadioButton();
        manualScanRadioButton = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        step6ExplTextArea = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        saveToFileButton = new javax.swing.JButton();
        successLabel = new javax.swing.JLabel();

        jToolBar1.setRollover(true);

        jButton1.setText("jButton1");

        userPanel.setBackground(new java.awt.Color(255, 255, 255));

        nameTextField.setBackground(new java.awt.Color(255, 255, 255));
        nameTextField.setForeground(new java.awt.Color(51, 51, 51));
        nameTextField.setText(" ?");

        uploadImageLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        uploadImageLabel.setIcon(
                new javax.swing.ImageIcon(getClass().getResource("/org/scify/talkandplay/resources/no-photo.png"))); // NOI18N
        uploadImageLabel.setToolTipText("");
        uploadImageLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        uploadImageLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        uploadImageLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                uploadImageLabelMouseClicked(evt);
            }
        });

        rotationSpeedSlider.setBackground(new java.awt.Color(255, 255, 255));
        rotationSpeedSlider.setFont(rotationSpeedSlider.getFont());
        rotationSpeedSlider.setForeground(new java.awt.Color(51, 51, 51));
        rotationSpeedSlider.setMajorTickSpacing(1);
        rotationSpeedSlider.setMaximum(10);
        rotationSpeedSlider.setMinimum(1);
        rotationSpeedSlider.setMinorTickSpacing(1);
        rotationSpeedSlider.setPaintLabels(true);
        rotationSpeedSlider.setSnapToTicks(true);
        rotationSpeedSlider.setValue(1);

        rotationSpeedLabel.setText("4.  ? ");

        saveAndNextButton.setBackground(new java.awt.Color(75, 161, 69));
        saveAndNextButton.setFont(saveAndNextButton.getFont());
        saveAndNextButton.setForeground(new java.awt.Color(255, 255, 255));
        saveAndNextButton.setText("  ");
        saveAndNextButton.setBorder(null);
        saveAndNextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAndNextButtonActionPerformed(evt);
            }
        });

        errorLabel.setFont(errorLabel.getFont());
        errorLabel.setForeground(new java.awt.Color(153, 0, 0));
        errorLabel.setText("error");

        saveAndBackButton.setBackground(new java.awt.Color(75, 161, 69));
        saveAndBackButton.setFont(saveAndBackButton.getFont());
        saveAndBackButton.setForeground(new java.awt.Color(255, 255, 255));
        saveAndBackButton.setText("  ");
        saveAndBackButton.setBorder(null);
        saveAndBackButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                saveAndBackButtonMouseClicked(evt);
            }
        });

        backButton.setBackground(new java.awt.Color(75, 161, 69));
        backButton.setFont(backButton.getFont());
        backButton.setForeground(new java.awt.Color(255, 255, 255));
        backButton.setText("");
        backButton.setBorder(null);

        nameLabel.setText("1. ?  ?");

        imageLabel.setText("2.  ? ?");

        sensorLabel.setText("3. ?   ");

        defaultGridSizeLabel.setText("5. ?  ");

        columnsTextField.setText("3");

        xLabel.setText("x");

        rowsTextField.setText("3");

        tilesLabel.setText("6.  ");

        soundCheckBox.setBackground(new java.awt.Color(255, 255, 255));
        soundCheckBox.setText("");

        textCheckBox.setBackground(new java.awt.Color(255, 255, 255));
        textCheckBox.setText("");

        imageCheckBox.setBackground(new java.awt.Color(255, 255, 255));
        imageCheckBox.setText("");

        selectionSensorTextField1.setText("?  ");

        navigationSensorTextField.setText("?  ");

        selectionSensorTextField2.setText("?  ");

        autoScanRadioButton.setBackground(new java.awt.Color(255, 255, 255));
        autoScanRadioButton.setText("");

        manualScanRadioButton.setBackground(new java.awt.Color(255, 255, 255));
        manualScanRadioButton.setText("?");

        step6ExplTextArea.setColumns(20);
        step6ExplTextArea.setRows(5);
        step6ExplTextArea.setText(
                "  5 ?  ?  ?   ?    .");
        jScrollPane1.setViewportView(step6ExplTextArea);

        jLabel1.setText(":");

        jLabel2.setText(":");

        jLabel3.setText(":");

        saveToFileButton.setBackground(new java.awt.Color(75, 161, 69));
        saveToFileButton.setFont(saveToFileButton.getFont().deriveFont((float) 18));
        saveToFileButton.setForeground(new java.awt.Color(255, 255, 255));
        saveToFileButton.setText("  ?");
        saveToFileButton.setBorder(null);
        saveToFileButton.setMaximumSize(new java.awt.Dimension(158, 15));
        saveToFileButton.setMinimumSize(new java.awt.Dimension(158, 15));
        saveToFileButton.setPreferredSize(new java.awt.Dimension(235, 62));
        saveToFileButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                saveToFileButtonMouseClicked(evt);
            }
        });

        successLabel.setForeground(new java.awt.Color(0, 153, 0));
        successLabel.setText("success");
        successLabel.setToolTipText("");

        javax.swing.GroupLayout userPanelLayout = new javax.swing.GroupLayout(userPanel);
        userPanel.setLayout(userPanelLayout);
        userPanelLayout.setHorizontalGroup(userPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(userPanelLayout.createSequentialGroup().addGroup(userPanelLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(userPanelLayout.createSequentialGroup().addGap(19, 19, 19)
                                .addGroup(userPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(userPanelLayout.createSequentialGroup().addComponent(nameLabel)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, userPanelLayout
                                                .createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(saveToFileButton,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(saveAndBackButton).addGap(6, 6, 6)))
                                .addComponent(saveAndNextButton))
                        .addGroup(userPanelLayout.createSequentialGroup().addGroup(userPanelLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(userPanelLayout.createSequentialGroup().addGap(19, 19, 19)
                                        .addGroup(userPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(userPanelLayout.createSequentialGroup().addGap(18, 18, 18)
                                                        .addComponent(nameTextField,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 223,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addComponent(imageLabel)
                                                .addGroup(userPanelLayout.createSequentialGroup().addGap(49, 49, 49)
                                                        .addComponent(uploadImageLabel)))
                                        .addGap(89, 89, 89)
                                        .addGroup(userPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(sensorLabel)
                                                .addGroup(userPanelLayout.createSequentialGroup().addGap(6, 6, 6)
                                                        .addGroup(userPanelLayout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(manualScanRadioButton)
                                                                .addComponent(autoScanRadioButton)
                                                                .addGroup(userPanelLayout.createSequentialGroup()
                                                                        .addGap(21, 21, 21)
                                                                        .addGroup(userPanelLayout
                                                                                .createParallelGroup(
                                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addGroup(
                                                                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                                                                        userPanelLayout
                                                                                                .createSequentialGroup()
                                                                                                .addComponent(
                                                                                                        jLabel1)
                                                                                                .addPreferredGap(
                                                                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                                                .addComponent(
                                                                                                        selectionSensorTextField1,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                        178,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                                .addGroup(userPanelLayout
                                                                                        .createSequentialGroup()
                                                                                        .addGroup(userPanelLayout
                                                                                                .createParallelGroup(
                                                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                                                .addComponent(
                                                                                                        jLabel3)
                                                                                                .addComponent(
                                                                                                        jLabel2,
                                                                                                        javax.swing.GroupLayout.Alignment.TRAILING))
                                                                                        .addPreferredGap(
                                                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                                        .addGroup(userPanelLayout
                                                                                                .createParallelGroup(
                                                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                                                .addComponent(
                                                                                                        selectionSensorTextField2,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                        178,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                .addComponent(
                                                                                                        navigationSensorTextField,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                        178,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))))))))
                                .addGroup(userPanelLayout.createSequentialGroup().addContainerGap().addComponent(
                                        successLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 402,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(160, 160, 160)
                                .addGroup(userPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(userPanelLayout.createSequentialGroup().addGroup(userPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                        userPanelLayout.createSequentialGroup()
                                                                .addComponent(rotationSpeedSlider,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(26, 26, 26))
                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                        userPanelLayout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(defaultGridSizeLabel)
                                                                .addComponent(rotationSpeedLabel)
                                                                .addGroup(userPanelLayout.createSequentialGroup()
                                                                        .addGap(6, 6, 6).addComponent(soundCheckBox)
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(textCheckBox)
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(imageCheckBox))
                                                                .addComponent(tilesLabel))
                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                        userPanelLayout.createSequentialGroup()
                                                                .addComponent(rowsTextField,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 41,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(xLabel)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(columnsTextField,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 41,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(94, 94, 94)))
                                                .addGap(0, 0, Short.MAX_VALUE))
                                        .addGroup(userPanelLayout.createSequentialGroup().addGap(6, 6, 6)
                                                .addComponent(jScrollPane1)))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(backButton).addGap(56, 56, 56))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                        userPanelLayout.createSequentialGroup().addContainerGap()
                                .addComponent(errorLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addContainerGap()));
        userPanelLayout.setVerticalGroup(userPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(userPanelLayout.createSequentialGroup().addGap(25, 25, 25).addGroup(userPanelLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(userPanelLayout.createSequentialGroup().addGroup(userPanelLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(userPanelLayout.createSequentialGroup().addComponent(nameLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(userPanelLayout.createSequentialGroup().addComponent(rotationSpeedLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(rotationSpeedSlider, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(userPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(userPanelLayout.createSequentialGroup().addGap(40, 40, 40)
                                                .addComponent(imageLabel))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                userPanelLayout.createSequentialGroup()
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(defaultGridSizeLabel)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(userPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(uploadImageLabel)
                                        .addGroup(userPanelLayout.createSequentialGroup().addGroup(userPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(xLabel)
                                                .addComponent(columnsTextField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(rowsTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(30, 30, 30).addComponent(tilesLabel)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(userPanelLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(soundCheckBox).addComponent(textCheckBox)
                                                        .addComponent(imageCheckBox))
                                                .addGap(31, 31, 31).addComponent(jScrollPane1,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 91,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 60, Short.MAX_VALUE)
                                .addGroup(userPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(saveAndNextButton).addComponent(backButton)
                                        .addComponent(saveAndBackButton).addComponent(saveToFileButton,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(userPanelLayout.createSequentialGroup().addComponent(sensorLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(autoScanRadioButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(userPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addGroup(userPanelLayout.createSequentialGroup().addGroup(userPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(selectionSensorTextField1,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel1)).addGap(8, 8, 8)
                                                .addComponent(manualScanRadioButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(userPanelLayout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel2)
                                                        .addComponent(selectionSensorTextField2,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(37, 37, 37))
                                        .addGroup(userPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(navigationSensorTextField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 15,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(errorLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(successLabel).addGap(111, 111, 111)))));

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

    private void initCustomComponents() {
        errorLabel.setVisible(false);
        successLabel.setVisible(false);

        setUI();
        setFocusListeners();
        setActionListeners();

        if (user != null) {
            parent.setPanelTitle("? ?");

            guiHelper.drawButton(saveAndNextButton);
            guiHelper.drawButton(saveAndBackButton);

            nameTextField.setText(user.getName());
            uploadImageLabel.setIcon(guiHelper.getIcon(user.getImage()));
            rotationSpeedSlider.setValue(user.getConfiguration().getRotationSpeed());
            rowsTextField.setText(String.valueOf(user.getConfiguration().getDefaultGridRow()));
            columnsTextField.setText(String.valueOf(user.getConfiguration().getDefaultGridColumn()));
            soundCheckBox.setSelected(user.getConfiguration().hasSound());
            imageCheckBox.setSelected(user.getConfiguration().hasImage());
            textCheckBox.setSelected(user.getConfiguration().hasText());

            selectionSensor = user.getConfiguration().getSelectionSensor();
            navigationSensor = user.getConfiguration().getNavigationSensor();

            //check if auto or manual scanning is set
            if (user.getConfiguration().getNavigationSensor() == null) {
                autoScanRadioButton.setSelected(true);

                enableTextField(selectionSensorTextField1);
                disableTextField(selectionSensorTextField2);
                disableTextField(navigationSensorTextField);

                selectionSensorTextField1.setText(setSensorText(user.getConfiguration().getSelectionSensor()));
            } else {
                manualScanRadioButton.setSelected(true);

                enableTextField(selectionSensorTextField2);
                enableTextField(navigationSensorTextField);
                disableTextField(selectionSensorTextField1);

                selectionSensorTextField2.setText(setSensorText(user.getConfiguration().getSelectionSensor()));
                navigationSensorTextField.setText(setSensorText(user.getConfiguration().getNavigationSensor()));
            }
        } else {
            parent.setPanelTitle("? ?");

            saveAndBackButton.setVisible(false);
            guiHelper.drawButton(saveAndNextButton);

            autoScanRadioButton.setSelected(true);
            enableTextField(selectionSensorTextField1);
            disableTextField(selectionSensorTextField2);
            disableTextField(navigationSensorTextField);
        }

        guiHelper.drawButton(backButton);
    }

    private boolean validateUser() {

        String name = nameTextField.getText();

        //name should not be empty
        if (name.isEmpty() || " ?".equals(name)) {
            errorLabel.setText("  ?  ?   ");
            errorLabel.setVisible(true);
            return false;
        }

        //name should be unique
        if (user == null || !name.equals(user.getName())) {
            List<User> users = userService.getUsers();
            for (User user : users) {
                if (user.getName().equals(name)) {
                    errorLabel.setText("  ? ? ");
                    errorLabel.setVisible(true);
                    return false;
                }
            }
        }

        //rows, columns should not be empty, should be integers
        if (rowsTextField.getText().isEmpty() || !StringUtils.isNumeric(rowsTextField.getText())
                || (StringUtils.isNumeric(rowsTextField.getText()) && (Integer.parseInt(rowsTextField.getText()) < 2
                        || Integer.parseInt(rowsTextField.getText()) > 6))
                || columnsTextField.getText().isEmpty() || !StringUtils.isNumeric(columnsTextField.getText())
                || (StringUtils.isNumeric(columnsTextField.getText())
                        && (Integer.parseInt(columnsTextField.getText()) < 2
                                || Integer.parseInt(columnsTextField.getText()) > 6))) {
            errorLabel.setText(
                    "       ?   ?, ?   2  6.");
            errorLabel.setVisible(true);
            return false;
        }

        //sensors should not be null
        if (autoScanRadioButton.isSelected() && selectionSensor == null) {
            errorLabel.setText("    ?   ");
            errorLabel.setVisible(true);
            return false;
        } else if (manualScanRadioButton.isSelected() && (selectionSensor == null || navigationSensor == null)) {
            errorLabel.setText(
                    "      ?   ");
            errorLabel.setVisible(true);
            return false;
        }

        //selection and navigation sensors should not be the same
        if (manualScanRadioButton.isSelected()) {

            if (selectionSensor instanceof MouseSensor && navigationSensor instanceof MouseSensor
                    && ((MouseSensor) selectionSensor).getButton() == ((MouseSensor) navigationSensor).getButton()
                    && ((MouseSensor) selectionSensor).getClickCount() == ((MouseSensor) navigationSensor)
                            .getClickCount()) {
                errorLabel.setText(
                        "     ?   ?");
                errorLabel.setVisible(true);
                return false;
            }

            if (selectionSensor instanceof KeyboardSensor && navigationSensor instanceof KeyboardSensor
                    && ((KeyboardSensor) selectionSensor).getKeyCode() == ((KeyboardSensor) navigationSensor)
                            .getKeyCode()) {
                errorLabel.setText(
                        "     ?   ?");
                errorLabel.setVisible(true);
                return false;
            }
        }

        /* //image or text should be selected
         if (!imageCheckBox.isSelected() && !textCheckBox.isSelected()) {
        errorLabel.setText("   ?       ?: , ");
        errorLabel.setVisible(true);
        return false;
         }*/

        return true;
    }

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

        if (validateUser()) {
            User newUser = new User(nameTextField.getText(), userImage);
            newUser.getConfiguration().setRotationSpeed(rotationSpeedSlider.getValue());
            newUser.getConfiguration().setNavigationSensor(navigationSensor);
            newUser.getConfiguration().setSelectionSensor(selectionSensor);
            newUser.getConfiguration().setDefaultGridRow(Integer.parseInt(rowsTextField.getText()));
            newUser.getConfiguration().setDefaultGridColumn(Integer.parseInt(columnsTextField.getText()));
            newUser.getConfiguration().setSound(soundCheckBox.isSelected());
            newUser.getConfiguration().setImage(imageCheckBox.isSelected());
            newUser.getConfiguration().setText(textCheckBox.isSelected());
            try {
                if (user == null) {
                    userService.save(newUser);
                } else {
                    userService.update(newUser, user.getName());
                }
                parent.changePanel(new ConfigurationPanel(newUser.getName(), parent));
            } catch (Exception ex) {
                Logger.getLogger(UserFormPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_saveAndNextButtonActionPerformed

    private void uploadImageLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_uploadImageLabelMouseClicked
        userImage = "";
        JFileChooser chooser = new JFileChooser();

        chooser.setDialogTitle(" ");
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.setFileFilter(
                new FileNameExtensionFilter("Image Files", "png", "jpg", "jpeg", "JPG", "JPEG", "gif"));
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            userImage = chooser.getSelectedFile().getAbsolutePath();
            uploadImageLabel.setIcon(guiHelper.getIcon(userImage));
        }
    }//GEN-LAST:event_uploadImageLabelMouseClicked

    private void saveAndBackButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_saveAndBackButtonMouseClicked
        if (validateUser()) {
            User newUser = new User(nameTextField.getText(), userImage);
            newUser.getConfiguration().setRotationSpeed(rotationSpeedSlider.getValue());
            newUser.getConfiguration().setNavigationSensor(navigationSensor);
            newUser.getConfiguration().setSelectionSensor(selectionSensor);
            newUser.getConfiguration().setDefaultGridRow(Integer.parseInt(rowsTextField.getText()));
            newUser.getConfiguration().setDefaultGridColumn(Integer.parseInt(columnsTextField.getText()));
            newUser.getConfiguration().setSound(soundCheckBox.isSelected());
            newUser.getConfiguration().setImage(imageCheckBox.isSelected());
            newUser.getConfiguration().setText(textCheckBox.isSelected());

            try {
                if (user == null) {
                    userService.save(newUser);
                } else {
                    userService.update(newUser, user.getName());
                }
                parent.changePanel(new MainPanel(parent));
            } catch (Exception ex) {
                Logger.getLogger(UserFormPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_saveAndBackButtonMouseClicked

    private void saveToFileButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_saveToFileButtonMouseClicked
        final JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(" ");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            UserService us = new UserService();
            //returns boolean to show success/failure of operation
            boolean result = us.storeUserToExternalFile(user.getName(), file.getAbsolutePath());
            //on success print success message, else print error message
            if (result) {
                errorLabel.setVisible(false);
                successLabel.setText(" ? ?.");
                successLabel.setVisible(true);
            } else {
                successLabel.setVisible(false);
                errorLabel.setText(
                        " ?  ?. ?  .");
                errorLabel.setVisible(true);
            }
        }
    }//GEN-LAST:event_saveToFileButtonMouseClicked

    /**
     * The action listeners for the text fields and radio buttons
     */
    private void setActionListeners() {
        selectionSensorTextField1.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent me) {
                if (selectionSensorTextField1.isEnabled()) {
                    selectionSensor = new MouseSensor(me.getButton(), me.getClickCount(), "mouse");
                    navigationSensor = null;
                    selectionSensorTextField1.setText(setSensorText(selectionSensor));
                }
            }
        });
        selectionSensorTextField1.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent ke) {
                if (selectionSensorTextField1.isEnabled()) {
                    selectionSensor = new KeyboardSensor(ke.getKeyCode(), String.valueOf(ke.getKeyChar()),
                            "keyboard");
                    navigationSensor = null;
                    selectionSensorTextField1.setText(setSensorText(selectionSensor));
                }
            }
        });

        navigationSensorTextField.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent me) {
                if (navigationSensorTextField.isEnabled()) {
                    navigationSensor = new MouseSensor(me.getButton(), me.getClickCount(), "mouse");
                    navigationSensorTextField.setText(setSensorText(navigationSensor));
                }
            }
        });
        navigationSensorTextField.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent ke) {
                if (navigationSensorTextField.isEnabled()) {
                    navigationSensor = new KeyboardSensor(ke.getKeyCode(), String.valueOf(ke.getKeyChar()),
                            "keyboard");
                    navigationSensorTextField.setText(setSensorText(navigationSensor));
                }
            }
        });

        selectionSensorTextField2.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent me) {
                if (selectionSensorTextField2.isEnabled()) {
                    selectionSensor = new MouseSensor(me.getButton(), me.getClickCount(), "mouse");
                    selectionSensorTextField2.setText(setSensorText(selectionSensor));
                }
            }
        });
        selectionSensorTextField2.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent ke) {
                if (selectionSensorTextField2.isEnabled()) {
                    selectionSensor = new KeyboardSensor(ke.getKeyCode(), String.valueOf(ke.getKeyChar()),
                            "keyboard");
                    selectionSensorTextField2.setText(setSensorText(selectionSensor));
                }
            }
        });

        backButton.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent me) {
                parent.changePanel(new MainPanel(parent));
            }
        });

        autoScanRadioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (autoScanRadioButton.isSelected()) {
                    disableTextField(selectionSensorTextField2);
                    disableTextField(navigationSensorTextField);
                    enableTextField(selectionSensorTextField1);
                }
            }
        });

        manualScanRadioButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (manualScanRadioButton.isSelected()) {
                    disableTextField(selectionSensorTextField1);
                    enableTextField(selectionSensorTextField2);
                    enableTextField(navigationSensorTextField);
                }
            }
        });

    }

    private void setFocusListeners() {
        selectionSensorTextField1.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent fe) {
                selectionSensorTextField1.setText("");
            }

            public void focusLost(FocusEvent fe) {
                if (selectionSensor == null) {
                    selectionSensorTextField1.setText("?  ");
                } else {
                    selectionSensorTextField1.setText(setSensorText(selectionSensor));
                }
            }
        });

        navigationSensorTextField.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent fe) {
                navigationSensorTextField.setText("");
            }

            public void focusLost(FocusEvent fe) {
                if (navigationSensor == null) {
                    navigationSensorTextField.setText("?  ");
                } else {
                    navigationSensorTextField.setText(setSensorText(navigationSensor));
                }
            }
        });

        selectionSensorTextField2.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent fe) {
                selectionSensorTextField2.setText("");
            }

            public void focusLost(FocusEvent fe) {
                if (selectionSensor == null) {
                    selectionSensorTextField2.setText("?  ");
                } else {
                    selectionSensorTextField2.setText(setSensorText(selectionSensor));
                }
            }
        });

        nameTextField.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent fe) {
                if (" ?".equals(nameTextField.getText())) {
                    nameTextField.setText("");
                }
            }

            public void focusLost(FocusEvent fe) {
                if (nameTextField.getText().isEmpty()) {
                    nameTextField.setText(" ?");
                }
            }
        });
    }

    private void setUI() {

        ButtonGroup scanButtons = new ButtonGroup();
        scanButtons.add(autoScanRadioButton);
        scanButtons.add(manualScanRadioButton);

        //set the titles of the form
        nameLabel.setFont(new Font(UIConstants.mainFont, Font.BOLD, 14));
        imageLabel.setFont(new Font(UIConstants.mainFont, Font.BOLD, 14));
        sensorLabel.setFont(new Font(UIConstants.mainFont, Font.BOLD, 14));
        rotationSpeedLabel.setFont(new Font(UIConstants.mainFont, Font.BOLD, 14));
        defaultGridSizeLabel.setFont(new Font(UIConstants.mainFont, Font.BOLD, 14));
        tilesLabel.setFont(new Font(UIConstants.mainFont, Font.BOLD, 14));

        nameTextField.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.GRAY));
        nameTextField.setFont(new Font(UIConstants.mainFont, Font.ITALIC, 14));
        nameTextField.setHorizontalAlignment(JTextField.CENTER);
        uploadImageLabel.setFont(new Font(UIConstants.mainFont, Font.BOLD, 14));

        rowsTextField.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.GRAY));
        rowsTextField.setHorizontalAlignment(JTextField.CENTER);
        columnsTextField.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.GRAY));
        columnsTextField.setHorizontalAlignment(JTextField.CENTER);

        disableTextField(selectionSensorTextField2);
        disableTextField(navigationSensorTextField);
        enableTextField(selectionSensorTextField1);

        //set the text color
        selectionSensorTextField1.setForeground(Color.decode(UIConstants.green));
        selectionSensorTextField2.setForeground(Color.decode(UIConstants.green));
        navigationSensorTextField.setForeground(Color.decode(UIConstants.green));

        selectionSensorTextField1.setHorizontalAlignment(JTextField.CENTER);
        selectionSensorTextField2.setHorizontalAlignment(JTextField.CENTER);
        navigationSensorTextField.setHorizontalAlignment(JTextField.CENTER);

        step6ExplTextArea.setEditable(false);
        step6ExplTextArea.setLineWrap(true);
        step6ExplTextArea.setWrapStyleWord(true);
        step6ExplTextArea.setBorder(javax.swing.BorderFactory.createEmptyBorder());
        jScrollPane1.setBorder(null);

        imageCheckBox.setSelected(true);
        imageCheckBox.setVisible(false);
    }

    private void disableTextField(JTextField textField) {
        textField.setEnabled(false);
        textField.setForeground(Color.decode(UIConstants.disabledColor));
        textField.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.decode(UIConstants.disabledColor)));
        textField.setBorder(BorderFactory.createCompoundBorder(textField.getBorder(),
                BorderFactory.createEmptyBorder(5, 5, 5, 5)));
    }

    private void enableTextField(JTextField textField) {
        textField.setEnabled(true);
        textField.setForeground(Color.decode(UIConstants.green));
        textField.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.decode(UIConstants.green)));
        textField.setBorder(BorderFactory.createCompoundBorder(textField.getBorder(),
                BorderFactory.createEmptyBorder(5, 5, 5, 5)));
    }

    private String setSensorText(Sensor s) {
        String text = "";

        if (s instanceof MouseSensor) {
            MouseSensor sensor = (MouseSensor) s;
            text += sensor.getClickCount();

            if (sensor.getButton() == 1 && sensor.getClickCount() == 1) {
                text += " ?? ";
            } else if (sensor.getButton() == 1 && sensor.getClickCount() > 1) {
                text += " ?? ";
            } else if (sensor.getButton() == 2 && sensor.getClickCount() == 1) {
                text += "  ";
            } else if (sensor.getButton() == 2 && sensor.getClickCount() > 1) {
                text += "  ";
            } else if (sensor.getButton() == 3 && sensor.getClickCount() == 1) {
                text += "  ";
            } else if (sensor.getButton() == 3 && sensor.getClickCount() > 1) {
                text += "  ";
            }

        } else if (s instanceof KeyboardSensor) {
            KeyboardSensor sensor = (KeyboardSensor) s;
            text += "? ";
            if (sensor.getKeyCode() == 10) {
                text += "enter";
            } else if (sensor.getKeyCode() == 32) {
                text += "space";
            } else {
                text += sensor.getKeyChar();
            }
        }

        return text;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton autoScanRadioButton;
    private javax.swing.JButton backButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JTextField columnsTextField;
    private javax.swing.JLabel defaultGridSizeLabel;
    private javax.swing.JLabel errorLabel;
    private javax.swing.JCheckBox imageCheckBox;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JRadioButton manualScanRadioButton;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JTextField navigationSensorTextField;
    private javax.swing.JLabel rotationSpeedLabel;
    private javax.swing.JSlider rotationSpeedSlider;
    private javax.swing.JTextField rowsTextField;
    private javax.swing.JButton saveAndBackButton;
    private javax.swing.JButton saveAndNextButton;
    private javax.swing.JButton saveToFileButton;
    private javax.swing.JTextField selectionSensorTextField1;
    private javax.swing.JTextField selectionSensorTextField2;
    private javax.swing.JLabel sensorLabel;
    private javax.swing.JCheckBox soundCheckBox;
    private javax.swing.JTextArea step6ExplTextArea;
    private javax.swing.JLabel successLabel;
    private javax.swing.JCheckBox textCheckBox;
    private javax.swing.JLabel tilesLabel;
    private javax.swing.JLabel uploadImageLabel;
    private javax.swing.JPanel userPanel;
    private javax.swing.JLabel xLabel;
    // End of variables declaration//GEN-END:variables
}