/*
 *****************************************************************************
 *                                                                           *
 *              IMPINJ CONFIDENTIAL AND PROPRIETARY                          *
 *                                                                           *
 * This source code is the sole property of Impinj, Inc. Reproduction or     *
 * utilization of this source code in whole or in part is forbidden without  *
 * the prior written consent of Impinj, Inc.                                 *
 *                                                                           *
 * (c) Copyright Impinj, Inc. 2014. All rights reserved.                     *
 *                                                                           *
 *****************************************************************************/

package com.impinj.stpexamples.inline;

import java.util.Properties;

public class InlineSystemFrame extends javax.swing.JFrame {
    public InlineSystem.SwingTableModelController controller;
    javax.swing.SwingWorker<Boolean, Void> swingWorker;
    javax.swing.Timer refreshTimer;
    /**
     * Creates new form InlineSystemFrame
     */
    public InlineSystemFrame(InlineSystem.SwingTableModelController tableModel, Properties prop) {
        this.controller = tableModel;
        initComponents();
        refreshTimer = new javax.swing.Timer(1000,
                new java.awt.event.ActionListener() {
                        public void actionPerformed(
                                java.awt.event.ActionEvent evt) {
                            controller.fireTableDataChanged();
                        }
                });
        firstVerifierTextField.setText(prop.getProperty("FirstVerifierIP"));
        firstEncoderTextField.setText(prop.getProperty("FirstEncoderIP"));
        secondVerifierTextField.setText(prop.getProperty("SecondVerifierIP"));
        secondEncoderTextField.setText(prop.getProperty("SecondEncoderIP"));
        epc1to4TextField.setText(prop.getProperty("Epc1to4"));
        epc5to8TextField.setText(prop.getProperty("Epc5to8"));
        refreshTimer.setRepeats(true);
    }

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

        startStopButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        connectDisconnectButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        statusTextArea = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        firstEncoderTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        firstVerifierTextField = new javax.swing.JTextField();
        secondEncoderTextField = new javax.swing.JTextField();
        secondVerifierTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        epc1to4TextField = new javax.swing.JTextField();
        epc5to8TextField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        startStopButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        startStopButton.setText("Start");
        startStopButton.setEnabled(false);
        startStopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startStopButtonActionPerformed(evt);
            }
        });

        jTable1.setModel(controller);
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);

        connectDisconnectButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        connectDisconnectButton.setText("Connect");
        connectDisconnectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectDisconnectButtonActionPerformed(evt);
            }
        });

        statusTextArea.setColumns(20);
        statusTextArea.setRows(5);
        jScrollPane2.setViewportView(statusTextArea);

        jLabel1.setText("First Verifer IP");

        firstEncoderTextField.setEnabled(false);

        jLabel2.setText("First Encoder IP");

        jLabel4.setText("Second Encoder IP");

        jLabel3.setText("Second Verifer IP");

        firstVerifierTextField.setEnabled(false);
        firstVerifierTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                firstVerifierTextFieldActionPerformed(evt);
            }
        });

        secondEncoderTextField.setEnabled(false);

        secondVerifierTextField.setEnabled(false);

        jLabel5.setText("EPC 1-4");

        jLabel6.setText("EPC 5-8");

        epc1to4TextField.setEnabled(false);

        epc5to8TextField.setEnabled(false);

        jLabel7.setForeground(new java.awt.Color(153, 0, 0));
        jLabel7.setText("* Settings above are made in the inline.properties file");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(connectDisconnectButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(startStopButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(firstVerifierTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(firstEncoderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(secondVerifierTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(secondEncoderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(epc5to8TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(epc1to4TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(8, 8, 8))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel7)
                        .addGap(174, 174, 174)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(firstVerifierTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(secondVerifierTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(epc1to4TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel4)
                        .addComponent(firstEncoderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)
                        .addComponent(epc5to8TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(secondEncoderTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(connectDisconnectButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(startStopButton))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

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

    private void startStopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startStopButtonActionPerformed
        if (startStopButton.getText().equals("Start")) {
            connectDisconnectButton.setEnabled(false);
            (new javax.swing.SwingWorker<Boolean, Void>() {
                @Override protected Boolean doInBackground()
                        throws com.impinj.stp.Device.DeviceException {
                    controller.startDevices();
                    return true;
                }

                @Override protected void done() {
                    boolean wasSuccessful = false;
                    try {
                        wasSuccessful = get();
                        statusTextArea.setText("Started");
                    } catch (InterruptedException ie) {
                    } catch (java.util.concurrent.ExecutionException jucee){
                        java.io.Writer trace = new java.io.StringWriter();
                        jucee.getCause().printStackTrace(new java.io.PrintWriter(trace));
                        statusTextArea.setText(trace.toString());
                    }
                    if (wasSuccessful){
                        startStopButton.setEnabled(true);
                        startStopButton.setText("Stop");
                        refreshTimer.start();
                    }
                }
            }).execute();
        } else {
            connectDisconnectButton.setEnabled(false);
            (new javax.swing.SwingWorker<Boolean, Void>() {
                @Override protected Boolean doInBackground()
                        throws com.impinj.stp.Device.DeviceException {
                    controller.disengageDevices();
                    return true;
                }

      @Override
      protected void done() {
                    boolean wasSuccessful = false;
                    try {
                        wasSuccessful = get();
                        statusTextArea.setText("Stopped");
                    } catch (InterruptedException ie) {
                    } catch (java.util.concurrent.ExecutionException jucee){
                        java.io.Writer trace = new java.io.StringWriter();
                        jucee.getCause().printStackTrace(new java.io.PrintWriter(trace));
                        statusTextArea.setText(trace.toString());
                    }
                    if (wasSuccessful){
                        startStopButton.setEnabled(true);
                        startStopButton.setText("Start");
                        refreshTimer.stop();
                    }
                }
            }).execute();
        }
    }//GEN-LAST:event_startStopButtonActionPerformed

    private void connectDisconnectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectDisconnectButtonActionPerformed
        if (connectDisconnectButton.getText().equals("Connect")) {
            connectDisconnectButton.setEnabled(false);
            (new javax.swing.SwingWorker<Boolean, Void>() {
                @Override protected Boolean doInBackground()
                        throws java.io.IOException,
                                com.impinj.stp.Device.DeviceException,
                                Exception {
                    controller.connectDevices();
                    return true;
                }

                @Override protected void done() {
                    boolean wasSuccessful = false;
                    try {
                        wasSuccessful = get();
                        statusTextArea.setText("");
                    } catch (InterruptedException ie) {
                    } catch (java.util.concurrent.ExecutionException jucee){
                        connectDisconnectButton.setEnabled(true);
                        java.io.Writer trace = new java.io.StringWriter();
                        jucee.getCause().printStackTrace(new java.io.PrintWriter(trace));
                        statusTextArea.setText(trace.toString());
                    }
                    if (wasSuccessful){
                        startStopButton.setEnabled(true);
                    }
                }
            }).execute();
        } else {
            connectDisconnectButton.setText("Connect");
            startStopButton.setEnabled(false);
        }
    }//GEN-LAST:event_connectDisconnectButtonActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton connectDisconnectButton;
    private javax.swing.JTextField epc1to4TextField;
    private javax.swing.JTextField epc5to8TextField;
    private javax.swing.JTextField firstEncoderTextField;
    private javax.swing.JTextField firstVerifierTextField;
    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.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField secondEncoderTextField;
    private javax.swing.JTextField secondVerifierTextField;
    private javax.swing.JButton startStopButton;
    private javax.swing.JTextArea statusTextArea;
    // End of variables declaration//GEN-END:variables

}
