/*
 * IhmView.java
 */

package ihm;

import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 * The application's main frame.
 */
public class IhmView extends FrameView implements ActionListener{

    public IhmView(SingleFrameApplication app)  {
        super(app);
        //initialisation du connecteur bdd
        startComm();
        //initialisation des Tags
        initTags();
        initComponents();
        //Après la création des composants
        afterInitComponents();
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = IhmApp.getApplication().getMainFrame();
            aboutBox = new IhmAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        IhmApp.getApplication().show(aboutBox);
    }

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

        mainPanel = new javax.swing.JPanel();
        jLabel_LV1 = new javax.swing.JLabel();
        jLabel_LT1 = new javax.swing.JLabel();
        jSlider_SPLIC1 = new javax.swing.JSlider();
        jProgressBar_LT1 = new javax.swing.JProgressBar();
        jProgressBar_SPLIC1 = new javax.swing.JProgressBar();
        jSeparator1 = new javax.swing.JSeparator();
        jRadioButton_LIC1_Auto = new javax.swing.JRadioButton();
        jRadioButton_LIC1_Manu = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jSlider_OPLIC1 = new javax.swing.JSlider();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        buttonGroup1 = new javax.swing.ButtonGroup();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ihm.IhmApp.class).getContext().getResourceMap(IhmView.class);
        jLabel_LV1.setText(resourceMap.getString("jLabel_LV1.text")); // NOI18N
        jLabel_LV1.setName("jLabel_LV1"); // NOI18N

        jLabel_LT1.setText(resourceMap.getString("jLabel_LT1.text")); // NOI18N
        jLabel_LT1.setName("jLabel_LT1"); // NOI18N

        jSlider_SPLIC1.setMaximum(16383);
        jSlider_SPLIC1.setMinorTickSpacing(1024);
        jSlider_SPLIC1.setOrientation(javax.swing.JSlider.VERTICAL);
        jSlider_SPLIC1.setPaintTicks(true);
        jSlider_SPLIC1.setToolTipText(resourceMap.getString("jSlider_SPLIC1.toolTipText")); // NOI18N
        jSlider_SPLIC1.setValue(5000);
        jSlider_SPLIC1.setName("jSlider_SPLIC1"); // NOI18N
        jSlider_SPLIC1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider_SPLIC1StateChanged(evt);
            }
        });

        jProgressBar_LT1.setMaximum(16383);
        jProgressBar_LT1.setOrientation(1);
        jProgressBar_LT1.setValue(5000);
        jProgressBar_LT1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jProgressBar_LT1.setName("jProgressBar_LT1"); // NOI18N
        jProgressBar_LT1.setStringPainted(true);

        jProgressBar_SPLIC1.setBackground(resourceMap.getColor("jProgressBar_SPLIC1.background")); // NOI18N
        jProgressBar_SPLIC1.setMaximum(16383);
        jProgressBar_SPLIC1.setOrientation(1);
        jProgressBar_SPLIC1.setValue(5000);
        jProgressBar_SPLIC1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jProgressBar_SPLIC1.setName("jProgressBar_SPLIC1"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        buttonGroup1.add(jRadioButton_LIC1_Auto);
        jRadioButton_LIC1_Auto.setText(resourceMap.getString("jRadioButton_LIC1_Auto.text")); // NOI18N
        jRadioButton_LIC1_Auto.setName("jRadioButton_LIC1_Auto"); // NOI18N
        jRadioButton_LIC1_Auto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton_LIC1_AutoActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton_LIC1_Manu);
        jRadioButton_LIC1_Manu.setText(resourceMap.getString("jRadioButton_LIC1_Manu.text")); // NOI18N
        jRadioButton_LIC1_Manu.setName("jRadioButton_LIC1_Manu"); // NOI18N
        jRadioButton_LIC1_Manu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton_LIC1_ManuActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jSlider_OPLIC1.setMaximum(16383);
        jSlider_OPLIC1.setMinorTickSpacing(1024);
        jSlider_OPLIC1.setOrientation(javax.swing.JSlider.VERTICAL);
        jSlider_OPLIC1.setPaintTicks(true);
        jSlider_OPLIC1.setToolTipText(resourceMap.getString("jSlider_OPLIC1.toolTipText")); // NOI18N
        jSlider_OPLIC1.setValue(5000);
        jSlider_OPLIC1.setName("jSlider_OPLIC1"); // NOI18N
        jSlider_OPLIC1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider_OPLIC1StateChanged(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jProgressBar_LT1, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(jProgressBar_SPLIC1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSlider_SPLIC1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(jSlider_OPLIC1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(48, 48, 48)
                                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel_LV1)
                                    .addComponent(jLabel_LT1)
                                    .addComponent(jRadioButton_LIC1_Manu)
                                    .addComponent(jRadioButton_LIC1_Auto)))
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)))
                .addGap(81, 81, 81))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(jLabel_LV1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel_LT1)
                        .addGap(42, 42, 42)
                        .addComponent(jRadioButton_LIC1_Auto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton_LIC1_Manu)
                        .addContainerGap(303, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jSlider_OPLIC1, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                            .addComponent(jSlider_SPLIC1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                            .addComponent(jProgressBar_SPLIC1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                            .addComponent(jProgressBar_LT1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addGap(145, 145, 145))))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(ihm.IhmApp.class).getContext().getActionMap(IhmView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 297, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void jSlider_SPLIC1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider_SPLIC1StateChanged
        splic1Tag.setValueInt(jSlider_SPLIC1.getValue());
    }//GEN-LAST:event_jSlider_SPLIC1StateChanged

    private void jRadioButton_LIC1_ManuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_LIC1_ManuActionPerformed
        lic1AutoManuTag.setValueBool(jRadioButton_LIC1_Manu.isSelected());        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton_LIC1_ManuActionPerformed

    private void jRadioButton_LIC1_AutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_LIC1_AutoActionPerformed
        lic1AutoManuTag.setValueBool(jRadioButton_LIC1_Manu.isSelected());        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton_LIC1_AutoActionPerformed

    private void jSlider_OPLIC1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider_OPLIC1StateChanged
       lv1Tag.setValueInt(jSlider_OPLIC1.getValue());
    }//GEN-LAST:event_jSlider_OPLIC1StateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel_LT1;
    private javax.swing.JLabel jLabel_LV1;
    private javax.swing.JProgressBar jProgressBar_LT1;
    private javax.swing.JProgressBar jProgressBar_SPLIC1;
    private javax.swing.JRadioButton jRadioButton_LIC1_Auto;
    private javax.swing.JRadioButton jRadioButton_LIC1_Manu;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSlider jSlider_OPLIC1;
    private javax.swing.JSlider jSlider_SPLIC1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
    private ConnecteurSql connecteur;
    private Tag lv1Tag,lt1Tag,splic1Tag;
    private Tag secondeTag;
    private Tag lic1AutoManuTag; //true=MANU
    private Timer t1;

    private void startComm() {
        try {
            connecteur = new ConnecteurSql();
            connecteur.getConnexion();

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,
                    "Erreur JDBC : " + ex.getMessage());
        }
    }

    private void initTags() {
        secondeTag = new Tag(connecteur, "Seconde");
        lt1Tag = new Tag(connecteur, "LT1");
        lv1Tag = new Tag(connecteur, "LV1");
        splic1Tag = new Tag(connecteur, "SP_LIC1");
        lic1AutoManuTag = new Tag(connecteur, "AUTOMANU_LIC1");
    }

    private void updateAll() {
        jLabel_LT1.setText(secondeTag.getValueString());
        jProgressBar_LT1.setValue(lt1Tag.getValueInt());
        jProgressBar_SPLIC1.setValue(splic1Tag.getValueInt());
        jSlider_OPLIC1.setValue(lv1Tag.getValueInt());
        jSlider_SPLIC1.setValue(splic1Tag.getValueInt());
        jLabel_LV1.setText(lv1Tag.getValueString());
        jRadioButton_LIC1_Manu.setSelected(lic1AutoManuTag.isValueBool());
        jRadioButton_LIC1_Auto.setSelected(!lic1AutoManuTag.isValueBool());
    }

    public void actionPerformed(ActionEvent ae) {
        updateAll();
    }

    private void afterInitComponents() {
        //Timer pour le rafraichissement des valeurs par rapport à la BDD
        t1 = new Timer(1000, this);
        t1.start();
    }

}
