/* * Copyright (c) 2014-2017 Paul Scherrer Institute. All rights reserved. */ import ch.psi.pshell.device.Device; import ch.psi.pshell.epics.Epics; import ch.psi.pshell.plot.LinePlotSeries; import ch.psi.pshell.plot.Plot; import ch.psi.pshell.scan.Scan; import ch.psi.pshell.scan.ScanListener; import ch.psi.pshell.scan.ScanRecord; import ch.psi.pshell.ui.App; import ch.psi.pshell.ui.Panel; import ch.psi.pshell.ui.Plugin; import ch.psi.utils.State; import ch.psi.utils.swing.SwingUtils; import java.util.ArrayList; import java.util.List; import javax.swing.DefaultComboBoxModel; /** * */ public class WireScan extends Panel { final String[] seriesNames = new String[]{"bpm1_x", "bpm1_y", "bpm1_q", "bpm2_x", "bpm2_y", "bpm2_q"}; final int[] seriesYAxis = new int[]{1, 1, 2, 1, 1, 2}; LinePlotSeries[] series = new LinePlotSeries[seriesNames.length]; public WireScan() { initComponents(); for (int i=0; i< seriesNames.length; i++){ series[i] = new LinePlotSeries(seriesNames[i], null, seriesYAxis[i]); plot.addSeries(series[i]); } plot.getAxis(Plot.AxisId.X).setLabel("Position"); plot.getAxis(Plot.AxisId.Y).setLabel("mm"); plot.getAxis(Plot.AxisId.Y2).setLabel("pc"); plot.setLegendVisible(true); } //Overridable callbacks @Override public void onInitialize(int runCount) { try { DefaultComboBoxModel model = new DefaultComboBoxModel(); eval("run('Devices/Elements')", true); List ret = (List) ((Plugin)this).eval("get_wire_scans()", true); for (String scan: ret){ model.addElement(scan); } comboWireScan.setModel(model); eval("run('Devices/WireScanner')", true); model = new DefaultComboBoxModel(); ret = (List) ((Plugin)this).eval("WireScanner.Selection", true); for (String scan: ret){ model.addElement(scan); } comboSelection.setModel(model); comboWireScanActionPerformed(null); model = new DefaultComboBoxModel(); ret = (List) ((Plugin)this).eval("get_bpms()", true); model.addElement(""); for (String scan: ret){ model.addElement(scan); } comboBpm3.setModel(model); if (App.hasArgument("ws")){ comboWireScan.setSelectedItem(App.getArgumentValue("ws")); } } catch (Exception ex) { showException(ex); } } @Override public void onStateChange(State state, State former) { buttonMoveStart.setEnabled(state==State.Ready); buttonScan.setEnabled(state==State.Ready); buttonAbort.setEnabled(state==State.Busy); } @Override public void onExecutedFile(String fileName, Object result) { } //Callback to perform update - in event thread @Override protected void doUpdate() { } //Plot management ScanListener scanListener = new ScanListener() { @Override public void onScanStarted(Scan scan, String plotTitle) { if ("WireScan".equals(getContext().getExecutionPars().getName())) { for (LinePlotSeries s : series){ s.clear(); } } } @Override public void onNewRecord(Scan scan, ScanRecord record) { try{ if ("WireScan".equals(getContext().getExecutionPars().getName())) { double position = (Double) record.getValues()[1]; for (int i=0; i< series.length; i++){ if (record.getValues()[4 + i] != null){ series[i].appendData(position,(Double) record.getValues()[4 + i]); } } } } catch(Exception ex){ ex.printStackTrace(); } } @Override public void onScanEnded(Scan scan, Exception ex) { getContext().removeScanListener(this); } }; @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { comboWireScan = new javax.swing.JComboBox(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); comboSelection = new javax.swing.JComboBox(); jLabel3 = new javax.swing.JLabel(); textBpm1 = new javax.swing.JTextField(); textBpm2 = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); comboBpm3 = new javax.swing.JComboBox(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jLabel10 = new javax.swing.JLabel(); buttonScan = new javax.swing.JButton(); buttonAbort = new javax.swing.JButton(); buttonMoveStart = new javax.swing.JButton(); spinnerStart = new javax.swing.JSpinner(); spinnerEnd = new javax.swing.JSpinner(); spinnerVel = new javax.swing.JSpinner(); spinnerCycles = new javax.swing.JSpinner(); panelStatus = new ch.psi.pshell.swing.DeviceValuePanel(); plot = new ch.psi.pshell.plot.LinePlotJFree(); comboWireScan.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { comboWireScanActionPerformed(evt); } }); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); jLabel1.setText("Wire Scan:"); jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); jLabel2.setText("Selection:"); comboSelection.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { comboSelectionActionPerformed(evt); } }); jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); jLabel3.setText("BPM 1:"); textBpm1.setEditable(false); textBpm2.setEditable(false); jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); jLabel4.setText("BPM 2:"); jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); jLabel5.setText("BPM 3:"); jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); jLabel6.setText("Start (µm):"); jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); jLabel7.setText("End (µm):"); jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); jLabel8.setText("Vel (µm/s):"); jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); jLabel9.setText("Cycles:"); jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); jLabel10.setText("Scan status:"); buttonScan.setText("Scan"); buttonScan.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonScanActionPerformed(evt); } }); buttonAbort.setText("Abort"); buttonAbort.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonAbortActionPerformed(evt); } }); buttonMoveStart.setText("Go to Start"); buttonMoveStart.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonMoveStartActionPerformed(evt); } }); spinnerStart.setModel(new javax.swing.SpinnerNumberModel(1000.0d, -10000.0d, 10000.0d, 1.0d)); spinnerEnd.setModel(new javax.swing.SpinnerNumberModel(1000.0d, -10000.0d, 10000.0d, 1.0d)); spinnerVel.setModel(new javax.swing.SpinnerNumberModel(200.0d, 1.0d, 10000.0d, 1.0d)); spinnerCycles.setModel(new javax.swing.SpinnerNumberModel(1, 1, 100, 1)); plot.setTitle(""); 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() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(comboWireScan, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(jLabel8) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(buttonMoveStart, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(buttonScan, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(buttonAbort, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(comboSelection, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(textBpm1) .addComponent(textBpm2) .addComponent(comboBpm3, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(spinnerStart) .addComponent(spinnerVel)) .addGap(46, 46, 46) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel7) .addGap(8, 8, 8) .addComponent(spinnerEnd)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel9) .addGap(8, 8, 8) .addComponent(spinnerCycles)))) .addComponent(panelStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(plot, javax.swing.GroupLayout.DEFAULT_SIZE, 451, Short.MAX_VALUE) .addGap(11, 11, 11)) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel10, jLabel2, jLabel3, jLabel4, jLabel5, jLabel6, jLabel7, jLabel8, jLabel9}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(comboWireScan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(comboSelection, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(jLabel7) .addComponent(spinnerEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(spinnerStart, 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.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel8) .addComponent(jLabel9) .addComponent(spinnerVel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(spinnerCycles, 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.BASELINE) .addComponent(jLabel3) .addComponent(textBpm1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(textBpm2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5) .addComponent(comboBpm3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER) .addComponent(jLabel10) .addComponent(panelStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 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.BASELINE) .addComponent(buttonAbort) .addComponent(buttonScan) .addComponent(buttonMoveStart))) .addComponent(plot, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)) .addContainerGap()) ); }// //GEN-END:initComponents private void comboWireScanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboWireScanActionPerformed try { List ret = (List) ((Plugin)this).eval("get_wire_scans_bpms('" + comboWireScan.getSelectedItem().toString()+ "')", true); textBpm1.setText((ret==null) ? "" : ret.get(0)); textBpm2.setText((ret==null) ? "" : ret.get(1)); comboSelection.setSelectedIndex(Epics.get(comboWireScan.getSelectedItem().toString() + ":WIRE_SP", Integer.class)); spinnerVel.setValue(Epics.get(comboWireScan.getSelectedItem().toString() + ":SCAN_VELO_SP", Double.class)); spinnerCycles.setValue(Epics.get(comboWireScan.getSelectedItem().toString() + ":NB_CYCL_SP", Integer.class)); if (panelStatus.getDevice()!=null){ panelStatus.getDevice().close(); } Device dev= (Device)eval("newScanInfoDevice(None, '" + comboWireScan.getSelectedItem().toString()+ "')", true); panelStatus.setDevice(dev); } catch (Exception ex) { panelStatus.setDevice(null); showException(ex); } }//GEN-LAST:event_comboWireScanActionPerformed private void comboSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboSelectionActionPerformed try { spinnerStart.setValue(Epics.get(comboWireScan.getSelectedItem().toString()+ ":" + comboSelection.getSelectedItem().toString() + "_START_SP", Double.class)); spinnerEnd.setValue(Epics.get(comboWireScan.getSelectedItem().toString() + ":" + comboSelection.getSelectedItem().toString() + "_END_SP", Double.class)); } catch (Exception ex) { showException(ex); } }//GEN-LAST:event_comboSelectionActionPerformed private void buttonScanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonScanActionPerformed ArrayList parameters = new ArrayList(); parameters.add(comboWireScan.getSelectedItem().toString()); parameters.add(comboSelection.getSelectedItem().toString()); parameters.add(spinnerStart.getValue()); parameters.add(spinnerEnd.getValue()); parameters.add(spinnerCycles.getValue()); parameters.add(spinnerVel.getValue()); parameters.add(comboBpm3.getSelectedItem().toString().isEmpty() ? null: comboBpm3.getSelectedItem().toString()); getContext().addScanListener(scanListener); try { runAsync("Diagnostics/WireScan", parameters).handle((ret, ex) -> { if (ex != null) { getLogger().info("Exception executing scan: " + ex); showException((Exception) ex); } else { SwingUtils.showMessage(WireScan.this, "Success", "Data file: \n" + getContext().getDataManager().getLastOutput()); } return ret; }); } catch (Exception ex) { showException(ex); } }//GEN-LAST:event_buttonScanActionPerformed private void buttonAbortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAbortActionPerformed try { abort(); } catch (Exception ex) { showException(ex); } }//GEN-LAST:event_buttonAbortActionPerformed private void buttonMoveStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMoveStartActionPerformed try { Epics.putq(comboWireScan.getSelectedItem().toString() + ":INIT.PROC", 1); } catch (Exception ex) { showException(ex); } }//GEN-LAST:event_buttonMoveStartActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton buttonAbort; private javax.swing.JButton buttonMoveStart; private javax.swing.JButton buttonScan; private javax.swing.JComboBox comboBpm3; private javax.swing.JComboBox comboSelection; private javax.swing.JComboBox comboWireScan; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; 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.JLabel jLabel8; private javax.swing.JLabel jLabel9; private ch.psi.pshell.swing.DeviceValuePanel panelStatus; private ch.psi.pshell.plot.LinePlotJFree plot; private javax.swing.JSpinner spinnerCycles; private javax.swing.JSpinner spinnerEnd; private javax.swing.JSpinner spinnerStart; private javax.swing.JSpinner spinnerVel; private javax.swing.JTextField textBpm1; private javax.swing.JTextField textBpm2; // End of variables declaration//GEN-END:variables }