Esempio n. 1
0
        private void _Execute()
        {
            DialogResult result = DialogResult.None;
            string       whatText;
            string       withText;
            bool         matchCase;

            using (var dialog = new SearchAndReplaceDialog())
            {
                result    = dialog.ShowDialog();
                whatText  = dialog.WhatText;
                withText  = dialog.WithText;
                matchCase = dialog.MatchCase;
            }

            if (result == DialogResult.OK)
            {
                using (var manager = new ApplicationManager())
                {
                    var page = manager.CurrentPage();
                    var ns   = page.GetNamespaceOfPrefix("one");

                    var ranges = page.Elements(ns + "Outline")?.Descendants(ns + "T")
                                 .Where(e => !e.DescendantNodes().OfType <XCData>().Any(d => d.Value.Equals(string.Empty)));

                    if (ranges != null)
                    {
                        var count = 0;

                        foreach (var range in ranges)
                        {
                            count += Replace(range, whatText, withText, matchCase);
                        }

                        manager.UpdatePageContent(page);

                        var msg = count == 1 ? "occurance was" : "occurances were";
                        MessageBox.Show($"{count} {msg} replaced", "Replaced",
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }
Esempio n. 2
0
        private void SearchAndReplace()
        {
            DialogResult result = DialogResult.None;
            string       whatText;
            string       withText;
            bool         matchCase;

            using (var dialog = new SearchAndReplaceDialog())
            {
                result = dialog.ShowDialog(owner);

                whatText  = dialog.WhatText;
                withText  = dialog.WithText;
                matchCase = dialog.MatchCase;
            }

            if (result == DialogResult.OK)
            {
                using (var manager = new ApplicationManager())
                {
                    var page = manager.CurrentPage();
                    var ns   = page.GetNamespaceOfPrefix("one");

                    var elements = page.Elements(ns + "Outline").Descendants(ns + "T")
                                   .Select(e => e.Parent)
                                   .Distinct()
                                   .Cast <XElement>();

                    if (elements.Any())
                    {
                        // if there is a selection range...
                        var countRange = elements.Elements(ns + "T").Count(e =>
                                                                           e.Attribute("selected")?.Value == "all" &&
                                                                           e.FirstNode is XCData && (e.FirstNode as XCData).Value.Length > 0);

                        if (countRange > 0)
                        {
                            // ...then further filter out only the selected range
                            elements = elements.Elements(ns + "T")
                                       .Where(t => t.Attribute("selected")?.Value == "all")
                                       .Select(t => t.Parent);
                        }
                    }

                    if (elements.Any())
                    {
                        int count  = 0;
                        var editor = new SearchAndReplaceEditor(ns, whatText, withText, matchCase);

                        // use ToList to avoid null ref exception while updating IEnumerated collection
                        var list = elements.ToList();
                        for (var i = 0; i < list.Count(); i++)
                        {
                            count += editor.SearchAndReplace(list[i]);
                        }

                        manager.UpdatePageContent(page);

                        //var msg = count == 1 ? "occurance was" : "occurances were";
                        //MessageBox.Show($"{count} {msg} replaced", "Replaced",
                        //	MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }