This commit is contained in:
boccioli_m
2015-09-02 09:01:33 +02:00
parent e2775a6491
commit d5f7c95549
8 changed files with 312 additions and 199 deletions

View File

@@ -2,12 +2,11 @@
* Copyright (c) 2015 Paul Scherrer Institute. All rights reserved.
*/
import ch.psi.pshell.ui.App;
import ch.psi.pshell.ui.View;
import ch.psi.utils.swing.SwingUtils;
import ch.psi.utils.swing.SwingUtils.OptionResult;
import ch.psi.utils.swing.SwingUtils.OptionType;
import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
@@ -27,13 +26,7 @@ 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;
/**
*
@@ -43,32 +36,44 @@ public class NewTest extends javax.swing.JPanel {
/**
* constants declarations
*/
public static Path TESTS_DEVICES_DEFAULT_DIR = Paths.get(".", "home", "script", "tests", "devices");
public static Path TESTS_TESTS_DEFAULT_DIR = Paths.get(".", "home", "script", "tests", "tests");
public static Path TESTS_CONFIG_DEFAULT_DIR = Paths.get(".", "home", "script", "tests", "config");
public static Path TEMPLATES_HELP_FILEPATH = Paths.get(".", "home", "script", "tests","templates","helpTemplate.html");
public static Path TEMPLATES_TESTSCRIPT_FILEPATH = Paths.get(".", "home", "script", "tests","templates","testTemplate.py");
public static Path TEMPLATES_TESTCONFIG_FILEPATH = Paths.get(".", "home", "script", "tests","templates","testTemplate.config");
public static Path TEMPLATES_DEVICECONFIG_FILEPATH = Paths.get(".", "home", "script", "tests","templates","deviceTemplate.config");
public static String TESTS_ROOT_DIR = Paths.get(".", "home", "script", "tests").toString();
public static Path TESTS_DEVICES_DEFAULT_DIR = Paths.get(TESTS_ROOT_DIR, "devices");
public static Path TESTS_TESTS_DEFAULT_DIR = Paths.get(TESTS_ROOT_DIR, "tests");
public static Path TESTS_CONFIG_DEFAULT_DIR = Paths.get(TESTS_ROOT_DIR, "config");
public static Path TEMPLATES_HELP_FILEPATH = Paths.get(TESTS_ROOT_DIR, "templates","helpTemplate.html");
public static Path TEMPLATES_TESTSCRIPT_FILEPATH = Paths.get(TESTS_ROOT_DIR, "templates","testTemplate.py");
public static Path TEMPLATES_TESTCONFIG_FILEPATH = Paths.get(TESTS_ROOT_DIR, "templates","testTemplate.config");
public static Path TEMPLATES_DEVICECONFIG_FILEPATH = Paths.get(TESTS_ROOT_DIR, "templates","deviceTemplate.config");
public static String TESTS_CONFIG_FILENAME = ".config";
public static String TESTS_HELP_FILENAME = "help.html";
public static String ALLOWED_CHARSET1 = "[a-z A-Z 0-9 . _ \\- \\[\\] () : , < > =]*"; //les restricted set of chars
public static String ALLOWED_CHARSET2 = "[a-z A-Z 0-9 . _ \\- \\[\\] ()]*"; //more restricted set of chars
public static String ALLOWED_CHARSET3 = "[a-zA-Z0-9._\\-]*"; //even more restricted set of chars
public static String NEW_TYPE_DEVICE = "Device";
public static String NEW_TYPE_TEST = "Test";
/**
* enumeration of table column indexes
*/
public enum TypeOfNewFile {
Test,
Device;
};
/**
* Creates new form TestingListDetails
*/
private String sType;
private TypeOfNewFile sType;
public NewTest() {
initComponents();
}
public NewTest(String hDetails) {
initComponents();
sType = hDetails;
if(hDetails.equals("Device")){
sType = TypeOfNewFile.Device;
}
else{
sType = TypeOfNewFile.Test;
}
fillComponents(sType);
}
@@ -305,12 +310,13 @@ public class NewTest extends javax.swing.JPanel {
/**
* fill table with example parameters
* @param newType the new can be NEW_TYPE_DEVICE or NEW_TYPE_TEST.
* @param newType of type TypeOfNewFile, can be device or test.
*/
private void fillComponents(String newType) {
private void fillComponents(TypeOfNewFile newType) {
// listener for mouse-click also on table header
jTableParams.getTableHeader().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if(evt.getButton() == java.awt.event.MouseEvent.BUTTON3){
jPopupMenuTable.show(jTableParams, evt.getX(), evt.getY());
@@ -318,8 +324,8 @@ public class NewTest extends javax.swing.JPanel {
}
});
//adapt labels according to type (NEW_TYPE_DEVICE or NEW_TYPE_TEST)
if(newType == NEW_TYPE_DEVICE){
//adapt labels according to type
if(newType == TypeOfNewFile.Device){
this.lblDescr.setText(newType + " "+ this.lblDescr.getText());
this.lblName.setText( newType + " "+ this.lblName.getText());
this.lblName.setToolTipText(newType + " name. Can be a PV name.");
@@ -337,7 +343,7 @@ public class NewTest extends javax.swing.JPanel {
File[] listOfTests = testsFolder.listFiles();
for (File listOfTest : listOfTests) {
if (listOfTest.isDirectory()) {
this.jComboBoxTestSuites.addItem(listOfTest.getName().toString());
this.jComboBoxTestSuites.addItem(listOfTest.getName());
}
}
}
@@ -451,21 +457,31 @@ public class NewTest extends javax.swing.JPanel {
"Please fill-in all compulsory fields (marked with *)");
return false;
}
if(sType.equals(NEW_TYPE_TEST) && this.txtContactName.getText().isEmpty()){
if(sType == TypeOfNewFile.Test && this.txtContactName.getText().isEmpty()){
SwingUtils.showMessage(this, "checkDataFields()",
"Please write a user name in the field Contact person");
return false;
}
String allowedChars = ALLOWED_CHARSET2;
if (sType.equals(NEW_TYPE_DEVICE)){
if (sType == TypeOfNewFile.Device){
allowedChars = ALLOWED_CHARSET1;
}
if(!(txtTestName.getText().matches(allowedChars) &&
txtTestDescription.getText().matches(ALLOWED_CHARSET1) &&
jComboBoxTestSuites.getSelectedItem().toString().matches(allowedChars)
if(!(txtTestName.getText().matches(allowedChars)
)){
SwingUtils.showMessage(this, "checkDataFields()",
"Please remove forbidden chars; allowed are: "+ allowedChars.replace("\\", "").replace("*", ""));
"Please remove forbidden chars from the name; allowed are: "+ allowedChars.replace("\\", "").replace("*", ""));
return false;
}
if(!(txtTestDescription.getText().matches(ALLOWED_CHARSET1)
)){
SwingUtils.showMessage(this, "checkDataFields()",
"Please remove forbidden chars from the description; allowed are: "+ ALLOWED_CHARSET1.replace("\\", "").replace("*", ""));
return false;
}
if(!(jComboBoxTestSuites.getSelectedItem().toString().matches(allowedChars)
)){
SwingUtils.showMessage(this, "checkDataFields()",
"Please remove forbidden chars from the tests suite name; allowed are: "+ allowedChars.replace("\\", "").replace("*", ""));
return false;
}
if(!(txtContactName.getText().matches(ALLOWED_CHARSET3)
@@ -483,14 +499,24 @@ public class NewTest extends javax.swing.JPanel {
SwingUtils.showMessage(this, "checkDataFields()", "You must fill-in all the values in the parameters table");
return false;
}
if(!(sParam.matches(ALLOWED_CHARSET2) &&
sVal.matches(ALLOWED_CHARSET2) &&
sDescr.matches(ALLOWED_CHARSET2))
if(!(sParam.matches(ALLOWED_CHARSET3))
){
SwingUtils.showMessage(this, "checkDataFields()",
"Please remove forbidden chars from parameters table; allowed are: "+ ALLOWED_CHARSET2.replace("\\", "").replace("*", ""));
"Please remove forbidden chars from parameters name in table; allowed are: "+ ALLOWED_CHARSET3.replace("\\", "").replace("*", ""));
return false;
}
}
if(!(sVal.matches(ALLOWED_CHARSET3) )
){
SwingUtils.showMessage(this, "checkDataFields()",
"Please remove forbidden chars from parameters value in table; allowed are: "+ ALLOWED_CHARSET3.replace("\\", "").replace("*", ""));
return false;
}
if(!(sDescr.matches(ALLOWED_CHARSET2))
){
SwingUtils.showMessage(this, "checkDataFields()",
"Please remove forbidden chars from parameters description in table; allowed are: "+ ALLOWED_CHARSET2.replace("\\", "").replace("*", ""));
return false;
}
}
return true;
}
@@ -501,7 +527,7 @@ public class NewTest extends javax.swing.JPanel {
*/
public void generateTestFiles(){
HashMap hmTestParams = (HashMap) getParametersFromTable();
if(sType.equals(NEW_TYPE_DEVICE)){
if(sType == TypeOfNewFile.Device){
generateDeviceFiles(this.txtTestName.getText(),
this.txtTestDescription.getText(),
this.jComboBoxTestSuites.getSelectedItem().toString(),
@@ -520,6 +546,7 @@ public class NewTest extends javax.swing.JPanel {
*
* @param sTestName name of the test
* @param sTestDescription description of the test
* @param sContactName user name of the contact person for this test
* @param sTestSuite test cases/test suite name
* @param hmTestParams test parameters. For the details of the structure, see buildParametersMap
*/
@@ -574,7 +601,17 @@ public class NewTest extends javax.swing.JPanel {
sTestDescription,
sTestSuite,
hmTestParams)) return;
SwingUtils.showMessage(this, "Test generated", "Test "+sTestName+" successfully generated in \n"+testDir.toString());
//SwingUtils.showMessage(this, "Test generated", "Test "+sTestName+" successfully generated in \n"+testDir.toString());
OptionResult res = SwingUtils.showOption(this, "Test generated",
"Test "+sTestName+" successfully generated in \n"+testDir.toString() + "\nDo you want to edit the testing script now?",
OptionType.YesNo);
if (res == OptionResult.Yes){
try {
((View)App.getInstance().getMainFrame()).openScript(makeTestFilePath(testDir, sTestName).toString());
} catch (IOException ex) {
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}else{
SwingUtils.showMessage(this, "generateTestFiles()", "Cannot find tests default directory " + TESTS_TESTS_DEFAULT_DIR.toString());
@@ -625,6 +662,9 @@ public class NewTest extends javax.swing.JPanel {
}
}
public Path makeTestFilePath(Path testDir, String sTestName){
return Paths.get(testDir.toString(), cleanFileName(sTestName+".py"));
}
/**
* generate the python script that runs the test
@@ -642,7 +682,7 @@ public class NewTest extends javax.swing.JPanel {
String sTestSuite,
HashMap hmTestParams){
boolean success = false;
Path path = Paths.get(testDir.toString(), cleanFileName(sTestName+".py"));
Path path = makeTestFilePath(testDir, sTestName);
try {
if(Files.exists(path)){
Files.delete(path);
@@ -720,7 +760,7 @@ public class NewTest extends javax.swing.JPanel {
props.load(in);
in.close();
FileOutputStream out = new FileOutputStream(configFile);
if(sType.equals(NEW_TYPE_DEVICE)){
if(sType == TypeOfNewFile.Device){
//creation of device config
props.setProperty("name", sTestName);
props.setProperty("description", sTestDescription);