Created a service to display message boxes (view models that want to report errors are usually in a shitty situation - now for simple applications view models can send and error message that will either be displayed or used by a unit test to check the view model is actually failing as expected)

git-svn-id: file:///srv/devel/repo-conversion/nuwi@46 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2019-02-07 18:03:11 +00:00
parent 14d0ea1371
commit 0f70f53f18
9 changed files with 566 additions and 1 deletions

View File

@ -57,6 +57,17 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Source\AutoBinding\ConventionBinder.cs" /> <Compile Include="Source\AutoBinding\ConventionBinder.cs" />
<Compile Include="Source\AutoBinding\IAutoBinder.cs" /> <Compile Include="Source\AutoBinding\IAutoBinder.cs" />
<Compile Include="Source\Messages\IMessageService.cs" />
<Compile Include="Source\Messages\MessageEventArgs.cs" />
<Compile Include="Source\Messages\MessageEventArgs.Test.cs">
<DependentUpon>MessageEventArgs.cs</DependentUpon>
</Compile>
<Compile Include="Source\Messages\MessageServiceHelper.cs" />
<Compile Include="Source\Messages\MessageText.cs" />
<Compile Include="Source\Messages\MessageText.Test.cs">
<DependentUpon>MessageText.cs</DependentUpon>
</Compile>
<Compile Include="Source\Messages\StandardMessageBoxManager.cs" />
<Compile Include="Source\NullActiveWindowTracker.cs" /> <Compile Include="Source\NullActiveWindowTracker.cs" />
<Compile Include="Source\ViewModels\DialogViewModel.cs" /> <Compile Include="Source\ViewModels\DialogViewModel.cs" />
<Compile Include="Source\ViewModels\DialogViewModel.Test.cs"> <Compile Include="Source\ViewModels\DialogViewModel.Test.cs">

View File

@ -0,0 +1,55 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Windows.Forms;
namespace Nuclex.Windows.Forms.Messages {
/// <summary>Performs simple user interaction</summary>
/// <remarks>
/// Methods provided by this service can be covered using plain old message boxes
/// and do not require special dialogs or calls to the task dialog API.
/// </remarks>
public interface IMessageService {
/// <summary>Triggered when a message is about to be displayed to the user</summary>
event EventHandler<MessageEventArgs> MessageDisplaying;
/// <summary>Triggered when the user has acknowledged the current message</summary>
event EventHandler MessageAcknowledged;
/// <summary>Asks the user a question that can be answered via several buttons</summary>
/// <param name="image">Image that will be shown on the message box</param>
/// <param name="text">Text that will be shown to the user</param>
/// <param name="buttons">Buttons available for the user to click on</param>
/// <returns>The button the user has clicked on</returns>
DialogResult ShowQuestion(
MessageBoxIcon image, MessageText text, MessageBoxButtons buttons
);
/// <summary>Displays a notification to the user</summary>
/// <param name="image">Image that will be shown on the message bx</param>
/// <param name="text">Text that will be shown to the user</param>
void ShowNotification(MessageBoxIcon image, MessageText text);
}
} // namespace Nuclex.Windows.Forms.Messages

View File

@ -0,0 +1,49 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Windows.Forms;
using NUnit.Framework;
namespace Nuclex.Windows.Forms.Messages {
/// <summary>Unit tests for the message box event argument container</summary>
[TestFixture]
internal class MessageEventArgsTest {
/// <summary>Verifies that the image associated with the message gets stored</summary>
[Test]
public void ImageIsStored() {
var arguments = new MessageEventArgs(MessageBoxIcon.Exclamation, null);
Assert.AreEqual(MessageBoxIcon.Exclamation, arguments.Image);
}
/// <summary>Verifies that the text associated with the message gets stored</summary>
[Test]
public void TextIsStored() {
var text = new MessageText();
var arguments = new MessageEventArgs(MessageBoxIcon.None, text);
Assert.AreSame(text, arguments.Text);
}
}
} // namespace Nuclex.Windows.Forms.Messages

View File

@ -0,0 +1,54 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Windows.Forms;
namespace Nuclex.Windows.Forms.Messages {
/// <summary>Provides a displayed message and its severity to event subscribers</summary>
public class MessageEventArgs : EventArgs {
/// <summary>Initializes a new message box event argument container</summary>
/// <param name="image">Image the message box will be displaying</param>
/// <param name="text">Text that will be displayed in the message box</param>
public MessageEventArgs(MessageBoxIcon image, MessageText text) {
this.image = image;
this.text = text;
}
/// <summary>Image that indicates the severity of the message being displayed</summary>
public MessageBoxIcon Image {
get { return this.image; }
}
/// <summary>Text that is being displayed in the message box</summary>
public MessageText Text {
get { return this.text; }
}
/// <summary>Image that indicates the severity of the message being displayed</summary>
private MessageBoxIcon image;
/// <summary>Text that is being displayed in the message box</summary>
private MessageText text;
}
} // namespace Nuclex.Windows.Forms.Messages

View File

@ -0,0 +1,108 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Windows.Forms;
namespace Nuclex.Windows.Forms.Messages {
/// <summary>Contains helper methods for the message service</summary>
public static class MessageServiceHelper {
/// <summary>Asks the user a question that can be answered with yes or no</summary>
/// <param name="messageService">
/// Message service that will be used to display the question
/// </param>
/// <param name="text">Text that will be shown on the message box</param>
/// <returns>The button the user has clicked on</returns>
public static DialogResult AskYesNo(
this IMessageService messageService, MessageText text
) {
return messageService.ShowQuestion(
MessageBoxIcon.Question, text, MessageBoxButtons.YesNo
);
}
/// <summary>Asks the user a question that can be answered with ok or cancel</summary>
/// <param name="messageService">
/// Message service that will be used to display the question
/// </param>
/// <param name="text">Text that will be shown on the message box</param>
/// <returns>The button the user has clicked on</returns>
public static DialogResult AskOkCancel(
this IMessageService messageService, MessageText text
) {
return messageService.ShowQuestion(
MessageBoxIcon.Question, text, MessageBoxButtons.OKCancel
);
}
/// <summary>
/// Asks the user a question that can be answered with yes, no or cancel
/// </summary>
/// <param name="messageService">
/// Message service that will be used to display the question
/// </param>
/// <param name="text">Text that will be shown on the message box</param>
/// <returns>The button the user has clicked on</returns>
public static DialogResult AskYesNoCancel(
this IMessageService messageService, MessageText text
) {
return messageService.ShowQuestion(
MessageBoxIcon.Question, text, MessageBoxButtons.YesNoCancel
);
}
/// <summary>Displays an informative message</summary>
/// <param name="messageService">
/// Message service that will be used to display the warning
/// </param>
/// <param name="text">Text to be displayed on the warning message</param>
public static void Inform(
this IMessageService messageService, MessageText text
) {
messageService.ShowNotification(MessageBoxIcon.Information, text);
}
/// <summary>Displays a warning</summary>
/// <param name="messageService">
/// Message service that will be used to display the warning
/// </param>
/// <param name="text">Text to be displayed on the warning message</param>
public static void Warn(
this IMessageService messageService, MessageText text
) {
messageService.ShowNotification(MessageBoxIcon.Warning, text);
}
/// <summary>Reports an error</summary>
/// <param name="messageService">
/// Message service that will be used to display the warning
/// </param>
/// <param name="text">Text to be displayed on the warning message</param>
public static void ReportError(
this IMessageService messageService, MessageText text
) {
messageService.ShowNotification(MessageBoxIcon.Error, text);
}
}
} // namespace Nuclex.Windows.Forms.Messages

View File

@ -0,0 +1,51 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Windows;
using NUnit.Framework;
namespace Nuclex.Windows.Forms.Messages {
/// <summary>Unit tests for the message text container</summary>
[TestFixture]
internal class MessageTextTest {
/// <summary>Ensures that the message text class provides a copy constructor</summary>
[Test]
public void HasCopyConstructor() {
var text = new MessageText() {
Caption = "Caption",
Message = "Message",
Details = "Details",
ExpandedDetails = "ExpandedDetails"
};
var copy = new MessageText(text);
Assert.AreEqual(text.Caption, copy.Caption);
Assert.AreEqual(text.Message, copy.Message);
Assert.AreEqual(text.Details, copy.Details);
Assert.AreEqual(text.ExpandedDetails, copy.ExpandedDetails);
}
}
} // namespace Nuclex.Windows.Forms.Messages

View File

@ -0,0 +1,55 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
namespace Nuclex.Windows.Forms.Messages {
/// <summary>Text that will be displayed in a message box</summary>
public class MessageText {
/// <summary>Initializs a new message text</summary>
public MessageText() { }
/// <summary>Initializes a new message text by copying another instance</summary>
/// <param name="other">Instance that will be copied</param>
public MessageText(MessageText other) {
Caption = other.Caption;
Message = other.Message;
Details = other.Details;
ExpandedDetails = other.ExpandedDetails;
}
/// <summary>The caption used when the is displayed in a message box</summary>
public string Caption { get; set; }
/// <summary>Main message being displayed to the user</summary>
public string Message { get; set; }
/// <summary>Message details shown below the main message</summary>
public string Details { get; set; }
/// <summary>
/// Additional informations the user can display by expanding
/// the message dialog. Can be null, in which case the message dialog
/// will not be expandable.
/// </summary>
public string ExpandedDetails { get; set; }
}
} // namespace Nuclex.Windows.Forms.Messages

View File

@ -0,0 +1,162 @@
#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Windows.Forms;
namespace Nuclex.Windows.Forms.Messages {
/// <summary>Uses task dialogs to display message boxes</summary>
public class StandardMessageBoxManager : IMessageService {
#region class MessageScope
/// <summary>Triggers the message displayed and acknowledged events</summary>
private class MessageScope : IDisposable {
/// <summary>
/// Initializes a new message scope, triggering the message displayed event
/// </summary>
/// <param name="self">Message service the scope belongs to</param>
/// <param name="image">Image of the message being displayed</param>
/// <param name="text">Text contained in the message being displayed</param>
public MessageScope(
StandardMessageBoxManager self, MessageBoxIcon image, MessageText text
) {
EventHandler<MessageEventArgs> messageDisplayed = self.MessageDisplaying;
if(messageDisplayed != null) {
messageDisplayed(this, new MessageEventArgs(image, text));
}
this.self = self;
}
/// <summary>Triggers the message acknowledged event</summary>
public void Dispose() {
EventHandler messageAcknowledged = self.MessageAcknowledged;
if(messageAcknowledged != null) {
messageAcknowledged(this, EventArgs.Empty);
}
}
/// <summary>Message service the scope belongs to</summary>
private StandardMessageBoxManager self;
}
#endregion // class MessageScope
/// <summary>Delegate for the standard message box show function</summary>
/// <param name="owner">Window that will modally display the message box</param>
/// <param name="text">Text that will be presented to the user</param>
/// <param name="caption">Contents of the message box' title bar</param>
/// <param name="buttons">Buttons available for the user to choose from</param>
/// <param name="icon">Icon that will be displayed next to the text</param>
/// <returns>The choice made by the user if multiple buttons were provided</returns>
private delegate DialogResult ShowMessageBoxDelegate(
IWin32Window owner,
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon
);
/// <summary>Triggered when a message is displayed to the user</summary>
public event EventHandler<MessageEventArgs> MessageDisplaying;
/// <summary>Triggered when the user has acknowledged the current message</summary>
public event EventHandler MessageAcknowledged;
/// <summary>Initializes a new task dialog message service</summary>
public StandardMessageBoxManager() : this(NullActiveWindowTracker.Default) { }
/// <summary>Initializes a new task dialog message service</summary>
/// <param name="tracker">
/// Active window tracker used to obtain the parent window for message boxes
/// </param>
public StandardMessageBoxManager(IActiveWindowTracker tracker) {
this.tracker = tracker;
this.showMessageDelegate = new ShowMessageBoxDelegate(MessageBox.Show);
}
/// <summary>Asks the user a question that can be answered via several buttons</summary>
/// <param name="image">Image that will be shown on the message box</param>
/// <param name="text">Text that will be shown to the user</param>
/// <param name="buttons">Buttons available for the user to click on</param>
/// <returns>The button the user has clicked on</returns>
public DialogResult ShowQuestion(
MessageBoxIcon image, MessageText text, MessageBoxButtons buttons
) {
using(var scope = new MessageScope(this, image, text)) {
return showMessageBoxInActiveUiThread(
text.Message,
text.Caption,
buttons,
image
);
}
}
/// <summary>Displays a notification to the user</summary>
/// <param name="image">Image that will be shown on the message bx</param>
/// <param name="text">Text that will be shown to the user</param>
public void ShowNotification(MessageBoxIcon image, MessageText text) {
using(var scope = new MessageScope(this, image, text)) {
showMessageBoxInActiveUiThread(
text.Message,
text.Caption,
MessageBoxButtons.OK,
image
);
}
}
/// <summary>Displays the message box in the active view's thread</summary>
/// <param name="message">Text that will be presented to the user</param>
/// <param name="caption">Contents of the message box' title bar</param>
/// <param name="buttons">Buttons available for the user to choose from</param>
/// <param name="image">Image that will be displayed next to the text</param>
/// <returns></returns>
private DialogResult showMessageBoxInActiveUiThread(
string message,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon image
) {
Form mainWindow = this.tracker.ActiveWindow;
if(mainWindow != null) {
return (DialogResult)mainWindow.Invoke(
this.showMessageDelegate,
(IWin32Window)mainWindow, message, caption, buttons, image
);
}
// No window tracker or unknown main window -- just show the message box
return MessageBox.Show(message, caption, buttons, image);
}
/// <summary>Provides the currently active top-level window</summary>
private IActiveWindowTracker tracker;
/// <summary>Delegate for the MessageBox.Show() method</summary>
private ShowMessageBoxDelegate showMessageDelegate;
}
} // namespace Nuclex.Windows.Forms.Messages

View File

@ -1,4 +1,24 @@
using System; #region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2019 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using NUnit.Framework; using NUnit.Framework;