// // // // // $Revision: 3772 $ // 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 { /// /// Delegate used for custom error callbacks. /// public delegate void ShowErrorDelegate(Exception ex, string message); /// /// Gets/Sets the custom error reporter callback delegate. /// If this property is null, the default messagebox is used. /// public static ShowErrorDelegate CustomErrorReporter { get; set; } /// /// Shows an exception error using the . /// public static void ShowError(Exception ex) { ShowError(ex, null); } /// /// Shows an error using a message box. /// public static void ShowError(string message) { ShowError(null, 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 string[] formatitems) { ShowError(null, Format(formatstring, formatitems)); } /// /// Shows an error. /// If is null, the message is shown inside /// a message box. /// Otherwise, the custom error reporter is used to display /// the exception error. /// public static void ShowError(Exception ex, string message) { if (message == null) message = string.Empty; if (ex != null) { LoggingService.Error(message, ex); LoggingService.Warn("Stack trace of last error log:\n" + Environment.StackTrace); if (CustomErrorReporter != null) { CustomErrorReporter(ex, message); return; } } else { LoggingService.Error(message); } ServiceManager.MessageService.ShowError(ex, message); } /// /// Shows a warning message. /// public static void ShowWarning(string message) { LoggingService.Warn(message); ServiceManager.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 string[] 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.MessageService.AskQuestion(question, caption); } public static bool AskQuestionFormatted(string caption, string formatstring, params string[] formatitems) { return AskQuestion(Format(formatstring, formatitems), caption); } public static bool AskQuestionFormatted(string formatstring, params string[] 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(StringParser.Parse(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.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.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 string[] formatitems) { ShowMessage(Format(formatstring, formatitems)); } public static void ShowMessageFormatted(string caption, string formatstring, params string[] formatitems) { ShowMessage(Format(formatstring, formatitems), caption); } public static void ShowMessage(string message, string caption) { LoggingService.Info(message); ServiceManager.MessageService.ShowMessage(message, caption); } static string Format(string formatstring, string[] formatitems) { try { return String.Format(StringParser.Parse(formatstring), formatitems); } catch (FormatException) { StringBuilder b = new StringBuilder(StringParser.Parse(formatstring)); foreach(string formatitem in formatitems) { b.Append("\nItem: "); b.Append(formatitem); } return b.ToString(); } } } }