/* * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved. */ import ch.psi.pshell.dev.Device; import ch.psi.pshell.dev.DeviceListener; import ch.psi.pshell.epics.Scienta; import ch.psi.pshell.plot.LinePlotSeries; import ch.psi.pshell.plot.RangeSelectionPlot.RangeSelection; import ch.psi.pshell.ui.Panel; import ch.psi.utils.Convert; import ch.psi.utils.IO; import ch.psi.utils.Serializer; import ch.psi.utils.State; import ch.psi.utils.swing.SwingUtils; import java.io.File; import java.nio.file.Files; import java.util.HashMap; import javax.swing.JFileChooser; import javax.swing.SwingUtilities; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.DefaultTableModel; /** * */ public class XPSSpectrum extends Panel { boolean detailedScan; boolean running; Scienta scienta; public XPSSpectrum() { initComponents(); rangeSelectionPanel.setAditionalColumns(new String[]{"Time", "Size", "Iter"}, new Class[]{Double.class, Double.class, Integer.class}); final DefaultTableModel model = (DefaultTableModel) rangeSelectionPanel.getTable().getModel(); model.addTableModelListener(new TableModelListener() { @Override public void tableChanged(TableModelEvent e) { if (e.getType() == TableModelEvent.INSERT) { final int row = e.getFirstRow(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { if (row > 0) { for (int col = 3; col < 6; col++) { model.setValueAt(model.getValueAt(row - 1, col), row, col); } } else { model.setValueAt(scienta.getStepTime().take(), row, 3); model.setValueAt(scienta.getStepSize().take(), row, 4); model.setValueAt(Integer.valueOf(1), row, 5); } } }); } } }); } @Override public void onInitialize(int runCount) { scienta = (Scienta) getDevice("scienta"); scienta.getTotalChannels().addListener(progressListener); scienta.getCurrentChannel().addListener(progressListener); } @Override public void onStateChange(State state, State former) { setEnabled(state == State.Ready); if (!state.isProcessing()){ if (running){ running = false; getView().getStatusBar().setProgress(-1); } } } @Override public void onExecutedFile(String fileName, Object result) { String script = IO.getPrefix(fileName); if (script != null) { switch (script) { case "XPSSpectrum": if (result instanceof Exception) { //SwingUtils.showMessage(getComponent(), "Error in " + fileName, exception.getMessage()); } else { if (detailedScan) { } else { Object[] ret = (Object[]) ((Object[]) result)[0]; if (ret.length > 0) { //double[] data = (double[]) Convert.wrapperArrayToPrimitiveArray(ret[0], Double.class); double[] xdata = (double[]) ret[0]; double[] ydata = (double[]) ret[1]; //double[] data = new double[]{1.0,1.0,1.0,1.0,1.0}; LinePlotSeries series = new LinePlotSeries("Data"); rangeSelectionPanel.setSeries(series); series.setData(xdata, ydata); } } } break; } } detailedScan = false; updateButtons(); } @Override public void setEnabled(boolean value) { super.setEnabled(value); rangeSelectionPanel.setEnabled(value); valueLow.setEnabled(value); valueHigh.setEnabled(value); updateButtons(); } void updateButtons() { buttonInitialScan.setEnabled(isEnabled()); buttonDetailedScan.setEnabled(isEnabled() && rangeSelectionPanel.getPlot().getSelectedRanges().length > 0); btLoad.setEnabled(isEnabled()); btSave.setEnabled(buttonDetailedScan.isEnabled()); } DeviceListener progressListener = new DeviceListener() { @Override public void onStateChanged(Device device, State state, State state1) { } @Override public void onValueChanged(Device device, Object o, Object o1) { if (running){ Double total = scienta.getTotalChannels().take(); Double current = scienta.getCurrentChannel().take(); if ((total!=null) && (current!=null)){ getView().getStatusBar().setProgress(scienta.getProgress()); } } } }; @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { buttonAbort = new javax.swing.JButton(); buttonInitialScan = new javax.swing.JButton(); rangeSelectionPanel = new ch.psi.pshell.swing.RangeSelectionPanel() { protected void onSeriesChanged() { updateButtons(); } protected void onSelectionChanged() { updateButtons(); } } ; buttonDetailedScan = new javax.swing.JButton(); btSave = new javax.swing.JButton(); btLoad = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); valueLow = new ch.psi.pshell.swing.ValueSelection(); jLabel2 = new javax.swing.JLabel(); valueHigh = new ch.psi.pshell.swing.ValueSelection(); buttonAbort.setText("Abort"); buttonAbort.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonAbortActionPerformed(evt); } }); buttonInitialScan.setText("Initial Scan"); buttonInitialScan.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonInitialScanActionPerformed(evt); } }); buttonDetailedScan.setText("Detailed Scan"); buttonDetailedScan.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonDetailedScanActionPerformed(evt); } }); btSave.setText("Save"); btSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btSaveActionPerformed(evt); } }); btLoad.setText("Load"); btLoad.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btLoadActionPerformed(evt); } }); jLabel1.setText("Low:"); valueLow.setDecimals(2); valueLow.setMaxValue(1000.0); valueLow.setMinValue(0.0); valueLow.setShowButtons(false); valueLow.setValue(0.0); jLabel2.setText("High:"); valueHigh.setDecimals(2); valueHigh.setMaxValue(1000.0); valueHigh.setMinValue(0.0); valueHigh.setShowButtons(false); valueHigh.setValue(100.0); 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) .addComponent(rangeSelectionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(buttonInitialScan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(18, 18, 18) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(valueLow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(valueHigh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(60, 60, 60) .addComponent(buttonDetailedScan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(buttonAbort)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(btLoad) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btSave))) .addContainerGap()) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btLoad, btSave, buttonAbort, buttonDetailedScan, buttonInitialScan}); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {valueHigh, valueLow}); 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(buttonInitialScan) .addComponent(buttonDetailedScan) .addComponent(buttonAbort) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER) .addComponent(jLabel2) .addComponent(valueHigh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(valueLow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1))) .addGap(18, 18, 18) .addComponent(rangeSelectionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE) .addGap(43, 43, 43)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btLoad, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(btSave, javax.swing.GroupLayout.Alignment.TRAILING)))) .addContainerGap()) ); }// //GEN-END:initComponents private void buttonAbortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAbortActionPerformed abort(); }//GEN-LAST:event_buttonAbortActionPerformed private void buttonInitialScanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonInitialScanActionPerformed try { //getApp().startTask(task); HashMap args = new HashMap<>(); args.put("ranges", new RangeSelection[]{new RangeSelection(valueLow.getValue(),valueHigh.getValue())}); rangeSelectionPanel.clear(); detailedScan = false; runAsync("XPSSpectrum", args); running = true; } catch (Exception ex) { showException(ex); } }//GEN-LAST:event_buttonInitialScanActionPerformed private void buttonDetailedScanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDetailedScanActionPerformed try { RangeSelection[] ranges = rangeSelectionPanel.getRangesOrdered(); HashMap args = new HashMap<>(); args.put("ranges", ranges); detailedScan = true; runAsync("XPSSpectrum", args); running = true; } catch (Exception ex) { showException(ex); } }//GEN-LAST:event_buttonDetailedScanActionPerformed private void btSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveActionPerformed try { JFileChooser chooser = new JFileChooser(getController().getSetup().getContextPath()); FileNameExtensionFilter filter = new FileNameExtensionFilter("XPS Spectrum scan definition file", "xps"); chooser.setFileFilter(filter); int rVal = chooser.showSaveDialog(this); if (rVal == JFileChooser.APPROVE_OPTION) { File f = chooser.getSelectedFile(); if (IO.getExtension(f).isEmpty()){ f = new File(f.getPath() + ".xps"); } Object[] obj = new Object[]{SwingUtils.getTableData(rangeSelectionPanel.getTable()), rangeSelectionPanel.getPlot().getSeries(0).getX(), rangeSelectionPanel.getPlot().getSeries(0).getY()}; Files.write(f.toPath(), Serializer.encode(obj, Serializer.EncoderType.bin)); } } catch (Exception ex) { showException(ex); } }//GEN-LAST:event_btSaveActionPerformed private void btLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLoadActionPerformed try { JFileChooser chooser = new JFileChooser(getController().getSetup().getContextPath()); FileNameExtensionFilter filter = new FileNameExtensionFilter("XPS Spectrum scan definition file", "xps"); chooser.setFileFilter(filter); int rVal = chooser.showOpenDialog(this); if (rVal == JFileChooser.APPROVE_OPTION) { rangeSelectionPanel.removeAllRanges(); Object[] obj = (Object[])Serializer.decode(Files.readAllBytes(chooser.getSelectedFile().toPath()), Serializer.EncoderType.bin); Object[][] table = (Object[][]) obj[0]; if (!rangeSelectionPanel.getPlot().hasData()){ double[] x = (double[]) obj[1]; double[] y = (double[]) obj[2]; LinePlotSeries series = new LinePlotSeries("Data"); rangeSelectionPanel.setSeries(series); series.setData(x, y); } for (Object[] row:table){ rangeSelectionPanel.getPlot().addRange((Double)row[0], (Double)row[2]); } SwingUtilities.invokeLater(new Runnable() { @Override public void run() { for (int row = 0; row < table.length; row++) { for (int col = 3; col < 6; col++) { rangeSelectionPanel.getTable().setValueAt(table[row][col], row , col); } } } }); } } catch (Exception ex) { showException(ex); } }//GEN-LAST:event_btLoadActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btLoad; private javax.swing.JButton btSave; private javax.swing.JButton buttonAbort; private javax.swing.JButton buttonDetailedScan; private javax.swing.JButton buttonInitialScan; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private ch.psi.pshell.swing.RangeSelectionPanel rangeSelectionPanel; private ch.psi.pshell.swing.ValueSelection valueHigh; private ch.psi.pshell.swing.ValueSelection valueLow; // End of variables declaration//GEN-END:variables }