Index: Core/Components/src/Core.Components.GraphSharp.Forms/PointedTreeGraphControl.cs =================================================================== diff -u -r6189fae3f21e2ee5eac80441472c2bcd5124c3da -r6dd9d30bf2f7b740320718ceea5513f39741fa95 --- Core/Components/src/Core.Components.GraphSharp.Forms/PointedTreeGraphControl.cs (.../PointedTreeGraphControl.cs) (revision 6189fae3f21e2ee5eac80441472c2bcd5124c3da) +++ Core/Components/src/Core.Components.GraphSharp.Forms/PointedTreeGraphControl.cs (.../PointedTreeGraphControl.cs) (revision 6dd9d30bf2f7b740320718ceea5513f39741fa95) @@ -22,6 +22,9 @@ using System; using System.Windows; using System.Windows.Forms; +using System.Windows.Media; +using Core.Common.Utils.Extensions; +using Core.Components.GraphSharp.Data; using Core.Components.GraphSharp.Forms.Layout; using Core.Components.PointedTree.Data; using Core.Components.PointedTree.Forms; @@ -36,6 +39,8 @@ { private ZoomControl zoomControl; private PointedTreeGraphLayout graphLayout; + private GraphNode data; + private PointedTreeGraph graph; /// /// Creates a new instance of . @@ -46,8 +51,25 @@ InitializeGraph(); } - public GraphNode Data { get; set; } + public GraphNode Data + { + get + { + return data; + } + set + { + graph.Clear(); + data = value; + + if (data != null) + { + DrawNode(data); + } + } + } + private void InitializeGraph() { zoomControl = new ZoomControl @@ -58,15 +80,42 @@ graphLayout = new PointedTreeGraphLayout(); + graph = new PointedTreeGraph(); + graphLayout.Graph = graph; + zoomControl.Content = graphLayout; var myResourceDictionary = new ResourceDictionary { Source = new Uri("pack://application:,,,/Core.Components.GraphSharp.Forms;component/Templates/PointedTreeGraphTemplate.xaml", UriKind.Absolute) }; zoomControl.Resources.MergedDictionaries.Add(myResourceDictionary); - wpfElementHost.Child = zoomControl; } + + private void DrawNode(GraphNode node, PointedTreeElementVertex parentVertex = null) + { + var vertex = new PointedTreeElementVertex(node.Title, + new SolidColorBrush(Color.FromArgb(node.Style.FillColor.A, + node.Style.FillColor.R, + node.Style.FillColor.G, + node.Style.FillColor.B)), + new SolidColorBrush(Color.FromArgb(node.Style.LineColor.A, + node.Style.LineColor.R, + node.Style.LineColor.G, + node.Style.LineColor.B)), + node.Style.LineWidth, + PointedTreeVertexType.Rectangle, + node.IsSelectable); + + graph.AddVertex(vertex); + + node.ChildNodes.ForEachElementDo(cn => DrawNode(cn, vertex)); + + if (parentVertex != null) + { + graph.AddEdge(new PointedTreeEdge(parentVertex, vertex)); + } + } } } \ No newline at end of file Index: Core/Components/test/Core.Components.GraphSharp.Forms.Test/PointedTreeGraphControlTest.cs =================================================================== diff -u -r6189fae3f21e2ee5eac80441472c2bcd5124c3da -r6dd9d30bf2f7b740320718ceea5513f39741fa95 --- Core/Components/test/Core.Components.GraphSharp.Forms.Test/PointedTreeGraphControlTest.cs (.../PointedTreeGraphControlTest.cs) (revision 6189fae3f21e2ee5eac80441472c2bcd5124c3da) +++ Core/Components/test/Core.Components.GraphSharp.Forms.Test/PointedTreeGraphControlTest.cs (.../PointedTreeGraphControlTest.cs) (revision 6dd9d30bf2f7b740320718ceea5513f39741fa95) @@ -24,18 +24,20 @@ using System.Windows; using System.Windows.Forms; using System.Windows.Forms.Integration; +using Core.Components.GraphSharp.Data; using Core.Components.GraphSharp.Forms.Layout; +using Core.Components.PointedTree.Data; using Core.Components.PointedTree.Forms; using NUnit.Framework; using WPFExtensions.Controls; namespace Core.Components.GraphSharp.Forms.Test { [TestFixture] + [Apartment(ApartmentState.STA)] public class PointedTreeGraphControlTest { [Test] - [Apartment(ApartmentState.STA)] public void Constructor_ExptedValues() { // Call @@ -56,8 +58,106 @@ ResourceDictionary templateDictionary = zoomControl.Resources.MergedDictionaries.First(); Assert.AreEqual("/Core.Components.GraphSharp.Forms;component/Templates/PointedTreeGraphTemplate.xaml", templateDictionary.Source.AbsolutePath); - Assert.IsInstanceOf(zoomControl.Content); + var graphLayout = (PointedTreeGraphLayout) zoomControl.Content; + Assert.IsInstanceOf(graphLayout.Graph); } } + + [Test] + public void GivenGraphControlWithoutData_WhenDataSetToGraphNode_ThenGraphControlUpdated() + { + // Given + using (var graphControl = new PointedTreeGraphControl()) + { + var doubleUsedNode = new GraphNode("Double used", new GraphNode[0], false); + var node = new GraphNode("Root", new[] + { + new GraphNode("Child 1", new [] + { + doubleUsedNode + }, false), + new GraphNode("Child 2", new [] + { + doubleUsedNode + }, false) + }, false); + + // When + graphControl.Data = node; + + // Then + var elementHost = graphControl.Controls[0] as ElementHost; + var zoomControl = (ZoomControl) elementHost.Child; + var graphLayout = (PointedTreeGraphLayout) zoomControl.Content; + PointedTreeGraph graph = graphLayout.Graph; + + Assert.AreEqual(5, graph.VertexCount); + Assert.AreEqual(4, graph.EdgeCount); + } + } + + [Test] + public void GivenGraphControlWithData_WhenDataSetToOtherGraphNode_ThenGraphControlUpdated() + { + // Given + var node = new GraphNode("Root", new[] + { + new GraphNode("Child 1", new GraphNode[0], false) + }, false); + + using (var graphControl = new PointedTreeGraphControl + { + Data = node + }) + { + var elementHost = graphControl.Controls[0] as ElementHost; + var zoomControl = (ZoomControl)elementHost.Child; + var graphLayout = (PointedTreeGraphLayout)zoomControl.Content; + PointedTreeGraph graph = graphLayout.Graph; + + // Precondition + Assert.AreEqual(2, graph.VertexCount); + Assert.AreEqual(1, graph.EdgeCount); + + // When + graphControl.Data = new GraphNode("Double used", new GraphNode[0], false); + + // Then + Assert.AreEqual(1, graph.VertexCount); + Assert.AreEqual(0, graph.EdgeCount); + } + } + + [Test] + public void GivenGraphControlWithData_WhenDataSetToNull_ThenGraphControlUpdated() + { + // Given + var node = new GraphNode("Root", new[] + { + new GraphNode("Child 1", new GraphNode[0], false) + }, false); + + using (var graphControl = new PointedTreeGraphControl + { + Data = node + }) + { + var elementHost = graphControl.Controls[0] as ElementHost; + var zoomControl = (ZoomControl)elementHost.Child; + var graphLayout = (PointedTreeGraphLayout)zoomControl.Content; + PointedTreeGraph graph = graphLayout.Graph; + + // Precondition + Assert.AreEqual(2, graph.VertexCount); + Assert.AreEqual(1, graph.EdgeCount); + + // When + graphControl.Data = null; + + // Then + Assert.AreEqual(0, graph.VertexCount); + Assert.AreEqual(0, graph.EdgeCount); + } + } } } \ No newline at end of file