Просмотр исходного кода

Added TreeView templating with a basic binding setup for MVVM pattern

Dexter89 12 лет назад
Родитель
Сommit
424f09bd9c

+ 40 - 0
tools/gui/crown-tests/GtkExt/TreeViewRowTemplate.cs

@@ -0,0 +1,40 @@
+using System;
+using System.Collections.Generic;
+
+namespace crown_tests.GtkExt
+{
+	public class TreeViewRowTemplate
+	{
+		public Type TargetType;
+		public Dictionary<String, Binding> ColumnBindings;
+
+		public TreeViewRowTemplate(Type targetType)
+		{
+			this.TargetType = targetType;
+			this.ColumnBindings = new Dictionary<string, Binding>();
+		}
+
+		public static TreeViewRowTemplate Create(Type targetType)
+		{
+			return new TreeViewRowTemplate (targetType);
+		}
+
+		public TreeViewRowTemplate SetBinding(String colName, String path)
+		{
+			ColumnBindings.Add (colName, new Binding () { Path = path });
+			return this;
+		}
+
+		public TreeViewRowTemplate SetBinding(String colName, String path, Func<object, object> Converter)
+		{
+			ColumnBindings.Add (colName, new Binding () { Path = path, Converter = Converter });
+			return this;
+		}
+	}
+
+	public class Binding
+	{
+		public String Path;
+		public Func<object, object> Converter;
+	}
+}

+ 72 - 0
tools/gui/crown-tests/GtkExt/TreeViewTemplating.cs

@@ -0,0 +1,72 @@
+using System;
+using System.Collections.Generic;
+
+namespace crown_tests.GtkExt
+{
+	public static class TreeViewTemplating
+	{
+		private static Dictionary<Gtk.TreeView, TreeViewTemplateInfo> mTemplateInfo;
+
+		static TreeViewTemplating() {
+			mTemplateInfo = new Dictionary<Gtk.TreeView, TreeViewTemplateInfo>();
+		}
+
+		private static TreeViewTemplateInfo GetInfo(Gtk.TreeView treeView)
+		{
+			TreeViewTemplateInfo info = null;
+			if (!mTemplateInfo.TryGetValue (treeView, out info)) {
+				info = new TreeViewTemplateInfo ();
+				mTemplateInfo [treeView] = info;
+			}
+			return info;
+		}
+
+		public static void AddRowTemplate(Gtk.TreeView treeView, TreeViewRowTemplate template)
+		{
+			GetInfo (treeView).RowTemplates.Add (template);
+		}
+
+		public static void ApplyTemplating(Gtk.TreeView treeView)
+		{
+			foreach (var col in treeView.Columns) {
+				col.SetCellDataFunc (col.CellRenderers[0], ValuePropertyDataFunc);
+			}
+		}
+
+		private static void ValuePropertyDataFunc(Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
+		{
+			var info = GetInfo ((Gtk.TreeView)column.TreeView);
+
+
+			var textCell = (cell as Gtk.CellRendererText);
+			textCell.Text = string.Empty;
+			var value = model.GetValue (iter, 0);
+			if (value == null)
+				return;
+
+			foreach (var rowTemplate in info.RowTemplates) {
+				if (value.GetType () == rowTemplate.TargetType) {
+					Binding b = null;
+					if (!rowTemplate.ColumnBindings.TryGetValue (column.Title, out b))
+						return;
+
+					var propInfo = value.GetType ().GetProperty (b.Path);
+					if (propInfo == null)
+						return;
+
+					var propValue = propInfo.GetValue (value, null);
+					if (b.Converter != null)
+						propValue = b.Converter (propValue);
+					textCell.Text = propValue == null ? String.Empty : propValue.ToString ();
+					return;
+				}
+			}
+		}
+
+		private class TreeViewTemplateInfo
+		{
+			public List<TreeViewRowTemplate> RowTemplates = new List<TreeViewRowTemplate> ();
+		}
+	}
+}
+

+ 30 - 20
tools/gui/crown-tests/MainWindow.cs

@@ -2,6 +2,7 @@
 using Gtk;
 using crown_tests.tests;
 using Newtonsoft.Json;
+using crown_tests.GtkExt;
 
 namespace crown_tests
 {
@@ -14,31 +15,41 @@ namespace crown_tests
 		{
 			this.Build ();
 
-			twTests.AppendColumn("Name", new Gtk.CellRendererText(), "text", 0);
-			twTests.AppendColumn("State", new Gtk.CellRendererText(), "text", 1);
+			twTests.AppendColumn ("Name", new Gtk.CellRendererText ());
+			twTests.AppendColumn ("State", new Gtk.CellRendererText ());
+			TreeViewTemplating.AddRowTemplate(twTests, 
+																				TreeViewRowTemplate.Create (typeof (TestCategory))
+																				.SetBinding ("Name", "Name"));
+			TreeViewTemplating.AddRowTemplate(twTests, 
+																				TreeViewRowTemplate.Create (typeof (Test))
+																				.SetBinding ("Name", "Name")
+																				.SetBinding ("State", "LastResult", (x) => object.Equals(x, 0) ? "Passed" : "Failed"));
+			TreeViewTemplating.ApplyTemplating (twTests);
+
 
 			LoadConfigData ();
 			LoadTestsData ();
 		}
 
-		protected void OnDeleteEvent (object sender, DeleteEventArgs a)
+		protected void OnDeleteEvent(object sender, DeleteEventArgs a)
 		{
 			Application.Quit ();
 			a.RetVal = true;
 		}
 
 		#region "My Code"
+
 		private void btnCreate_Click(object o, EventArgs args)
 		{
-			var creator = new TestSourceCreator(mContainer, txtTestFolder.Text);
-			creator.Create();
+			var creator = new TestSourceCreator (mContainer, txtTestFolder.Text);
+			creator.Create ();
 		}
 
 		private void btnExecute_Click(object o, EventArgs args)
 		{
-			var executor = new TestExecutor(mContainer, txtCrownTestsExe.Text);
-			executor.ExecuteAll();
-			RefreshData();
+			var executor = new TestExecutor (mContainer, txtCrownTestsExe.Text);
+			executor.ExecuteAll ();
+			RefreshData ();
 		}
 
 		private void LoadConfigData()
@@ -49,29 +60,28 @@ namespace crown_tests
 
 		private void LoadTestsData()
 		{
-			var testsJsonFullfileName = System.IO.Path.Combine(txtTestFolder.Text, "tests.json");
-			mContainer = JsonConvert.DeserializeObject<TestContainer>(System.IO.File.ReadAllText(testsJsonFullfileName));
+			var testsJsonFullfileName = System.IO.Path.Combine (txtTestFolder.Text, "tests.json");
+			mContainer = JsonConvert.DeserializeObject<TestContainer> (System.IO.File.ReadAllText (testsJsonFullfileName));
 
-			RefreshData();
+			RefreshData ();
 		}
 
 		private void RefreshData()
 		{
-			var treeStore = twTests.Model as Gtk.TreeStore; // new Gtk.TreeStore(typeof(string), typeof(string));
+			var treeStore = twTests.Model as Gtk.TreeStore;
 			if (treeStore == null)
-				treeStore = new Gtk.TreeStore(typeof(string), typeof(string));
-			treeStore.Clear();
-			foreach (var category in mContainer.Categories)
-			{
-				var iter = treeStore.AppendValues(category.Name);
-				foreach (var test in category.Tests)
-				{
-					treeStore.AppendValues(iter, test.Name, test.LastResult == 0 ? "Passed" : "Failed");
+				treeStore = new Gtk.TreeStore (typeof(object));
+			treeStore.Clear ();
+			foreach (var category in mContainer.Categories) {
+				var iter = treeStore.AppendValues (category);
+				foreach (var test in category.Tests) {
+					treeStore.AppendValues (iter, test);
 				}
 			}
 
 			twTests.Model = treeStore;
 		}
+
 		#endregion
 	}
 }

+ 5 - 0
tools/gui/crown-tests/crown-tests.csproj

@@ -70,6 +70,8 @@
     <Compile Include="gtk-gui\generated.cs" />
     <Compile Include="gtk-gui\crown_tests.MainWindow.cs" />
     <Compile Include="MainWindow.cs" />
+    <Compile Include="GtkExt\TreeViewTemplating.cs" />
+    <Compile Include="GtkExt\TreeViewRowTemplate.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
@@ -84,4 +86,7 @@
       <LogicalName>gui.stetic</LogicalName>
     </EmbeddedResource>
   </ItemGroup>
+  <ItemGroup>
+    <Folder Include="GtkExt\" />
+  </ItemGroup>
 </Project>

+ 3 - 3
tools/gui/crown-tests/tests/Test.cs

@@ -10,11 +10,11 @@ namespace crown_tests.tests
   public class Test
   {
     [JsonProperty]
-    public String Name;
+		public String Name { get; set; }
     [JsonProperty]
-    public String Description;
+		public String Description { get; set; }
 
-    public int LastResult;
+		public int LastResult { get; set; }
 
     public String GetFunctionName()
     {

+ 3 - 3
tools/gui/crown-tests/tests/TestCategory.cs

@@ -10,11 +10,11 @@ namespace crown_tests.tests
   public class TestCategory
   {
     [JsonProperty]
-    public String Name;
+		public String Name { get; set; }
     [JsonProperty]
-    public String Description;
+		public String Description { get; set; }
     [JsonProperty]
-    public List<Test> Tests;
+		public List<Test> Tests { get; set; }
 
     public TestCategory(String name, String description)
     {

+ 1 - 1
tools/gui/crown-tests/tests/TestContainer.cs

@@ -10,7 +10,7 @@ namespace crown_tests.tests
   public class TestContainer
   {
     [JsonProperty]
-    public List<TestCategory> Categories;
+		public List<TestCategory> Categories { get; set; }
 
     public TestContainer()
     {

+ 1 - 0
tools/gui/toolbox.sln

@@ -55,6 +55,7 @@ Global
 		$1.inheritsSet = null
 		$1.scope = text/x-csharp
 		$0.CSharpFormattingPolicy = $2
+		$2.BeforeMethodDeclarationParentheses = False
 		$2.AfterDelegateDeclarationParameterComma = True
 		$2.inheritsSet = Mono
 		$2.inheritsScope = text/x-csharp