Beispiel #1
0
        /// <summary>
        /// Occurs after a new custom XMl part is loaded into the document
        /// Useful for updating the Navigator
        /// </summary>
        /// <param name="NewPart"></param>
        private void OnPartAfterLoad(CustomXMLPart NewPart)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            try
            {
                var converter = new CMLConverter();
                //get the chemistry
                var chemModel = converter.Import(NewPart.XML);
                //find out which content control macthes the custom XML part
                try
                {
                    // ReSharper disable once InconsistentNaming
                    var matchingCC = (from ContentControl cc in _doc.ContentControls
                                      orderby cc.Range.Start
                                      where CustomXmlPartHelper.GuidFromTag(cc?.Tag) == CustomXmlPartHelper.GetCmlId(NewPart)
                                      select cc).First();

                    //get the ordinal position of the content control
                    int start = 0;
                    foreach (ContentControl cc in _doc.ContentControls)
                    {
                        if (cc.ID == matchingCC.ID)
                        {
                            break;
                        }
                        start += 1;
                    }

                    //insert the new navigator item at the ordinal position
                    var newNavItem = new NavigatorItem()
                    {
                        CMLId             = matchingCC?.Tag,
                        ChemicalStructure = NewPart.XML,
                        XMLPart           = NewPart,
                        Name = chemModel.ConciseFormula
                    };
                    try
                    {
                        NavigatorItems.Insert(start, newNavItem);
                    }
                    catch (ArgumentOutOfRangeException) //can happen when there are more content controls than navigator items
                    {
                        //so simply insert the new navigator item at the end
                        NavigatorItems.Add(newNavItem);
                    }
                }
                catch (InvalidOperationException)
                {
                    //sequence contains no elements - thrown on close
                    //just ignore
                }
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex).ShowDialog();
            }
        }
Beispiel #2
0
        /// <summary>
        /// handles deletion of an XML Part...removes the corresponding navigator item
        /// </summary>
        /// <param name="OldPart">The custom XML part that gets deleted</param>
        private void OnPartBeforeDelete(CustomXMLPart OldPart)
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            try
            {
                var oldPart = NavigatorItems.FirstOrDefault(ni => CustomXmlPartHelper.GetCmlId(ni.XMLPart) == CustomXmlPartHelper.GetCmlId(OldPart));
                NavigatorItems.Remove(oldPart);
            }
            catch (Exception ex)
            {
                new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex).ShowDialog();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Loads up the model initially from the document
        /// </summary>
        private void LoadModel()
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            try
            {
                var converter = new CMLConverter();
                if (NavigatorItems.Any())
                {
                    NavigatorItems.Clear();
                }
                if (_doc != null)
                {
                    Dictionary <string, int> added = new Dictionary <string, int>();

                    var navItems = from ContentControl ccs in _doc.ContentControls
                                   join CustomXMLPart part in _parts
                                   on CustomXmlPartHelper.GuidFromTag(ccs?.Tag) equals CustomXmlPartHelper.GetCmlId(part)
                                   orderby ccs.Range.Start
                                   let chemModel = converter.Import(part.XML)
                                                   select new NavigatorItem
                    {
                        CMLId = ccs?.Tag, ChemicalStructure = part.XML, XMLPart = part, Name = chemModel.ConciseFormula
                    };

                    foreach (NavigatorItem navigatorItem in navItems)
                    {
                        string guid = CustomXmlPartHelper.GuidFromTag(navigatorItem.CMLId);
                        if (!string.IsNullOrEmpty(guid))
                        {
                            if (!added.ContainsKey(guid))
                            {
                                NavigatorItems.Add(navigatorItem);
                                added.Add(guid, 1);
                            }
                        }
                    }

                    Debug.WriteLine("Number of items loaded = {0}", NavigatorItems.Count);
                }
            }
            catch (Exception ex)
            {
                using (var form = new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex))
                {
                    form.ShowDialog();
                }
            }
        }