Commit 7d3cf94

mokhan <mokhan@ce5e1baf-6525-42e4-a1b2-857ea38da20a>
2009-03-28 14:32:33
refactoring the menus.
git-svn-id: https://svn.xp-dev.com/svn/mokhan-mo.money@119 ce5e1baf-6525-42e4-a1b2-857ea38da20a
1 parent 729fec3
trunk/product/MyMoney/Infrastructure/eventing/EventAggregator.cs
@@ -1,6 +1,8 @@
 using System;
 using System.Collections.Generic;
+using System.Linq.Expressions;
 using System.Threading;
+using MoMoney.Infrastructure.Extensions;
 using MoMoney.Utility.Extensions;
 
 namespace MoMoney.Infrastructure.eventing
@@ -10,19 +12,19 @@ namespace MoMoney.Infrastructure.eventing
         void subscribe_to<Event>(IEventSubscriber<Event> subscriber) where Event : IEvent;
         void subscribe<Listener>(Listener subscriber) where Listener : class;
         void publish<Event>(Event the_event_to_broadcast) where Event : IEvent;
-        void publish<T>(Action<T> call) where T : class;
+        void publish<T>(Expression<Action<T>> call) where T : class;
         void publish<Event>() where Event : IEvent, new();
     }
 
     public class EventAggregator : IEventAggregator
     {
         readonly SynchronizationContext context;
-        readonly HashSet<object> subscribers;
+        readonly List<object> subscribers;
         readonly object mutex;
 
         public EventAggregator(SynchronizationContext context)
         {
-            subscribers = new HashSet<object>();
+            subscribers = new List<object>();
             mutex = new object();
             this.context = context;
         }
@@ -42,9 +44,10 @@ namespace MoMoney.Infrastructure.eventing
             process(() => subscribers.call_on_each<IEventSubscriber<Event>>(x => x.notify(the_event_to_broadcast)));
         }
 
-        public void publish<T>(Action<T> call) where T : class
+        public void publish<T>(Expression<Action<T>> call) where T : class
         {
-            process(() => subscribers.each(x => x.call_on(call)));
+            this.log().debug("publishing: {0}", call);
+            process(() => subscribers.each(x => x.call_on(call.Compile())));
         }
 
         public void publish<Event>() where Event : IEvent, new()
@@ -59,7 +62,7 @@ namespace MoMoney.Infrastructure.eventing
 
         void process(Action action)
         {
-            context.Send(x => action(), null);
+            context.Send(x => action(), new object());
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Presentation/Model/Menu/File/FileMenu.cs
@@ -3,7 +3,6 @@ using MoMoney.Presentation.Model.keyboard;
 using MoMoney.Presentation.Model.Menu.File.Commands;
 using MoMoney.Presentation.Model.Projects;
 using MoMoney.Presentation.Resources;
-using MoMoney.Presentation.Views.Menu.Mappers;
 
 namespace MoMoney.Presentation.Model.Menu.File
 {
@@ -15,7 +14,7 @@ namespace MoMoney.Presentation.Model.Menu.File
     {
         readonly IProject project;
 
-        public FileMenu(IProject project, ISubMenuToToolStripMenuItemMapper mapper) : base(mapper)
+        public FileMenu(IProject project)
         {
             this.project = project;
         }
trunk/product/MyMoney/Presentation/Model/Menu/File/SubMenu.cs
@@ -1,25 +1,22 @@
 using System.Collections.Generic;
 using System.Windows.Forms;
-using MoMoney.Presentation.Views.Menu.Mappers;
+using MoMoney.Utility.Extensions;
 
 namespace MoMoney.Presentation.Model.Menu.File
 {
     public abstract class SubMenu : ISubMenu
     {
-        readonly ISubMenuToToolStripMenuItemMapper mapper;
-
-        protected SubMenu(ISubMenuToToolStripMenuItemMapper mapper)
-        {
-            this.mapper = mapper;
-        }
-
         public abstract string name { get; }
 
         public abstract IEnumerable<IMenuItem> all_menu_items();
 
         public void add_to(MenuStrip strip)
         {
-            strip.Items.Add(mapper.map_from(this));
+            strip.SuspendLayout();
+            var menu_item = new ToolStripMenuItem(name);
+            strip.Items.Add(menu_item);
+            all_menu_items().each(x => menu_item.DropDownItems.Add(x.build()));
+            strip.ResumeLayout();
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Presentation/Model/Menu/Help/HelpMenu.cs
@@ -5,7 +5,6 @@ using MoMoney.Presentation.Presenters.Commands;
 using MoMoney.Presentation.Presenters.Shell;
 using MoMoney.Presentation.Presenters.updates;
 using MoMoney.Presentation.Resources;
-using MoMoney.Presentation.Views.Menu.Mappers;
 
 namespace MoMoney.Presentation.Model.Menu.Help
 {
@@ -17,7 +16,7 @@ namespace MoMoney.Presentation.Model.Menu.Help
     {
         readonly IRunPresenterCommand command;
 
-        public HelpMenu(IRunPresenterCommand command, ISubMenuToToolStripMenuItemMapper mapper) : base(mapper)
+        public HelpMenu(IRunPresenterCommand command)
         {
             this.command = command;
         }
trunk/product/MyMoney/Presentation/Model/Menu/window/WindowMenu.cs
@@ -1,7 +1,6 @@
 using System.Collections.Generic;
 using MoMoney.Presentation.Model.Menu.File;
 using MoMoney.Presentation.Model.Menu.File.Commands;
-using MoMoney.Presentation.Views.Menu.Mappers;
 
 namespace MoMoney.Presentation.Model.Menu.window
 {
@@ -11,10 +10,6 @@ namespace MoMoney.Presentation.Model.Menu.window
 
     public class WindowMenu : SubMenu, IWindowMenu
     {
-        public WindowMenu(ISubMenuToToolStripMenuItemMapper mapper) : base(mapper)
-        {
-        }
-
         public override string name
         {
             get { return "&Window"; }
trunk/product/MyMoney/Presentation/Model/Menu/create.cs
@@ -1,4 +1,5 @@
 using MoMoney.Infrastructure.Container;
+using MoMoney.Infrastructure.eventing;
 
 namespace MoMoney.Presentation.Model.Menu
 {
@@ -16,7 +17,7 @@ namespace MoMoney.Presentation.Model.Menu
 
         public static IToolbarItemBuilder a_tool_bar_item()
         {
-            return new ToolBarItemBuilder(resolve.dependency_for<IDependencyRegistry>());
+            return new ToolBarItemBuilder(resolve.dependency_for<IDependencyRegistry>(),resolve.dependency_for<IEventAggregator>());
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Presentation/Model/Menu/IToolbarButton.cs
@@ -5,5 +5,6 @@ namespace MoMoney.Presentation.Model.Menu
     public interface IToolbarButton
     {
         void add_to(ToolStrip collection);
+        void refresh();
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Presentation/Model/Menu/MenuItem.cs
@@ -13,19 +13,19 @@ namespace MoMoney.Presentation.Model.Menu
 
     public class MenuItem : IMenuItem
     {
-        public MenuItem(string name, Action command, HybridIcon underlying_icon, ShortcutKey key,
+        public MenuItem(string name, Action command, HybridIcon icon, ShortcutKey key,
                         Func<bool> can_be_clicked)
         {
             this.name = name;
             this.command = command;
-            this.underlying_icon = underlying_icon;
+            this.icon = icon;
             this.key = key;
             this.can_be_clicked = can_be_clicked;
         }
 
         string name { get; set; }
         Action command { get; set; }
-        HybridIcon underlying_icon { get; set; }
+        HybridIcon icon { get; set; }
         ShortcutKey key { get; set; }
         readonly Func<bool> can_be_clicked;
 
@@ -33,7 +33,7 @@ namespace MoMoney.Presentation.Model.Menu
         {
             var item = new ToolStripMenuItem(name)
                            {
-                               Image = underlying_icon,
+                               Image = icon,
                                ShortcutKeys = key,
                                Enabled = can_be_clicked()
                            };
@@ -45,6 +45,8 @@ namespace MoMoney.Presentation.Model.Menu
         {
             var item = new System.Windows.Forms.MenuItem(name) {ShowShortcut = true, Enabled = can_be_clicked()};
             item.Click += (sender, e) => command();
+            //item.Popup += (o,e)=> {}
+            item.DrawItem += (sender, args) => item.Enabled = can_be_clicked();
             return item;
         }
     }
trunk/product/MyMoney/Presentation/Model/Menu/ToolBarItemBuilder.cs
@@ -1,6 +1,7 @@
 using System;
 using System.Windows.Forms;
 using MoMoney.Infrastructure.Container;
+using MoMoney.Infrastructure.eventing;
 using MoMoney.Presentation.Resources;
 using MoMoney.Utility.Core;
 
@@ -9,41 +10,39 @@ namespace MoMoney.Presentation.Model.Menu
     public class ToolBarItemBuilder : IToolbarItemBuilder, IToolbarButton
     {
         readonly IDependencyRegistry registry;
-        string tooltip_text;
-        ICommand the_command;
-        HybridIcon icon_to_display;
         Func<bool> the_condition;
+        readonly ToolStripButton item;
 
-        public ToolBarItemBuilder(IDependencyRegistry registry)
+        public ToolBarItemBuilder(IDependencyRegistry registry, IEventAggregator aggregator)
         {
             this.registry = registry;
-            tooltip_text = "";
-            the_command = new EmptyCommand();
-            icon_to_display = ApplicationIcons.Empty;
+            aggregator.subscribe(this);
             the_condition = () => true;
+            item = new ToolStripButton {};
         }
 
         public IToolbarItemBuilder with_tool_tip_text_as(string text)
         {
-            tooltip_text = text;
+            item.Text = text;
             return this;
         }
 
         public IToolbarItemBuilder when_clicked_executes<Command>() where Command : ICommand
         {
-            the_command = registry.get_a<Command>();
+            item.Click += (sender, args) => registry.get_a<Command>();
             return this;
         }
 
         public IToolbarItemBuilder displays_icon(HybridIcon icon)
         {
-            icon_to_display = icon;
+            item.Image = icon;
             return this;
         }
 
         public IToolbarItemBuilder can_be_clicked_when(Func<bool> condition)
         {
             the_condition = condition;
+            item.Enabled = condition();
             return this;
         }
 
@@ -54,14 +53,12 @@ namespace MoMoney.Presentation.Model.Menu
 
         public void add_to(ToolStrip tool_strip)
         {
-            var item = new ToolStripButton
-                           {
-                               ToolTipText = tooltip_text,
-                               Image = icon_to_display,
-                               Enabled = the_condition()
-                           };
-            item.Click += (sender, args) => the_command.run();
             tool_strip.Items.Add(item);
         }
+
+        public void refresh()
+        {
+            item.Enabled = the_condition();
+        }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Presentation/Presenters/Commands/RunPresenterCommand.cs
@@ -11,9 +11,9 @@ namespace MoMoney.Presentation.Presenters.Commands
     {
         private readonly IApplicationController application_controller;
 
-        public RunPresenterCommand(IApplicationController applicationController)
+        public RunPresenterCommand(IApplicationController application_controller)
         {
-            application_controller = applicationController;
+            this.application_controller = application_controller;
         }
 
         public void run<Presenter>() where Presenter : IPresenter
trunk/product/MyMoney/Presentation/Presenters/Menu/ApplicationMenuModule.cs
@@ -1,4 +1,5 @@
 using MoMoney.Infrastructure.eventing;
+using MoMoney.Infrastructure.Extensions;
 using MoMoney.Presentation.Core;
 using MoMoney.Presentation.Model.messages;
 using MoMoney.Presentation.Presenters.Commands;
@@ -26,32 +27,29 @@ namespace MoMoney.Presentation.Presenters.Menu
 
         public void run()
         {
-            //broker.subscribe_to<newprojectopened>(this);
-            //broker.subscribe_to<ClosingProjectEvent>(this);
-            //broker.subscribe_to<SavedChangesEvent>(this);
-            //broker.subscribe_to<UnsavedChangesEvent>(this);
+            this.log().debug("hooking up the main menu");
             broker.subscribe(this);
             command.run<IApplicationMenuPresenter>();
         }
 
         public void notify(NewProjectOpened message)
         {
-            command.run<IApplicationMenuPresenter>();
+            //command.run<IApplicationMenuPresenter>();
         }
 
         public void notify(ClosingProjectEvent message)
         {
-            command.run<IApplicationMenuPresenter>();
+            //command.run<IApplicationMenuPresenter>();
         }
 
         public void notify(SavedChangesEvent message)
         {
-            command.run<IApplicationMenuPresenter>();
+            //command.run<IApplicationMenuPresenter>();
         }
 
         public void notify(UnsavedChangesEvent message)
         {
-            command.run<IApplicationMenuPresenter>();
+            //command.run<IApplicationMenuPresenter>();
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Presentation/Presenters/Shell/ToolbarModule.cs
@@ -1,5 +1,6 @@
 using MoMoney.Infrastructure.eventing;
 using MoMoney.Presentation.Core;
+using MoMoney.Presentation.Model.Menu;
 using MoMoney.Presentation.Model.messages;
 using MoMoney.Presentation.Presenters.Commands;
 
@@ -10,7 +11,6 @@ namespace MoMoney.Presentation.Presenters.Shell
                                       IEventSubscriber<ClosingProjectEvent>,
                                       IEventSubscriber<SavedChangesEvent>,
                                       IEventSubscriber<UnsavedChangesEvent>
-
     {
     }
 
@@ -33,22 +33,22 @@ namespace MoMoney.Presentation.Presenters.Shell
 
         public void notify(NewProjectOpened message)
         {
-            command.run<IToolbarPresenter>();
+            broker.publish<IToolbarButton>(x => x.refresh());
         }
 
         public void notify(ClosingProjectEvent message)
         {
-            command.run<IToolbarPresenter>();
+            broker.publish<IToolbarButton>(x => x.refresh());
         }
 
         public void notify(SavedChangesEvent message)
         {
-            command.run<IToolbarPresenter>();
+            broker.publish<IToolbarButton>(x => x.refresh());
         }
 
         public void notify(UnsavedChangesEvent message)
         {
-            command.run<IToolbarPresenter>();
+            broker.publish<IToolbarButton>(x => x.refresh());
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Presentation/Presenters/Shell/ToolBarPresenter.cs
@@ -27,11 +27,7 @@ namespace MoMoney.Presentation.Presenters.Shell
 
         public void run()
         {
-            shell.region<ToolStrip>(x =>
-                                        {
-                                            x.Items.Clear();
-                                            buttons().each(y => y.add_to(x));
-                                        });
+            shell.region<ToolStrip>(x => buttons().each(y => y.add_to(x)));
         }
 
         IEnumerable<IToolbarButton> buttons()
trunk/product/MyMoney/Presentation/Views/Menu/Mappers/SubMenuToToolStripMenuItemMapper.cs
@@ -5,17 +5,17 @@ using MoMoney.Utility.Extensions;
 
 namespace MoMoney.Presentation.Views.Menu.Mappers
 {
-    public interface ISubMenuToToolStripMenuItemMapper : IMapper<ISubMenu, ToolStripMenuItem>
-    {
-    }
+    //public interface ISubMenuToToolStripMenuItemMapper : IMapper<ISubMenu, ToolStripMenuItem>
+    //{
+    //}
 
-    public class SubMenuToToolStripMenuItemMapper : ISubMenuToToolStripMenuItemMapper
-    {
-        public ToolStripMenuItem map_from(ISubMenu item)
-        {
-            var tool_strip_menu_item = new ToolStripMenuItem(item.name);
-            item.all_menu_items().each(x => tool_strip_menu_item.DropDownItems.Add(x.build()));
-            return tool_strip_menu_item;
-        }
-    }
+    //public class SubMenuToToolStripMenuItemMapper : ISubMenuToToolStripMenuItemMapper
+    //{
+    //    public ToolStripMenuItem map_from(ISubMenu item)
+    //    {
+    //        var menu_item = new ToolStripMenuItem(item.name);
+    //        item.all_menu_items().each(x => menu_item.DropDownItems.Add(x.build()));
+    //        return menu_item;
+    //    }
+    //}
 }
\ No newline at end of file
trunk/product/MyMoney/Utility/Extensions/ConversionExtensions.cs
@@ -26,18 +26,12 @@ namespace MoMoney.Utility.Extensions
 
         public static void call_on<T>(this object target, Action<T> action) where T : class
         {
-            if (target as T != null)
-            {
-                action(target as T);
-            }
+            if (target as T != null) action(target as T);
         }
 
         public static void call_on_each<T>(this IEnumerable items, Action<T> action) where T : class
         {
-            foreach (var item in items)
-            {
-                item.call_on(action);
-            }
+            foreach (var item in items) item.call_on(action);
         }
     }
 }
\ No newline at end of file