Files
x07db/plugins/TimeResolved.java
gac-ISS e9ba08ce4f V2
2025-08-27 14:36:28 +02:00

483 lines
20 KiB
Java

import ch.psi.pshell.framework.Context;
import ch.psi.pshell.scan.Scan;
import ch.psi.pshell.framework.ScriptProcessor;
import ch.psi.pshell.framework.Setup;
import ch.psi.pshell.utils.EncoderJson;
import ch.psi.pshell.utils.IO;
import ch.psi.pshell.utils.State;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
*/
public class TimeResolved extends ScriptProcessor {
File currentFile;
public static final String FILE_EXTENSION = "tmr";
public TimeResolved() {
initComponents();
}
@Override
public String getType() {
return "Time Resolved";
}
@Override
public boolean canSave() {
return true;
}
@Override
public boolean createMenuNew() {
return true;
}
@Override
public boolean createFilePanel() {
return true;
}
@Override
public String getDescription() {
return "Time Resolved scan definition file (*." + FILE_EXTENSION + ")";
}
@Override
public String[] getExtensions() {
return new String[]{FILE_EXTENSION};
}
@Override
public String getHomePath() {
return "{home}/parameters";
}
//Overridable callbacks
@Override
public void onInitialize(int runCount) {
this.startTimer(1000);
}
@Override
public void onStateChange(State state, State former) {
buttonStart.setEnabled(state == State.Ready);
buttonAbort.setEnabled((state == State.Busy) && isRunning());
buttonScienta.setEnabled(state.isInitialized());
buttonAddToQueue.setEnabled(buttonStart.isEnabled());
boolean editing = !isExecuting();
buttonClear.setEnabled(editing);
buttonOpen.setEnabled(editing);
buttonSave.setEnabled(getState().isInitialized());
textName.setEnabled(editing);
spinnerScans.setEnabled(editing);
updateSeq();
}
@Override
public void onTimer() {
try{
if (isRunning()){
Scan scan = Context.getExecutionPars().getScan();
if (scan!=null){
int index = scan.getRecordIndex();
textCurScan.setText(String.valueOf(index));
return;
}
}
} catch(Exception ex){
getLogger().log(Level.WARNING, null, ex);
}
textCurScan.setText("");
}
void updateSeq() {
try {
textFileId.setText(String.valueOf(Context.getFileSequentialNumber()));
} catch (Exception ex) {
textFileId.setText("");
}
}
@Override
public void onExecutedFile(String fileName, Object result) {
}
//Callback to perform update - in event thread
@Override
protected void doUpdate() {
}
String getScanName() {
String name = textName.getText().trim();
return name.isEmpty() ? null : name;
}
@Override
public String getScript() {
return "templates/TimeResolved";
}
@Override
public Map<String, Object> getArgs() {
HashMap<String, Object> args = new HashMap<>();
args.put("FILE", null);
args.put("NAME", getScanName());
args.put("SCANS", spinnerScans.getValue());
return args;
}
@Override
public void saveAs(String fileName) throws IOException {
currentFile = new File(fileName);
ArrayList data = new ArrayList();
data.add(textName.getText());
data.add(spinnerScans.getValue());
String json = EncoderJson.encode(data, true);
Files.write(currentFile.toPath(), json.getBytes());
}
@Override
public void open(String fileName) throws IOException {
if (fileName==null){
currentFile = null;
textName.setText("");
spinnerScans.setValue(0);
} else {
Path path = Paths.get(fileName);
String json = new String(Files.readAllBytes(path));
currentFile = path.toFile();
Object[] vector = (Object[]) EncoderJson.decode(json, Object[].class);
textName.setText(String.valueOf(vector[0]));
spinnerScans.setValue(vector[1]);
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane2 = new javax.swing.JScrollPane();
jEditorPane1 = new javax.swing.JEditorPane();
buttonStart = new javax.swing.JButton();
buttonAbort = new javax.swing.JButton();
buttonScienta = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
spinnerScans = new javax.swing.JSpinner();
jLabel1 = new javax.swing.JLabel();
textName = new javax.swing.JTextField();
buttonOpen = new javax.swing.JButton();
buttonSave = new javax.swing.JButton();
buttonAddToQueue = new javax.swing.JButton();
buttonClear = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
textFileId = new javax.swing.JTextField();
buttonResetId = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
textCurScan = new javax.swing.JTextField();
jScrollPane2.setViewportView(jEditorPane1);
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);
}
});
buttonScienta.setText("Scienta Panel");
buttonScienta.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonScientaActionPerformed(evt);
}
});
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Parameters"));
jLabel3.setText("Scans:");
spinnerScans.setModel(new javax.swing.SpinnerNumberModel(10, 1, 1000000, 1));
jLabel1.setText("Name:");
buttonOpen.setText("Open");
buttonOpen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonOpenActionPerformed(evt);
}
});
buttonSave.setText("Save");
buttonSave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonSaveActionPerformed(evt);
}
});
buttonAddToQueue.setText("Add To Queue");
buttonAddToQueue.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonAddToQueueActionPerformed(evt);
}
});
buttonClear.setText("Clear");
buttonClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonClearActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(spinnerScans, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textName)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonClear)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonOpen)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonSave)))
.addGap(14, 14, 14))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttonAddToQueue)
.addContainerGap())
);
jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel3});
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textName)
.addComponent(jLabel1)
.addComponent(buttonOpen)
.addComponent(buttonSave)
.addComponent(buttonClear))
.addGap(11, 11, 11)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(spinnerScans, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
.addComponent(buttonAddToQueue)
.addContainerGap())
);
jLabel2.setText("File ID:");
textFileId.setEditable(false);
textFileId.setHorizontalAlignment(javax.swing.JTextField.CENTER);
buttonResetId.setText("Reset");
buttonResetId.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonResetIdActionPerformed(evt);
}
});
jLabel4.setText("Current Scan:");
textCurScan.setEditable(false);
textCurScan.setHorizontalAlignment(javax.swing.JTextField.CENTER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(buttonStart, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(33, 33, 33)
.addComponent(buttonAbort)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textCurScan, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(buttonScienta, 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)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textFileId, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(buttonResetId, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonAbort, buttonStart});
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonResetId, textCurScan, textFileId});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(8, 8, 8)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(textFileId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(textCurScan, 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(buttonResetId)
.addComponent(buttonScienta))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(buttonStart)
.addComponent(buttonAbort))
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void buttonStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStartActionPerformed
try {
execute();
} catch (Exception ex) {
showException(ex);
}
}//GEN-LAST:event_buttonStartActionPerformed
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 buttonScientaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonScientaActionPerformed
try {
this.showDevicePanel("scienta");
} catch (Exception ex) {
showException(ex);
}
}//GEN-LAST:event_buttonScientaActionPerformed
private void buttonResetIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonResetIdActionPerformed
try {
Context.setFileSequentialNumber(0);
updateSeq();
} catch (Exception ex) {
showException(ex);
}
}//GEN-LAST:event_buttonResetIdActionPerformed
private void buttonOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOpenActionPerformed
try {
open();
} catch (Exception ex) {
showException(ex);
}
}//GEN-LAST:event_buttonOpenActionPerformed
private void buttonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveActionPerformed
try {
JFileChooser chooser = new JFileChooser(Setup.expandPath(getHomePath()));
FileNameExtensionFilter filter = new FileNameExtensionFilter(getDescription(), getExtensions());
chooser.setFileFilter(filter);
try {
if (currentFile != null) {
chooser.setSelectedFile(currentFile);
} else if (getScanName() != null) {
File file = Paths.get(chooser.getCurrentDirectory().getAbsolutePath(), getScanName()).toFile();
chooser.setSelectedFile(file);
}
} catch (Exception ex) {
this.showException(ex);
}
int rVal = chooser.showSaveDialog(this);
if (rVal == JFileChooser.APPROVE_OPTION) {
String fileName = chooser.getSelectedFile().getAbsolutePath();
if (IO.getExtension(chooser.getSelectedFile().getAbsolutePath()).isEmpty()) {
fileName += "." + FILE_EXTENSION;
}
saveAs(fileName);
}
} catch (Exception ex) {
showException(ex);
}
}//GEN-LAST:event_buttonSaveActionPerformed
private void buttonAddToQueueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddToQueueActionPerformed
try {
queue(); //TODO: Data/file
} catch (Exception ex) {
showException( ex);
}
}//GEN-LAST:event_buttonAddToQueueActionPerformed
private void buttonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearActionPerformed
try {
open(null);
} catch (Exception ex) {
showException( ex);
}
}//GEN-LAST:event_buttonClearActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton buttonAbort;
private javax.swing.JButton buttonAddToQueue;
private javax.swing.JButton buttonClear;
private javax.swing.JButton buttonOpen;
private javax.swing.JButton buttonResetId;
private javax.swing.JButton buttonSave;
private javax.swing.JButton buttonScienta;
private javax.swing.JButton buttonStart;
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSpinner spinnerScans;
private javax.swing.JTextField textCurScan;
private javax.swing.JTextField textFileId;
private javax.swing.JTextField textName;
// End of variables declaration//GEN-END:variables
}