/*
* Copyright (c) 2015 Paul Scherrer Institute. All rights reserved.
*/
import ch.psi.utils.swing.SwingUtils;
import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.table.DefaultTableModel;
import org.apache.commons.io.FilenameUtils;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author boccioli_m
*/
public class TestingListDetails extends javax.swing.JPanel {
/**
* Creates new form TestingListDetails
*/
public TestingListDetails() {
initComponents();
}
public TestingListDetails(HashMap hDetails) {
initComponents();
fillComponents(hDetails);
}
public static String TESTS_CONFIG_FILENAME = ".config";
public static Path TESTS_TESTS_DEFAULT_DIR = Paths.get(".", "home", "script", "tests", "tests");
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
txtDeviceName = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
txtDeviceDescription = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
txtTestSuite = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
txtTestName = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTableParams = new javax.swing.JTable();
jLabel7 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
txtTestResult = new javax.swing.JTextArea();
jLabel8 = new javax.swing.JLabel();
jScrollPane3 = new javax.swing.JScrollPane();
txtTestDescription = new javax.swing.JTextArea();
jScrollPane4 = new javax.swing.JScrollPane();
jEditorPaneHelp = new javax.swing.JEditorPane();
jButtonOk = new javax.swing.JButton();
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel1.setText("Device name");
txtDeviceName.setEditable(false);
txtDeviceName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
txtDeviceName.setText("jTextField1");
jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel2.setText("Device Description");
txtDeviceDescription.setEditable(false);
txtDeviceDescription.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
txtDeviceDescription.setText("jTextField2");
jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel4.setText("Test Case/Suite");
txtTestSuite.setEditable(false);
txtTestSuite.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
txtTestSuite.setText("jTextField2");
jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel5.setText("Test Name");
txtTestName.setEditable(false);
txtTestName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
txtTestName.setText("jTextField2");
jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel6.setText("Last Test Result");
jTableParams.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jTableParams.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Parameter", "Value", "Description"
}
) {
boolean[] canEdit = new boolean [] {
false, true, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTableParams.setDragEnabled(true);
jTableParams.setRowHeight(20);
jScrollPane1.setViewportView(jTableParams);
if (jTableParams.getColumnModel().getColumnCount() > 0) {
jTableParams.getColumnModel().getColumn(0).setMinWidth(150);
jTableParams.getColumnModel().getColumn(0).setPreferredWidth(150);
jTableParams.getColumnModel().getColumn(0).setMaxWidth(150);
jTableParams.getColumnModel().getColumn(1).setPreferredWidth(80);
jTableParams.getColumnModel().getColumn(1).setMaxWidth(100);
}
jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel7.setText("Test Parameters");
jScrollPane2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
txtTestResult.setEditable(false);
txtTestResult.setColumns(20);
txtTestResult.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
txtTestResult.setLineWrap(true);
txtTestResult.setRows(5);
jScrollPane2.setViewportView(txtTestResult);
jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel8.setText("Test Description");
jScrollPane3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
txtTestDescription.setEditable(false);
txtTestDescription.setColumns(20);
txtTestDescription.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
txtTestDescription.setLineWrap(true);
txtTestDescription.setRows(5);
jScrollPane3.setViewportView(txtTestDescription);
jEditorPaneHelp.setContentType("text/html"); // NOI18N
jEditorPaneHelp.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jEditorPaneHelp.setDropMode(javax.swing.DropMode.INSERT);
jScrollPane4.setViewportView(jEditorPaneHelp);
jButtonOk.setText("Save");
jButtonOk.setToolTipText("Save parameters values");
jButtonOk.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonOkActionPerformed(evt);
}
});
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(jLabel4)
.addComponent(jLabel5)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel7)
.addComponent(jLabel6)
.addComponent(jLabel8))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtDeviceName)
.addComponent(txtDeviceDescription)
.addComponent(txtTestSuite)
.addComponent(txtTestName)
.addComponent(jScrollPane3)
.addComponent(jScrollPane2)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE)))
.addComponent(jButtonOk, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)
.addContainerGap())
);
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(jLabel1)
.addComponent(txtDeviceName, 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(jLabel2)
.addComponent(txtDeviceDescription, 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(jLabel4)
.addComponent(txtTestSuite, 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(jLabel5)
.addComponent(txtTestName, 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.LEADING)
.addComponent(jLabel8)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel7)
.addGap(0, 216, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonOk))
.addComponent(jScrollPane4))
.addContainerGap())
);
}// //GEN-END:initComponents
private void fillComponents(HashMap hDetails) {
//add values to each text box
this.txtDeviceName.setText(hDetails.get("deviceName").toString());
this.txtDeviceDescription.setText(hDetails.get("deviceDescription").toString());
this.txtTestDescription.setText(hDetails.get("testDescription").toString());
this.txtTestSuite.setText(hDetails.get("testSuite").toString());
this.txtTestName.setText(hDetails.get("testName").toString());
this.txtTestResult.setText(hDetails.get("time").toString() + "\n" + hDetails.get("testResult").toString());
//help text
showHelp(String.valueOf(hDetails.get("testPath")), String.valueOf(hDetails.get("testHelp")));
//parameters table
HashMap hParams = (HashMap) hDetails.get("parameters");
String name="", value="", description="";
DefaultTableModel model = (DefaultTableModel) jTableParams.getModel();
for(Object entry : hParams.keySet()){
Object param = entry;
if(param instanceof String){
name = (String) param;
HashMap attributes = (HashMap) hParams.get(param);
value = (String) attributes.get("value");
description = (String) attributes.get("description");
model.addRow(new Object[]{name, value, description});
}
jTableParams.setModel(model);
}
}
//load and show help
private void showHelp(String sTestPath, String sHelpText){
this.jEditorPaneHelp.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent hle) {
if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType())) {
//System.out.println(hle.getURL());
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(hle.getURL().toURI());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
this.jEditorPaneHelp.setEditable(false);
//if there is no input, exit
if(sTestPath == "" && sHelpText == ""){
return;
}
File fTest = new File(sTestPath);
String sParentPath = fTest.getParent().toString();
File fHelp = new File(FilenameUtils.separatorsToSystem(sParentPath + "/help.html"));
if(fHelp.isFile()){//if local help file existis:
try {
this.jEditorPaneHelp.setPage(fHelp.toURI().toURL());
} catch (IOException ex) {
this.jEditorPaneHelp.setText(String.valueOf(ex));
}
} else { //help.html file is present. see if a plain text has been given from .config file
if(sHelpText.isEmpty() || sHelpText == "null"){
this.jEditorPaneHelp.setText("[No help available]");
//this.jEditorPaneHelp.setVisible(false);
}else {
this.jEditorPaneHelp.setText(sHelpText);
}
}
}
/**
* Build a map with optional parameters to be passed to the testing script.
* The map is like this: parameters \_ name | \_ value | \_ description \_
* name | \_ value | \_ description ... the name 'name' is the mapping key.
* 'value' and 'description' are constant mapping keys of a nested map.
*
* @param parametersString string containing the parameters. Syntax:
* name:value:description[;name:value:description;...]
*/
private HashMap buildParametersMap(String parametersString) {
HashMap mParameters = new HashMap(); // contains name and attributes
HashMap mParameterAttributes = new HashMap(); //contians value and description
String[] dsParameterAttributes = null;
String[] dsParameters = parametersString.split(";");
for (String sParameter : dsParameters) {
dsParameterAttributes = sParameter.split(":");
if (dsParameterAttributes.length > 2) {
mParameterAttributes = new HashMap();
mParameterAttributes.put("value", (Object) dsParameterAttributes[1]);
mParameterAttributes.put("description", dsParameterAttributes[2]);
//add parameter name and attributes (value + description)
mParameters.put(dsParameterAttributes[0], mParameterAttributes);
}
}
return mParameters;
}
private HashMap getParametersFromTable(){
HashMap mParameters = new HashMap(); // contains name and attributes
HashMap mParameterAttributes = new HashMap(); //contians value and description
for(int row=0 ; row