// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Text; using ICSharpCode.Core.Services; namespace ICSharpCode.Core { /// /// Class with static methods to show message boxes. /// All text displayed using the MessageService is passed to the /// to replace ${res} markers. /// public static class MessageService { /// /// Shows an exception error. /// public static void ShowException(Exception ex) { ShowException(ex, null); } /// /// Shows an error using a message box. /// public static void ShowError(string message) { LoggingService.Error(message); ServiceManager.Instance.MessageService.ShowError(message); } /// /// Shows an error using a message box. /// is first passed through the /// , /// then through , using the formatitems as arguments. /// public static void ShowErrorFormatted(string formatstring, params object[] formatitems) { ShowError(Format(formatstring, formatitems)); } /// /// Shows an exception. /// public static void ShowException(Exception ex, string message) { LoggingService.Error(message, ex); LoggingService.Warn("Stack trace of last exception log:\n" + Environment.StackTrace); ServiceManager.Instance.MessageService.ShowException(ex, message); } /// /// Shows an exception. /// public static void ShowHandledException(Exception ex) { ShowHandledException(ex, null); } /// /// Shows an exception. /// public static void ShowHandledException(Exception ex, string message) { LoggingService.Error(message, ex); LoggingService.Warn("Stack trace of last exception log:\n" + Environment.StackTrace); message = GetMessage(message, ex); ServiceManager.Instance.MessageService.ShowError(message); } static string GetMessage(string message, Exception ex) { if (message == null) { return ex.Message; } return message + "\r\n\r\n" + ex.Message; } /// /// Shows a warning message. /// public static void ShowWarning(string message) { LoggingService.Warn(message); ServiceManager.Instance.MessageService.ShowWarning(message); } /// /// Shows a warning message. /// is first passed through the /// , /// then through , using the formatitems as arguments. /// public static void ShowWarningFormatted(string formatstring, params object[] formatitems) { ShowWarning(Format(formatstring, formatitems)); } /// /// Asks the user a Yes/No question, using "Yes" as the default button. /// Returns true if yes was clicked, false if no was clicked. /// public static bool AskQuestion(string question, string caption) { return ServiceManager.Instance.MessageService.AskQuestion(question, caption); } public static bool AskQuestionFormatted(string caption, string formatstring, params object[] formatitems) { return AskQuestion(Format(formatstring, formatitems), caption); } public static bool AskQuestionFormatted(string formatstring, params object[] formatitems) { return AskQuestion(Format(formatstring, formatitems)); } /// /// Asks the user a Yes/No question, using "Yes" as the default button. /// Returns true if yes was clicked, false if no was clicked. /// public static bool AskQuestion(string question) { return AskQuestion(question, StringParser.Parse("${res:Global.QuestionText}")); } /// /// Shows a custom dialog. /// /// The title of the dialog. /// The description shown in the dialog. /// /// The number of the button that is the default accept button. /// Use -1 if you don't want to have an accept button. /// /// /// The number of the button that is the cancel button. /// Use -1 if you don't want to have a cancel button. /// /// The captions of the buttons. /// The number of the button that was clicked, or -1 if the dialog was closed without clicking a button. public static int ShowCustomDialog(string caption, string dialogText, int acceptButtonIndex, int cancelButtonIndex, params string[] buttontexts) { return ServiceManager.Instance.MessageService.ShowCustomDialog(caption, dialogText, acceptButtonIndex, cancelButtonIndex, buttontexts); } /// /// Shows a custom dialog. /// /// The title of the dialog. /// The description shown in the dialog. /// The captions of the buttons. /// The number of the button that was clicked. public static int ShowCustomDialog(string caption, string dialogText, params string[] buttontexts) { return ShowCustomDialog(caption, dialogText, -1, -1, buttontexts); } public static string ShowInputBox(string caption, string dialogText, string defaultValue) { return ServiceManager.Instance.MessageService.ShowInputBox(caption, dialogText, defaultValue); } static string defaultMessageBoxTitle = "MessageBox"; static string productName = "Application Name"; /// /// Gets/Sets the name of the product using ICSharpCode.Core. /// Is used by the string parser as replacement for ${ProductName}. /// public static string ProductName { get { return productName; } set { productName = value; } } /// /// Gets/Sets the default title for message boxes displayed /// by the message service. /// public static string DefaultMessageBoxTitle { get { return defaultMessageBoxTitle; } set { defaultMessageBoxTitle = value; } } public static void ShowMessage(string message) { ShowMessage(message, DefaultMessageBoxTitle); } public static void ShowMessageFormatted(string formatstring, params object[] formatitems) { ShowMessage(Format(formatstring, formatitems)); } public static void ShowMessageFormatted(string caption, string formatstring, params object[] formatitems) { ShowMessage(Format(formatstring, formatitems), caption); } public static void ShowMessage(string message, string caption) { LoggingService.Info(message); ServiceManager.Instance.MessageService.ShowMessage(message, caption); } static string Format(string formatstring, object[] formatitems) { try { return String.Format(StringParser.Parse(formatstring), formatitems); } catch (FormatException ex) { LoggingService.Warn(ex); StringBuilder b = new StringBuilder(StringParser.Parse(formatstring)); foreach(object formatitem in formatitems) { b.Append("\nItem: "); b.Append(formatitem); } return b.ToString(); } } } }