/// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public PressureLossReportFormats getAllFormats(bool bCheckDomain = true)
        {
            XmlSerializer             serializer = new XmlSerializer(typeof(PressureLossReportFormats));
            PressureLossReportFormats formats    = new PressureLossReportFormats();

            if (!File.Exists(formatFileName))
            {
                return(null);
            }

            using (TextReader reader = new StreamReader(formatFileName))
            {
                try
                {
                    PressureLossReportHelper  helper     = PressureLossReportHelper.instance;
                    PressureLossReportFormats allformats = serializer.Deserialize(reader) as PressureLossReportFormats;
                    if (allformats != null)
                    {
                        foreach (PressureLossReportData data in allformats)
                        {
                            if ((bCheckDomain && data.Domain == helper.Domain) || !bCheckDomain)
                            {
                                formats.Add(data);
                            }
                        }
                    }

                    return(formats);
                }
                catch (System.InvalidOperationException)
                {
                    return(formats);
                }
            }
        }
 /// <summary>
 /// save a data list--it means a PressureLossReportFormats obj
 /// </summary>
 /// <param name="path"></param>
 /// <param name="formats"></param>
 private void save(PressureLossReportFormats formats)
 {
     foreach (PressureLossReportData data in formats)
     {
         save(data);
     }
 }
        /// <summary>
        /// save the data to the xml file, please catch InvalidOperationException to
        /// identify the existing file is broken.
        /// </summary>
        /// <param name="data">format data</param>
        public void save(PressureLossReportData data)
        {
            try
            {
                XmlSerializer             serializer = new XmlSerializer(typeof(PressureLossReportFormats));
                PressureLossReportFormats formats    = new PressureLossReportFormats();
                if (File.Exists(formatFileName))
                {
                    formats = getAllFormats(false);
                    //formats can't been null, it may throw exception.
                    //this format exists
                    if (formats != null && formats.Contains(data))
                    {
                        formats.Remove(data);
                    }
                }

                formats.Add(data);
                using (TextWriter writer = new StreamWriter(formatFileName))
                {
                    serializer.Serialize(writer, formats);
                }
            }
            catch
            {
                //do nothing
            }
        }
        /// <summary>
        /// deserialize the format xml file to get the PressureLossReportData object.
        /// </summary>
        /// <param name="formatName">format name</param>
        /// <returns></returns>
        public PressureLossReportData getData(string formatName)
        {
            PressureLossReportFormats formats = getAllFormats();

            if (formats != null)
            {
                foreach (PressureLossReportData data in formats)
                {
                    if (0 == string.Compare(data.Name, formatName))
                    {
                        return(data);
                    }
                }
            }
            return(null);
        }
        public PressureLossReportData getLastUsedReportData()
        {
            PressureLossReportFormats formats = getAllFormats();

            if (formats != null)
            {
                foreach (PressureLossReportData data in formats)
                {
                    if (data.Name.Contains(lastUsed))
                    {
                        return(data);
                    }
                }
            }

            return(null);
        }
        private void buttonFormatDelete_Click(object sender, EventArgs e)
        {
            PressureLossReportDataManager reportDataMgr = PressureLossReportDataManager.Instance;
            PressureLossReportFormats     allFormats    = reportDataMgr.getAllFormats();

            if (allFormats == null || allFormats.Count < 1 || this.comboBoxFormat.Items.Count < 1)
            {
                return;
            }

            if (allFormats.Count == 1 || (allFormats.Count == 2 && reportDataMgr.getLastUsedReportData() != null))
            {
                UIHelperFunctions.postWarning(ReportResource.deleteFormatTitle, ReportResource.deleteLastFormatMsg, ReportResource.deleteLastFormatSubMsg);
                return;
            }

            TaskDialog tdlg = new TaskDialog(ReportResource.deleteFormatTitle);

            tdlg.MainInstruction   = ReportResource.deleteFormatMsg;
            tdlg.AllowCancellation = true;
            tdlg.CommonButtons     = TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No;
            tdlg.DefaultButton     = TaskDialogResult.No;
            tdlg.TitleAutoPrefix   = false; // suppress the prefix of title.
            if (tdlg.Show() == TaskDialogResult.Yes)
            {
                // delete the report format, and update the combo list, set the next one as current
                string name = this.comboBoxFormat.SelectedItem.ToString();
                if (name == reportDataMgr.getLastUsedReportName())
                {
                    PressureLossReportData lastData = reportDataMgr.getLastUsedReportData();
                    if (lastData != null)
                    {
                        reportDataMgr.remove(lastData.Name);
                    }
                }

                reportDataMgr.remove(name);

                this.comboBoxFormat.Items.Remove(name);

                if (this.comboBoxFormat.Items.Count > 0)
                {
                    this.comboBoxFormat.SelectedIndex = 0;
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <param name="formatName"></param>
        public void remove(string formatName)
        {
            PressureLossReportFormats formats = getAllFormats(false);

            if (formats != null)
            {
                PressureLossReportHelper helper = PressureLossReportHelper.instance;
                foreach (PressureLossReportData data in formats)
                {
                    if (0 == string.Compare(data.Name, formatName) && helper.Domain == data.Domain)
                    {
                        formats.Remove(data);
                        break;
                    }
                }
                clear();
                save(formats);
            }
        }
 public void executeUpgrades()
 {
     try
     {
         //get format manager
         PressureLossReportDataManager reportDataMgr = PressureLossReportDataManager.Instance;
         PressureLossReportFormats     formats       = reportDataMgr.getAllFormats();
         if (formats != null && formats.Count > 0)
         {
             foreach (PressureLossReportData data in formats)
             {
                 //get format version
                 upgrades(data);
             }
         }
     }
     catch
     {
     }
 }
 /// <summary>
 /// save a data list--it means a PressureLossReportFormats obj
 /// </summary>
 /// <param name="path"></param>
 /// <param name="formats"></param>
 private void save(PressureLossReportFormats formats)
 {
     foreach (PressureLossReportData data in formats)
      {
     save(data);
      }
 }
        /// <summary>
        /// save the data to the xml file, please catch InvalidOperationException to 
        /// identify the existing file is broken.
        /// </summary>
        /// <param name="data">format data</param>
        public void save(PressureLossReportData data)
        {
            try
             {
            XmlSerializer serializer = new XmlSerializer(typeof(PressureLossReportFormats));
            PressureLossReportFormats formats = new PressureLossReportFormats();
            if (File.Exists(formatFileName))
            {
               formats = getAllFormats(false);
               //formats can't been null, it may throw exception.
               //this format exists
               if (formats != null && formats.Contains(data))
               {
                  formats.Remove(data);
               }
            }

            formats.Add(data);
            using (TextWriter writer = new StreamWriter(formatFileName))
            {
               serializer.Serialize(writer, formats);
            }
             }
             catch
             {
            //do nothing
             }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public PressureLossReportFormats getAllFormats(bool bCheckDomain = true)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(PressureLossReportFormats));
             PressureLossReportFormats formats = new PressureLossReportFormats();
             if (!File.Exists(formatFileName))
            return null;

             using (TextReader reader = new StreamReader(formatFileName))
             {
            try
            {
               PressureLossReportHelper helper = PressureLossReportHelper.instance;
               PressureLossReportFormats allformats = serializer.Deserialize(reader) as PressureLossReportFormats;
               if (allformats != null)
               {
                  foreach (PressureLossReportData data in allformats)
                  {
                     if ((bCheckDomain && data.Domain == helper.Domain) || !bCheckDomain)
                        formats.Add(data);

                  }
               }

               return formats;
            }
            catch (System.InvalidOperationException)
            {
               return formats;
            }
             }
        }
        private void FillingData()
        {
            try
            {
                //Way 1:
                //1. Fill in the combo: report format names or "untitled format"
                //?: Which i=one is the default selected?
                PressureLossReportHelper      helper        = PressureLossReportHelper.instance;
                PressureLossReportDataManager reportDataMgr = PressureLossReportDataManager.Instance;
                PressureLossReportFormats     formats       = reportDataMgr.getAllFormats();
                if (formats != null && formats.Count > 0)
                {
                    PressureLossReportData lastData = reportDataMgr.getLastUsedReportData();
                    string lastUsedName             = "";
                    foreach (PressureLossReportData data in formats)
                    {
                        if (lastData != null && lastData.Name == data.Name)
                        {
                            lastUsedName = reportDataMgr.getLastUsedReportName();
                            if (formats.Count == 1)
                            {
                                this.comboBoxFormat.Items.Add(lastUsedName);
                            }
                        }
                        else
                        {
                            this.comboBoxFormat.Items.Add(data.Name);
                        }
                    }

                    if (lastUsedName.Length > 0 && this.comboBoxFormat.Items.IndexOf(lastUsedName) > -1)
                    {
                        this.comboBoxFormat.SelectedIndex = this.comboBoxFormat.Items.IndexOf(lastUsedName);
                    }
                    else
                    {
                        this.comboBoxFormat.SelectedIndex = 0;
                    }
                    settings = reportDataMgr.getData(this.comboBoxFormat.SelectedItem.ToString());

                    resetSettings();
                }


                if (settings == null) //fill in default values
                {
                    settings = new PressureLossReportData();

                    generateDefaultFormatData();
                    settings.Name = ReportResource.formatDefaultName;
                    reportDataMgr.save(settings);
                    this.comboBoxFormat.Items.Add(settings.Name);
                    this.comboBoxFormat.SelectedIndex = 0;
                }

                if (settings != null)
                {
                    fillSettingsControlsFromFormat(settings);
                }

                this.buttonUp.Enabled   = false;
                this.buttonDown.Enabled = false;

                this.listBoxAvailableFields.Focus();
                this.listBoxAvailableFields.SetSelected(0, true);

                //title
                if (helper.Domain == ReportResource.pipeDomain)
                {
                    this.Text = ReportResource.pipeSettingsDlgTitle;
                }
                else
                {
                    this.Text = ReportResource.ductSettingsDlgTitle;
                }
            }
            catch
            {
                //do nothing
            }
        }