Commit 16d384b

mo khan <mo@mokhan.ca>
2009-05-13 18:37:13
added factory to create bindable text boxes
1 parent 220d787
product/Gorilla.Commons.Windows.Forms/Helpers/BindableTextBox.cs
@@ -1,3 +1,7 @@
+using System;
+using System.Collections.Generic;
+using Gorilla.Commons.Utility.Extensions;
+
 namespace Gorilla.Commons.Windows.Forms.Helpers
 {
     public interface IBindableTextBox<T>
@@ -10,10 +14,12 @@ namespace Gorilla.Commons.Windows.Forms.Helpers
     public class BindableTextBox<T> : IBindableTextBox<T>
     {
         readonly ITextControl<T> control;
+        readonly IList<Action<IBindableTextBox<T>>> actions = new List<Action<IBindableTextBox<T>>>();
 
         public BindableTextBox(ITextControl<T> control)
         {
             this.control = control;
+            control.when_text_is_changed = () => actions.each(x => x(this));
         }
 
         public void bind_to(T item)
@@ -30,5 +36,10 @@ namespace Gorilla.Commons.Windows.Forms.Helpers
         {
             return control.text();
         }
+
+        public void on_leave(Action<IBindableTextBox<T>> action)
+        {
+            actions.Add(action);
+        }
     }
 }
\ No newline at end of file
product/Gorilla.Commons.Windows.Forms/Helpers/BindableTextBoxFactory.cs
@@ -0,0 +1,25 @@
+using System;
+using System.Linq.Expressions;
+using Gorilla.Commons.Utility.Extensions;
+
+namespace Gorilla.Commons.Windows.Forms.Helpers
+{
+    static public class BindableTextBoxFactory
+    {
+        static public IBindableTextBox<ItemToBindTo> create_for<ItemToBindTo>(ITextControl<ItemToBindTo> text_control, params ITextBoxCommand<ItemToBindTo>[] commands)
+        {
+            var textbox = new BindableTextBox<ItemToBindTo>(text_control);
+            commands.each(x => textbox.on_leave(y => x.run(y)));
+            return textbox;
+        }
+    }
+
+    static public class BindableTextBoxExtensions
+    {
+        static public IBindableTextBox<ItemToBindTo> rebind_with<ItemToBindTo>(
+            this ITextControl<ItemToBindTo> text_control, Expression<Func<string, ItemToBindTo>> rebind)
+        {
+            return BindableTextBoxFactory.create_for(text_control, new RebindTextBoxCommand<ItemToBindTo>(rebind));
+        }
+    }
+}
\ No newline at end of file
product/Gorilla.Commons.Windows.Forms/Helpers/BindableTextBoxSpecs.cs
@@ -1,5 +1,7 @@
 using developwithpassion.bdd.contexts;
 using Gorilla.Commons.Testing;
+using Gorilla.Commons.Utility.Core;
+using Gorilla.Commons.Utility.Extensions;
 
 namespace Gorilla.Commons.Windows.Forms.Helpers
 {
@@ -34,13 +36,19 @@ namespace Gorilla.Commons.Windows.Forms.Helpers
         static string result;
     }
 
-    //public class when_the_value_of_a_textbox_changes : concerns_for_text_box
-    //{
-    //    it should_apply_the_new_value_to_the_text_control = () => {
-                         
-    //    };
+    [Concern(typeof (BindableTextBox<>))]
+    public class when_an_action_needs_to_be_performed_when_the_value_of_a_textbox_changes : concerns_for_text_box
+    {
+        it should_perform_that_action = () => action.was_told_to(x => x.run());
+
+        context c = () => { action = an<ICommand>(); };
 
-    //    because b = () => {
-    //    };
-    //}
+        because b = () =>
+                        {
+                            sut.downcast_to<BindableTextBox<string>>().on_leave(x => action.run());
+                            control.when_text_is_changed();
+                        };
+
+        static ICommand action;
+    }
 }
\ No newline at end of file
product/Gorilla.Commons.Windows.Forms/Helpers/ITextControl.cs
@@ -1,3 +1,5 @@
+using System;
+
 namespace Gorilla.Commons.Windows.Forms.Helpers
 {
     public interface ITextControl<ItemToStore>
@@ -5,5 +7,6 @@ namespace Gorilla.Commons.Windows.Forms.Helpers
         void set_selected_item(ItemToStore item);
         ItemToStore get_selected_item();
         string text();
+        Action when_text_is_changed { get; set; }
     }
 }
\ No newline at end of file
product/Gorilla.Commons.Windows.Forms/Helpers/TextControl.cs
@@ -1,3 +1,4 @@
+using System;
 using System.Windows.Forms;
 
 namespace Gorilla.Commons.Windows.Forms.Helpers
@@ -10,6 +11,8 @@ namespace Gorilla.Commons.Windows.Forms.Helpers
         public TextControl(TextBox textbox)
         {
             this.textbox = textbox;
+            when_text_is_changed = () => { };
+            textbox.Leave += (sender, args) => when_text_is_changed();
         }
 
         public void set_selected_item(ItemToStore item)
@@ -27,5 +30,7 @@ namespace Gorilla.Commons.Windows.Forms.Helpers
         {
             return textbox.Text;
         }
+
+        public Action when_text_is_changed { get; set; }
     }
 }
\ No newline at end of file
product/Gorilla.Commons.Windows.Forms/Helpers/TextControlSpecs.cs
@@ -2,6 +2,7 @@ using System;
 using System.Windows.Forms;
 using developwithpassion.bdd.contexts;
 using Gorilla.Commons.Testing;
+using Gorilla.Commons.Utility.Core;
 
 namespace Gorilla.Commons.Windows.Forms.Helpers
 {
@@ -35,4 +36,28 @@ namespace Gorilla.Commons.Windows.Forms.Helpers
 
         static DateTime date;
     }
+
+    [Concern(typeof (TextControl<>))]
+    public class when_the_text_changes_on_a_text_control_and_action_is_specified : behaves_like_text_control
+    {
+        it should_invoke_the_action_bound_to_it = () => action.was_told_to(x => x.run());
+
+        context c = () => { action = an<ICommand>(); };
+
+        because b = () =>
+                        {
+                            sut.when_text_is_changed = () => action.run();
+                            textbox.control_is(x => x.OnLeave(new EventArgs()));
+                        };
+
+        static ICommand action;
+    }
+
+    [Concern(typeof (TextControl<>))]
+    public class when_the_text_changes_on_a_text_control_and_action_is_not_specified : behaves_like_text_control
+    {
+        it should_not_blow_up = () => { };
+
+        because b = () => textbox.control_is(x => x.OnLeave(new EventArgs()));
+    }
 }
\ No newline at end of file
product/Gorilla.Commons.Windows.Forms/Gorilla.Commons.Windows.Forms.csproj
@@ -81,6 +81,7 @@
     <Compile Include="Helpers\BindableListExtensions.cs" />
     <Compile Include="Helpers\BindableListFactory.cs" />
     <Compile Include="Helpers\BindableTextBox.cs" />
+    <Compile Include="Helpers\BindableTextBoxFactory.cs" />
     <Compile Include="Helpers\BindableTextBoxSpecs.cs" />
     <Compile Include="Helpers\BitmapRegion.cs" />
     <Compile Include="Helpers\ButtonExtensions.cs" />