/*
 * IgorTalkDialog.java
 *
 * Created on October 12, 2008, 12:17 PM
 */

/**
 *
 * @author  Nathan
 */
public class IgorTalkDialog extends javax.swing.JDialog {

  private IgorTalkServer iserver;
  private InstruView instruview;

  /** Creates new form IgorTalkDialog */
  public IgorTalkDialog(InstruView instruview, boolean modal) {
    super(instruview.instruViewFrame, modal);
    initComponents();
    this.instruview = instruview;
    
    // call to make sure iserver is not null
    iserver = instruview.getIgorTalkServer();
  }

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

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jTextField1 = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jButton6 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Igor Talk Server");

    jButton1.setText("Close");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        closeDialog(evt);
      }
    });

    jButton2.setText("Start Server");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        startServer(evt);
      }
    });

    jButton5.setText("Stop Server");
    jButton5.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        stopServer(evt);
      }
    });

    org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
        .add(jButton2)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
        .add(jButton5)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 194, Short.MAX_VALUE)
        .add(jButton1))
    );
    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
        .add(jButton1)
        .add(jButton2)
        .add(jButton5))
    );

    jButton3.setText("Plot Data");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        plotSpectraData(evt);
      }
    });

    jButton4.setText("Copy Selected Data");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton4ActionPerformed(evt);
      }
    });

    jTextField1.setText("Execute; Enter simple command");
    jTextField1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jTextField1ActionPerformed(evt);
      }
    });

    jLabel1.setText("Command");

    jButton6.setText("Waterfall Plot");
    jButton6.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        waterfallPlot(evt);
      }
    });

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
          .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
            .add(jButton4)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
            .add(jButton3)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 105, Short.MAX_VALUE)
            .add(jButton6))
          .add(layout.createSequentialGroup()
            .add(12, 12, 12)
            .add(jLabel1)
            .add(18, 18, 18)
            .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)))
        .add(4, 4, 4))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(jButton4)
          .add(jButton3)
          .add(jButton6))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
          .add(jLabel1))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    );

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

private void closeDialog(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeDialog
// TODO add your handling code here:
  setVisible(false);
  dispose();
}//GEN-LAST:event_closeDialog

private void startServer(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startServer
  iserver = instruview.getIgorTalkServer();
}//GEN-LAST:event_startServer

private void plotSpectraData(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_plotSpectraData
  instruview.plotSpectraDataUsingIgor();
}//GEN-LAST:event_plotSpectraData

// Method to send the selected data
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
  instruview.copySpectraDataToIgor();
}//GEN-LAST:event_jButton4ActionPerformed

// close the server conection
private void stopServer(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopServer
  instruview.stopIgorTalkServer();
}//GEN-LAST:event_stopServer

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
  iserver.sendCommand(jTextField1.getText());
}//GEN-LAST:event_jTextField1ActionPerformed

// do a water plot of 3D data
private void waterfallPlot(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_waterfallPlot
  instruview.plotWaterfallUsingIgor();
}//GEN-LAST:event_waterfallPlot

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JButton jButton3;
  private javax.swing.JButton jButton4;
  private javax.swing.JButton jButton5;
  private javax.swing.JButton jButton6;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JTextField jTextField1;
  // End of variables declaration//GEN-END:variables
}
