Files
ncs/plugins/TestingPanel.java
boccioli_m a4baadf2e6 Closedown
2015-06-03 15:09:12 +02:00

416 lines
19 KiB
Java

/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
import ch.psi.pshell.dev.Motor;
import ch.psi.pshell.plot.LinePlotBase;
import ch.psi.pshell.plot.LinePlotJFree;
import ch.psi.pshell.plot.LinePlotSeries;
import ch.psi.pshell.plot.Plot;
import ch.psi.pshell.swing.MotorPanel;
import ch.psi.pshell.ui.App;
import ch.psi.utils.swing.MonitoredPanel;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.State;
import ch.psi.utils.swing.SwingUtils;
import ch.psi.wsaf.ApplicationStateException;
import ch.psi.wsaf.Task;
import ch.psi.wsaf.TaskRunMode;
import ch.psi.wsaf.TaskRunningException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
/**
*
*/
public class TestingPanel extends Panel {
MotorPanel motorPanel;
LinePlotBase plot ;
LinePlotSeries series;
Motor motor;
Task task = new Task() {
@Override
protected Object execute() throws Exception {
series.clear();
for (int i=0;i<1000;i++){
double pos = motor.read();
Thread.sleep(2);
plot.getAxis(Plot.AxisId.X).setRange(0, 1000);
series.appendData(i, pos);
}
return true;
}
};
@Override
protected JPanel create() {
getApp().addTask(task);
return new NetbeansPluginPanel();
}
@Override
protected void onInitialize(int runCount){
motor = (Motor) TestingPanel.this.getDevice("motor");
motorPanel.setDevice(motor);
}
protected void onExecutedFile(String fileName) {
try {
switch(fileName){
case "power-supply":
Object ret = eval("ret");
if (ret != null) {
SwingUtils.showMessage(getComponent(), "", ret.toString() + " - " + ret.getClass().getName());
}
break;
}
}
catch (Exception ex) {
SwingUtils.showException(getComponent(), ex);
}
}
public class NetbeansPluginPanel extends MonitoredPanel {
public NetbeansPluginPanel() {
initComponents();
motorPanel = new MotorPanel();
panel.add(motorPanel);
onInitialize(-1);
plot.setTitle("bo");
series=new LinePlotSeries("Position");
plot.addSeries(series);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
buttonExecute = new javax.swing.JButton();
buttonAbort = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
check = new javax.swing.JCheckBox();
spinner = new javax.swing.JSpinner();
panel = new javax.swing.JPanel();
jButton2 = new javax.swing.JButton();
cmTestPS = new javax.swing.JButton();
jCheckBox1 = new javax.swing.JCheckBox();
jButton4 = new javax.swing.JButton();
txtTestPSRes = new javax.swing.JTextField();
jButton3 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
txtOutput = new javax.swing.JTextArea();
plot = new ch.psi.pshell.plot.LinePlotJFree();
buttonExecute.setText("Execute test11");
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);
}
});
jButton1.setText("Test with params");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
check.setText("Var1");
spinner.setModel(new javax.swing.SpinnerNumberModel(1, 1, 10, 1));
panel.setLayout(new java.awt.BorderLayout());
jButton2.setText("Java Code");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
cmTestPS.setText("Test PS");
cmTestPS.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmTestPSActionPerformed(evt);
}
});
jCheckBox1.setActionCommand("check2");
jCheckBox1.setLabel("check2");
jCheckBox1.setName("check2"); // NOI18N
jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox1ActionPerformed(evt);
}
});
jButton4.setText("Test LS");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton3.setText("jButton3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
txtOutput.setColumns(20);
txtOutput.setRows(5);
jScrollPane1.setViewportView(txtOutput);
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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(check)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(buttonExecute, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(buttonAbort, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jButton1)
.addComponent(spinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(87, 87, 87)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, 394, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(cmTestPS)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtTestPSRes, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jCheckBox1)
.addComponent(jButton4))
.addGap(0, 0, Short.MAX_VALUE))))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, 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)
.addComponent(jButton2))))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(plot, 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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(jCheckBox1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cmTestPS)
.addComponent(txtTestPSRes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(spinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(buttonExecute)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonAbort))
.addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(check))
.addComponent(jScrollPane1, 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(jButton1)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(plot, 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 script
//run("test1");
//Alternatives to run script with arguments
eval("start = 22.0");
eval("end = 32.0");
eval("step = 10");
run("test11");
/*
HashMap args = new HashMap();
args.put("start", 12.0);
args.put("end", 52.0);
args.put("step", 10);
run("test11", args);
*/
//eval("run('test11', locals = {'start':10.0, 'end':50.0, 'step':40})");
// evalAsync("run('test11', locals = {'start':10.0, 'end':50.0, 'step':40})");
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
}//GEN-LAST:event_buttonExecuteActionPerformed
private void buttonAbortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAbortActionPerformed
abort();
}//GEN-LAST:event_buttonAbortActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
try {
boolean var1 = check.isSelected();
int var2 = (Integer)spinner.getValue();
int i = 0;
HashMap args = new HashMap();
args.put("relative", var1);
args.put("steps", var2);
run("args", args);
//evalAsync("run('args', locals = {'relative':" + (var1 ? "True" :"False") + ", 'steps':" + var2 + "})");
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
try {
getApp().startTask(task);
task.start(TaskRunMode.async);
} catch (Exception ex) {
SwingUtils.showException(NetbeansPluginPanel.this, ex);
}
/*
new Thread(() -> {
try {
series.clear();
for (int i=0;i<1000;i++){
double pos = motor.read();
Thread.sleep(2);
plot.getAxis(Plot.AxisId.X).setRange(0, 1000);
series.appendData(i, pos);
}
} catch (Exception ex) {
SwingUtils.showException(NetbeansPluginPanel.this, ex);
}
}).start(); */
}//GEN-LAST:event_jButton2ActionPerformed
private void cmTestPSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmTestPSActionPerformed
try {
boolean var1 = jCheckBox1.isSelected();
int var2 = 2;// (Integer)spinner.getValue();
int result;
HashMap args = new HashMap();
args.put("relative", var1);
args.put("steps", var2);
run("power-supply", args);
Object ret = eval("ret");
//try to print the result
txtTestPSRes.setText(ret.toString());
//evalAsync("run('args', locals = {'relative':" + (var1 ? "True" :"False") + ", 'steps':" + var2 + "})");
} catch (Exception ex) {
SwingUtils.showException(this, ex);
} // TODO add your handling code here:
}//GEN-LAST:event_cmTestPSActionPerformed
private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jCheckBox1ActionPerformed
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:
try {
boolean var1 = jCheckBox1.isSelected();
int var2 = 2;// (Integer)spinner.getValue();
HashMap args = new HashMap();
args.put("relative", var1);
args.put("steps", var2);
run("motor-slide", args);
//evalAsync("run('args', locals = {'relative':" + (var1 ? "True" :"False") + ", 'steps':" + var2 + "})");
} catch (Exception ex) {
SwingUtils.showException(this, ex);
} // TODO add your handling code here:
}//GEN-LAST:event_jButton4ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
txtOutput.setText("Working Directory = " +
System.getProperty("user.dir"));
}//GEN-LAST:event_jButton3ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton buttonAbort;
private javax.swing.JButton buttonExecute;
private javax.swing.JCheckBox check;
private javax.swing.JButton cmTestPS;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel panel;
private ch.psi.pshell.plot.LinePlotJFree plot;
private javax.swing.JSpinner spinner;
private javax.swing.JTextArea txtOutput;
private javax.swing.JTextField txtTestPSRes;
// End of variables declaration//GEN-END:variables
}
}