Click here to Skip to main content
15,860,861 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 78.6K   834   69  
A WPF hybrid smart client that synchronises your goals with the Toodledo online To-do service.
//===============================================================================
// Goal Book.
// Copyright © 2009 Mark Brownsword. 
//===============================================================================

#region Using Statements
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
#endregion

namespace GoalBook.Shell.Windows
{
    /// <summary>
    /// Interaction logic for Dialog.xaml
    /// </summary>
    public partial class Dialog : Window
    {        
        #region Constructor
        /// <summary>
        /// Initializes a new instance of <see cref="ViewDialog"/>
        /// </summary>
        public Dialog()
        {
            InitializeComponent();                       
        }
        #endregion

        #region Events
        /// <summary>
        /// Gets fired when the dialog needs to be validated.
        /// </summary>
        public event EventHandler<CancelEventArgs> ValidateDialog;

        /// <summary>
        /// Gets fired before the dialog closes.
        /// </summary>
        public event EventHandler<CancelEventArgs> CanCloseDialog;
        #endregion

        #region Private Members
        /// <summary>
        /// On Can Close Dialog.
        /// </summary>
        /// <returns>True when Can Close Dialog</returns>
        private bool OnCanCloseDialog()
        {
            EventHandler<CancelEventArgs> canCloseDialog = CanCloseDialog;
            bool cancelled = false;

            if (canCloseDialog != null)
            {
                CancelEventArgs args = new CancelEventArgs();
                canCloseDialog(this, args);

                cancelled = args.Cancel;
            }

            return !cancelled;
        }

        /// <summary>
        /// Do Event. Validate form.
        /// </summary>
        /// <returns>True if valid</returns>
        private bool OnValidate()
        {
            EventHandler<CancelEventArgs> validateDialog = ValidateDialog;
            bool cancelled = false;

            if (validateDialog != null)
            {
                CancelEventArgs args = new CancelEventArgs();
                validateDialog(this, args);

                cancelled = args.Cancel;
            }

            return !cancelled;
        }

        /// <summary>
        /// Close Dialog with specified result.
        /// </summary>
        /// <param name="result">result parameter</param>
        private void CloseDialog(bool? result)
        {            
            this.IsCancelled = result == null;
            this.DialogResult = result;

            this.Close();            
        }
        #endregion

        #region Event handlers
        /// <summary>
        /// Handle Window_Loaded event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">RoutedEventArgs parameter</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            IsCancelled = true;
            IsDirty = false;
        }

        /// <summary>
        /// Handle ButtonAccept_PreviewKeyUp event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">KeyEventArgs parameter</param>
        private void ButtonAccept_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (OnValidate())
                {
                    e.Handled = true;
                    CloseDialog(true);
                }
            }
        }

        /// <summary>
        /// Handle ButtonAccept_PreviewMouseLeftButtonUp event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">MouseButtonEventArgs parameter</param>
        private void ButtonAccept_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (OnValidate())
            {
                e.Handled = true;
                CloseDialog(true);
            }
        }

        /// <summary>
        /// Handle ButtonReject_PreviewKeyUp event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">KeyEventArgs parameter</param>
        private void ButtonReject_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {                
                e.Handled = true;
                CloseDialog(false);
            }            
        }

        /// <summary>
        /// Handle ButtonReject_PreviewMouseLeftButtonUp event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">MouseButtonEventArgs parameter</param>
        private void ButtonReject_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {            
            e.Handled = true;
            CloseDialog(false);                   
        }

        /// <summary>
        /// Handle ButtonCancel_PreviewKeyUp event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">KeyEventArgs parameter</param>
        private void ButtonCancel_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                CloseDialog(null);
            } 
        }

        /// <summary>
        /// Handle ButtonCancel_PreviewMouseLeftButtonUp event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">MouseButtonEventArgs parameter</param>
        private void ButtonCancel_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            CloseDialog(null); 
        }

        /// <summary>
        /// Handle Window_PreviewKeyUp event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">KeyEventArgs parameter</param>
        private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                CloseDialog(null);
            }
        }

        /// <summary>
        /// Handle Window_Closing event.
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">CancelEventArgs parameter</param>
        private void Window_Closing(object sender, CancelEventArgs e)
        {
            if (this.IsCancelled && this.IsDirty)
            {
                e.Cancel = !OnCanCloseDialog();               
            }            
        }
        #endregion

        #region Properties
        /// <summary>
        /// Reference to ButtonPanelVisibility. Using a DependencyProperty as the backing store for 
        /// ButtonPanelVisibility.  This enables animation, styling, binding, etc... 
        /// </summary>
        public Visibility ButtonPanelVisibility
        {
            get { return (Visibility)GetValue(ButtonPanelVisibilityProperty); }
            set { SetValue(ButtonPanelVisibilityProperty, value); }
        }        
        public static readonly DependencyProperty ButtonPanelVisibilityProperty = DependencyProperty.Register("ButtonPanelVisibility", 
            typeof(Visibility), typeof(Dialog), new UIPropertyMetadata(Visibility.Visible));
        
        /// <summary>
        /// Gets or sets the view to show in the dialog
        /// </summary>
        public DependencyObject View
        {
            get { return (DependencyObject)GetValue(ViewProperty); }
            set { SetValue(ViewProperty, value); }
        }
        public static readonly DependencyProperty ViewProperty = DependencyProperty.Register("View",
            typeof(DependencyObject), typeof(Dialog), new UIPropertyMetadata(null));

        /// <summary>
        /// Gets value of IsCancelled property.
        /// </summary>
        public bool IsCancelled { get; private set; }

        /// <summary>
        /// Gets or sets value of IsDirty property.
        /// </summary>
        public bool IsDirty { get; set; }
        #endregion
    }
}

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