view Chronosv2/source/Presentation/Windows/Controls/MessageWindowElement.cs @ 12:6a0449185449

SCC changed from TFS to HG
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:47:35 +0700
parents 443821e55f06
children
line wrap: on
line source

/*
The MIT License

Copyright (c) 2009-2010. Carlos Guzmán Álvarez. http://chronoswpf.codeplex.com/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using System.Windows;
using System.Windows.Input;
using Chronos.Extensions.Windows;
using Chronos.Presentation.Core.VirtualDesktops;
using Chronos.Presentation.Core.Windows;
using nRoute.Services;

namespace Chronos.Presentation.Windows.Controls
{
    /// <summary>
    /// Provides the ability to create modal message windows (like messageboxes)
    /// </summary>
    public sealed class MessageWindowElement 
        : WindowElement
    {
        #region · Dependency Properties ·

        /// <summary>
        /// Identifies the Buttons dependency property.
        /// </summary>
        public static readonly DependencyProperty ButtonsProperty =
            DependencyProperty.Register("Buttons", typeof(DialogButton), typeof(MessageWindowElement),
                new FrameworkPropertyMetadata(DialogButton.Ok));

        #endregion

        #region · Sync Object ·

        static readonly object SyncObject = new object();

        #endregion

        #region · Static Commands ·

        /// <summary>
        /// Accept command
        /// </summary>
        public static RoutedCommand AcceptCommand;

        /// <summary>
        /// Cancel command
        /// </summary>
        public static RoutedCommand CancelCommand;

        #endregion

        #region · Static Constructors ·

        /// <summary>
        /// Initializes the <see cref="MessageWindowElement"/> class.
        /// </summary>
        static MessageWindowElement()
        {
            MessageWindowElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageWindowElement),
                new FrameworkPropertyMetadata(typeof(MessageWindowElement)));

            KeyboardNavigation.IsTabStopProperty.OverrideMetadata(typeof(MessageWindowElement),
                new FrameworkPropertyMetadata(false));

            MessageWindowElement.AcceptCommand = new RoutedCommand("Accept", typeof(MessageWindowElement));
            MessageWindowElement.CancelCommand = new RoutedCommand("Cancel", typeof(MessageWindowElement));
        }

        #endregion
        
        #region · Show "Factory" Methods ·

        /// <summary>
        /// Shows a new <see cref="MessageWindowElement"/> with the given message
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static DialogResult Show(string message)
        {
            return MessageWindowElement.Show("Message", message, DialogButton.OkCancel);
        }

        /// <summary>
        /// Shows a new <see cref="MessageWindowElement"/> with the given caption and message
        /// </summary>
        /// <param name="caption"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static DialogResult Show(string caption, string message)
        {
            return MessageWindowElement.Show(caption, message, DialogButton.OkCancel);
        }

        /// <summary>
        /// Shows a new <see cref="MessageWindowElement"/> with the given caption, message and buttons
        /// </summary>
        /// <param name="caption"></param>
        /// <param name="message"></param>
        /// <param name="buttons"></param>
        /// <returns></returns>
        public static DialogResult Show(string caption, string message, DialogButton buttons) 
        {
            lock (SyncObject)
            {
                DialogResult result = DialogResult.None;

                System.Windows.Application.Current.Dispatcher.Invoke(
                    (Action)delegate
                    {
                        MessageWindowElement window = new MessageWindowElement
                        {
                            Title           = caption,
                            Content         = message,
                            Buttons         = buttons,
                            StartupLocation = StartupPosition.CenterParent
                        };

                        result = ServiceLocator.GetService<IVirtualDesktopManager>().ShowDialog(window);
                    });

                return result;
            }
        }

        #endregion

        #region · Properties ·

        /// <summary>
        /// Gets or sets the button combination to be shown
        /// </summary>
        public DialogButton Buttons
        {
            get { return (DialogButton)base.GetValue(MessageWindowElement.ButtonsProperty); }
            set { base.SetValue(MessageWindowElement.ButtonsProperty, value); }
        }

        #endregion

        #region · Constructors ·

        /// <summary>
        /// Initializes a new instance of the <see cref="MessageWindowElement"/> class
        /// </summary>
        public MessageWindowElement()
            : base()
        {
            CommandBinding bindinAccept = new CommandBinding(MessageWindowElement.AcceptCommand, new ExecutedRoutedEventHandler(OnAccept));
            this.CommandBindings.Add(bindinAccept);

            CommandBinding bindingCancel = new CommandBinding(MessageWindowElement.CancelCommand, new ExecutedRoutedEventHandler(OnCancel));
            this.CommandBindings.Add(bindingCancel);
        }

        #endregion

        #region · Protected Methods ·

        /// <summary>
        /// Focuses the window
        /// </summary>
        protected override void GiveFocus()
        {
            this.SetFocus();
        }

        #endregion

        #region · Command Actions ·

        private void OnAccept(object sender, ExecutedRoutedEventArgs e)
        {
            if (this.Buttons == DialogButton.Ok ||
                this.Buttons == DialogButton.OkCancel)
            {
                this.DialogResult = DialogResult.Ok;
            }
            else
            {
                this.DialogResult = DialogResult.Yes;
            }

            this.Hide();
        }

        private void OnCancel(object sender, ExecutedRoutedEventArgs e)
        {
            if (this.Buttons == DialogButton.OkCancel)
            {
                this.DialogResult = DialogResult.Cancel;
            }
            else
            {
                this.DialogResult = DialogResult.No;
            }

            this.Hide();
        }

        #endregion
    }
}