Click here to Skip to main content
11,438,007 members (44,591 online)
Click here to Skip to main content
Add your own
alternative version

GoalBook - A Hybrid Smart Client

, 25 Sep 2009 CPOL
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
GoalBook.zip
GoalBook
Dependencies
CAG WPF 2
Microsoft.Practices.Composite.dll
Microsoft.Practices.Composite.Presentation.dll
Microsoft.Practices.Composite.UnityExtensions.dll
Microsoft.Practices.ObjectBuilder2.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
vssver2.scc
CSLA
Csla.dll
Csla.XmlSerializers.dll
vssver2.scc
Infragistics
Infragistics3.Wpf.DataPresenter.v9.1.Express.dll
Infragistics3.Wpf.Editors.v9.1.Express.dll
Infragistics3.Wpf.v9.1.Express.dll
vssver2.scc
WPFToolkit
vssver2.scc
WPFToolkit.dll
GoalBook.Controls
CheckedList
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Goals
GoalBook.Goals.csproj.user
Properties
vssver2.scc
Views
vssver2.scc
vssver2.scc
GoalBook.Infrastructure
Comparers
vssver2.scc
Constants
vssver2.scc
Controls
Converters
vssver2.scc
Enums
vssver2.scc
Events
vssver2.scc
GoalBook.Infrastructure.csproj.user
Helpers
vssver2.scc
Interfaces
vssver2.scc
ObjectModel
vssver2.scc
Printing
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Notes
GoalBook.Notes.csproj.user
Properties
vssver2.scc
Resources
arrow_redo.png
arrow_undo.png
cut.png
page_copy.png
paste_plain.png
text_bold.png
text_indent.png
text_indent_remove.png
text_italic.png
text_list_bullets.png
text_list_numbers.png
vssver2.scc
world_link.png
Views
vssver2.scc
vssver2.scc
GoalBook.Public
Encryption
vssver2.scc
HtmlConverter
vssver2.scc
HtmlParser
vssver2.scc
Misc
vssver2.scc
Properties
vssver2.scc
vssver2.scc
GoalBook.Shell
App.ico
Commands
vssver2.scc
GoalBook.Shell.csproj.user
Journal.ico
Journal48.ico
Misc
vssver2.scc
Modules
vssver2.scc
Properties
licenses.licx
vssver2.scc
Resources
arrow_refresh.png
arrow_undo.png
checked.gif
cross.png
disk.png
email.png
erase.png
error.png
exclamation.png
flag_red.png
group.png
help.png
information.png
magnifier.png
page_red.png
pencil.png
printer.png
user.png
vssver2.scc
Wave.jpg
world.png
world_link.png
Services
vssver2.scc
Splash.png
Views
vssver2.scc
vssver2.scc
Windows
vssver2.scc
GoalBook.snk
GoalBook.Synchronisation
Events
vssver2.scc
Properties
vssver2.scc
ToodleDo
vssver2.scc
vssver2.scc
GoalBook.Tasks
Controls
vssver2.scc
Properties
vssver2.scc
Views
vssver2.scc
vssver2.scc
vssver2.scc
//===============================================================================
// Goal Book.
// Copyright © 2009 Mark Brownsword. 

// Credit to CompositeWPFContrib project for core functionality of Dialog window.
// http://www.codeplex.com/CompositeWPFContrib
//===============================================================================

#region Using Statements
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using GoalBook.Infrastructure;
using GoalBook.Infrastructure.Constants;
using GoalBook.Infrastructure.Enums;
using GoalBook.Infrastructure.Events;
using GoalBook.Infrastructure.Interfaces;
using GoalBook.Shell.Views;
#endregion

namespace GoalBook.Shell.Windows
{
    /// <summary>
    /// Provides a separate workspace for modal dialogs.
    /// </summary>
    public class DialogPresenter
    {
        private Dialog _dialogWindow;
                        
        /// <summary>
        /// ShowView.
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="view"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public bool? ShowView(Window owner, UserControl view, DialogOptions options)
        {
            // Initialise DialogWindow.
            _dialogWindow = new Dialog();
            _dialogWindow.View = view as DependencyObject;
            _dialogWindow.Owner = owner;

            // Apply the dialog options if provided
            if (options != null) { SetDialogOptions(_dialogWindow, options); }

            // Show dialog and return result.
            bool? result = _dialogWindow.ShowDialog();
            return _dialogWindow.IsCancelled ? null : result;
        }

        /// <summary>
        /// ShowView.
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="view"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public bool? ShowView(Window owner, UserControl view, DialogOptions options, Func<bool> validationAction)
        {
            // Initialise DialogWindow.
            _dialogWindow = new Dialog();
            _dialogWindow.View = view as DependencyObject;
            _dialogWindow.Owner = owner;

            //Initialise the ValidationAction.
            _dialogWindow.ValidateDialog += new EventHandler<CancelEventArgs>((sender, e) =>
            {
                e.Cancel = false;

                if (validationAction != null)
                {
                    // Validate action will return false when validation fails
                    // invert this result to cancel the close action.
                    e.Cancel = !validationAction();
                }
            });

            _dialogWindow.buttonAccept.IsEnabled = true;

            // Apply the dialog options if provided
            if (options != null) { SetDialogOptions(_dialogWindow, options); }

            // Show dialog and return result.
            bool? result = _dialogWindow.ShowDialog();
            return _dialogWindow.IsCancelled ? null : result;
        }
       
        /// <summary>
        /// Displays a view in the dialog workspace and returns the dialog result 
        /// after the dialog has been closed.        
        /// </summary>        
        public bool? ShowView(Window owner, IErrorInfoDialogView view, DialogOptions options, IDataErrorInfo errorInfo)
        {
            _dialogWindow = new Dialog();
            _dialogWindow.View = view as DependencyObject;
            _dialogWindow.Owner = owner;
            
            //Subscribe to the view's ErrorInfoChanged event.
            view.ErrorInfoChanged += new ErrorInfoChangedEventHandler(View_ErrorInfoChanged);
            
            if (errorInfo.Error.Length > 0)
            {
                //Intialise the dialog interface (save 
                //button and error image) when error exists.
                view.RaiseErrorInfoChanged(errorInfo);
            }

            // Subscribe to _dialogWindow.CanCloseDialog
            _dialogWindow.CanCloseDialog += new EventHandler<CancelEventArgs>(DialogWindow_CanCloseDialog);                
                                    
            // Apply the dialog options if provided
            if (options != null) { SetDialogOptions(_dialogWindow, options); }

            // Show dialog and return result.
            return _dialogWindow.ShowDialog(); 
        }

        /// <summary>
        /// Displays a view in the dialog workspace and returns the dialog result 
        /// after the dialog has been closed.        
        /// </summary>        
        public bool? ShowView(Window owner, IErrorInfoDialogView view, DialogOptions options, Func<bool> validationAction, IDataErrorInfo errorInfo)
        {
            _dialogWindow = new Dialog();
            _dialogWindow.View = view as DependencyObject;
            _dialogWindow.Owner = owner;

            //Initialise the ValidationAction.
            _dialogWindow.ValidateDialog += new EventHandler<CancelEventArgs>((sender, e) =>
            {
                e.Cancel = false;

                if (validationAction != null)
                {
                    // Validate action will return false when validation fails
                    // invert this result to cancel the close action.
                    e.Cancel = !validationAction();
                }
            });

            //Subscribe to the view's ErrorInfoChanged event.
            view.ErrorInfoChanged += new ErrorInfoChangedEventHandler(View_ErrorInfoChanged);

            if (errorInfo.Error.Length > 0)
            {
                //Intialise the dialog interface (save 
                //button and error image) when error exists.
                view.RaiseErrorInfoChanged(errorInfo);
            }

            // Subscribe to _dialogWindow.CanCloseDialog
            _dialogWindow.CanCloseDialog += new EventHandler<CancelEventArgs>(DialogWindow_CanCloseDialog);

            // Apply the dialog options if provided
            if (options != null) { SetDialogOptions(_dialogWindow, options); }

            // Show dialog and return result.
            return _dialogWindow.ShowDialog();
        }
                
        /// <summary>
        /// Handle View_ErrorInfoChanged event. Implements the validation mechanism
        /// for the Dialog window using IDataErrorInfo interface.
        /// </summary>        
        private void View_ErrorInfoChanged(ErrorInfoChangedEventArgs e)
        {            
            if (e.ErrorInfo == null) { return; }
            if (this._dialogWindow == null) { return; }

            _dialogWindow.IsDirty = true;
            _dialogWindow.buttonAccept.IsEnabled = (e.ErrorInfo.Error.Length == 0);            
            _dialogWindow.imageValidation.Visibility = (e.ErrorInfo.Error.Length > 0 ? Visibility.Visible : Visibility.Hidden);
            _dialogWindow.imageValidation.ToolTip = e.ErrorInfo.Error;            
        }

        /// <summary>
        /// Handle DialogWindow_CanCloseDialog event.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DialogWindow_CanCloseDialog(object sender, CancelEventArgs e)
        {
            DialogOptions options = new DialogOptions();
            options.AllowResize = false;
            options.DialogTitle = Properties.Resources.ConfirmCancelCaption;
            options.DialogMessage = Properties.Resources.ConfirmCancelMessage;
            options.IconUri = new Uri(MenuConstants.MESSAGE_EXCLAMATION_IMAGE_URI);
            options.Buttons = ButtonType.YesNo;

            Mouse.SetCursor(Cursors.Wait);
            MessageView view = new MessageView(options.DialogMessage);
            Dialog dialogWindow = new Dialog();            
            dialogWindow.View = view as DependencyObject;
            dialogWindow.Owner = _dialogWindow;

            SetDialogOptions(dialogWindow, options);

            // Show dialog and return result.            
            e.Cancel = !dialogWindow.ShowDialog().Value;
        }

        /// <summary>
        /// Set Dialog Options.
        /// </summary>
        /// <param name="dialogWindow"></param>
        /// <param name="options"></param>
        private void SetDialogOptions(Dialog dialogWindow, DialogOptions options)
        {
            if (options == null) { return; }

            dialogWindow.ResizeMode = ResizeMode.NoResize; //options.AllowResize ? ResizeMode.CanResizeWithGrip : ResizeMode.NoResize;            
            dialogWindow.Title = options.DialogTitle ?? string.Empty;

            //Set dialog icon. If no uri is specified then use the app icon.
            dialogWindow.Icon = options.IconUri == null ?
                BitmapFrame.Create(new Uri(MenuConstants.APP_IMAGE_URI)) :
                BitmapFrame.Create(options.IconUri);

            switch (options.Buttons)
            {
                case ButtonType.OK:
                    dialogWindow.buttonAccept.Content = Properties.Resources.OKText;
                    dialogWindow.buttonAccept.Margin = new Thickness(10);
                    dialogWindow.buttonAccept.IsEnabled = true;
                    dialogWindow.buttonReject.Visibility =
                    dialogWindow.buttonCancel.Visibility = Visibility.Collapsed;
                    break;
                case ButtonType.OKCancel:
                    dialogWindow.buttonAccept.Content = Properties.Resources.OKText;
                    dialogWindow.buttonCancel.Focus();
                    dialogWindow.buttonReject.Visibility = Visibility.Collapsed;
                    break;
                case ButtonType.YesNo:
                    dialogWindow.buttonAccept.Content = Properties.Resources.YesText;
                    dialogWindow.buttonAccept.IsEnabled = true;
                    dialogWindow.buttonReject.Focus();
                    dialogWindow.buttonReject.Visibility = Visibility.Visible;
                    dialogWindow.buttonCancel.Visibility = Visibility.Collapsed;
                    break;
                case ButtonType.YesNoCancel:
                    dialogWindow.buttonAccept.Content = Properties.Resources.YesText;
                    dialogWindow.buttonAccept.IsEnabled = true;
                    dialogWindow.buttonAccept.Focus();
                    dialogWindow.buttonReject.Visibility = Visibility.Visible;
                    break;
            }

            if (options.DialogSize.Width != 0 && options.DialogSize.Height != 0)
            {
                dialogWindow.Width = dialogWindow.MinWidth = options.DialogSize.Width;
                dialogWindow.Height = dialogWindow.MinHeight = options.DialogSize.Height;
                dialogWindow.SizeToContent = SizeToContent.Manual;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mark Brownsword
Software Developer (Senior)
Australia Australia
I've been working as a software developer since 2000 and hold a Bachelor of Business degree from The Open Polytechnic of New Zealand. Computers are for people and I aim to build applications for people that they would want to use.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150506.1 | Last Updated 25 Sep 2009
Article Copyright 2009 by Mark Brownsword
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid