using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Microsoft.Win32.TaskScheduler
{
///
/// Displays a in a control. Mimics list in MMC.
///
[System.Drawing.ToolboxBitmap(typeof(Microsoft.Win32.TaskScheduler.TaskEditDialog), "TaskControl")]
public partial class TaskListView : UserControl
{
private TaskCollection coll;
///
/// Initializes a new instance of the class.
///
public TaskListView()
{
InitializeComponent();
smallImageList.Images.Add(new System.Drawing.Icon(EditorProperties.Resources.ts, 0x10, 0x10));
}
///
/// Occurs when task selected in the list.
///
public event EventHandler TaskSelected;
///
/// Gets or sets the associated with this control.
///
/// The for this control, or null if there is no . The default is null.
public override ContextMenuStrip ContextMenuStrip
{
get { return listView1.ContextMenuStrip; }
set
{
if (listView1.ContextMenuStrip != value)
{
if (listView1.ContextMenuStrip != null)
listView1.ContextMenuStrip.Opening -= listView1ContextMenuStrip_Opening;
listView1.ContextMenuStrip = value;
if (value != null)
listView1.ContextMenuStrip.Opening += listView1ContextMenuStrip_Opening;
}
}
}
///
/// Gets or sets the zero-based index of the currently selected item in a .
///
///
/// A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.
///
public int SelectedIndex
{
get
{
return listView1.SelectedIndices.Count == 0 ? -1 : listView1.SelectedIndices[0];
}
set
{
foreach (int i in listView1.SelectedIndices)
listView1.Items[i].Selected = false;
if (value != -1)
listView1.Items[value].Selected = true;
}
}
///
/// Gets or sets the tasks.
///
/// The tasks.
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), RefreshProperties(RefreshProperties.All)]
public TaskCollection Tasks
{
get { return coll; }
set
{
coll = value;
listView1.BeginUpdate();
listView1.Items.Clear();
if (coll != null)
foreach (var item in coll)
listView1.Items.Add(LVIFromTask(item));
listView1.EndUpdate();
}
}
///
/// Retrieves the item at the specified location.
///
/// The x-coordinate of the location to search for an item (expressed in client coordinates).
/// The y-coordinate of the location to search for an item (expressed in client coordinates).
/// A that represents the item at the specified position. If there is no item at the specified location, the method returns null.
public Task GetItemAt(int x, int y)
{
ListViewItem item = listView1.GetItemAt(x, y);
return item == null ? null : (Task)item.Tag;
}
///
/// Raises the event.
///
/// The instance containing the event data.
protected virtual void OnTaskSelected(TaskSelectedEventArgs e)
{
EventHandler handler = TaskSelected;
if (handler != null)
handler(this, e);
}
private void listView1ContextMenuStrip_Opening(object sender, CancelEventArgs e)
{
if (listView1.SelectedItems.Count <= 0)
e.Cancel = true;
}
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
if (this.ContextMenuStrip != null && e.Button == System.Windows.Forms.MouseButtons.Right)
{
ListViewItem item = listView1.GetItemAt(e.X, e.Y);
if (item != null)
{
item.Selected = true;
if (this.ContextMenuStrip != null)
this.ContextMenuStrip.Show(listView1, e.Location);
}
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
Task t = null;
if (listView1.SelectedIndices.Count == 1)
t = (Task)listView1.SelectedItems[0].Tag;
OnTaskSelected(new TaskSelectedEventArgs(t));
}
private ListViewItem LVIFromTask(Task task)
{
bool disabled = task.State == TaskState.Disabled;
TaskDefinition td = null;
try { td = task.Definition; } catch { }
ListViewItem lvi = new ListViewItem(new string[] {
task.Name,
TaskEnumGlobalizer.GetString(task.State),
td == null ? "" : task.Definition.Triggers.ToString(),
disabled || task.NextRunTime < DateTime.Now ? string.Empty : task.NextRunTime.ToString("G"),
task.LastRunTime == DateTime.MinValue ? EditorProperties.Resources.Never : task.LastRunTime.ToString("G"),
task.LastRunTime == DateTime.MinValue ? string.Empty : task.State == TaskState.Running ? string.Format(EditorProperties.Resources.LastResultRunning, task.LastTaskResult) : ((task.LastTaskResult == 0 ? EditorProperties.Resources.LastResultSuccessful : string.Format("(0x{0:X})", task.LastTaskResult))),
td == null ? "" : task.Definition.RegistrationInfo.Author,
string.Empty
}, 0) { Tag = task };
return lvi;
}
///
/// Event args for when a task is selected.
///
public class TaskSelectedEventArgs : EventArgs
{
///
/// Empty class.
///
public static new readonly TaskSelectedEventArgs Empty = new TaskSelectedEventArgs();
private TaskSelectedEventArgs() : base() { }
///
/// Initializes a new instance of the class.
///
/// The task.
internal TaskSelectedEventArgs(Task task = null)
{
this.Task = task;
}
///
/// Gets the task.
///
/// The task.
public Task Task { get; private set; }
}
}
}