Click here to Skip to main content
15,892,059 members
Articles / Desktop Programming / WPF

GoalBook - A Hybrid Smart Client

Rate me:
Please Sign up or sign in to vote.
4.86/5 (24 votes)
25 Sep 2009CPOL10 min read 79.4K   834   69  
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
//===============================================================================
// 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)


Written By
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.

Comments and Discussions