main
 1using System;
 2using System.ComponentModel;
 3using System.Windows.Forms;
 4using MoMoney.Presentation.Model.Menu.File;
 5using momoney.presentation.views;
 6using MoMoney.Presentation.Winforms.Resources;
 7
 8namespace MoMoney.Presentation.Winforms.Views
 9{
10    public partial class SaveChangesView : ApplicationWindow, ISaveChangesView
11    {
12        readonly IWin32Window window;
13        bool can_be_closed;
14        ControlAction<EventArgs> save_action = x => { };
15        ControlAction<EventArgs> do_not_save_action = x => { };
16        ControlAction<EventArgs> cancel_action = x => { };
17        ControlAction<CancelEventArgs> closing_action = x => { };
18
19        public SaveChangesView(IWin32Window window)
20        {
21            this.window = window;
22            InitializeComponent();
23            ux_image.Image = ApplicationImages.Splash;
24            ux_image.SizeMode = PictureBoxSizeMode.StretchImage;
25
26            titled("Unsaved Changes")
27                .create_tool_tip_for("Save", "Save the document, and then close it.", save_button)
28                .create_tool_tip_for("Don't Save", "Discard any unsaved changes.", do_not_save_button)
29                .create_tool_tip_for("Cancel", "Go back.", cancel_button);
30
31            save_button.Click += (sender, e) => save_action(e);
32            do_not_save_button.Click += (sender, e) => do_not_save_action(e);
33            cancel_button.Click += (sender, e) => cancel_action(e);
34            Closing += (sender, e) => closing_action(e);
35        }
36
37        public void attach_to(ISaveChangesPresenter presenter)
38        {
39            can_be_closed = false;
40            save_action = x => { execute(presenter.save); };
41            do_not_save_action = x => { execute(presenter.dont_save); };
42            cancel_action = x => { execute(presenter.cancel); };
43            closing_action = x => { if (!can_be_closed) presenter.cancel(); };
44        }
45
46        public void prompt_user_to_save()
47        {
48            ShowDialog(window);
49        }
50
51        void execute(Action action)
52        {
53            can_be_closed = true;
54            Hide();
55            Close();
56            action();
57        }
58    }
59}