Files
dev/plugins/HarmonicScan.java
2020-06-17 10:41:16 +02:00

335 lines
16 KiB
Java

/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
import ch.psi.pshell.core.Context.ContextStateException;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.State;
import ch.psi.utils.swing.DsvEditor;
import ch.psi.utils.swing.Editor.EditorDialog;
import ch.psi.utils.swing.SwingUtils;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Properties;
import javax.swing.DefaultComboBoxModel;
/**
*
*/
public class HarmonicScan extends Panel {
public HarmonicScan() {
initComponents();
}
@Override
public void onInitialize(int runCount) {
super.onInitialize(runCount);
}
@Override
public void onStart() {
super.onStart();
loadConfig();
}
@Override
public void onStateChange(State state, State former) {
setEnabled(state == State.Ready);
}
@Override
public void onExecutedFile(String fileName, Object result) {
}
Path getEnergyTableFile() {
return Paths.get(getContext().getSetup().getConfigPath(), "harmonic_scan_energies.properties");
}
@Override
public void setEnabled(boolean value) {
buttonExecute.setEnabled(value);
comboSetup.setEnabled(value);
comboPolarizarion.setEnabled(value);
buttonConfigure.setEnabled(value);
}
void loadConfig() {
DefaultComboBoxModel model = (DefaultComboBoxModel) comboSetup.getModel();
model.removeAllElements();
try {
for (String line : Files.readAllLines(getEnergyTableFile())) {
if ((line != null) && (!line.trim().isEmpty())) {
String[] tokens = line.split("=");
if (tokens.length > 0) {
model.addElement(tokens[0].trim());
}
}
}
} catch (Exception ex) {
}
}
void setElement() throws Exception {
Properties prop = new Properties();
prop.load(new FileInputStream(getEnergyTableFile().toFile()));
String selection = comboSetup.getSelectedItem().toString();
String val = prop.getProperty(selection);
String[] tokens = val.split(" ");
if (tokens.length != 8) {
throw new Exception("Invalid file format");
}
spinnerEnergy.setValue(Double.valueOf(tokens[0].trim()));
}
EditorDialog dlgConfig;
void editEnergyTable() throws IOException{
if ((dlgConfig==null) || (!dlgConfig.isShowing())){
String[] columns = new String[]{"Element", "Energy"};
Class[] types = new Class[]{String.class, Double.class};
DsvEditor editor = new DsvEditor(columns , types, " ");
dlgConfig = editor.getDialog(getView(),false);
editor.load(getEnergyTableFile().toString());
editor.setTitle("Harmonic Scan Energy Table");
}
dlgConfig.setSize(640, 320);
showWindow(dlgConfig);
}
void run() throws ContextStateException {
HashMap args = new HashMap();
args.put("ID_ENERGY", (Double) spinnerEnergy.getValue());
args.put("HALFWIDTH", (Double) spinnerHalfwidth.getValue());
args.put("STEP", (Double) spinnerStep.getValue());
args.put("POLARIZATION ", comboPolarizarion.getSelectedItem().toString());
runAsync("HarmonicScan", args);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
buttonGroupPlot = new javax.swing.ButtonGroup();
jPanel1 = new javax.swing.JPanel();
comboSetup = new javax.swing.JComboBox();
buttonConfigure = new javax.swing.JButton();
jLabel10 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
comboPolarizarion = new javax.swing.JComboBox();
jLabel1 = new javax.swing.JLabel();
spinnerEnergy = new javax.swing.JSpinner();
jLabel2 = new javax.swing.JLabel();
spinnerHalfwidth = new javax.swing.JSpinner();
jLabel3 = new javax.swing.JLabel();
spinnerStep = new javax.swing.JSpinner();
jPanel3 = new javax.swing.JPanel();
buttonExecute = new javax.swing.JButton();
buttonAbort = new javax.swing.JButton();
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Setup"));
comboSetup.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
comboSetupActionPerformed(evt);
}
});
buttonConfigure.setText("Configure");
buttonConfigure.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonConfigureActionPerformed(evt);
}
});
jLabel10.setText("Element:");
jLabel11.setText("Polarization:");
comboPolarizarion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Manual", "Circ+", "Circ-", "LinV", "LinH", " " }));
jLabel1.setText("Energy");
spinnerEnergy.setModel(new javax.swing.SpinnerNumberModel(700.0d, 0.0d, 9999.0d, 1.0d));
jLabel2.setText("Halfwidth:");
spinnerHalfwidth.setModel(new javax.swing.SpinnerNumberModel(10.0d, 0.0d, 9999.0d, 1.0d));
jLabel3.setText("Step:");
spinnerStep.setModel(new javax.swing.SpinnerNumberModel(0.5d, 0.0d, 9999.0d, 1.0d));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel11, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(comboSetup, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonConfigure, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(comboPolarizarion, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(80, 80, 80)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(spinnerHalfwidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(spinnerStep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(spinnerEnergy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(9, Short.MAX_VALUE))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonConfigure, comboPolarizarion, comboSetup});
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {spinnerEnergy, spinnerHalfwidth, spinnerStep});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(comboSetup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel10)
.addComponent(jLabel1)
.addComponent(spinnerEnergy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonConfigure))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(spinnerHalfwidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel11)
.addComponent(comboPolarizarion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(spinnerStep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(135, Short.MAX_VALUE))
);
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Scan Control"));
buttonExecute.setText("Start");
buttonExecute.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonExecuteActionPerformed(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(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(buttonAbort, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(buttonExecute, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(buttonExecute)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonAbort)
.addContainerGap())
);
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(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void buttonExecuteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonExecuteActionPerformed
try {
run();
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
}//GEN-LAST:event_buttonExecuteActionPerformed
private void buttonAbortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAbortActionPerformed
try {
abort();
} catch (InterruptedException ex) {
SwingUtils.showException(this, ex);
}
}//GEN-LAST:event_buttonAbortActionPerformed
private void comboSetupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboSetupActionPerformed
try {
setElement();
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
}//GEN-LAST:event_comboSetupActionPerformed
private void buttonConfigureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonConfigureActionPerformed
try {
editEnergyTable();
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
}//GEN-LAST:event_buttonConfigureActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton buttonAbort;
private javax.swing.JButton buttonConfigure;
private javax.swing.JButton buttonExecute;
private javax.swing.ButtonGroup buttonGroupPlot;
private javax.swing.JComboBox comboPolarizarion;
private javax.swing.JComboBox comboSetup;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JSpinner spinnerEnergy;
private javax.swing.JSpinner spinnerHalfwidth;
private javax.swing.JSpinner spinnerStep;
// End of variables declaration//GEN-END:variables
}