/*
Copyright (c) Microsoft Corporation
All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License
at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER
EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF
TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions and
limitations under the License.
*/
using System.Drawing;
using System;
using System.Windows.Forms;
namespace Microsoft.Research.Tools
{
///
/// Delegate used for invoking status messages from accross threads.
///
/// Message to display.
/// Severity of status.
public delegate void StatusDelegate(string message, StatusKind statusKind);
///
/// A class which writes status messages.
/// It remembers the default color of the status strip.
///
public class StatusWriter
{
///
/// The default color of the status strip; assumes that all status strips have the same color.
///
private Color defaultStatusStripBackColor;
///
/// Status strip used to display messages.
///
private ToolStripStatusLabel label;
///
/// The parent of the status strip, invoked when doing cross-thread operations.
///
private Control parentControl;
private StatusDelegate crossInvoke;
///
/// Initialize a status writer.
///
/// Status strip holding the messages.
/// Parent which contains the status writer.
/// (May be invoked recursively across threads).
/// Delegate to invoke cross-threads in parent.
public StatusWriter(ToolStripStatusLabel strip, Control parent, StatusDelegate crossInvoke)
{
this.label = strip;
this.parentControl = parent;
this.defaultStatusStripBackColor = strip.BackColor;
this.crossInvoke = crossInvoke;
}
///
/// Write a message on the status strip, with a given severity.
///
/// Message to display.
/// Severity of message.
public void Status(string message, StatusKind kind)
{
if (parentControl.InvokeRequired)
{
parentControl.Invoke(this.crossInvoke, new object[] { message, kind });
}
else
{
if (kind == StatusKind.Error)
this.label.BackColor = Color.Red;
else
this.label.BackColor = defaultStatusStripBackColor;
if (kind == StatusKind.LongOp)
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
else
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
this.label.Text = message;
this.parentControl.Refresh();
Console.WriteLine("{0}: {1}, {2}", DateTime.Now.ToString("hh:mm:ss.ff"), kind, message);
}
}
}
}