Ejemplo n.º 1
0
        public HistoryWindow()
        {
            InitializeComponent();
            RootGrid.Children.Add(graphite);
            var n0 = new Node()
            {
                Title = "邵金麟", InitialPosition = new Point(140, 20)
            };
            var n01 = new Node()
            {
                Title = "李伯生", InitialPosition = new Point(120, 40)
            };
            var n02 = new Node()
            {
                Title = "张伯伦", InitialPosition = new Point(160, 40)
            };
            var n011 = new Node()
            {
                Title = "王丙乾", InitialPosition = new Point(100, 60)
            };
            var n012 = new Node()
            {
                Title = "陈浩然", InitialPosition = new Point(180, 60)
            };
            var n0111 = new Node()
            {
                Title = "索罗斯", InitialPosition = new Point(80, 80)
            };
            var n0121 = new Node()
            {
                Title = "金正恩", InitialPosition = new Point(200, 80)
            };
            var n0122 = new Node()
            {
                Title = "胡宗南", InitialPosition = new Point(100, 100)
            };
            var n01221 = new Node()
            {
                Title = "姜波", InitialPosition = new Point(190, 100)
            };

            graphite.AddNode(n0);
            graphite.AddNode(n01);
            graphite.AddNode(n02);
            graphite.AddNode(n011);
            graphite.AddNode(n012);
            graphite.AddNode(n0111);
            graphite.AddNode(n0121);
            graphite.AddNode(n0122);
            graphite.AddNode(n01221);
            graphite.AddEdge(n0, n01);
            graphite.AddEdge(n0, n02);
            graphite.AddEdge(n01, n011);
            graphite.AddEdge(n01, n012);
            graphite.AddEdge(n011, n0111);
            graphite.AddEdge(n012, n0121);
            graphite.AddEdge(n012, n0122);
            graphite.AddEdge(n0122, n01221);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Imports the specified stream.
        /// Note that we are very permissive for mistakes in the XML,
        /// here you can do as you please of course.
        /// </summary>
        /// <param name="stream">The stream.</param>
        private void Import(Stream stream)
        {
            try
            {
                string xmldoc;
                var    reader = new StreamReader(stream);
                xmldoc = reader.ReadToEnd();
                XDocument doc = XDocument.Parse(xmldoc);
                if (doc == null)
                {
                    return;
                }
                Node n;
                #region Load the nodes
                foreach (XElement element in doc.Root.Elements())
                {
                    if (element.Attributes("id").Count() == 0)
                    {
                        continue; //ID is mendatory
                    }

                    if (element.Name == "Person")
                    {
                        n = new Node
                        {
                            Type = NodeType.Person,
                            Info = GetBlob(element),
                            ID   = element.Attribute("id").Value
                        };
                    }
                    else if (element.Name == "Idea")
                    {
                        n = new Node {
                            Type = NodeType.Idea, ID = element.Attribute("id").Value
                        };
                        if (element.Attributes("Title").Count() > 0)
                        {
                            n.Title = element.Attribute("Title").Value;
                        }
                        else
                        {
                            n.Title = "?";
                        }
                        if (element.Attributes("Info").Count() > 0)
                        {
                            n.Info = element.Attribute("Info").Value;
                        }
                        else
                        {
                            n.Info = "Not specified";
                        }
                    }
                    else if (element.Name == "Node")
                    {
                        n = new Node {
                            Type = NodeType.Standard, ID = element.Attribute("id").Value
                        };
                        if (element.Attributes("Title").Count() > 0)
                        {
                            n.Title = element.Attribute("Title").Value;
                        }
                        else
                        {
                            n.Title = "?";
                        }
                    }
                    else
                    {
                        continue;//unknown node type
                    }
                    if (n != null)
                    {
                        nodeDict.Add(n.ID, n);
                        canvas.AddNode(n);
                    }
                }
                #endregion
                #region Load the edges

                foreach (XElement element in doc.Root.Elements())
                {
                    try
                    {
                        if (element.Attributes("Links").Count() > 0)
                        {
                            string[] links = element.Attribute("Links").Value.Split(',');
                            string   id    = element.Attribute("id").Value;
                            foreach (string s in links)
                            {
                                try
                                {
                                    canvas.AddEdge(nodeDict[id], nodeDict[s], rand.Next(15, 784).ToString());
                                }
                                catch (Exception)
                                {
                                    continue;//impossible link or missing node
                                }
                            }
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
                #endregion
            }
            catch (Exception)
            {
                //shouldn't happen
            }
        }