Esempio n. 1
0
        private void ReadSampleGraph()
        {
            string fileName = string.Format("Resources" + Path.DirectorySeparatorChar + "{0}.graphml", graphChooserBox.SelectedItem.ToString());
            DictionaryMapper <IGraph, string> descriptionMapper = new DictionaryMapper <IGraph, string>();

            graphControl.Graph.Clear();
            var ioHandler = new GraphMLIOHandler();

            ioHandler.AddRegistryInputMapper <INode, string>("Description");
            ioHandler.AddRegistryInputMapper <INode, string>("ToolTip");
            ioHandler.AddRegistryInputMapper <INode, string>("Url");
            ioHandler.AddInputMapper <IGraph, string>("GraphDescription", descriptionMapper);
            ioHandler.Read(graphControl.Graph, fileName);
            graphDescriptionTextBlock.Text = descriptionMapper[graphControl.Graph.GetFoldingView().Manager.MasterGraph] ?? string.Empty;
            graphControl.FitGraphBounds(new InsetsD(10));
        }
        /// <summary>
        /// Initializes the graph from the supplied GraphML file and creates the model from it.
        /// </summary>
        /// <remarks>
        /// While this reads the graph from a GraphML file and constructs the model from an already-finished graph, a
        /// real-world application would likely create the model from whichever data source is available and then create
        /// the graph from it.
        /// </remarks>
        private void InitGraphAndModel()
        {
            var graph = new DefaultGraph
            {
                NodeDefaults = { Style            = new NodeControlNodeStyle("NodeStyle")
                                 {
                                     OutlineShape = new Ellipse()
                                 } },
                EdgeDefaults = { Style = new EdgeSegmentControlEdgeStyle("EdgeStyle") }
            };
            var ioh = new GraphMLIOHandler();

            // Parse node kinds and other info
            IMapper <INode, NodeKind> nodeKinds = new DictionaryMapper <INode, NodeKind>();
            IMapper <INode, NodeInfo> nodeInfos = new DictionaryMapper <INode, NodeInfo>();

            ioh.AddInputMapper("NetworkMonitoring.NodeKind", nodeKinds);
            ioh.AddInputMapper("NetworkMonitoring.NodeInfo", nodeInfos);

            ioh.Read(graph, @"Resources\network.graphml");

            foreach (var node in graph.Nodes)
            {
                // Create and attach the model node to the graph node.
                var modelNode = new ModelNode
                {
                    Name    = nodeInfos[node].Name,
                    Ip      = nodeInfos[node].Ip,
                    Enabled = true,
                    Kind    = nodeKinds[node]
                };
                node.Tag = modelNode;

                // Add the label
                var label = graph.AddLabel(node, "", FreeLabelModel.Instance.CreateDefaultParameter(), nodeLabelStyle, tag: modelNode);
                // Attach event handler for changing label visibility, so that the graph redraws accordingly.
                // Since visibility can change via clicking on the node *and* from within the label, we have to use an event
                // handler on the model node here.
                modelNode.PropertyChanged += delegate(object sender, PropertyChangedEventArgs args) {
                    if (args.PropertyName == "LabelVisible")
                    {
                        GraphControl.Invalidate();
                    }
                };
            }

            foreach (var edge in graph.Edges)
            {
                // Create and attach the model edge to the graph edge
                var modelEdge = new ModelEdge
                {
                    Source = (ModelNode)edge.GetSourceNode().Tag,
                    Target = (ModelNode)edge.GetTargetNode().Tag
                };
                edge.Tag = modelEdge;

                // Add the edge label
                var label = graph.AddLabel(edge, "", NinePositionsEdgeLabelModel.CenterCentered, edgeLabelStyle, tag: modelEdge);
            }

            // Create the mappings from model items to graph elements.
            modelNodeToINode = graph.Nodes.ToDictionary(node => (ModelNode)node.Tag);
            modelEdgeToIEdge = graph.Edges.ToDictionary(edge => (ModelEdge)edge.Tag);

            model = new NetworkModel(modelNodeToINode.Keys, modelEdgeToIEdge.Keys);
            GraphControl.Graph = graph;
        }