private bool LoadTemplateInfo()
        {
            if (givenTemplate != null)
            {
                templateInfo = givenTemplate.info; return(true);
            }                                                                              // nothing to do, if an other caller than the UI already provided a loaded template

            if (givenTemplateName != null)
            {
                templatePath = givenTemplateName;                               // another caller than the UI provided the name of the template to load
            }
            else
            {
                templatePath = (comboBox1.SelectedItem as TemplateItem).Value;  // UI-call
            }
            if (XML_handling.ParseTemplateInfo(templatePath, out templateInfo, out ErrorCollector errorCollector))
            {
                return(true);
            }

            MessageBox.Show(errorCollector.GetErrorMessage());
            if (givenTemplateName != null)
            {
                DialogResult = DialogResult.Cancel;
                Close(); // terminate the whole selection process if an other caller than the UI provided rubbish input
            }
            return(false);
        }
        private static bool MenuCall_GetSelection(string templateName, out List <HHTypes_Files> hhTypes_files)
        {
            hhTypes_files = new List <HHTypes_Files>();
            if (!XML_handling.ParseTemplateInfo(Path.Combine(Program.getHHOTfolder(), templateName), out Template.TemplateInfo templateInfo, out ErrorCollector errorCollector))
            {
                MessageBox.Show(errorCollector.GetErrorMessage());
                return(false);
            }
            try
            {
                // first let user select files
                // and find out which HH-types the selected files contain:
                // i.e. to each distinct HH-type found in any of the files assign a list of files that contain it
                SelectFilesForm selectFilesForm = new SelectFilesForm(templateInfo);
                if (selectFilesForm.ShowDialog() == DialogResult.Cancel)
                {
                    return(false);
                }

                // then let user select HH-types
                if (templateName == TEMPLATE_NAME_BARS_PER_HHTYPE)
                {
                    hhTypes_files = selectFilesForm.hhTypes_files; // breakdown per HH-type does not require user-selection of HH-types
                }
                else
                {
                    SelectHHTypesForm selectHHTypesForm = new SelectHHTypesForm(templateInfo,
                                                                                (from hhtf in selectFilesForm.hhTypes_files select hhtf.hhTypeName).ToList()); // the (rather stupid) dialog gets just the names of the HH-types ...
                    if (selectHHTypesForm.ShowDialog() == DialogResult.Cancel)
                    {
                        return(false);
                    }
                    foreach (int index in selectHHTypesForm.selectedHHTypesIndices) // ... and returns the inidces of the selected HH-types ...
                    {
                        hhTypes_files.Add(selectFilesForm.hhTypes_files[index]);    // ... which are used to copy from the SelectFilesForm-structure to the result of this function
                    }
                    // warn if a selected file is dropped, because it does not contain any of the selected hh-types
                    string notCovered = Environment.NewLine;
                    foreach (string selectedFile in GetPlainFileList(selectFilesForm.hhTypes_files))
                    {
                        if (!GetPlainFileList(hhTypes_files).Contains(selectedFile))
                        {
                            notCovered += Path.GetFileName(selectedFile) + Environment.NewLine;
                        }
                    }
                    if (notCovered.Trim() != string.Empty && MessageBox.Show($"The following file(s) do not contain any of the selected HH-types and are therefore neglected: {notCovered.TrimEnd()}",
                                                                             string.Empty, MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    {
                        return(false);
                    }
                }
                return(true);
            }
            catch (Exception e) { MessageBox.Show(e.Message); return(false); }
        }
        private void SelectDefaultTemplateComboForm_Load(object sender, EventArgs e)
        {
            if (givenTemplateName != null || givenTemplate != null)
            {
                btnOK_Click(null, null);
            }

            // Preload default templates into combobox
            if (Directory.Exists(EnvironmentInfo.GetUserSelectableTemplateFolder()))
            {
                // Try reading only the brand templates
                string[] allTemplates = Directory.GetFiles(EnvironmentInfo.GetUserSelectableTemplateFolder(), DefGeneral.BRAND_NAME + "*.xml");
                // If no brand-specific template is found, load all available templates
                if (allTemplates.Length == 0)
                {
                    allTemplates = Directory.GetFiles(EnvironmentInfo.GetUserSelectableTemplateFolder(), "*.xml");
                }

                // try to retieve last selected template from session info
                TSDictionary pluginSessionInfo    = UISessionInfo.GetSessionUserSettings(StatisticsPresenter.USER_SETTINGS_ID);
                string       lastSelectedTemplate = pluginSessionInfo != null && pluginSessionInfo.ContainsKey(StatisticsPresenter.LAST_SELECTED_TEMPLATE)
                    ? pluginSessionInfo.GetItem <string>(StatisticsPresenter.LAST_SELECTED_TEMPLATE) : null;

                int selectedIndex = 0;
                foreach (string templatePath in allTemplates)
                {
                    if (XML_handling.ParseTemplateInfo(templatePath, out Template.TemplateInfo templateInfo, out ErrorCollector errorCollector))
                    {
                        comboBox1.Items.Add(new TemplateItem(templateInfo.name, templatePath));
                        if (templateInfo.name == lastSelectedTemplate)
                        {
                            selectedIndex = comboBox1.Items.Count - 1;
                        }
                    }
                }
                if (comboBox1.Items.Count > 0)
                {
                    comboBox1.SelectedIndex = selectedIndex;
                }
            }
            else
            {
                MessageBox.Show("The default template folder was not found!");
            }
        }
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            if (!LoadTemplateInfo())
            {
                return;
            }

            Visible = false;

            if (givenFilePackages == null)
            {
                SelectFilePackages();
            }
            else
            {
                filePackages = givenFilePackages; DialogResult = DialogResult.OK;
            }

            if (givenUserInput != null)
            {
                userInput = givenUserInput;
            }
            else if (DialogResult != DialogResult.Cancel && templateInfo.GetUserVariables().Count != 0)
            {
                TakeUserInput takeUserInput = new TakeUserInput(templateInfo);
                takeUserInput.ShowDialog();
                userInput    = takeUserInput.userVariables;
                DialogResult = takeUserInput.DialogResult;
            }

            // if all done, fully parse the selected template
            if (DialogResult == DialogResult.OK)
            {
                if (givenTemplate != null)
                {
                    template = givenTemplate;
                }
                else if (templatePath != "")
                {
                    if (!XML_handling.ParseTemplate(templatePath, out template, out ErrorCollector errorCollector))
                    {
                        DialogResult = DialogResult.Cancel;
                    }
                    if (errorCollector.HasErrors())
                    {
                        MessageBox.Show(errorCollector.GetErrorMessage());
                    }
                }
                else
                {
                    MessageBox.Show("Error! no template or templatepath given!");
                    DialogResult = DialogResult.Cancel;
                }
                if (template != null)
                {
                    template.info = templateInfo;
                }
            }

            if (sender != null && template != null && template.info != null && template.info.name != null)
            {
                // save selected template in session info
                TSDictionary pluginSessionInfo = new TSDictionary();
                pluginSessionInfo.SetItem(StatisticsPresenter.LAST_SELECTED_TEMPLATE, template.info.name);
                UISessionInfo.SetSessionUserSettings(StatisticsPresenter.USER_SETTINGS_ID, pluginSessionInfo);
            }

            Close();
        }