Files
ncs/plugins/TestingList.java
boccioli_m 495d462fd6 Closedown
2015-06-09 14:56:20 +02:00

436 lines
18 KiB
Java

/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
import ch.psi.utils.swing.MonitoredPanel;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.swing.SwingUtils;
import ch.psi.wsaf.Task;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.SimpleFormatter;
import javax.swing.JFileChooser;
import org.apache.commons.io.FilenameUtils;
public class TestingList extends Panel {
NetbeansPluginPanel testingList;
Task task = new Task() {
@Override
protected Object execute() throws Exception {
return true;
}
};
@Override
protected JPanel create() {
try {
testingList = new NetbeansPluginPanel();
} catch (IOException ex) {
Logger.getLogger(TestingList.class.getName()).log(Level.SEVERE, null, ex);
}
return testingList;
}
//listen to script end of execution and get return value
@Override
protected void onExecutedFile(String fileName) {
try {
switch(fileName){
case "power-supply":
Object ret = eval("ret");
boolean success = (boolean) eval("success");
Object deviceName = eval("deviceName");
Object testName = eval("testName");
String sSuccess = (success == true) ? "Success": "Failed";
if (ret != null) {
//SwingUtils.showMessage(getComponent(), "", ret.toString() + " - " + ret.getClass().getName());
testingList.showResult(deviceName.toString(), testName.toString(), ret.toString(), sSuccess);
}
break;
}
}
catch (Exception ex) {
SwingUtils.showException(getComponent(), ex);
}
}
public class NetbeansPluginPanel extends MonitoredPanel {
Logger logger = Logger.getLogger("TestsLog");
//these paths are converted to unix or win path according to host OS
private String TESTS_DEVICES_DEFAULT_DIR = FilenameUtils.separatorsToSystem("/home/script/tests/production/");
private String TESTS_TESTS_DEFAULT_DIR = FilenameUtils.separatorsToSystem("/home/script/tests/production/");
private String TESTS_LOG_DEFAULT_DIR = FilenameUtils.separatorsToSystem("/home/script/tests/log/TestsLog.txt");
//table1 columns indexes
private int COL_CHECK = 0;
private int COL_TIME = 1;
private int COL_RESULT = 6;
private int COL_SUCCESS = 7;
private int COL_ICON = 8;
private void initLogger(){
try {
FileHandler fh;
// This block configure the logger with handler and formatter
fh = new FileHandler(new java.io.File( "." ).getCanonicalPath() + TESTS_LOG_DEFAULT_DIR);
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public NetbeansPluginPanel() throws IOException {
initComponents();
initLogger();
buildTable();
loadTests();
}
@SuppressWarnings("unchecked")
public void showResult(String deviceName, String testName, String res, String status){
int rowD=-1, colT=-1;
//search for device name in table
for (int row = 0; row <= jTable1.getRowCount() - 1; row++) {
for (int col = 0; col <= jTable1.getColumnCount() - 1; col++) {
if (deviceName.equals(jTable1.getValueAt(row, col))) {
rowD = row;
break;
}
}
}
if (rowD>0){
//search for test name in table
for (int col = 0; col <= jTable1.getColumnCount() - 1; col++) {
if (testName.equals(jTable1.getValueAt(rowD, col))) {
colT = col;
break;
}
}
}
String iconSource = "/icons/button_help-16px.png";
switch(status){
case "Success":
iconSource = "/icons/button_ok-16px.png";
logger.log(Level.INFO, "Test "+ testName + " result: " + res + " (" + status + ")");
break;
case "Failed":
iconSource = "/icons/button_close-16px.png";
logger.log(Level.SEVERE, "Test "+ testName + " result: " + res + " (" + status + ")");
break;
case "Running":
iconSource = "/icons/button_play-16px.png";
logger.log(Level.INFO, "Running Test "+ testName + " ... ");
break;
}
ImageIcon icon = new ImageIcon(getClass().getResource(iconSource));
jTable1.setValueAt(icon, rowD, COL_ICON);
if(colT>0 && rowD>0){
jTable1.setValueAt(getNow(), rowD, COL_TIME);
jTable1.setValueAt(res, rowD, COL_RESULT);
jTable1.setValueAt(status, rowD, COL_SUCCESS);
jTable1.getCellEditor(rowD, COL_SUCCESS);
}
/*else{ //no test found in table. appen new one
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.addRow(new Object[]{true, getNow(), deviceName, testName, res, status, icon });
}*/
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jButtonRun = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jCheckBox1 = new javax.swing.JCheckBox();
jScrollPane1 = new javax.swing.JScrollPane();
jTextField1 = new javax.swing.JTextArea();
setLayout(new java.awt.BorderLayout(10, 10));
jButtonRun.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/button_play.png"))); // NOI18N
jButtonRun.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonRunActionPerformed(evt);
}
});
add(jButtonRun, java.awt.BorderLayout.CENTER);
jTable1.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Select", "Time", "Device", "Description", "Test Suite", "Test", "Result", "Status", ""
}
) {
Class[] types = new Class [] {
java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, javax.swing.Icon.class
};
boolean[] canEdit = new boolean [] {
true, true, true, true, true, true, true, true, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.getTableHeader().setReorderingAllowed(false);
jTable1.setRowHeight(22);
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTable1MouseClicked(evt);
}
});
jTable1.addInputMethodListener(new java.awt.event.InputMethodListener() {
public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
jTable1CaretPositionChanged(evt);
}
public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
jTable1InputMethodTextChanged(evt);
}
});
jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
jTable1KeyTyped(evt);
}
});
jScrollPane2.setViewportView(jTable1);
add(jScrollPane2, java.awt.BorderLayout.PAGE_END);
jCheckBox1.setText("All");
jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBox1ActionPerformed(evt);
}
});
add(jCheckBox1, java.awt.BorderLayout.LINE_START);
jTextField1.setColumns(20);
jTextField1.setRows(5);
jScrollPane1.setViewportView(jTextField1);
add(jScrollPane1, java.awt.BorderLayout.PAGE_START);
}// </editor-fold>//GEN-END:initComponents
private void jButtonRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRunActionPerformed
try {
boolean var1 = true;
int var2 = 2;// (Integer)spinner.getValue();
int result;
String deviceName = "RS";
String testName = "Range Shifter Tests";
HashMap args = new HashMap();
args.put("relative", var1);
args.put("steps", var2);
args.put("deviceName",deviceName);
args.put("testName",testName);
run("power-supply", args);
//this is just to try, must be modified
showResult(deviceName, testName, "Test running", "Running");
//evalAsync("run('args', locals = {'relative':" + (var1 ? "True" :"False") + ", 'steps':" + var2 + "})");
} catch (Exception ex) {
SwingUtils.showException(this, ex);
}
}//GEN-LAST:event_jButtonRunActionPerformed
private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
boolean bSelected = jCheckBox1.isSelected();
for (int row = 0; row <= jTable1.getRowCount() - 1; row++) {
jTable1.setValueAt(bSelected, row, 0);
}
updateStatus();
}//GEN-LAST:event_jCheckBox1ActionPerformed
public void updateStatus(){
String sStatus, iconSource;
boolean bSelected;
for (int row = 0; row <= jTable1.getRowCount() - 1; row++) {
bSelected = (boolean) jTable1.getValueAt(row, 0);
if(bSelected){
sStatus = "Pending";
iconSource = "/icons/button_pause-16px.png";
}
else{
sStatus = "Disabled";
iconSource = "/icons/button_stop-16px.png";
}
ImageIcon icon = new ImageIcon(getClass().getResource(iconSource));
jTable1.setValueAt(bSelected, row, COL_CHECK);
jTable1.setValueAt(icon, row, COL_ICON);
jTable1.setValueAt(sStatus, row, COL_SUCCESS);
}
}
private void jTable1InputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jTable1InputMethodTextChanged
// TODO add your handling code here:
}//GEN-LAST:event_jTable1InputMethodTextChanged
private void jTable1CaretPositionChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jTable1CaretPositionChanged
// TODO add your handling code here:
}//GEN-LAST:event_jTable1CaretPositionChanged
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
updateStatus();
// TODO add your handling code here:
}//GEN-LAST:event_jTable1MouseClicked
private void jTable1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyTyped
updateStatus();
// TODO add your handling code here:
}//GEN-LAST:event_jTable1KeyTyped
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButtonRun;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable jTable1;
private javax.swing.JTextArea jTextField1;
// End of variables declaration//GEN-END:variables
//table management
public void buildTable(){
String sDate = getNow();
ImageIcon icon = new ImageIcon(getClass().getResource("/icons/button_pause-16px.png"));
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
/*model.addRow(new Object[] {false, sDate, "LS", "Check Status", "No comm", "Pending", icon});
model.addRow(new Object[] {true, sDate, "RS", "Calibrate", "Calibration done", "Pending", icon});
model.addRow(new Object[] {false, sDate, "MWD", "Display Test", "Sequence terminated", "Pending", icon});
model.addRow(new Object[] {false, sDate, "FIXL", "Lamp Test", "Failure", "Pending", icon});*/
jTable1.setModel(model);
jTable1.getColumnModel().getColumn(COL_ICON).setMaxWidth(27);
jTable1.getColumnModel().getColumn(COL_CHECK).setMaxWidth(27);
updateStatus();
}
public void addToTable(String deviceName, String deviceDescription, String testName){
String sDate = "";
if(testName.equals("") || deviceName.equals("")) return;
ImageIcon icon = new ImageIcon(getClass().getResource("/icons/button_pause-16px.png"));
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.addRow(new Object[] {false, sDate, deviceName, deviceDescription, testName, "", "", "Pending", icon});
jTable1.setModel(model);
updateStatus();
}
public String getNow(){
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
//initial try, not needed any more
public void loadTest() throws FileNotFoundException, IOException{
Properties prop = new Properties();
String fileName = new java.io.File( "." ).getCanonicalPath()+ "\\home\\script\\tests\\devices\\LS\\.config";
this.jTextField1.setText(fileName);
InputStream is = new FileInputStream(fileName);
prop.load(is);
addToTable(prop.getProperty("name"), prop.getProperty("description"), prop.getProperty("tests"));
this.jTextField1.setText(prop.getProperty("name"));
}
public void loadTests() throws FileNotFoundException, IOException{
Properties prop = new Properties();
String fileName = new java.io.File( "." ).getCanonicalPath()+ TESTS_DEVICES_DEFAULT_DIR;
this.jTextField1.setText(fileName + "\n");
File folder = new File(fileName);
File testsFolder = null;
String sTestName;
//search of devices and their tests
File[] listOfFiles = folder.listFiles();
for (File listOfFile : listOfFiles) {
if (listOfFile.isFile()) {
this.jTextField1.append("File " + listOfFile.getName());
} else if (listOfFile.isDirectory()) {
this.jTextField1.append("Directory " + listOfFile.getName());
File configFile = new File(listOfFile.getPath() + FilenameUtils.separatorsToSystem("/.config"));
if(configFile.exists() && !configFile.isDirectory()){
InputStream is = new FileInputStream(configFile);
prop.load(is);
sTestName = new java.io.File( "." ).getCanonicalPath()+ TESTS_TESTS_DEFAULT_DIR + prop.getProperty("tests");
//config of device was loaded. now load the config of each test belonging to the device
testsFolder = new File(sTestName);
if(testsFolder.exists() && testsFolder.isDirectory()){
File[] listOfTests = testsFolder.listFiles();
for (File listOfTest : listOfTests) {
if (listOfTest.isDirectory()) {
this.jTextField1.append("\nTest " + listOfTest.getName());
}
}
}
addToTable(prop.getProperty("name"), prop.getProperty("description"), prop.getProperty("tests"));
this.jTextField1.append(prop.getProperty("name"));
this.jTextField1.append(prop.getProperty("description"));
this.jTextField1.append(prop.getProperty("tests"));
}
}
}
}
public void logMessage(String message){
}
public void selectFile(){
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(NetbeansPluginPanel.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
System.out.println("Opening: " + file.getName() + ".");
} else {
System.out.println("Open command cancelled by user." );
}
}
}
}