Exemple #1
0
        public virtual void FromXml(XmlElement e)
        {
            foreach (XmlNode node in e.ChildNodes)
            {
                if (!(node is XmlElement))
                {
                    continue;
                }

                XmlElement element = node as XmlElement;

                if (element.Name != "node" && element.Name != "leaf")
                {
                    Console.WriteLine(element.Name);
                    Debug.Assert(element.Name == "node" || element.Name == "leaf");
                }

                string name = element.GetAttribute("name");

                if (ChildrenNames().Contains(name))
                {
                    IDocLeaf child = ChildByName(name)
                                     ?? CreateChildByName(Type.GetType(element.GetAttribute("type")), name);

                    (child as IXml).FromXml(element);
                }
            }
        }
Exemple #2
0
        static void TestPathBasics()
        {
            IDocLeaf doc = DocNode.Create <TestDocument>();

            string       pathToMyInt = "TestList.2";
            DocObj <int> myInt       = PathUtils.ChildByPath <DocObj <int> >(
                doc as IDocNode, pathToMyInt);

            UnitTest.Test(myInt.Value == 2);
            UnitTest.Test(PathUtils.PathByChild(myInt) == pathToMyInt);

            string        pathToMyBool = "TestBool";
            DocObj <bool> myBool       = PathUtils.ChildByPath <DocObj <bool> >(
                doc as IDocNode, pathToMyBool);

            UnitTest.Test(myBool.Value == true);
            UnitTest.Test(PathUtils.PathByChild(myBool) == pathToMyBool);

            string       pathToMyInt2 = "SubDoc.TestInt0";
            DocObj <int> myInt2       = PathUtils.ChildByPath <DocObj <int> >(
                doc as IDocNode, pathToMyInt2);

            UnitTest.Test(myInt2.Value == 2);
            UnitTest.Test(PathUtils.PathByChild(myInt2) == pathToMyInt2);
        }
Exemple #3
0
            public IEnumerable <IAtomicOperation> CreateActions(IDocLeaf sender)
            {
                DocList <T>             docList = sender as DocList <T>;
                List <IAtomicOperation> actions = new List <IAtomicOperation>();

                if (NewItems != null)
                {
                    for (int i = 0; i < NewItems.Count; i++)
                    {
                        T obj = (T)NewItems[i];

                        actions.Add(new DocListActionAdd(
                                        sender, NewStartingIndex + i, obj));
                    }
                }

                if (OldItems != null)
                {
                    for (int i = OldItems.Count - 1; i >= 0; i--)
                    {
                        T obj = (T)OldItems[i];

                        actions.Add(new DocListActionRemove(
                                        sender, docList.IndexOf(obj), obj));
                    }
                }

                return(actions);
            }
Exemple #4
0
        public static XmlDocument ToXmlDump(this IDocLeaf leaf)
        {
            HashSet <object> findReferences = new HashSet <object>();
            XmlDocument      doc            = new XmlDocument();

            doc.AppendChild(leaf.DumpWholeTree(leaf.Name, findReferences, doc));
            return(doc);
        }
Exemple #5
0
        public static IDocLeaf RootDocument(IDocLeaf doc)
        {
            IDocLeaf root = doc;

            while (root.Parent != null)
            {
                root = root.Parent;
            }
            return(root);
        }
Exemple #6
0
            public DocListActionRemove(IDocLeaf sender, int index, T value)
                : base(sender)
            {
                if (!(sender as IList <T>).Contains(value))
                {
                    throw new Exception("Can not remove item not in list");
                }

                Value = value;
                Index = index;
            }
Exemple #7
0
        public PreferencesItem(Text displayName, Text helpText, IDocLeaf initialItem)
        {
            DisplayName = displayName;
            Help        = helpText;
            Item        = initialItem;

            ResolveChildrenLinks();

            if (Item is DocBase)
            {
                (Item as DocBase).Help = Help;
            }
        }
Exemple #8
0
        public static T ChildByPath <T>(IDocNode root, string path) where T : IDocLeaf
        {
            string[] names = SplitPath(path);

            IDocLeaf cur = root;

            foreach (string name in names)
            {
                cur = (cur as IDocNode).ChildByName(name);
            }

            return((T)cur);
        }
Exemple #9
0
        public void ResolveChildrenLinks()
        {
            foreach (var childName in ChildrenNames())
            {
                IDocLeaf child = ChildByName(childName);

                child.ResolveParentLink(this, childName);

                if (child is IDocNode)
                {
                    (child as IDocNode).ResolveChildrenLinks();
                }
            }
        }
Exemple #10
0
        public static void CollectChildrenRecursive(IDocLeaf root, List <IDocLeaf> collection)
        {
            collection.Add(root);

            if (root is IDocNode)
            {
                IDocNode node = (IDocNode)root;

                IEnumerable <IDocLeaf> children = node.Children();
                foreach (IDocLeaf child in children)
                {
                    CollectChildrenRecursive(child, collection);
                }
            }
        }
Exemple #11
0
        protected IDocLeaf CreateChildByName(Type type, string name)
        {
            IDocLeaf child = Activator.CreateInstance(type) as IDocLeaf;

            if (child is IDocNode)
            {
                (child as IDocNode).ResolveChildrenLinks();
            }

            PropertyUtils.SetByName(this, name, child);

            child.ResolveParentLink(this, name);

            return(child);
        }
Exemple #12
0
        public static void RemoveHookRecursive(IDocLeaf doc, EventHandler <EventArgs> listener)
        {
            if (doc is IDoc)
            {
                (doc as IDoc).Hook -= listener;
            }

            if (doc is DocNode)
            {
                foreach (IDocLeaf leaf in (doc as DocNode).Children())
                {
                    RemoveHookRecursive(leaf, listener);
                }
            }
        }
Exemple #13
0
        public static void CheckParentChildrenLink(IDocLeaf doc, IDocLeaf parent)
        {
            if (!Object.ReferenceEquals(doc.Parent, parent))
            {
                throw new Exception("CheckParentChildrenLink failed");
            }

            if (doc is IDocNode)
            {
                foreach (IDocLeaf children in (doc as IDocNode).Children())
                {
                    CheckParentChildrenLink(children, doc);
                }
            }
        }
Exemple #14
0
        public static string PathByChild(IDocLeaf child)
        {
            List <string> path = new List <string>();

            IDocLeaf obj = child;

            while (obj.Parent != null)
            {
                path.Add(obj.Name);
                obj = obj.Parent;
            }

            path.Reverse();

            return(BuildPath(path.ToArray()));
        }
Exemple #15
0
        private static XmlElement DumpWholeTree(this IDocLeaf leaf, string name, HashSet <object> alreadyDumped, XmlDocument doc)
        {
            bool seemsToBeAReference = alreadyDumped.Contains(leaf);

            XmlElement element = doc.CreateElement("Leaf");

            element.SetAttribute("Class", leaf.GetType().Name);
            element.SetAttribute("Name", name);
            element.SetAttribute("Owner", (!seemsToBeAReference).ToString());

            if (!seemsToBeAReference)
            {
                alreadyDumped.Add(leaf);

                if (leaf is DocBase)
                {
                    string[] names = PropertyUtils.NamesByType(leaf.GetType(), PropertyUtils.IsSupported);

                    foreach (var n in names)
                    {
                        IDocLeaf child = PropertyUtils.ByName(leaf, n) as IDocLeaf;

                        if (child != null)
                        {
                            element.AppendChild(child.DumpWholeTree(n, alreadyDumped, doc));
                        }
                    }
                }
                else if (leaf is IDocNode)
                {
                    foreach (var child in (leaf as IDocNode).Children())
                    {
                        if (child != null)
                        {
                            element.AppendChild(child.DumpWholeTree(child.Name, alreadyDumped, doc));
                        }
                    }
                }
            }

            return(element);
        }
Exemple #16
0
 public DocListActionAdd(IDocLeaf sender, int index, T value)
     : base(sender)
 {
     Value = value;
     Index = index;
 }
Exemple #17
0
 public static bool IsInHistoryTree(this IDocLeaf doc)
 {
     return(History.Instance.Root != null &&
            History.Instance.Root == RootDocument(doc));
 }
Exemple #18
0
 public DocObjAction(IDocLeaf document, T newValue)
     : base(document)
 {
     OldValue = (document as DocObj <T>).ForceValue;
     NewValue = newValue;
 }
Exemple #19
0
 public IEnumerable <IAtomicOperation> CreateActions(IDocLeaf sender)
 {
     yield return(new DocObjAction(sender, NewValue));
 }
Exemple #20
0
 public DocAtomicOperation(IDocLeaf document)
     : base(document.GetHashCode())
 {
     Sender = document;
 }