Esempio n. 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();
            }
        }
        private void NextControl()
        {
            string module = $"{_product}.{_class}.{MethodBase.GetCurrentMethod().Name}()";

            NavigatorItem ni = DataContext as NavigatorItem;
            var           currentSelPoint = ActiveDocument.ActiveWindow.Selection;

            try
            {
                var linkedControls = from Word.ContentControl cc in ActiveDocument.ContentControls
                                     orderby cc.Range.Start
                                     where CustomXmlPartHelper.GuidFromTag(cc?.Tag) == CustomXmlPartHelper.GetCmlId(ni.XMLPart)
                                     select cc;

                // Grab current selection point
                int current = currentSelPoint.Range.End;
                foreach (Word.ContentControl cc in linkedControls)
                {
                    if (cc.Range.Start > current)
                    {
                        cc.Range.Select();
                        ActiveDocument.ActiveWindow.ScrollIntoView(cc.Range);
                        return;
                    }
                }

                // Rewind to Start of document
                current = 0;
                foreach (Word.ContentControl cc in linkedControls)
                {
                    if (cc.Range.Start > current)
                    {
                        cc.Range.Select();
                        ActiveDocument.ActiveWindow.ScrollIntoView(cc.Range);
                        return;
                    }
                }
            }
            catch (NullReferenceException ex)
            {
                using (var form = new ReportError(Globals.Chem4WordV3.Telemetry, Globals.Chem4WordV3.WordTopLeft, module, ex))
                {
                    form.ShowDialog();
                }
            }
        }