Переглянути джерело

Extracted generic templating logic and TreeView-specific logic

Dexter89 11 роки тому
батько
коміт
36f5b9a4ae

+ 9 - 1
tools/gui/crown-tests/GtkExt/BindingEngine.cs

@@ -1,7 +1,7 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 
 
-namespace crown_tests
+namespace crown_tests.GtkExt
 {
 {
 	public static class BindingEngine
 	public static class BindingEngine
 	{
 	{
@@ -63,9 +63,17 @@ namespace crown_tests
 		}
 		}
 	}
 	}
 
 
+	public class BindingTargetValueChangedEventArgs: EventArgs
+	{
+		public Object NewValue;
+	}
+	public delegate void BindingTargetValueChangedEventHandler(object sender, BindingTargetValueChangedEventArgs e);
+
 	public interface IBindingTarget
 	public interface IBindingTarget
 	{
 	{
+
 		void Update(Binding binding, Object newValue);
 		void Update(Binding binding, Object newValue);
+		event BindingTargetValueChangedEventHandler ValueChanged;
 	}
 	}
 }
 }
 
 

+ 39 - 0
tools/gui/crown-tests/GtkExt/BindingTargets/EntryBindingTarget.cs

@@ -0,0 +1,39 @@
+using System;
+
+namespace crown_tests.GtkExt
+{
+	sealed class EntryBindingTarget: IBindingTarget
+	{
+		private Gtk.Entry mEntry;
+
+		public EntryBindingTarget(Gtk.Entry Entry)
+		{
+			mEntry = Entry;
+		}
+
+		public override bool Equals(object obj)
+		{
+			var other = obj as EntryBindingTarget;
+			if (other == null)
+				return false;
+			return (mEntry == other.mEntry);
+		}
+
+		public override int GetHashCode()
+		{
+			return mEntry.GetHashCode();
+		}
+
+		#region IBindingTarget implementation
+
+		public event BindingTargetValueChangedEventHandler ValueChanged;
+
+		public void Update(Binding binding, object newValue)
+		{
+			mEntry.Text = newValue as String;
+		}
+
+		#endregion
+	}
+}
+

+ 43 - 0
tools/gui/crown-tests/GtkExt/BindingTargets/TreeViewIterBindingTarget.cs

@@ -0,0 +1,43 @@
+using System;
+
+namespace crown_tests.GtkExt
+{
+	sealed class TreeViewIterBindingTarget: IBindingTarget
+	{
+		private Gtk.TreeIter mIter;
+		private Gtk.TreeView mTreeView;
+		private Gtk.TreeViewColumn mColumn;
+
+		public TreeViewIterBindingTarget(Gtk.TreeView treeView, Gtk.TreeIter iter, Gtk.TreeViewColumn column)
+		{
+			mTreeView = treeView;
+			mIter = iter;
+			mColumn = column;
+		}
+
+		public override bool Equals(object obj)
+		{
+			var other = obj as TreeViewIterBindingTarget;
+			if (other == null)
+				return false;
+			return other.mIter.Equals(mIter) && (mTreeView == other.mTreeView) && (mColumn == other.mColumn);
+		}
+
+		public override int GetHashCode()
+		{
+			return mIter.GetHashCode();
+		}
+
+		#region IBindingTarget implementation
+
+		public event BindingTargetValueChangedEventHandler ValueChanged;
+
+		public void Update(Binding binding, object newValue)
+		{
+			mTreeView.Model.EmitRowChanged(mTreeView.Model.GetPath(mIter), mIter);
+		}
+
+		#endregion
+	}
+}
+

+ 1 - 1
tools/gui/crown-tests/GtkExt/IPropertyChanged.cs

@@ -1,6 +1,6 @@
 using System;
 using System;
 
 
-namespace crown_tests
+namespace crown_tests.GtkExt
 {
 {
 	public class PropertyChangedEventArgs: EventArgs
 	public class PropertyChangedEventArgs: EventArgs
 	{
 	{

+ 39 - 0
tools/gui/crown-tests/GtkExt/Templating.cs

@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+
+namespace crown_tests.GtkExt
+{
+	public interface ITemplate
+	{
+		void Apply(Gtk.Widget widget);
+	}
+
+	public static class Templating
+	{
+		private static Dictionary<Gtk.Widget, ITemplate> mTemplates;
+
+		static Templating()
+		{
+			mTemplates = new Dictionary<Gtk.Widget, ITemplate>();
+		}
+
+		public static ITemplate GetTemplate(Gtk.Widget widget)
+		{
+			ITemplate template = null;
+			mTemplates.TryGetValue(widget, out template);
+			return template;
+		}
+
+		public static void ApplyTemplate(Gtk.Widget widget, ITemplate template)
+		{
+			if (GetTemplate(widget) != null) {
+				Console.WriteLine("Templating.ApplyTemplate: template already applied for this widget");
+				return;
+			}
+
+			mTemplates.Add(widget, template);
+			template.Apply(widget);
+		}
+	}
+}
+

+ 63 - 0
tools/gui/crown-tests/GtkExt/TreeViewTemplate.cs

@@ -0,0 +1,63 @@
+using System;
+using System.Collections.Generic;
+
+namespace crown_tests.GtkExt
+{
+	public class TreeViewTemplate: ITemplate
+	{
+		public List<TreeViewRowTemplate> RowTemplates = new List<TreeViewRowTemplate>();
+
+		public TreeViewTemplate()
+		{
+		}
+
+		public TreeViewTemplate AddRowTemplate(TreeViewRowTemplate rowTemplate)
+		{
+			RowTemplates.Add(rowTemplate);
+			return this;
+		}
+
+		public void Apply(Gtk.Widget widget) 
+		{
+			Gtk.TreeView treeView = widget as Gtk.TreeView;
+			if (treeView == null) {
+				Console.WriteLine("TreeViewTemplate.Apply: Invalid widget type for this template");
+				return;
+			}
+
+			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 treeView = (Gtk.TreeView)column.TreeView;
+			var info = Templating.GetTemplate(treeView) as TreeViewTemplate;
+
+			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) {
+					//Here we have a value, which is the source for Binding, and a BindingInfo that is given by rowTemplate.ColumnBindings[column.Title] .
+					//The instance of the BindingInfo is shared among all values (rows), since it was defined once in the rowTemplate.
+
+					BindingInfo bindingInfo = null;
+					if (!rowTemplate.ColumnBindings.TryGetValue(column.Title, out bindingInfo))
+						return;
+
+					//The actual binding, on the other hand, is specific to the current (row,column) pair.
+					Binding binding = BindingEngine.GetOrCreateBinding(value, new TreeViewIterBindingTarget(treeView, iter, column), bindingInfo);
+					var propValue = binding.GetSourceValue();
+					textCell.Text = propValue == null ? String.Empty : propValue.ToString();
+					return;
+				}
+			}
+		}
+	}
+}
+

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

@@ -1,108 +0,0 @@
-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 treeView = (Gtk.TreeView)column.TreeView;
-			var info = GetInfo(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) {
-					//Here we have a value, which is the source for Binding, and a BindingInfo that is given by rowTemplate.ColumnBindings[column.Title] .
-					//The instance of the BindingInfo is shared among all values (rows), since it was defined once in the rowTemplate.
-
-					BindingInfo bindingInfo = null;
-					if (!rowTemplate.ColumnBindings.TryGetValue(column.Title, out bindingInfo))
-						return;
-
-					//The actual binding, on the other hand, is specific to the current (row,column) pair.
-					Binding binding = BindingEngine.GetOrCreateBinding(value, new TreeViewIterBindingTarget(treeView, iter, column), bindingInfo);
-					var propValue = binding.GetSourceValue();
-					textCell.Text = propValue == null ? String.Empty : propValue.ToString();
-					return;
-				}
-			}
-		}
-
-		private class TreeViewTemplateInfo
-		{
-			public List<TreeViewRowTemplate> RowTemplates = new List<TreeViewRowTemplate>();
-		}
-
-		private class TreeViewIterBindingTarget: IBindingTarget
-		{
-			private Gtk.TreeIter mIter;
-			private Gtk.TreeView mTreeView;
-			private Gtk.TreeViewColumn mColumn;
-
-			public TreeViewIterBindingTarget(Gtk.TreeView treeView, Gtk.TreeIter iter, Gtk.TreeViewColumn column)
-			{
-				mTreeView = treeView;
-				mIter = iter;
-				mColumn = column;
-			}
-
-			public override bool Equals(object obj)
-			{
-				var other = obj as TreeViewIterBindingTarget;
-				if (other == null)
-					return false;
-				return other.mIter.Equals(mIter) && (mTreeView == other.mTreeView) && (mColumn == other.mColumn);
-			}
-
-			public override int GetHashCode()
-			{
-				return mIter.GetHashCode();
-			}
-
-			#region IBindingTarget implementation
-
-			public void Update(Binding binding, object newValue)
-			{
-				mTreeView.Model.EmitRowChanged(mTreeView.Model.GetPath(mIter), mIter);
-			}
-
-			#endregion
-		}
-	}
-}
-

+ 1 - 1
tools/gui/crown-tests/GtkExt/ViewModelBase.cs

@@ -1,6 +1,6 @@
 using System;
 using System;
 
 
-namespace crown_tests
+namespace crown_tests.GtkExt
 {
 {
 	public class ViewModelBase: IPropertyChanged
 	public class ViewModelBase: IPropertyChanged
 	{
 	{

+ 7 - 9
tools/gui/crown-tests/MainWindow.cs

@@ -17,15 +17,13 @@ namespace crown_tests
 
 
 			twTests.AppendColumn("Name", new Gtk.CellRendererText());
 			twTests.AppendColumn("Name", new Gtk.CellRendererText());
 			twTests.AppendColumn("State", 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"));
-			TreeViewTemplating.ApplyTemplating(twTests);
-
+			Templating.ApplyTemplate(twTests,
+				new TreeViewTemplate()
+				  .AddRowTemplate(TreeViewRowTemplate.Create(typeof(TestCategory))
+																						 .SetBinding("Name", "Name"))
+					.AddRowTemplate(TreeViewRowTemplate.Create(typeof(Test))
+																						 .SetBinding("Name", "Name")
+																						 .SetBinding("State", "LastResult")));
 
 
 			LoadConfigData();
 			LoadConfigData();
 			LoadTestsData();
 			LoadTestsData();

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

@@ -70,11 +70,14 @@
     <Compile Include="gtk-gui\generated.cs" />
     <Compile Include="gtk-gui\generated.cs" />
     <Compile Include="gtk-gui\crown_tests.MainWindow.cs" />
     <Compile Include="gtk-gui\crown_tests.MainWindow.cs" />
     <Compile Include="MainWindow.cs" />
     <Compile Include="MainWindow.cs" />
-    <Compile Include="GtkExt\TreeViewTemplating.cs" />
     <Compile Include="GtkExt\TreeViewRowTemplate.cs" />
     <Compile Include="GtkExt\TreeViewRowTemplate.cs" />
     <Compile Include="GtkExt\IPropertyChanged.cs" />
     <Compile Include="GtkExt\IPropertyChanged.cs" />
     <Compile Include="GtkExt\ViewModelBase.cs" />
     <Compile Include="GtkExt\ViewModelBase.cs" />
     <Compile Include="GtkExt\BindingEngine.cs" />
     <Compile Include="GtkExt\BindingEngine.cs" />
+    <Compile Include="GtkExt\BindingTargets\TreeViewIterBindingTarget.cs" />
+    <Compile Include="GtkExt\BindingTargets\EntryBindingTarget.cs" />
+    <Compile Include="GtkExt\Templating.cs" />
+    <Compile Include="GtkExt\TreeViewTemplate.cs" />
   </ItemGroup>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
@@ -91,5 +94,6 @@
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <Folder Include="GtkExt\" />
     <Folder Include="GtkExt\" />
+    <Folder Include="GtkExt\BindingTargets\" />
   </ItemGroup>
   </ItemGroup>
 </Project>
 </Project>

+ 1 - 0
tools/gui/crown-tests/tests/Test.cs

@@ -3,6 +3,7 @@ using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using System.Text;
 using System.Text;
+using crown_tests.GtkExt;
 
 
 namespace crown_tests.tests
 namespace crown_tests.tests
 {
 {