Example #1
0
        internal static List <ExtensionOrGroup> GetLookGroupDrawInfo(string cc, TreeListNode node)
        {
            if (CountryAdministrator.IsAddOn(cc) || node.Tag == null || node.Tag as BaseTreeListTag == null)
            {
                return(new List <ExtensionOrGroup>());
            }
            BaseTreeListTag nodeTag = node.Tag as BaseTreeListTag;

            List <string> lookGroupIds = (from lgPol in GetCountryConfig(cc).LookGroup_Policy where lgPol.PolicyID == nodeTag.GetDefaultPolicyRow().ID select lgPol.LookGroupID).ToList();

            if (nodeTag.GetDefaultFunctionRow() != null)
            {
                lookGroupIds.AddRange((from lgFun in GetCountryConfig(cc).LookGroup_Function where lgFun.FunctionID == nodeTag.GetDefaultFunctionRow().ID select lgFun.LookGroupID));
            }
            if (nodeTag.GetDefaultParameterRow() != null)
            {
                lookGroupIds.AddRange((from lgPar in GetCountryConfig(cc).LookGroup_Parameter where lgPar.ParameterID == nodeTag.GetDefaultParameterRow().ID select lgPar.LookGroupID));
            }

            List <ExtensionOrGroup> lookGroups = new List <ExtensionOrGroup>();

            foreach (CountryConfig.LookGroupRow lg in GetCountryConfig(cc).LookGroup)
            {
                if (lookGroupIds.Contains(lg.ID))
                {
                    lookGroups.Add(new ExtensionOrGroup(lg));
                }
            }
            return(lookGroups);
        }
        internal static bool RestoreCountry(EM_UI_MainForm mainForm, string backUpFolder = "")
        {
            bool reportSuccess = false;

            if (backUpFolder == string.Empty) //called via button in MainForm
            {
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                folderBrowserDialog.SelectedPath = EMPath.Folder_BackUps(EM_AppContext.FolderEuromodFiles);
                folderBrowserDialog.Description  = "Please select the back-up folder";
                if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                {
                    return(true);
                }
                backUpFolder  = folderBrowserDialog.SelectedPath;
                reportSuccess = true;
            }
            else //called from a catch-branch, i.e. using just generated back-up in Temp/BackUp
            {
                backUpFolder = EMPath.Folder_BackUps(EM_AppContext.FolderEuromodFiles) + backUpFolder;
            }

            string countryShortName = mainForm.GetCountryShortName();
            bool   isAddOn          = CountryAdministrator.IsAddOn(countryShortName);

            try
            {
                //check if backUpFolder contains the necessary files and if the files actually contain a backup of the loaded country/add-on ...
                string errorMessage;
                if (!Country.DoesFolderContainValidXMLFiles(countryShortName, out errorMessage, backUpFolder, isAddOn,
                                                            true)) //check if countryShortName corresponds to country's short name as stored in the XML-file
                {
                    throw new System.ArgumentException(errorMessage);
                }

                //... if yes, copy files form backup-folder to Countries-folder
                if (!Country.CopyXMLFiles(countryShortName, out errorMessage, backUpFolder))
                {
                    throw new System.ArgumentException(errorMessage);
                }

                mainForm.ReloadCountry();

                if (reportSuccess) //report success only if called via button (and not if called by a failed function)
                {
                    UserInfoHandler.ShowSuccess("Successfully restored using back-up stored in" + Environment.NewLine + backUpFolder + ".");
                }

                return(true);
            }
            catch (Exception exception)
            {
                UserInfoHandler.ShowError("Restore failed because of the error stated below." + Environment.NewLine +
                                          "You may want to try a manual restore via the button in the ribbon 'Country Tools'." + Environment.NewLine + Environment.NewLine +
                                          exception.Message);
                return(false);
            }
        }
Example #3
0
        internal static List <ExtensionOrGroup> GetExtensionDrawInfo(string cc, TreeListNode node)
        {
            if (CountryAdministrator.IsAddOn(cc) || node.Tag == null || node.Tag as BaseTreeListTag == null)
            {
                return(new List <ExtensionOrGroup>());
            }
            BaseTreeListTag nodeTag = node.Tag as BaseTreeListTag;

            Dictionary <string, LookDef.STYLE> extIdsAndStyles = new Dictionary <string, LookDef.STYLE>();

            foreach (CountryConfig.Extension_PolicyRow extPolRow in from ePol in GetCountryConfig(cc).Extension_Policy where ePol.PolicyID == nodeTag.GetDefaultPolicyRow().ID select ePol)
            {
                extIdsAndStyles.Add(extPolRow.ExtensionID, extPolRow.BaseOff ? LookDef.STYLE.EXTENSION_OFF : LookDef.STYLE.EXTENSION_ON);
            }
            if (nodeTag.GetDefaultFunctionRow() != null)
            {
                foreach (CountryConfig.Extension_FunctionRow extFunRow in from eFun in GetCountryConfig(cc).Extension_Function where eFun.FunctionID == nodeTag.GetDefaultFunctionRow().ID select eFun)
                { // function-setting with regard to BaseOff overwrites policy-setting (in fact only BaseOff=true may overwrite BaseOff=false, but this is not the point to check for observing this rule)
                    if (extIdsAndStyles.ContainsKey(extFunRow.ExtensionID))
                    {
                        extIdsAndStyles[extFunRow.ExtensionID] = extFunRow.BaseOff ? LookDef.STYLE.EXTENSION_OFF : LookDef.STYLE.EXTENSION_ON;
                    }
                    else
                    {
                        extIdsAndStyles.Add(extFunRow.ExtensionID, extFunRow.BaseOff ? LookDef.STYLE.EXTENSION_OFF : LookDef.STYLE.EXTENSION_ON);
                    }
                }
            }
            if (nodeTag.GetDefaultParameterRow() != null)
            {
                foreach (CountryConfig.Extension_ParameterRow extParRow in from ePar in GetCountryConfig(cc).Extension_Parameter where ePar.ParameterID == nodeTag.GetDefaultParameterRow().ID select ePar)
                { // see overwriting rule above
                    if (extIdsAndStyles.ContainsKey(extParRow.ExtensionID))
                    {
                        extIdsAndStyles[extParRow.ExtensionID] = extParRow.BaseOff ? LookDef.STYLE.EXTENSION_OFF : LookDef.STYLE.EXTENSION_ON;
                    }
                    else
                    {
                        extIdsAndStyles.Add(extParRow.ExtensionID, extParRow.BaseOff ? LookDef.STYLE.EXTENSION_OFF : LookDef.STYLE.EXTENSION_ON);
                    }
                }
            }

            List <ExtensionOrGroup> extensions = new List <ExtensionOrGroup>();

            foreach (GlobLocExtensionRow e in GetExtensions(cc))
            {
                if (extIdsAndStyles.ContainsKey(e.ID))
                {
                    extensions.Add(new ExtensionOrGroup(e, extIdsAndStyles[e.ID]));
                }
            }
            return(extensions);
        }
        internal static void SaveDirectXMLAction(EM_UI_MainForm mainForm)
        {
            string countryShortName = mainForm.GetCountryShortName();

            CountryAdministrator.GetCountryConfigFacade(countryShortName).GetCountryConfig().AcceptChanges();
            if (!CountryAdministrator.IsAddOn(countryShortName))
            {
                CountryAdministrator.GetDataConfigFacade(countryShortName).GetDataConfig().AcceptChanges();
            }
            mainForm.GetUndoManager().Reset();       //reset undo-manager with the drawback that actions up until now cannot be undone
            lock (EM_UI_MainForm._performActionLock) //to not get into conflict with auto-saving
            {
                CountryAdministrator.WriteXML(countryShortName);
            }
        }
        private static Dictionary <string, Image> GetRelevantGroupMenuItems(string cc, string menuItemName)
        {
            Dictionary <string, Image> relevantGroups = new Dictionary <string, Image>();

            // (1) add to group: only show groups which still can be added, i.e. where not already all selected elements belong to
            if (menuItemName == MENU_GROUP_ADD)
            {
                foreach (CountryConfig.LookGroupRow groupRow in GetCountryConfig(cc).LookGroup)
                {
                    bool canBeAdded = false;
                    foreach (TreeListNode node in GetSelectedNodes(cc))
                    {
                        if (CountryAdministrator.IsAddOn(cc) || node.Tag == null || node.Tag as BaseTreeListTag == null)
                        {
                            continue;
                        }
                        BaseTreeListTag nodeTag = node.Tag as BaseTreeListTag;
                        if (nodeTag.GetDefaultParameterRow() != null) // means: if (isParameterRow)
                        {
                            if ((from lgPar in GetCountryConfig(cc).LookGroup_Parameter
                                 where lgPar.LookGroupID == groupRow.ID && lgPar.ParameterID == nodeTag.GetDefaultParameterRow().ID
                                 select lgPar).Any())
                            {
                                continue;                       // node belongs to group
                            }
                        }
                        if (nodeTag.GetDefaultFunctionRow() != null) // means: if (isFunctionRow || isParameterRow)
                        {
                            if ((from lgFun in GetCountryConfig(cc).LookGroup_Function
                                 where lgFun.LookGroupID == groupRow.ID && lgFun.FunctionID == nodeTag.GetDefaultFunctionRow().ID
                                 select lgFun).Any())
                            {
                                continue;                       // node belongs to group
                            }
                        }
                        // means: if (isPolicyRow || isFunctionRow || isParameterRow)
                        if ((from lgPol in GetCountryConfig(cc).LookGroup_Policy
                             where lgPol.LookGroupID == groupRow.ID && lgPol.PolicyID == nodeTag.GetDefaultPolicyRow().ID
                             select lgPol).Any())
                        {
                            continue;             // node belongs to group
                        }
                        canBeAdded = true; break; // node, nor parent nodes, do belong to group: can be added
                    }
                    if (!canBeAdded)
                    {
                        continue;
                    }
                    ExtensionOrGroup eg = new ExtensionOrGroup(groupRow);
                    relevantGroups.Add(eg.name, eg.look.GetMenuImage());
                }
            }
            // (2) remove from group: only show groups which can be removed, i.e. where any of the selected elements belongs to
            else if (menuItemName == MENU_GROUP_REMOVE)
            {
                List <string> lookGroupIds = new List <string>();
                foreach (TreeListNode node in GetSelectedNodes(cc))
                {
                    if (CountryAdministrator.IsAddOn(cc) || node.Tag == null || node.Tag as BaseTreeListTag == null)
                    {
                        continue;
                    }
                    BaseTreeListTag nodeTag = node.Tag as BaseTreeListTag;
                    // "belongs to" means, that the function/parameter itself has to belong to the group, not only the parent
                    if (nodeTag.GetDefaultParameterRow() != null) // means: if (isParameterRow)
                    {
                        lookGroupIds.AddRange(from lgPar in GetCountryConfig(cc).LookGroup_Parameter where lgPar.ParameterID == nodeTag.GetDefaultParameterRow().ID select lgPar.LookGroupID);
                    }
                    else if (nodeTag.GetDefaultFunctionRow() != null) // means: if (isFunctionRow)
                    {
                        lookGroupIds.AddRange(from lgFun in GetCountryConfig(cc).LookGroup_Function where lgFun.FunctionID == nodeTag.GetDefaultFunctionRow().ID select lgFun.LookGroupID);
                    }
                    else // means: if (isPolicyRow)
                    {
                        lookGroupIds.AddRange(from lgPol in GetCountryConfig(cc).LookGroup_Policy where lgPol.PolicyID == nodeTag.GetDefaultPolicyRow().ID select lgPol.LookGroupID);
                    }
                }
                foreach (CountryConfig.LookGroupRow groupRow in from lg in GetCountryConfig(cc).LookGroup where lookGroupIds.Contains(lg.ID) select lg)
                {
                    ExtensionOrGroup eg = new ExtensionOrGroup(groupRow);
                    relevantGroups.Add(eg.name, eg.look.GetMenuImage());
                }
            }
            // (3) all other menu items (Expand,etc.): show all groups
            else
            {
                foreach (CountryConfig.LookGroupRow groupRow in GetCountryConfig(cc).LookGroup)
                {
                    ExtensionOrGroup eg = new ExtensionOrGroup(groupRow);
                    relevantGroups.Add(eg.name, eg.look.GetMenuImage());
                }
            }
            return(relevantGroups.Any() ? relevantGroups : new Dictionary <string, Image>()
            {
                { NO_ITEMS_DEFINED, null }
            });
        }
        private static Dictionary <string, Image> GetRelevantExtensionMenuItems(string cc, string menuItemName)
        {
            Dictionary <string, Image> relevantExtensions = new Dictionary <string, Image>();
            bool globalOnly = menuItemName == MENU_EXTENSION_PRIVATE_ALL || menuItemName == MENU_EXTENSION_NOT_PRIVATE_ALL;

            // (1) add to extension: only show extensions which still can be added, i.e. where not already all selected elements belong to
            if (menuItemName == MENU_EXTENSION_ADDON || menuItemName == MENU_EXTENSION_ADDOFF)
            {
                foreach (GlobLocExtensionRow extRow in globalOnly ? ExtensionAndGroupManager.GetGlobalExtensions() : ExtensionAndGroupManager.GetExtensions(cc))
                {
                    bool canBeAdded = false;
                    foreach (TreeListNode node in GetSelectedNodes(cc))
                    {
                        if (CountryAdministrator.IsAddOn(cc) || node.Tag == null || node.Tag as BaseTreeListTag == null)
                        {
                            continue;
                        }
                        BaseTreeListTag nodeTag = node.Tag as BaseTreeListTag;
                        if (nodeTag.GetDefaultParameterRow() != null) // means: if (isParameterRow)
                        {
                            if ((from ePar in GetCountryConfig(cc).Extension_Parameter
                                 where ePar.ExtensionID == extRow.ID && ePar.ParameterID == nodeTag.GetDefaultParameterRow().ID
                                 select ePar).Any())
                            {
                                continue;                      // node belongs to extension
                            }
                        }
                        if (nodeTag.GetDefaultFunctionRow() != null) // means: if (isFunctionRow || isParameterRow)
                        {
                            var ef = from eFun in GetCountryConfig(cc).Extension_Function
                                     where eFun.ExtensionID == extRow.ID && eFun.FunctionID == nodeTag.GetDefaultFunctionRow().ID
                                     select eFun;
                            if (ef.Any())
                            {
                                if (nodeTag.GetDefaultParameterRow() == null)
                                {
                                    continue;                                           // is actually function-node and belongs to extension
                                }
                                // a parameter-node can still be added as switch-off, if function-node is added as switch-on
                                if (ef.First().BaseOff || menuItemName == MENU_EXTENSION_ADDON)
                                {
                                    continue;
                                }
                            }
                        }
                        // means: if (isPolicyRow || isFunctionRow || isParameterRow)
                        var ep = from ePol in GetCountryConfig(cc).Extension_Policy
                                 where ePol.ExtensionID == extRow.ID && ePol.PolicyID == nodeTag.GetDefaultPolicyRow().ID
                                 select ePol;
                        if (ep.Any())
                        {
                            if (nodeTag.GetDefaultParameterRow() == null && nodeTag.GetDefaultFunctionRow() == null)
                            {
                                continue;                                                                                      // is actually policy-node and belongs to extension
                            }
                            // a parameter- or function-node can still be added as switch-off, if policy-node is added as switch-on
                            if (ep.First().BaseOff || menuItemName == MENU_EXTENSION_ADDON)
                            {
                                continue;
                            }
                        }
                        canBeAdded = true; break; // node, nor parent nodes, do belong to extension: can be added
                    }
                    if (!canBeAdded)
                    {
                        continue;
                    }
                    ExtensionOrGroup eg = new ExtensionOrGroup(extRow);
                    relevantExtensions.Add(eg.name, eg.look.GetMenuImage());
                }
            }
            // (2) remove from extension: only show extensions which can be removed, i.e. where any of the selected elements belongs to
            else if (menuItemName == MENU_EXTENSION_REMOVE)
            {
                List <string> extIds = new List <string>();
                foreach (TreeListNode node in GetSelectedNodes(cc))
                {
                    if (CountryAdministrator.IsAddOn(cc) || node.Tag == null || node.Tag as BaseTreeListTag == null)
                    {
                        continue;
                    }
                    BaseTreeListTag nodeTag = node.Tag as BaseTreeListTag;
                    // "belongs to" means, that the function/parameter itself has to belong to the extension, not only the parent
                    if (nodeTag.GetDefaultParameterRow() != null) // means: if (isParameterRow)
                    {
                        extIds.AddRange(from ePar in GetCountryConfig(cc).Extension_Parameter where ePar.ParameterID == nodeTag.GetDefaultParameterRow().ID select ePar.ExtensionID);
                    }
                    else if (nodeTag.GetDefaultFunctionRow() != null) // means: if (isFunctionRow)
                    {
                        extIds.AddRange(from eFun in GetCountryConfig(cc).Extension_Function where eFun.FunctionID == nodeTag.GetDefaultFunctionRow().ID select eFun.ExtensionID);
                    }
                    else // means: if (isPolicyRow)
                    {
                        extIds.AddRange(from ePol in GetCountryConfig(cc).Extension_Policy where ePol.PolicyID == nodeTag.GetDefaultPolicyRow().ID select ePol.ExtensionID);
                    }
                }
                foreach (GlobLocExtensionRow gleRow in from e in globalOnly ? ExtensionAndGroupManager.GetGlobalExtensions() : ExtensionAndGroupManager.GetExtensions(cc)
                         where extIds.Contains(e.ID) select e)
                {
                    ExtensionOrGroup eg = new ExtensionOrGroup(gleRow);
                    relevantExtensions.Add(eg.name, eg.look.GetMenuImage());
                }
            }
            // (3) all other menu items (Expand,etc.): show all extensions
            else
            {
                foreach (GlobLocExtensionRow extRow in globalOnly ? ExtensionAndGroupManager.GetGlobalExtensions() : ExtensionAndGroupManager.GetExtensions(cc))
                {
                    ExtensionOrGroup eg = new ExtensionOrGroup(extRow);
                    relevantExtensions.Add(eg.name, eg.look.GetMenuImage());
                }
            }
            return(relevantExtensions.Any() ? relevantExtensions : new Dictionary <string, Image>()
            {
                { NO_ITEMS_DEFINED, null }
            });
        }