164 lines
4.7 KiB
C#
164 lines
4.7 KiB
C#
using System;
|
|
using System.Windows;
|
|
using System.Windows.Forms;
|
|
using JD.Kernel.Classes;
|
|
using JD.Kernel.StaticHelpers;
|
|
using JD.LogService;
|
|
using JD.Threading.Dispatch;
|
|
using MessageBox = System.Windows.MessageBox;
|
|
|
|
namespace JD.MessageService
|
|
{
|
|
/// <summary>
|
|
/// Default message service implementation that pops dialogs.
|
|
/// </summary>
|
|
public class GuiMessageService : IMessageService, IDisposable
|
|
{
|
|
#region Variables
|
|
|
|
private static Window m_mainForm;
|
|
private TrayBalloon m_balloon;
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
public void Dispose()
|
|
{
|
|
DisposableHelper.SafeDispose(m_balloon);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// The main application form. Set this so that message boxes know their parent.
|
|
/// </summary>
|
|
public static System.Windows.Window MainForm
|
|
{
|
|
get
|
|
{
|
|
return m_mainForm;
|
|
}
|
|
set
|
|
{
|
|
m_mainForm = value;
|
|
}
|
|
}
|
|
|
|
private TrayBalloon Balloon
|
|
{
|
|
get
|
|
{
|
|
Throw.InvalidOperationIf(m_balloon == null, "Message service is not initialized");
|
|
return m_balloon;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <summary>
|
|
/// Initializes the resources of the message service (should be called in the Main thread).
|
|
/// </summary>
|
|
public void Initialize()
|
|
{
|
|
Throw.InvalidOperationIf(m_balloon != null, "Cannot initialize twice");
|
|
m_balloon = new TrayBalloon();
|
|
}
|
|
|
|
public void Show(LogCategory category, string message, string caption)
|
|
{
|
|
MessageBoxImage icon = GetMessageBoxIconFromCategory(category);
|
|
|
|
SafeThreadCall(delegate
|
|
{
|
|
MessageBox.Show(
|
|
message,
|
|
caption,
|
|
MessageBoxButton.OK,
|
|
icon,
|
|
MessageBoxResult.OK);
|
|
});
|
|
}
|
|
|
|
private static MessageBoxImage GetMessageBoxIconFromCategory(LogCategory category)
|
|
{
|
|
switch (category)
|
|
{
|
|
case LogCategory.Message:
|
|
return MessageBoxImage.Information;
|
|
case LogCategory.Warning:
|
|
return MessageBoxImage.Warning;
|
|
case LogCategory.Error:
|
|
return MessageBoxImage.Error;
|
|
default:
|
|
throw new NotImplementedException("Code path not implemented!");
|
|
}
|
|
}
|
|
|
|
public MessageBoxResult AskQuestion(string question, string caption, MessageBoxImage icon, MessageBoxButton buttons, MessageBoxResult defaultResult)
|
|
{
|
|
MessageBoxResult result = MessageBoxResult.Cancel;
|
|
|
|
SafeThreadCall(delegate
|
|
{
|
|
result = MessageBox.Show(question, caption, buttons, icon, defaultResult);
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
public MessageDialog.Button ShowMessageDialog(MessageDialog dialog)
|
|
{
|
|
MessageDialog.Button result = null;
|
|
SafeThreadCall(delegate
|
|
{
|
|
var parentWindow = (IWin32Window)MainForm;
|
|
result = TaskDialog.Show(parentWindow, dialog);
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
public string ShowInputBox(string question, string caption, string defaultValue, MessageService.ValidationPredicate validationPredicate)
|
|
{
|
|
string result = null;
|
|
SafeThreadCall(delegate
|
|
{
|
|
using (InputBox inputBox = new InputBox(question, caption, defaultValue))
|
|
{
|
|
string errorMessage = null;
|
|
|
|
do
|
|
{
|
|
inputBox.ShowDialog((IWin32Window)MainForm, errorMessage);
|
|
|
|
if (inputBox.Result == null || validationPredicate == null || (errorMessage = validationPredicate(inputBox.Result)) == null)
|
|
{
|
|
result = inputBox.Result;
|
|
break;
|
|
}
|
|
}
|
|
while (true);
|
|
}
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
public IBalloon ShowBalloon(TimeSpan timeout, string title, string text, ToolTipIcon icon, Action clickAction)
|
|
{
|
|
return Balloon.Show(timeout, title, text, icon, clickAction);
|
|
}
|
|
|
|
private static void SafeThreadCall(System.Action action)
|
|
{
|
|
GuiDispatcher.Current.Dispatch(action);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|