Commit c139429

mokhan <mokhan@ce5e1baf-6525-42e4-a1b2-857ea38da20a>
2009-04-14 03:25:05
trying to figure out how to cache presenters using the application controller.
git-svn-id: https://svn.xp-dev.com/svn/mokhan-mo.money@156 ce5e1baf-6525-42e4-a1b2-857ea38da20a
1 parent d2e1021
trunk/product/MyMoney/boot/global_error_handling.cs
@@ -20,7 +20,7 @@ namespace MoMoney.boot
         void handle(Exception e)
         {
             e.add_to_log();
-            resolve.dependency_for<IEventAggregator>().publish(new unhandled_error_occurred(e));
+            resolve.dependency_for<IEventAggregator>().publish(new UnhandledErrorOccurred(e));
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/boot/WindowsFormsApplication.cs
@@ -59,7 +59,7 @@ namespace MoMoney.boot
             catch (Exception e)
             {
                 this.log().error(e);
-                resolve.dependency_for<IEventAggregator>().publish(new unhandled_error_occurred(e));
+                resolve.dependency_for<IEventAggregator>().publish(new UnhandledErrorOccurred(e));
             }
         }
     }
trunk/product/MyMoney/Presentation/Core/ApplicationController.cs
@@ -17,6 +17,7 @@ namespace MoMoney.Presentation.Core
         readonly IPresenterRegistry registered_presenters;
         readonly IShell shell;
         //readonly IDictionary<Type, IPresenter> open_presenters;
+        //readonly object mutex = new object();
 
         public ApplicationController(IPresenterRegistry registered_presenters, IShell shell)
         {
@@ -27,25 +28,54 @@ namespace MoMoney.Presentation.Core
 
         public void run<Presenter>() where Presenter : IPresenter
         {
-            //if (open_presenters.ContainsKey(typeof(Presenter)))
+            //if (open_presenters.ContainsKey(typeof (Presenter)))
             //{
-            //    this.log().debug("from cached presenter: {0}", typeof(Presenter));
-            //    run(open_presenters[typeof(Presenter)]);
+            //    this.log().debug("from cached presenter: {0}", typeof (Presenter));
+            //    run(open_presenters[typeof (Presenter)]);
             //}
             //else
             //{
-                //this.log().debug("adding presenter to cache: {0}", typeof (Presenter));
-                var presenter = registered_presenters.find_an_implementation_of<IPresenter, Presenter>();
-                //open_presenters.Add(typeof (Presenter), presenter);
-                run(presenter);
+            //    if (typeof (IContentPresenter).IsAssignableFrom(typeof (Presenter)))
+            //    {
+            //        this.log().debug("adding presenter to cache: {0}", typeof (Presenter));
+            //        var presenter = registered_presenters.find_an_implementation_of<IPresenter, Presenter>();
+            //        within_lock(() => open_presenters.Add(typeof (Presenter), presenter));
+            //        run(presenter);
+            //    }
+            //    else
+            //    {
+            //        this.log().debug("running uncached presenter: {0}", typeof (Presenter));
+            run(registered_presenters.find_an_implementation_of<IPresenter, Presenter>());
+            //}
             //}
         }
 
+        //void within_lock(Action action)
+        //{
+        //    lock (mutex) action();
+        //}
+
         public void run(IPresenter presenter)
         {
             presenter.run();
             if (presenter.is_an_implementation_of<IContentPresenter>())
-                shell.add(presenter.downcast_to<IContentPresenter>().View);
+            {
+                var content_presenter = presenter.downcast_to<IContentPresenter>();
+                var view = content_presenter.View;
+
+                view.on_activated = x => content_presenter.activate();
+                view.on_deactivate = x => content_presenter.deactivate();
+                view.on_closing = x => x.Cancel = !content_presenter.can_close();
+                //view.on_closed = x => remove(presenter);
+
+                shell.add(view);
+            }
         }
+
+        //void remove<Presenter>(Presenter presenter) where Presenter : IPresenter
+        //{
+        //    this.log().debug("removing {0}", presenter);
+        //    within_lock(() => open_presenters.Remove(typeof (Presenter)));
+        //}
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Presentation/Core/ContentPresenter.cs
@@ -1,3 +1,4 @@
+using MoMoney.Infrastructure.Extensions;
 using MoMoney.Presentation.Views.core;
 
 namespace MoMoney.Presentation.Core
@@ -11,11 +12,26 @@ namespace MoMoney.Presentation.Core
             this.view = view;
         }
 
-        public abstract void run();
-
         IDockedContentView IContentPresenter.View
         {
             get { return view; }
         }
+
+        public abstract void run();
+
+        public virtual void activate()
+        {
+            this.log().debug("activated: {0}", this);
+        }
+
+        public virtual void deactivate()
+        {
+            this.log().debug("deactivated: {0}", this);
+        }
+
+        public virtual bool can_close()
+        {
+            return true;
+        }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Presentation/Core/IContentPresenter.cs
@@ -5,5 +5,8 @@ namespace MoMoney.Presentation.Core
     public interface IContentPresenter : IPresenter
     {
         IDockedContentView View { get; }
+        void activate();
+        void deactivate();
+        bool can_close();
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Presentation/Model/messages/unhandled_error_occurred.cs → trunk/product/MyMoney/Presentation/Model/messages/UnhandledErrorOccurred.cs
@@ -3,9 +3,9 @@ using MoMoney.Infrastructure.eventing;
 
 namespace MoMoney.Presentation.Model.messages
 {
-    public class unhandled_error_occurred : IEvent
+    public class UnhandledErrorOccurred : IEvent
     {
-        public unhandled_error_occurred(Exception error)
+        public UnhandledErrorOccurred(Exception error)
         {
             this.error = error;
         }
trunk/product/MyMoney/Presentation/Presenters/Shell/UnhandledErrorPresenter.cs
@@ -8,7 +8,7 @@ using MoMoney.Presentation.Views.Shell;
 namespace MoMoney.Presentation.Presenters.Shell
 {
     public interface IUnhandledErrorPresenter : IModule, IPresenter,
-                                                IEventSubscriber<unhandled_error_occurred>
+                                                IEventSubscriber<UnhandledErrorOccurred>
     {
         void restart_application();
     }
@@ -32,7 +32,7 @@ namespace MoMoney.Presentation.Presenters.Shell
             broker.subscribe_to(this);
         }
 
-        public void notify(unhandled_error_occurred message)
+        public void notify(UnhandledErrorOccurred message)
         {
             view.display(message.error);
         }
trunk/product/MyMoney/Presentation/Presenters/Shell/UnhandledErrorPresenterSpecs.cs
@@ -37,7 +37,7 @@ namespace MoMoney.Presentation.Presenters.Shell
     {
         it should_display_the_error = () => view.was_told_to(x => x.display(error));
 
-        because b = () => sut.notify(new unhandled_error_occurred(error));
+        because b = () => sut.notify(new UnhandledErrorOccurred(error));
 
         static readonly Exception error = new Exception();
     }
trunk/product/MyMoney/Presentation/Views/billing/AddBillPaymentView.cs
@@ -14,11 +14,14 @@ namespace MoMoney.Presentation.Views.billing
     {
         ControlAction<EventArgs> submit_clicked = x => { };
 
+         ControlAction<EventArgs> activated = x => { };
+
         public AddBillPaymentView()
         {
             InitializeComponent();
             titled("Add Bill Payment");
             ux_submit_button.Click += (sender, e) => submit_clicked(e);
+            Activated += (sender, args) => activated(args);
         }
 
         public void attach_to(IAddBillPaymentPresenter presenter)
trunk/product/MyMoney/Presentation/Views/core/ApplicationDockedWindow.cs
@@ -1,14 +1,16 @@
 using System;
+using System.ComponentModel;
 using System.Linq;
 using System.Windows.Forms;
 using MoMoney.Presentation.Resources;
 using MoMoney.Presentation.Views.helpers;
+using MoMoney.Presentation.Views.updates;
 using MoMoney.Utility.Extensions;
 using WeifenLuo.WinFormsUI.Docking;
 
 namespace MoMoney.Presentation.Views.core
 {
-    public interface IApplicationDockedWindow : IDockedContentView
+    public interface IApplicationDockedWindow : IWindowEvents, IDockedContentView
     {
         IApplicationDockedWindow create_tool_tip_for(string title, string caption, Control control);
         IApplicationDockedWindow titled(string title, params object[] arguments);
@@ -27,8 +29,42 @@ namespace MoMoney.Presentation.Views.core
             Icon = ApplicationIcons.Application;
             dock_state = DockState.Document;
             HideOnClose = true;
+
+            on_activated = x => { };
+            on_deactivate = x => { };
+            on_closed = x => { };
+            on_closing = x => { };
+        }
+
+        protected override void OnActivated(EventArgs e)
+        {
+            base.OnActivated(e);
+            on_activated(e);
+        }
+
+        protected override void OnDeactivate(EventArgs e)
+        {
+            base.OnDeactivate(e);
+            on_deactivate(e);
         }
 
+        protected override void OnClosing(CancelEventArgs e)
+        {
+            base.OnClosing(e);
+            on_closing(e);
+        }
+
+        protected override void OnClosed(EventArgs e)
+        {
+            base.OnClosed(e);
+            on_closed(e);
+        }
+
+        public ControlAction<EventArgs> on_activated { get; set; }
+        public ControlAction<EventArgs> on_deactivate { get; set; }
+        public ControlAction<EventArgs> on_closed { get; set; }
+        public ControlAction<CancelEventArgs> on_closing { get; set; }
+
         public IApplicationDockedWindow create_tool_tip_for(string title, string caption, Control control)
         {
             var tip = new ToolTip {IsBalloon = true, ToolTipTitle = title};
trunk/product/MyMoney/Presentation/Views/core/ApplicationWindow.cs
@@ -1,6 +1,9 @@
 using System;
+using System.ComponentModel;
 using System.Windows.Forms;
+using MoMoney.Infrastructure.Extensions;
 using MoMoney.Presentation.Resources;
+using MoMoney.Presentation.Views.updates;
 
 namespace MoMoney.Presentation.Views.core
 {
@@ -19,8 +22,18 @@ namespace MoMoney.Presentation.Views.core
             InitializeComponent();
             Icon = ApplicationIcons.Application;
             //this.log().debug("created {0}", GetType());
+
+            on_activated = x => { };
+            on_deactivate = x => { };
+            on_closed = x => { };
+            on_closing = x => { };
         }
 
+        public ControlAction<EventArgs> on_activated { get; set; }
+        public ControlAction<EventArgs> on_deactivate { get; set; }
+        public ControlAction<EventArgs> on_closed { get; set; }
+        public ControlAction<CancelEventArgs> on_closing { get; set; }
+
         public IApplicationWindow create_tool_tip_for(string title, string caption, Control control)
         {
             var tip = new ToolTip {IsBalloon = true, ToolTipTitle = title};
@@ -50,6 +63,34 @@ namespace MoMoney.Presentation.Views.core
             return this;
         }
 
+        protected override void OnActivated(EventArgs e)
+        {
+            base.OnActivated(e);
+            this.log().debug("activated: {0}", this);
+            on_activated(e);
+        }
+
+        protected override void OnDeactivate(EventArgs e)
+        {
+            base.OnDeactivate(e);
+            this.log().debug("deactivated: {0}", this);
+            on_deactivate(e);
+        }
+
+        protected override void OnClosing(CancelEventArgs e)
+        {
+            base.OnClosing(e);
+            this.log().debug("closing: {0}", this);
+            on_closing(e);
+        }
+
+        protected override void OnClosed(EventArgs e)
+        {
+            base.OnClosed(e);
+            this.log().debug("closed: {0}", this);
+            on_closed(e);
+        }
+
         Control adapt(IDisposable item)
         {
             return new ControlAdapter(item);
trunk/product/MyMoney/Presentation/Views/core/IView.cs
@@ -4,7 +4,7 @@ using MoMoney.Presentation.Core;
 
 namespace MoMoney.Presentation.Views.core
 {
-    public interface IView : ISynchronizeInvoke, IDisposable
+    public interface IView : IWindowEvents, ISynchronizeInvoke, IDisposable
     {
     }
 
trunk/product/MyMoney/Presentation/Views/core/IWindowEvents.cs
@@ -0,0 +1,14 @@
+using System;
+using System.ComponentModel;
+using MoMoney.Presentation.Views.updates;
+
+namespace MoMoney.Presentation.Views.core
+{
+    public interface IWindowEvents
+    {
+        ControlAction<EventArgs> on_activated { get; set; }
+        ControlAction<EventArgs> on_deactivate { get; set; }
+        ControlAction<EventArgs> on_closed { get; set; }
+        ControlAction<CancelEventArgs> on_closing { get; set; }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/Presentation/Views/Shell/UnhandledErrorView.cs
@@ -11,8 +11,9 @@ namespace MoMoney.Presentation.Views.Shell
     {
         ControlAction<EventArgs> close_action = x => { };
         ControlAction<EventArgs> restart_action = x => { };
+        readonly IWin32Window window;
 
-        public UnhandledErrorView()
+        public UnhandledErrorView(IWin32Window window)
         {
             InitializeComponent();
             ux_image.Image = ApplicationImages.Splash;
@@ -24,6 +25,7 @@ namespace MoMoney.Presentation.Views.Shell
 
             close_button.Click += (sender, args) => close_action(args);
             restart_button.Click += (sender, args) => restart_action(args);
+            this.window = window;
         }
 
         public void attach_to(IUnhandledErrorPresenter presenter)
@@ -35,7 +37,7 @@ namespace MoMoney.Presentation.Views.Shell
         public void display(Exception exception)
         {
             ux_message.Text = exception.ToString();
-            ShowDialog();
+            ShowDialog(window);
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Tasks/application/RegisterNewCompanyCommand.cs
@@ -19,9 +19,7 @@ namespace MoMoney.Tasks.application
 
         public void run(RegisterNewCompany item)
         {
-            factory
-                .create()
-                .change_name_to(item.company_name);
+            factory.create().change_name_to(item.company_name);
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/MyMoney.csproj
@@ -365,7 +365,7 @@
     <Compile Include="Presentation\Model\Menu\window\WindowMenu.cs" />
     <Compile Include="Presentation\Model\messages\closing_project_event.cs" />
     <Compile Include="Presentation\Model\messages\closing_the_application.cs" />
-    <Compile Include="Presentation\Model\messages\unhandled_error_occurred.cs" />
+    <Compile Include="Presentation\Model\messages\UnhandledErrorOccurred.cs" />
     <Compile Include="Presentation\Model\Navigation\branches\add_bill_payment_branch.cs" />
     <Compile Include="Presentation\Model\Navigation\branches\AddNewIncomeBranch.cs" />
     <Compile Include="Presentation\Model\Navigation\branches\view_all_bills_branch.cs" />
@@ -494,6 +494,7 @@
       <DependentUpon>ApplicationWindow.cs</DependentUpon>
     </Compile>
     <Compile Include="Presentation\Views\core\ICommandDialog.cs" />
+    <Compile Include="Presentation\Views\core\IWindowEvents.cs" />
     <Compile Include="Presentation\Views\dialogs\ISaveChangesView.cs" />
     <Compile Include="Presentation\Views\dialogs\SaveChangesView.cs">
       <SubType>Form</SubType>