Github user FSchumacher commented on a diff in the pull request:

    https://github.com/apache/jmeter/pull/432#discussion_r235118443
  
    --- Diff: src/core/org/apache/jmeter/gui/action/SelectTemplatesDialog.java 
---
    @@ -137,67 +160,127 @@ private void checkDirtyAndLoad(final ActionEvent 
actionEvent)
             if (template == null) {
                 return;
             }
    +        
templateList.setValues(TemplateManager.getInstance().reset().getTemplateNames());
 // reload the templates before loading
    +        
             final boolean isTestPlan = template.isTestPlan();
             // Check if the user wants to drop any changes
    -        if (isTestPlan) {
    -            ActionRouter.getInstance().doActionNow(new 
ActionEvent(actionEvent.getSource(), actionEvent.getID(), 
ActionNames.CHECK_DIRTY));
    -            GuiPackage guiPackage = GuiPackage.getInstance();
    -            if (guiPackage.isDirty()) {
    -                // Check if the user wants to create from template
    -                int response = 
JOptionPane.showConfirmDialog(GuiPackage.getInstance().getMainFrame(),
    -                        
JMeterUtils.getResString("cancel_new_from_template"), // $NON-NLS-1$
    -                        JMeterUtils.getResString("template_load?"),  // 
$NON-NLS-1$
    -                        JOptionPane.YES_NO_CANCEL_OPTION,
    -                        JOptionPane.QUESTION_MESSAGE);
    -                if(response == JOptionPane.YES_OPTION) {
    -                    ActionRouter.getInstance().doActionNow(new 
ActionEvent(actionEvent.getSource(), actionEvent.getID(), ActionNames.SAVE));
    -                }
    -                if (response == JOptionPane.CLOSED_OPTION || response == 
JOptionPane.CANCEL_OPTION) {
    -                    return; // Don't clear the plan
    -                }
    -            }
    +        if (isTestPlan && !checkDirty(actionEvent)) {
    +            return;
             }
             ActionRouter.getInstance().doActionNow(new 
ActionEvent(actionEvent.getSource(), actionEvent.getID(), 
ActionNames.STOP_THREAD));
             final File parent = template.getParent();
    -        final File fileToCopy = parent != null 
    +        File fileToCopy = parent != null 
                   ? new File(parent, template.getFileName())
    -              : new File(JMeterUtils.getJMeterHome(), 
template.getFileName());       
    -        Load.loadProjectFile(actionEvent, fileToCopy, !isTestPlan, false);
    -        this.setVisible(false);
    +              : new File(JMeterUtils.getJMeterHome(), 
template.getFileName());
    +        File temporaryGeneratedFile = null;
    +        try {
    +            // handle customized templates (the .jmx.fmkr files)
    +            if (template.getParameters() != null && 
!template.getParameters().isEmpty()) {
    +                File jmxFile = new File(fileToCopy.getAbsolutePath());
    +                Map<String, String> userParameters = getUserParameters();
    +                Configuration templateCfg = 
TemplateUtil.getTemplateConfig();
    +                try {
    +                    temporaryGeneratedFile = 
File.createTempFile(template.getName(), ".output");
    +                    fileToCopy = temporaryGeneratedFile;
    +                    TemplateUtil.processTemplate(jmxFile, 
temporaryGeneratedFile, templateCfg, userParameters);
    +                } catch (IOException | TemplateException ex) {
    +                    log.error("Error generating output file {} from 
template {}", temporaryGeneratedFile, jmxFile, ex);
    +                    return;
    +                }
    +            }
    +            Load.loadProjectFile(actionEvent, fileToCopy, !isTestPlan, 
false);
    +            this.dispose();
    +        } finally {
    +            if(temporaryGeneratedFile != null && 
!temporaryGeneratedFile.delete()) {
    +                log.warn("Could not delete generated output file {} from 
template {}", temporaryGeneratedFile, fileToCopy);
    +            }
    +        }
    +    }
    +
    +    /**
    +     * @param actionEvent
    +     * @return true if we can continue
    +     */
    +    boolean checkDirty(final ActionEvent actionEvent) {
    +        ActionRouter.getInstance().doActionNow(new 
ActionEvent(actionEvent.getSource(), actionEvent.getID(), 
ActionNames.CHECK_DIRTY));
    +        GuiPackage guiPackage = GuiPackage.getInstance();
    +        if (guiPackage.isDirty()) {
    +            // Check if the user wants to create from template
    +            int response = 
JOptionPane.showConfirmDialog(GuiPackage.getInstance().getMainFrame(),
    +                    JMeterUtils.getResString("cancel_new_from_template"), 
// $NON-NLS-1$
    +                    JMeterUtils.getResString("template_load?"),  // 
$NON-NLS-1$
    +                    JOptionPane.YES_NO_CANCEL_OPTION,
    +                    JOptionPane.QUESTION_MESSAGE);
    +            if(response == JOptionPane.YES_OPTION) {
    +                ActionRouter.getInstance().doActionNow(new 
ActionEvent(actionEvent.getSource(), actionEvent.getID(), ActionNames.SAVE));
    +                return true;
    +            }
    +            if (response == JOptionPane.CLOSED_OPTION || response == 
JOptionPane.CANCEL_OPTION) {
    +                return false; // Don't clear the plan
    +            }
    +        }
    +        return true;
    +    }
    +    
    +    private Map<String, String> getUserParameters(){
    +        Map<String, String> userParameters = new LinkedHashMap<>();
    +        for(Entry<String, JLabeledTextField> entry : 
parametersTextFields.entrySet()) {
    --- End diff --
    
    `for` is not a function, so - for me - there is a space missing in between 
`for` and the parentheses.


---

Reply via email to