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

import ch.psi.pshell.device.Motor;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.State;
import ch.psi.utils.swing.SwingUtils;
import java.awt.Component;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JCheckBox;
import javax.swing.SpinnerNumberModel;

/**
 *
 */
public class HoloScan extends Panel {

    public HoloScan() {
        initComponents();            
    }
        
    //Overridable callbacks
    @Override
    public void onInitialize(int runCount) {
        Motor phi = (Motor) getDevice("motor");   
        Motor theta = (Motor) getDevice("motor2");   
        motorPanelPhi.setDevice(phi);
        motorPanelTheta.setDevice(theta);
        spinnerFromPhi.setModel(new SpinnerNumberModel(phi.getMinValue(), phi.getMinValue(), phi.getMaxValue(), 1.0));
        spinnerToPhi.setModel(new SpinnerNumberModel(phi.getMaxValue(), phi.getMinValue(), phi.getMaxValue(), 1.0));
        spinnerFromTheta.setModel(new SpinnerNumberModel(theta.getMinValue(), theta.getMinValue(), theta.getMaxValue(), 1.0));
        spinnerToTheta.setModel(new SpinnerNumberModel(theta.getMaxValue(), theta.getMinValue(), theta.getMaxValue(), 1.0));
    }

    @Override
    public void onStateChange(State state, State former) {
        setEnabled(isEnabled());
    }

    @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 : panelPhi.getComponents()) {
            c.setEnabled(enableControls);
        }            
        buttonStart.setEnabled(enableControls);
        
        spinnerStepSizePhi.setEnabled(enableControls && radioStepSize.isSelected());
        spinnerStepsPhi.setEnabled(enableControls && radioSteps.isSelected());
        spinnerFromPhi.setEnabled(enableControls && radioAbsolute.isSelected());
        spinnerToPhi.setEnabled(enableControls && radioAbsolute.isSelected());
        spinnerRangePhi.setEnabled(enableControls && !radioAbsolute.isSelected());

        spinnerStepSizeTheta.setEnabled(spinnerStepSizePhi.isEnabled());
        spinnerStepsTheta.setEnabled(spinnerStepsPhi.isEnabled());
        spinnerFromTheta.setEnabled(spinnerFromPhi.isEnabled());
        spinnerToTheta.setEnabled(spinnerToPhi.isEnabled());
        spinnerRangeTheta.setEnabled(spinnerRangePhi.isEnabled());
    }

    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);
        if (radioRelative.isSelected()) {
            args.put("PHI_RANGE", new Double[]{-(Double) spinnerRangePhi.getValue() / 2, (Double) spinnerRangePhi.getValue() / 2});
            args.put("THETA_RANGE", new Double[]{-(Double) spinnerRangeTheta.getValue() / 2, (Double) spinnerRangeTheta.getValue() / 2});
        } else {
            args.put("PHI_RANGE", new Double[]{(Double) spinnerFromPhi.getValue(), (Double) spinnerToPhi.getValue()});
            args.put("THETA_RANGE", new Double[]{(Double) spinnerFromTheta.getValue(), (Double) spinnerToTheta.getValue()});
        }
        ArrayList steps = new ArrayList();
        if (radioStepSize.isSelected()) {            
            steps.add((Double) spinnerStepSizePhi.getValue());
            steps.add((Double) spinnerStepSizeTheta.getValue());            
        } else {
            steps.add((Integer) spinnerStepsPhi.getValue());
            steps.add((Integer) spinnerStepsTheta.getValue());
        }
        args.put("STEPS", steps);
        args.put("LATENCY", (Double) 0.0);
        args.put("RELATIVE", radioRelative.isSelected());
        
        runAsync("HoloScan", args);
    }


        @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();
        panelPhi = new javax.swing.JPanel();
        motorPanelPhi = new ch.psi.pshell.swing.MotorReadoutPanel();
        jLabel2 = new javax.swing.JLabel();
        spinnerRangePhi = new javax.swing.JSpinner();
        radioStepSize = new javax.swing.JRadioButton();
        radioSteps = new javax.swing.JRadioButton();
        spinnerStepSizePhi = new javax.swing.JSpinner();
        spinnerStepsPhi = new javax.swing.JSpinner();
        radioAbsolute = new javax.swing.JRadioButton();
        spinnerFromPhi = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        spinnerToPhi = new javax.swing.JSpinner();
        radioRelative = new javax.swing.JRadioButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        motorPanelTheta = new ch.psi.pshell.swing.MotorReadoutPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        spinnerFromTheta = new javax.swing.JSpinner();
        spinnerToTheta = new javax.swing.JSpinner();
        jLabel10 = new javax.swing.JLabel();
        spinnerRangeTheta = new javax.swing.JSpinner();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        spinnerStepSizeTheta = new javax.swing.JSpinner();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        spinnerStepsTheta = new javax.swing.JSpinner();
        panelSensors = new javax.swing.JPanel();
        checkImage = new javax.swing.JCheckBox();
        checkImageIntegration = new javax.swing.JCheckBox();
        checkSpectrum = new javax.swing.JCheckBox();
        checkCounts1 = new javax.swing.JCheckBox();
        checkTotalCount = 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();

        panelPhi.setBorder(javax.swing.BorderFactory.createTitledBorder("Positioners"));
        panelPhi.setPreferredSize(new java.awt.Dimension(239, 538));

        jLabel2.setText("Range Phi:");

        spinnerRangePhi.setModel(new javax.swing.SpinnerNumberModel(1.0d, 0.001d, 100.0d, 1.0d));
        spinnerRangePhi.setEnabled(false);

        buttonGroup1.add(radioStepSize);
        radioStepSize.setSelected(true);
        radioStepSize.setText("Step Size");
        radioStepSize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioStepSizeActionPerformed(evt);
            }
        });

        buttonGroup1.add(radioSteps);
        radioSteps.setText("Number of Steps");
        radioSteps.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioStepSizeActionPerformed(evt);
            }
        });

        spinnerStepSizePhi.setModel(new javax.swing.SpinnerNumberModel(0.1d, 0.001d, 10.0d, 0.1d));

        spinnerStepsPhi.setModel(new javax.swing.SpinnerNumberModel(10, 1, 1000, 1));
        spinnerStepsPhi.setEnabled(false);

        buttonGroup2.add(radioAbsolute);
        radioAbsolute.setSelected(true);
        radioAbsolute.setText("Absolute Scan");
        radioAbsolute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioAbsoluteActionPerformed(evt);
            }
        });

        spinnerFromPhi.setModel(new javax.swing.SpinnerNumberModel(1.0d, 0.0d, 1.0d, 1.0d));

        jLabel4.setText("Phi From:");

        jLabel5.setText("Phi To:");

        spinnerToPhi.setModel(new javax.swing.SpinnerNumberModel(1.0d, 0.0d, 1.0d, 1.0d));

        buttonGroup2.add(radioRelative);
        radioRelative.setText("Relative Scan");
        radioRelative.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioAbsoluteActionPerformed(evt);
            }
        });

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel6.setText("Phi:");

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel7.setText("Theta:");

        jLabel8.setText("Theta To:");

        jLabel9.setText("Theta From:");

        spinnerFromTheta.setModel(new javax.swing.SpinnerNumberModel(1.0d, 0.0d, 1.0d, 1.0d));

        spinnerToTheta.setModel(new javax.swing.SpinnerNumberModel(1.0d, 0.0d, 1.0d, 1.0d));

        jLabel10.setText("Range Theta:");

        spinnerRangeTheta.setModel(new javax.swing.SpinnerNumberModel(1.0d, 0.001d, 100.0d, 1.0d));
        spinnerRangeTheta.setEnabled(false);

        jLabel11.setText("Phi:");

        jLabel12.setText("Theta:");

        spinnerStepSizeTheta.setModel(new javax.swing.SpinnerNumberModel(0.1d, 0.001d, 10.0d, 0.1d));

        jLabel13.setText("Phi:");

        jLabel14.setText("Theta:");

        spinnerStepsTheta.setModel(new javax.swing.SpinnerNumberModel(10, 1, 1000, 1));
        spinnerStepsTheta.setEnabled(false);

        javax.swing.GroupLayout panelPhiLayout = new javax.swing.GroupLayout(panelPhi);
        panelPhi.setLayout(panelPhiLayout);
        panelPhiLayout.setHorizontalGroup(
            panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPhiLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelPhiLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(motorPanelPhi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(panelPhiLayout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(motorPanelTheta, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(panelPhiLayout.createSequentialGroup()
                        .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(radioSteps)
                            .addComponent(radioStepSize)
                            .addComponent(radioAbsolute)
                            .addComponent(radioRelative))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPhiLayout.createSequentialGroup()
                        .addGap(0, 74, Short.MAX_VALUE)
                        .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPhiLayout.createSequentialGroup()
                                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(spinnerFromPhi, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(spinnerRangePhi, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(spinnerToPhi, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(spinnerFromTheta, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(spinnerToTheta, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(spinnerRangeTheta, javax.swing.GroupLayout.Alignment.TRAILING)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPhiLayout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(spinnerStepSizePhi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPhiLayout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(spinnerStepSizeTheta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPhiLayout.createSequentialGroup()
                                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel14)
                                    .addComponent(jLabel13))
                                .addGap(6, 6, 6)
                                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(spinnerStepsPhi, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(spinnerStepsTheta, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap())
        );

        panelPhiLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {radioStepSize, radioSteps});

        panelPhiLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {spinnerRangePhi, spinnerStepSizePhi, spinnerStepsPhi});

        panelPhiLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel6, jLabel7});

        panelPhiLayout.setVerticalGroup(
            panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPhiLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel6)
                    .addComponent(motorPanelPhi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel7)
                    .addComponent(motorPanelTheta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(radioAbsolute)
                .addGap(3, 3, 3)
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerFromPhi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(3, 3, 3)
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerToPhi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(3, 3, 3)
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerFromTheta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(3, 3, 3)
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerToTheta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addComponent(radioRelative)
                .addGap(1, 1, 1)
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(spinnerRangePhi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(1, 1, 1)
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(spinnerRangeTheta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(radioStepSize)
                .addGap(3, 3, 3)
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerStepSizePhi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addGap(3, 3, 3)
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerStepSizeTheta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addGap(1, 1, 1)
                .addComponent(radioSteps)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerStepsPhi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPhiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerStepsTheta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelSensors.setBorder(javax.swing.BorderFactory.createTitledBorder("Sensors"));
        panelSensors.setPreferredSize(new java.awt.Dimension(239, 422));

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

        checkImageIntegration.setSelected(true);
        checkImageIntegration.setText("Scienta Image Integration");
        checkImageIntegration.setToolTipText("");
        checkImageIntegration.setContentAreaFilled(false);
        checkImageIntegration.setName("integration"); // NOI18N

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

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

        checkTotalCount.setSelected(true);
        checkTotalCount.setText("Total Counts");
        checkTotalCount.setName("sinp"); // 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("Current");
        checkCurrent.setName("current"); // NOI18N

        checkCur1.setText("Cur 1");
        checkCur1.setName("cur1"); // NOI18N

        checkCur2.setText("Cur 2");
        checkCur2.setName("cur2"); // NOI18N

        checkCur3.setText("Cur 3");
        checkCur3.setName("cur3"); // 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, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(panelSensorsLayout.createSequentialGroup()
                        .addGroup(panelSensorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(checkImage)
                            .addComponent(checkImageIntegration)
                            .addComponent(checkSpectrum)
                            .addComponent(checkCounts1)
                            .addComponent(checkTotalCount)
                            .addComponent(checkCounts2)
                            .addComponent(checkCounts3)
                            .addComponent(checkCounts4)
                            .addComponent(checkCurrent)
                            .addComponent(checkCur1)
                            .addComponent(checkCur2)
                            .addComponent(checkCur3))
                        .addGap(0, 56, Short.MAX_VALUE)))
                .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(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)
                .addGap(18, 18, Short.MAX_VALUE)
                .addComponent(buttonScientaSetup)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Scan Control"));
        jPanel3.setPreferredSize(new java.awt.Dimension(239, 119));

        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);
            }
        });

        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)
                    .addComponent(buttonStart, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonAbort, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))
                .addContainerGap())
        );
        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)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        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(panelPhi, 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(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelPhi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelSensors, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 538, 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
        abort();        
    }//GEN-LAST:event_buttonAbortActionPerformed

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

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

    // 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 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 checkImage;
    private javax.swing.JCheckBox checkImageIntegration;
    private javax.swing.JCheckBox checkSpectrum;
    private javax.swing.JCheckBox checkTotalCount;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    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 ch.psi.pshell.swing.MotorReadoutPanel motorPanelPhi;
    private ch.psi.pshell.swing.MotorReadoutPanel motorPanelTheta;
    private javax.swing.JPanel panelPhi;
    private javax.swing.JPanel panelSensors;
    private javax.swing.JRadioButton radioAbsolute;
    private javax.swing.JRadioButton radioRelative;
    private javax.swing.JRadioButton radioStepSize;
    private javax.swing.JRadioButton radioSteps;
    private javax.swing.JSpinner spinnerFromPhi;
    private javax.swing.JSpinner spinnerFromTheta;
    private javax.swing.JSpinner spinnerRangePhi;
    private javax.swing.JSpinner spinnerRangeTheta;
    private javax.swing.JSpinner spinnerStepSizePhi;
    private javax.swing.JSpinner spinnerStepSizeTheta;
    private javax.swing.JSpinner spinnerStepsPhi;
    private javax.swing.JSpinner spinnerStepsTheta;
    private javax.swing.JSpinner spinnerToPhi;
    private javax.swing.JSpinner spinnerToTheta;
    // End of variables declaration//GEN-END:variables
}

