/*
 * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
 */

import ch.psi.pshell.epics.Scienta;
import ch.psi.pshell.scan.Scan;
import ch.psi.pshell.scan.ScanListener;
import ch.psi.pshell.scan.ScanRecord;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.Convert;
import ch.psi.utils.State;
import ch.psi.utils.swing.SwingUtils;
import java.awt.Component;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import javax.swing.JCheckBox;
import javax.swing.JSpinner;
import javax.swing.JToggleButton;

/**
 *
 */
public class PhotonEnergy extends Panel {

    public PhotonEnergy() {
        initComponents();
        
        //Component persistence
        ArrayList<Component> persistedComponents = new ArrayList<>();
        persistedComponents.addAll(Arrays.asList(SwingUtils.getComponentsByType(this, JToggleButton.class)));
        persistedComponents.addAll(Arrays.asList(SwingUtils.getComponentsByType(this, JSpinner.class)));        
        setPersistedComponents(persistedComponents.toArray(new Component[0]));
        startTimer(1000);
    }
    boolean running;

    //Overridable callbacks
    @Override
    public void onStart() {
        super.onStart();
        getContext().addScanListener(scanListener);
    }

    @Override
    public void onStop() {
        super.onStop();
        getContext().removeScanListener(scanListener);
    }

    @Override
    public void onInitialize(int runCount) {
        Scienta scienta = (Scienta) getDevice("scienta");  
        dvpAcqTime.setDevice(scienta.getAcquisitionTime());
        updateTable();
    }
    
    protected void onTimer(){
        try{            
            int seconds = (int) (Double.valueOf(dvpAcqTime.getLabel().getText()) * table.getRowCount());                                        
            textTotalTime.setText(LocalTime.ofSecondOfDay(seconds).toString());
        } catch (Exception ex){                    
            textTotalTime.setText("");
        }
    }    

    ScanListener scanListener = new ScanListener() {
        @Override
        public void onScanStarted(Scan scan, String string) {
            if (running) {
                table.setRowSelectionInterval(0, 0);
                SwingUtils.scrollToVisible(table, 0, 0);
            }
        }

        @Override
        public void onNewRecord(Scan scan, ScanRecord rec) {
            if (running) {
                int index = rec.getIndex() + 1;
                if (index < table.getRowCount()) {
                    table.setRowSelectionInterval(index, index);
                    SwingUtils.scrollToVisible(table, index, 0);
                }
            }
        }

        @Override
        public void onScanEnded(Scan scan, Exception ex) {
            if (running) {
                table.clearSelection();
            }
        }
    };

    @Override
    public void onStateChange(State state, State former) {
        setEnabled(isEnabled());
        if (!state.isProcessing()) {
            running = false;
        }
    }

    @Override
    public void onExecutedFile(String fileName, Object result) {
    }

    @Override
    public void setEnabled(boolean value) {
        super.setEnabled(value);
        buttonAbort.setEnabled(value && getState().isInitialized());

        boolean enableControls = (value && (getState() == State.Ready));
        for (Component c : panelSensors.getComponents()) {
            c.setEnabled(enableControls);
        }
        for (Component c : panelPositioner.getComponents()) {
            c.setEnabled(enableControls);
        }
        
        table.setEnabled(enableControls);
        buttonStart.setEnabled(enableControls);
        checkEndScan.setEnabled(enableControls);
        spinnerLow.setEnabled(enableControls && radioSwept.isSelected());
        spinnerHigh.setEnabled(enableControls && radioSwept.isSelected());
        spinnerCenter.setEnabled(enableControls && radioFixed.isSelected());
        
        buttonScientaSetup.setEnabled(value);
    }

    Double[][] getVector() {
        ArrayList<Double[]> ret = new ArrayList<>();
        Double start = (Double) spinnerStart.getValue();
        Double end = (Double) spinnerEnd.getValue();
        Double stepSize = (Double) spinnerStep.getValue();
        Double low = (Double) spinnerLow.getValue();
        Double center = (Double) spinnerCenter.getValue();
        Double high = (Double) spinnerHigh.getValue();

        if (end > start) {
            int step = 0;
            Double energy = start;
            do {
                if (energy > end) {
                    energy = end;
                }

                if (radioSwept.isSelected()) {
                    if (radioCfs.isSelected()) {
                        ret.add(new Double[]{energy, low, high});
                    } else {
                        ret.add(new Double[]{energy, low + (stepSize * step), high + (stepSize * step)});
                    }
                } else {
                    if (radioCfs.isSelected()) {
                        ret.add(new Double[]{energy, center});
                    } else {
                        ret.add(new Double[]{energy, center + (stepSize * step)});
                    }
                }

                step++;
                energy += stepSize;
                energy = Convert.roundDouble(energy, 3);
            } while (energy <= end);
        }
        return ret.toArray(new Double[0][0]);
    }

    void updateTable() {
        String[] titles = radioSwept.isSelected() ? new String[]{"Eph", "Elow", "Ehigh"} : new String[]{"Eph", "Ecenter"};
        table.setModel(new javax.swing.table.DefaultTableModel(getVector(), titles) {
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        });
    }

    void startScan() throws Exception {
        HashMap<String, Object> args = new HashMap<>();
        ArrayList<String> sensors = new ArrayList();
        for (Component c : panelSensors.getComponents()) {
            if ((c instanceof JCheckBox) && ((JCheckBox) c).isSelected()) {
                sensors.add(c.getName());
            }
        }
        args.put("SENSORS", sensors);
        args.put("VECTOR", getVector());
        args.put("LATENCY", (Double) spinnerLatency.getValue());
        args.put("MODE", radioSwept.isSelected() ? "swept" :"fixed");
        args.put("TYPE", radioCis.isSelected() ? "CIS" :"CFS");
        args.put("STEP", (Double) spinnerStep.getValue());
        args.put("ENDSCAN", checkEndScan.isSelected());                
        
        runAsync("PhotonEnergy", args);        
        running = true;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        panelPositioner = new javax.swing.JPanel();
        radioCis = new javax.swing.JRadioButton();
        radioCfs = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        spinnerLatency = new javax.swing.JSpinner();
        radioSwept = new javax.swing.JRadioButton();
        radioFixed = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        spinnerStart = new javax.swing.JSpinner();
        jLabel5 = new javax.swing.JLabel();
        spinnerEnd = new javax.swing.JSpinner();
        spinnerStep = new javax.swing.JSpinner();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        spinnerCenter = new javax.swing.JSpinner();
        spinnerLow = new javax.swing.JSpinner();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        spinnerHigh = new javax.swing.JSpinner();
        panelSensors = new javax.swing.JPanel();
        checkImage = new javax.swing.JCheckBox();
        checkImageIntegration = new javax.swing.JCheckBox();
        checkAngleDistribution = new javax.swing.JCheckBox();
        checkSpectrum = new javax.swing.JCheckBox();
        checkTotalCount = new javax.swing.JCheckBox();
        checkCounts1 = new javax.swing.JCheckBox();
        checkCounts2 = new javax.swing.JCheckBox();
        checkCounts3 = new javax.swing.JCheckBox();
        checkCounts4 = new javax.swing.JCheckBox();
        checkCurrent = new javax.swing.JCheckBox();
        checkCur1 = new javax.swing.JCheckBox();
        checkCur2 = new javax.swing.JCheckBox();
        checkCur3 = new javax.swing.JCheckBox();
        buttonScientaSetup = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        buttonStart = new javax.swing.JButton();
        buttonAbort = new javax.swing.JButton();
        checkEndScan = new javax.swing.JCheckBox();
        labelAcqTime = new javax.swing.JLabel();
        dvpAcqTime = new ch.psi.pshell.swing.DeviceValuePanel();
        textTotalTime = new javax.swing.JLabel();
        labelTotalTime = new javax.swing.JLabel();

        panelPositioner.setBorder(javax.swing.BorderFactory.createTitledBorder("Acquisition"));

        buttonGroup1.add(radioCis);
        radioCis.setSelected(true);
        radioCis.setText("Constant Initial State");
        radioCis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioCisActionPerformed(evt);
            }
        });

        buttonGroup1.add(radioCfs);
        radioCfs.setText("Constant Final State");
        radioCfs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioCisActionPerformed(evt);
            }
        });

        jLabel3.setText("Latency(s):");

        spinnerLatency.setModel(new javax.swing.SpinnerNumberModel(0.0d, 0.0d, 10.0d, 0.1d));

        buttonGroup2.add(radioSwept);
        radioSwept.setSelected(true);
        radioSwept.setText("Swept");
        radioSwept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioSweptActionPerformed(evt);
            }
        });

        buttonGroup2.add(radioFixed);
        radioFixed.setText("Fixed");
        radioFixed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioSweptActionPerformed(evt);
            }
        });

        jLabel4.setText("Eph Start");

        spinnerStart.setModel(new javax.swing.SpinnerNumberModel(100.0d, 0.0d, 2000.0d, 50.0d));
        spinnerStart.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinnerEnergyChanged(evt);
            }
        });

        jLabel5.setText("Eph End");

        spinnerEnd.setModel(new javax.swing.SpinnerNumberModel(200.0d, 0.0d, 2000.0d, 50.0d));
        spinnerEnd.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinnerEnergyChanged(evt);
            }
        });

        spinnerStep.setModel(new javax.swing.SpinnerNumberModel(50.0d, 0.001d, 1000.0d, 10.0d));
        spinnerStep.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinnerEnergyChanged(evt);
            }
        });

        jLabel6.setText("Eph Step");

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        table.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(table);

        jLabel7.setText("E center:");

        spinnerCenter.setModel(new javax.swing.SpinnerNumberModel(50.0d, 0.0d, 2000.0d, 10.0d));
        spinnerCenter.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinnerEnergyChanged(evt);
            }
        });

        spinnerLow.setModel(new javax.swing.SpinnerNumberModel(50.0d, 0.0d, 2000.0d, 10.0d));
        spinnerLow.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinnerEnergyChanged(evt);
            }
        });

        jLabel8.setText("E low:");

        jLabel9.setText("E high:");

        spinnerHigh.setModel(new javax.swing.SpinnerNumberModel(100.0d, 0.0d, 2000.0d, 10.0d));
        spinnerHigh.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinnerEnergyChanged(evt);
            }
        });

        javax.swing.GroupLayout panelPositionerLayout = new javax.swing.GroupLayout(panelPositioner);
        panelPositioner.setLayout(panelPositionerLayout);
        panelPositionerLayout.setHorizontalGroup(
            panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPositionerLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(radioCfs, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(panelPositionerLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(spinnerLatency, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPositionerLayout.createSequentialGroup()
                        .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panelPositionerLayout.createSequentialGroup()
                                .addComponent(radioFixed)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPositionerLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(spinnerLow, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                            .addComponent(spinnerHigh)
                            .addComponent(spinnerCenter)))
                    .addGroup(panelPositionerLayout.createSequentialGroup()
                        .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(radioCis)
                            .addComponent(radioSwept))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPositionerLayout.createSequentialGroup()
                        .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                        .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(spinnerStep, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                            .addComponent(spinnerEnd)
                            .addComponent(spinnerStart))))
                .addContainerGap())
        );
        panelPositionerLayout.setVerticalGroup(
            panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPositionerLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(radioSwept)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerLow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerHigh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radioFixed)
                .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerCenter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addComponent(radioCis)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radioCfs)
                .addGap(18, 18, 18)
                .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(spinnerStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(spinnerEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(spinnerStep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(panelPositionerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(spinnerLatency, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        panelSensors.setBorder(javax.swing.BorderFactory.createTitledBorder("Sensors"));

        checkImage.setSelected(true);
        checkImage.setText("Scienta Image");
        checkImage.setToolTipText("");
        checkImage.setContentAreaFilled(false);
        checkImage.setName("Scienta.dataMatrix"); // NOI18N

        checkImageIntegration.setSelected(true);
        checkImageIntegration.setText("Scienta Energy Distribution");
        checkImageIntegration.setToolTipText("");
        checkImageIntegration.setContentAreaFilled(false);
        checkImageIntegration.setName("EnergyDistribution"); // NOI18N

        checkAngleDistribution.setSelected(true);
        checkAngleDistribution.setText("Scienta Angle Distribution");
        checkAngleDistribution.setToolTipText("");
        checkAngleDistribution.setContentAreaFilled(false);
        checkAngleDistribution.setName("AngleDistribution"); // NOI18N

        checkSpectrum.setText("Scienta Spectrum");
        checkSpectrum.setName("Scienta.spectrum"); // NOI18N

        checkTotalCount.setSelected(true);
        checkTotalCount.setText("Total Counts");
        checkTotalCount.setName("Counts"); // NOI18N

        checkCounts1.setText("Counts Region 1");
        checkCounts1.setName("CountsR1"); // NOI18N

        checkCounts2.setText("Counts Region 2");
        checkCounts2.setName("CountsR2"); // NOI18N

        checkCounts3.setText("Counts Region 3");
        checkCounts3.setName("CountsR3"); // NOI18N

        checkCounts4.setText("Counts Region 4");
        checkCounts4.setName("CountsR4"); // NOI18N

        checkCurrent.setText("Machine Current");
        checkCurrent.setName("MachineCurrent"); // NOI18N

        checkCur1.setText("Sample Current");
        checkCur1.setName("SampleCurrent"); // NOI18N

        checkCur2.setText("Ref Current");
        checkCur2.setName("RefCurrent"); // NOI18N

        checkCur3.setText("Aux Current");
        checkCur3.setName("AuxCurrent"); // NOI18N

        buttonScientaSetup.setText("Scienta Setup");
        buttonScientaSetup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonScientaSetupActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelSensorsLayout = new javax.swing.GroupLayout(panelSensors);
        panelSensors.setLayout(panelSensorsLayout);
        panelSensorsLayout.setHorizontalGroup(
            panelSensorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSensorsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelSensorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(buttonScientaSetup, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)
                    .addComponent(checkImage)
                    .addComponent(checkImageIntegration)
                    .addComponent(checkSpectrum)
                    .addComponent(checkCounts1)
                    .addComponent(checkTotalCount)
                    .addComponent(checkCounts2)
                    .addComponent(checkCounts3)
                    .addComponent(checkCounts4)
                    .addComponent(checkCurrent)
                    .addComponent(checkCur1)
                    .addComponent(checkCur2)
                    .addComponent(checkCur3)
                    .addComponent(checkAngleDistribution))
                .addContainerGap())
        );
        panelSensorsLayout.setVerticalGroup(
            panelSensorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSensorsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(checkImage)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkImageIntegration)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkAngleDistribution)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkSpectrum)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkTotalCount)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkCounts1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkCounts2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkCounts3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkCounts4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkCurrent)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkCur1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkCur2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkCur3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(buttonScientaSetup)
                .addGap(26, 26, 26))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Scan Control"));

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

        buttonAbort.setText("Abort");
        buttonAbort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAbortActionPerformed(evt);
            }
        });

        checkEndScan.setText("Turn off beam at end");

        labelAcqTime.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        labelAcqTime.setText("Acquisition time(s):");

        textTotalTime.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        textTotalTime.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        labelTotalTime.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        labelTotalTime.setText("Total time:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(checkEndScan)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(buttonStart, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonAbort, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGap(0, 1, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelAcqTime)
                            .addComponent(labelTotalTime))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textTotalTime, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(dvpAcqTime, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {dvpAcqTime, textTotalTime});

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {labelAcqTime, labelTotalTime});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(buttonStart)
                .addGap(18, 18, 18)
                .addComponent(buttonAbort)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(dvpAcqTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelAcqTime))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(labelTotalTime)
                    .addComponent(textTotalTime))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(checkEndScan)
                .addGap(26, 26, 26))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {dvpAcqTime, textTotalTime});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelSensors, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panelPositioner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(60, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jPanel3, panelPositioner, panelSensors});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelPositioner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelSensors, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonScientaSetupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonScientaSetupActionPerformed
        try {
            showDevicePanel("Scienta");
        } catch (Exception ex) {
            SwingUtils.showException(this, ex);
        }
    }//GEN-LAST:event_buttonScientaSetupActionPerformed

    private void buttonStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStartActionPerformed
        try {
            startScan();
        } catch (Exception ex) {
            SwingUtils.showException(this, ex);
        }
    }//GEN-LAST:event_buttonStartActionPerformed

    private void buttonAbortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAbortActionPerformed
        try {
            abort();
        } catch (Exception ex) {
            SwingUtils.showException(this, ex);
        }        
    }//GEN-LAST:event_buttonAbortActionPerformed

    private void radioCisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioCisActionPerformed
        setEnabled(isEnabled());
        updateTable();
    }//GEN-LAST:event_radioCisActionPerformed

    private void radioSweptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioSweptActionPerformed
        setEnabled(isEnabled());
        updateTable();
    }//GEN-LAST:event_radioSweptActionPerformed

    private void spinnerEnergyChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerEnergyChanged
        updateTable();
    }//GEN-LAST:event_spinnerEnergyChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAbort;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton buttonScientaSetup;
    private javax.swing.JButton buttonStart;
    private javax.swing.JCheckBox checkAngleDistribution;
    private javax.swing.JCheckBox checkCounts1;
    private javax.swing.JCheckBox checkCounts2;
    private javax.swing.JCheckBox checkCounts3;
    private javax.swing.JCheckBox checkCounts4;
    private javax.swing.JCheckBox checkCur1;
    private javax.swing.JCheckBox checkCur2;
    private javax.swing.JCheckBox checkCur3;
    private javax.swing.JCheckBox checkCurrent;
    private javax.swing.JCheckBox checkEndScan;
    private javax.swing.JCheckBox checkImage;
    private javax.swing.JCheckBox checkImageIntegration;
    private javax.swing.JCheckBox checkSpectrum;
    private javax.swing.JCheckBox checkTotalCount;
    private ch.psi.pshell.swing.DeviceValuePanel dvpAcqTime;
    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.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelAcqTime;
    private javax.swing.JLabel labelTotalTime;
    private javax.swing.JPanel panelPositioner;
    private javax.swing.JPanel panelSensors;
    private javax.swing.JRadioButton radioCfs;
    private javax.swing.JRadioButton radioCis;
    private javax.swing.JRadioButton radioFixed;
    private javax.swing.JRadioButton radioSwept;
    private javax.swing.JSpinner spinnerCenter;
    private javax.swing.JSpinner spinnerEnd;
    private javax.swing.JSpinner spinnerHigh;
    private javax.swing.JSpinner spinnerLatency;
    private javax.swing.JSpinner spinnerLow;
    private javax.swing.JSpinner spinnerStart;
    private javax.swing.JSpinner spinnerStep;
    private javax.swing.JTable table;
    private javax.swing.JLabel textTotalTime;
    // End of variables declaration//GEN-END:variables
}
