using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace AIStudio.Wpf.DiagramDesigner.Services { /// /// This class implements the IMessageBoxService for WPF purposes. /// public class WPFMessageBoxService : IMessageBoxService { #region IMessageBoxService Members /// /// Displays an error dialog with a given message. /// /// The message to be displayed. public void ShowError(string message) { ShowMessage(message, "Error", CustomDialogIcons.Stop); } /// /// Displays an error dialog with a given message and caption. /// /// The message to be displayed. /// The caption of the message box window public void ShowError(string message, string caption) { ShowMessage(message, caption, CustomDialogIcons.Stop); } /// /// Displays an error dialog with a given message. /// /// The message to be displayed. public void ShowInformation(string message) { ShowMessage(message, "Information", CustomDialogIcons.Information); } /// /// Displays an error dialog with a given message. /// /// The message to be displayed. /// The caption of the message box window public void ShowInformation(string message, string caption) { ShowMessage(message, caption, CustomDialogIcons.Information); } /// /// Displays an error dialog with a given message. /// /// The message to be displayed. public void ShowWarning(string message) { ShowMessage(message, "Warning", CustomDialogIcons.Warning); } /// /// Displays an error dialog with a given message. /// /// The message to be displayed. /// The caption of the message box window public void ShowWarning(string message, string caption) { ShowMessage(message, caption, CustomDialogIcons.Warning); } /// /// Displays a Yes/No dialog and returns the user input. /// /// The message to be displayed. /// The icon to be displayed. /// User selection. public CustomDialogResults ShowYesNo(string message, CustomDialogIcons icon) { return ShowQuestionWithButton(message, icon, CustomDialogButtons.YesNo); } /// /// Displays a Yes/No dialog and returns the user input. /// /// The message to be displayed. /// The caption of the message box window /// The icon to be displayed. /// User selection. public CustomDialogResults ShowYesNo(string message, string caption, CustomDialogIcons icon) { return ShowQuestionWithButton(message, caption, icon, CustomDialogButtons.YesNo); } /// /// Displays a Yes/No dialog with a default button selected, and returns the user input. /// /// The message to be displayed. /// The caption of the message box window /// The icon to be displayed. /// Default result for the message box /// User selection. public CustomDialogResults ShowYesNo(string message, string caption, CustomDialogIcons icon, CustomDialogResults defaultResult) { return ShowQuestionWithButton(message, caption, icon, CustomDialogButtons.YesNo, defaultResult); } /// /// Displays a Yes/No/Cancel dialog and returns the user input. /// /// The message to be displayed. /// The icon to be displayed. /// User selection. public CustomDialogResults ShowYesNoCancel(string message, CustomDialogIcons icon) { return ShowQuestionWithButton(message, icon, CustomDialogButtons.YesNoCancel); } /// /// Displays a Yes/No/Cancel dialog and returns the user input. /// /// The message to be displayed. /// The caption of the message box window /// The icon to be displayed. /// User selection. public CustomDialogResults ShowYesNoCancel(string message, string caption, CustomDialogIcons icon) { return ShowQuestionWithButton(message, caption, icon, CustomDialogButtons.YesNoCancel); } /// /// Displays a Yes/No/Cancel dialog with a default button selected, and returns the user input. /// /// The message to be displayed. /// The caption of the message box window /// The icon to be displayed. /// Default result for the message box /// User selection. public CustomDialogResults ShowYesNoCancel(string message, string caption, CustomDialogIcons icon, CustomDialogResults defaultResult) { return ShowQuestionWithButton(message, caption, icon, CustomDialogButtons.YesNoCancel, defaultResult); } /// /// Displays a OK/Cancel dialog and returns the user input. /// /// The message to be displayed. /// The icon to be displayed. /// User selection. public CustomDialogResults ShowOkCancel(string message, CustomDialogIcons icon) { return ShowQuestionWithButton(message, icon, CustomDialogButtons.OKCancel); } /// /// Displays a OK/Cancel dialog and returns the user input. /// /// The message to be displayed. /// The caption of the message box window /// The icon to be displayed. /// User selection. public CustomDialogResults ShowOkCancel(string message, string caption, CustomDialogIcons icon) { return ShowQuestionWithButton(message, caption, icon, CustomDialogButtons.OKCancel); } /// /// Displays a OK/Cancel dialog with a default result selected, and returns the user input. /// /// The message to be displayed. /// The caption of the message box window /// The icon to be displayed. /// Default result for the message box /// User selection. public CustomDialogResults ShowOkCancel(string message, string caption, CustomDialogIcons icon, CustomDialogResults defaultResult) { return ShowQuestionWithButton(message, caption, icon, CustomDialogButtons.OKCancel, defaultResult); } #endregion #region Private Methods /// /// Shows a standard System.Windows.MessageBox using the parameters requested /// /// The message to be displayed. /// The heading to be displayed /// The icon to be displayed. private void ShowMessage(string message, string caption, CustomDialogIcons icon) { MessageBox.Show(message, caption, MessageBoxButton.OK, GetImage(icon)); } /// /// Shows a standard System.Windows.MessageBox using the parameters requested /// but will return a translated result to enable adhere to the IMessageBoxService /// implementation required. /// /// This abstraction allows for different frameworks to use the same ViewModels but supply /// alternative implementations of core service interfaces /// /// The message to be displayed. /// The icon to be displayed. /// /// CustomDialogResults results to use private CustomDialogResults ShowQuestionWithButton(string message, CustomDialogIcons icon, CustomDialogButtons button) { MessageBoxResult result = MessageBox.Show(message, "Please confirm...", GetButton(button), GetImage(icon)); return GetResult(result); } /// /// Shows a standard System.Windows.MessageBox using the parameters requested /// but will return a translated result to enable adhere to the IMessageBoxService /// implementation required. /// /// This abstraction allows for different frameworks to use the same ViewModels but supply /// alternative implementations of core service interfaces /// /// The message to be displayed. /// The caption of the message box window /// The icon to be displayed. /// /// CustomDialogResults results to use private CustomDialogResults ShowQuestionWithButton(string message, string caption, CustomDialogIcons icon, CustomDialogButtons button) { MessageBoxResult result = MessageBox.Show(message, caption, GetButton(button), GetImage(icon)); return GetResult(result); } /// /// Shows a standard System.Windows.MessageBox using the parameters requested /// but will return a translated result to enable adhere to the IMessageBoxService /// implementation required. /// /// This abstraction allows for different frameworks to use the same ViewModels but supply /// alternative implementations of core service interfaces /// /// The message to be displayed. /// The caption of the message box window /// The icon to be displayed. /// /// Default result for the message box /// CustomDialogResults results to use private CustomDialogResults ShowQuestionWithButton(string message, string caption, CustomDialogIcons icon, CustomDialogButtons button, CustomDialogResults defaultResult) { MessageBoxResult result = MessageBox.Show(message, caption, GetButton(button), GetImage(icon), GetResult(defaultResult)); return GetResult(result); } /// /// Translates a CustomDialogIcons into a standard WPF System.Windows.MessageBox MessageBoxImage. /// This abstraction allows for different frameworks to use the same ViewModels but supply /// alternative implementations of core service interfaces /// /// The icon to be displayed. /// A standard WPF System.Windows.MessageBox MessageBoxImage private MessageBoxImage GetImage(CustomDialogIcons icon) { MessageBoxImage image = MessageBoxImage.None; switch (icon) { case CustomDialogIcons.Information: image = MessageBoxImage.Information; break; case CustomDialogIcons.Question: image = MessageBoxImage.Question; break; case CustomDialogIcons.Exclamation: image = MessageBoxImage.Exclamation; break; case CustomDialogIcons.Stop: image = MessageBoxImage.Stop; break; case CustomDialogIcons.Warning: image = MessageBoxImage.Warning; break; } return image; } /// /// Translates a CustomDialogButtons into a standard WPF System.Windows.MessageBox MessageBoxButton. /// This abstraction allows for different frameworks to use the same ViewModels but supply /// alternative implementations of core service interfaces /// /// The button type to be displayed. /// A standard WPF System.Windows.MessageBox MessageBoxButton private MessageBoxButton GetButton(CustomDialogButtons btn) { MessageBoxButton button = MessageBoxButton.OK; switch (btn) { case CustomDialogButtons.OK: button = MessageBoxButton.OK; break; case CustomDialogButtons.OKCancel: button = MessageBoxButton.OKCancel; break; case CustomDialogButtons.YesNo: button = MessageBoxButton.YesNo; break; case CustomDialogButtons.YesNoCancel: button = MessageBoxButton.YesNoCancel; break; } return button; } /// /// Translates a standard WPF System.Windows.MessageBox MessageBoxResult into a /// CustomDialogIcons. /// This abstraction allows for different frameworks to use the same ViewModels but supply /// alternative implementations of core service interfaces /// /// The standard WPF System.Windows.MessageBox MessageBoxResult /// CustomDialogResults results to use private CustomDialogResults GetResult(MessageBoxResult result) { CustomDialogResults customDialogResults = CustomDialogResults.None; switch (result) { case MessageBoxResult.Cancel: customDialogResults = CustomDialogResults.Cancel; break; case MessageBoxResult.No: customDialogResults = CustomDialogResults.No; break; case MessageBoxResult.None: customDialogResults = CustomDialogResults.None; break; case MessageBoxResult.OK: customDialogResults = CustomDialogResults.OK; break; case MessageBoxResult.Yes: customDialogResults = CustomDialogResults.Yes; break; } return customDialogResults; } /// /// Translates a CustomDialogResults into a standard WPF System.Windows.MessageBox MessageBoxResult /// This abstraction allows for different frameworks to use the same ViewModels but supply /// alternative implementations of core service interfaces /// /// The CustomDialogResults /// The standard WPF System.Windows.MessageBox MessageBoxResult results to use private MessageBoxResult GetResult(CustomDialogResults result) { MessageBoxResult customDialogResults = MessageBoxResult.None; switch (result) { case CustomDialogResults.Cancel: customDialogResults = MessageBoxResult.Cancel; break; case CustomDialogResults.No: customDialogResults = MessageBoxResult.No; break; case CustomDialogResults.None: customDialogResults = MessageBoxResult.None; break; case CustomDialogResults.OK: customDialogResults = MessageBoxResult.OK; break; case CustomDialogResults.Yes: customDialogResults = MessageBoxResult.Yes; break; } return customDialogResults; } #endregion } }