/// <summary> /// Initialize loading from and saving to graphml-files. /// </summary> private void InitializeGraphML() { // initialize (de-)serialization for load/save commands var graphmlHandler = new GraphMLIOHandler(); graphmlHandler.Parsed += (sender, e) => { // after loading apply wrap node styles, node label models and port location models in rotatable decorators var graph = graphControl.Graph; foreach (var node in graph.Nodes.Where(node => !graph.IsGroupNode(node))) { if (!(node.Style is RotatableNodeStyleDecorator)) { graph.SetStyle(node, new RotatableNodeStyleDecorator(node.Style)); } foreach (var label in node.Labels.Where(label => !(label.LayoutParameter.Model is RotatableNodeLabelModelDecorator))) { graph.SetLabelLayoutParameter(label, new RotatableNodeLabelModelDecorator(label.LayoutParameter.Model).CreateWrappingParameter(label.LayoutParameter)); } foreach (var port in node.Ports.Where(port => !(port.LocationParameter.Model is RotatablePortLocationModelDecorator))) { graph.SetPortLocationParameter(port, RotatablePortLocationModelDecorator.Instance.CreateWrappingParameter(port.LocationParameter)); } } }; graphControl.GraphMLIOHandler = graphmlHandler; }
/// <summary> /// Register input and output handlers that store the data in the mapper as GraphMLAttributes resp. can read them back. /// </summary> private void EnableDataPersistence() { // We get the IO handler that is used by the GraphControl for serialization and deserialization. var ioh = graphControl.GraphMLIOHandler; IMapperRegistry registry = manager.MasterGraph.MapperRegistry; IMapper <INode, DateTime> dateMapper = registry.GetMapper <INode, DateTime>(DateTimeMapperKey); if (dateMapper != null) { // The OutputHandler just stores the string value of the attribute // We need to provide the symbolic name of the attribute in the GraphML file, the data source as an IMapper and the // GraphML type of the attribute ioh.AddOutputMapper(DateTimeMapperKey, null, dateMapper, delegate(object args, HandleSerializationEventArgs e) { if (e.Item is DateTime) { e.Writer.WriteString(((DateTime)e.Item).ToString(CultureInfo.InvariantCulture)); } e.Handled = true; }, KeyType.String); // To read back a DateTime value from a string GraphML attribute, we have to provide an additional (very simple...) callback method. ioh.AddInputMapper((elem) => GraphMLIOHandler.MatchesName(elem, DateTimeMapperKey) && GraphMLIOHandler.MatchesType(elem, KeyType.String), dateMapper, delegate(object args, HandleDeserializationEventArgs e) { //The actual value is a text node that can be retrieved from the event try { DateTime dateTime = DateTime.Parse(e.XmlNode.ToString(), CultureInfo.InvariantCulture); e.Result = dateTime; } catch (Exception exception) { Console.WriteLine(exception); e.Result = DateTime.Now; } }); } }
/// <summary> /// Reads the graph from a file with given file name. /// </summary> virtual protected void ReadGraph(string filename) { GraphMLIOHandler ioh = new GraphMLIOHandler(); ioh.Read(GraphControl.Graph, new StreamReader(filename)); GraphControl.UpdateContentRect(); GraphControl.FitContent(); }
/// <summary> /// Asynchronously loads a compressed graphmlz file. /// </summary> private static async Task <IGraph> LoadGraphMlz(string filepath) { return(await Task.Run(() => { var graph = new DefaultGraph(); var ioh = new GraphMLIOHandler(); using (var stream = new GZipStream(File.OpenRead(filepath), CompressionMode.Decompress)) { ioh.Read(graph, stream); } return graph; })); }
/// <summary> /// Populates the palette with the graphs stored in the resources folder. /// </summary> private void PopulatePalette() { var ioHandler = new GraphMLIOHandler(); foreach (var file in Directory.GetFiles("Resources")) { var graph = new DefaultGraph(); ioHandler.Read(graph, file); PaletteListBox.Items.Add(graph); } }
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> /// Called when the application has been loaded /// </summary> private void OnLoad(object sender, EventArgs e) { // show a notification because the multi-page layout takes some time ShowLoadingIndicator(true); InitializeCoreLayouts(); InitializeInputMode(); // load the original graph modelGraph = new DefaultGraph(); GraphMLIOHandler ioHandler = new GraphMLIOHandler(); ioHandler.Read(modelGraph, "Resources/pop-artists-small.graphml"); // add the page bounds visual pageBoundsVisualCreator = new PageBoundsVisualCreator(); graphControl.BackgroundGroup.AddChild(pageBoundsVisualCreator); // calculate the multi-page layout RunMultipageLayout(); }
/// <summary> /// Populates the palette with the graphs stored in the resources folder. /// </summary> private void InitializePalette() { //Handle list item drawing paletteListBox.DrawItem += OnDrawItem; // register for the mouse down event to initiate the drag operation paletteListBox.MouseDown += OnMouseDown; // populate the palette var ioHandler = new GraphMLIOHandler(); foreach (var file in Directory.GetFiles("Resources").Where(f => f.EndsWith("graphml"))) { var graph = new DefaultGraph(); ioHandler.Read(graph, file); paletteListBox.Items.Add(graph); } }
/// <summary> /// Configures a <see cref="GraphMLIOHandler"/> instance to read GraphML files written by /// previous versions of yFiles.NET. /// </summary> public static void ConfigureGraphMLCompatibility(GraphMLIOHandler handler) { // Register our three special namespaces containing MarkupExtensions that can then just be picked up by namespace and name // and don't have to be mapped manually (see below). // These mappings are akin to the assembly attributes at the top of this file. handler.AddXamlNamespaceMapping(YfilesCommonMarkupNS20, typeof(CommonMarkup.StaticExtension)); handler.AddXamlNamespaceMapping(YfilesNetXamlNS10, typeof(NodeScaledPortLocationModelExtension)); handler.AddXamlNamespaceMapping(YfilesCommonNS20, typeof(Common.LabelExtension)); handler.AddXamlNamespaceMapping(FormsNS10, typeof(SolidBrushExtension)); // Provides a mapping between XML element names in specific yFiles.NET 2.5 namespaces // to either their equivalent in the current library version, or to specially-written // MarkupExtensions that surface the old API and map it to the correct instances in the new API. var mappings = new Dictionary <XName, Type> { // General graph stuff { XName.Get("Bend", YfilesCommonNS20), typeof(BendExtension) }, { XName.Get("NodeViewState", YfilesCommonNS20), typeof(FolderNodeStateExtension) }, { XName.Get("EdgeViewState", YfilesCommonNS20), typeof(FoldingEdgeStateExtension) }, { XName.Get("GraphSettings", YfilesCommonNS20), typeof(GraphSettings) }, { XName.Get("NodeDefaults", YfilesCommonNS20), typeof(NodeDefaults) }, { XName.Get("EdgeDefaults", YfilesCommonNS20), typeof(EdgeDefaults) }, { XName.Get("GraphMLReference", YfilesCommonNS20), typeof(GraphMLReferenceExtension) }, // Void styles { XName.Get("VoidPortStyle", YfilesCommonNS20), typeof(VoidPortStyle) }, { XName.Get("VoidNodeStyle", YfilesCommonNS20), typeof(VoidNodeStyle) }, { XName.Get("VoidEdgeStyle", YfilesCommonNS20), typeof(VoidEdgeStyle) }, { XName.Get("VoidLabelStyle", YfilesCommonNS20), typeof(VoidLabelStyle) }, // Node styles { XName.Get("ShapeNodeStyle", YfilesNetXamlNS10), typeof(ShapeNodeStyle) }, { XName.Get("BevelNodeStyle", YfilesNetXamlNS10), typeof(BevelNodeStyle) }, { XName.Get("UriImageNodeStyle", YfilesNetXamlNS10), typeof(UriImageNodeStyleExtension) }, { XName.Get("MemoryImageNodeStyle", YfilesNetXamlNS10), typeof(MemoryImageNodeStyle) }, { XName.Get("Bitmap", YfilesNetXamlNS10), typeof(BitmapExtension) }, { XName.Get("GeneralPathNodeStyle", YfilesNetXamlNS10), typeof(GeneralPathNodeStyle) }, { XName.Get("GeneralPathMarkup", YfilesNetXamlNS10), typeof(GeneralPathExtension) }, { XName.Get("MoveTo", YfilesNetXamlNS10), typeof(MoveTo) }, { XName.Get("LineTo", YfilesNetXamlNS10), typeof(LineTo) }, { XName.Get("QuadTo", YfilesNetXamlNS10), typeof(QuadTo) }, { XName.Get("CubicTo", YfilesNetXamlNS10), typeof(CubicTo) }, { XName.Get("Close", YfilesNetXamlNS10), typeof(Close) }, { XName.Get("PanelNodeStyle", YfilesNetXamlNS10), typeof(PanelNodeStyle) }, { XName.Get("ShinyPlateNodeStyle", YfilesNetXamlNS10), typeof(ShinyPlateNodeStyle) }, { XName.Get("ShadowNodeStyleDecorator", YfilesNetXamlNS10), typeof(ShadowNodeStyleDecorator) }, { XName.Get("CollapsibleNodeStyleDecorator", YfilesNetXamlNS10), typeof(CollapsibleNodeStyleDecorator) }, { XName.Get("TableNodeStyle", YfilesNetXamlNS10), typeof(TableNodeStyle) }, // Port styles { XName.Get("NodeStylePortStyleAdapter", YfilesNetXamlNS10), typeof(NodeStylePortStyleAdapter) }, // Edge styles { XName.Get("PolylineEdgeStyle", YfilesNetXamlNS10), typeof(PolylineEdgeStyleExtension) }, { XName.Get("ArcEdgeStyle", YfilesNetXamlNS10), typeof(ArcEdgeStyle) }, // Label styles { XName.Get("NodeStyleLabelStyleAdapter", YfilesNetXamlNS10), typeof(NodeStyleLabelStyleAdapter) }, // Auxiliary style classes { XName.Get("Arrow", YfilesNetXamlNS10), typeof(Arrow) }, { XName.Get("DefaultArrow", YfilesNetXamlNS10), typeof(Arrows) }, { XName.Get("Table", YfilesCommonNS20), typeof(TableExtension) }, { XName.Get("Row", YfilesCommonNS20), typeof(Common.RowExtension) }, { XName.Get("Column", YfilesCommonNS20), typeof(Common.ColumnExtension) }, // Label models and their parameters { XName.Get("FreeLabelModel", YfilesNetXamlNS10), typeof(FreeLabelModel) }, { XName.Get("FixedLabelModelParameter", YfilesNetXamlNS10), typeof(FixedLabelModelParameterExtension) }, { XName.Get("AnchoredLabelModelParameter", YfilesNetXamlNS10), typeof(AnchoredLabelModelParameterExtension) }, { XName.Get("CompositeLabelModel", YfilesNetXamlNS10), typeof(CompositeLabelModel) }, { XName.Get("CompositeLabelModelParameter", YfilesNetXamlNS10), typeof(CompositeLabelModelParameterExtension) }, { XName.Get("GenericModel", YfilesNetXamlNS10), typeof(GenericLabelModelExtension) }, { XName.Get("GenericLabelModelParameter", YfilesNetXamlNS10), typeof(GenericLabelModelParameterExtension) }, { XName.Get("DescriptorWrapperLabelModel", YfilesNetXamlNS10), typeof(DescriptorWrapperLabelModel) }, { XName.Get("DescriptorWrapperLabelModelParameter", YfilesNetXamlNS10), typeof(DescriptorWrapperLabelModelParameterExtension) }, { XName.Get("FreeNodeLabelModel", YfilesNetXamlNS10), typeof(FreeNodeLabelModel) }, { XName.Get("RatioAnchoredLabelModelParameter", YfilesNetXamlNS10), typeof(RatioAnchoredLabelModelParameterExtension) }, { XName.Get("SandwichLabelModel", YfilesNetXamlNS10), typeof(SandwichLabelModel) }, { XName.Get("SandwichParameter", YfilesNetXamlNS10), typeof(SandwichParameterExtension) }, { XName.Get("ExteriorLabelModel", YfilesNetXamlNS10), typeof(ExteriorLabelModel) }, { XName.Get("ExteriorLabelModelParameter", YfilesNetXamlNS10), typeof(ExteriorLabelModelParameterExtension) }, { XName.Get("InteriorLabelModel", YfilesNetXamlNS10), typeof(InteriorLabelModel) }, { XName.Get("InteriorLabelModelParameter", YfilesNetXamlNS10), typeof(InteriorLabelModelParameterExtension) }, { XName.Get("InteriorStretchLabelModel", YfilesNetXamlNS10), typeof(InteriorStretchLabelModel) }, { XName.Get("InteriorStretchLabelModelParameter", YfilesNetXamlNS10), typeof(InteriorStretchLabelModelParameterExtension) }, { XName.Get("NinePositionsEdgeLabelModel", YfilesNetXamlNS10), typeof(NinePositionsEdgeLabelModel) }, { XName.Get("NinePositionsEdgeLabelParameter", YfilesNetXamlNS10), typeof(Xaml.NinePositionsEdgeLabelModelParameterExtension) }, { XName.Get("FreeEdgeLabelModel", YfilesNetXamlNS10), typeof(FreeEdgeLabelModel) }, { XName.Get("FreeEdgeLabelModelParameter", YfilesNetXamlNS10), typeof(FreeEdgeLabelModelParameterExtension) }, { XName.Get("SmartEdgeLabelModel", YfilesNetXamlNS10), typeof(SmartEdgeLabelModel) }, { XName.Get("SmartEdgeLabelModelParameter", YfilesNetXamlNS10), typeof(SmartEdgeLabelModelParameterExtension) }, { XName.Get("BendAnchoredPortLocationModel", YfilesNetXamlNS10), typeof(BendAnchoredPortLocationModel) }, { XName.Get("BendAnchoredParameter", YfilesNetXamlNS10), typeof(BendAnchoredParameterExtension) }, { XName.Get("GenericPortLocationModel", YfilesNetXamlNS10), typeof(Xaml.GenericPortLocationModelExtension) }, { XName.Get("GenericPortLocationParameter", YfilesNetXamlNS10), typeof(GenericPortLocationParameterExtension) }, { XName.Get("SegmentRatioPortLocationModel", YfilesNetXamlNS10), typeof(SegmentRatioPortLocationModel) }, { XName.Get("SegmentRatioParameterParameter", YfilesNetXamlNS10), typeof(SegmentRatioParameterExtension) }, { XName.Get("StripeLabelModel", YfilesNetXamlNS10), typeof(StripeLabelModel) }, { XName.Get("StripeLabelModelParameter", YfilesNetXamlNS10), typeof(StripeLabelModelParameterExtension) }, { XName.Get("StretchStripeLabelModel", YfilesNetXamlNS10), typeof(StretchStripeLabelModel) }, { XName.Get("StretchStripeLabelModelParameter", YfilesNetXamlNS10), typeof(StretchStripeLabelModelParameterExtension) } // , // // { XName.Get("SolidBrush", FormsNS10), typeof(SolidBrushExtension) } }; handler.QueryType += (sender, args) => { Type result; if (mappings.TryGetValue(args.XmlName, out result)) { args.Result = result; } }; }
/// <summary> /// Configures a <see cref="GraphMLIOHandler"/> instance to read GraphML files written by /// previous versions of yFiles.NET. /// </summary> public static void ConfigureGraphMLCompatibility(this GraphMLIOHandler handler) { GraphMLCompatibility.ConfigureGraphMLCompatibility(handler); }
/// <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; }