using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Microsoft.Win32.TaskScheduler
{
///
/// A dialog for selecting tasks or task folders.
///
[ToolboxItem(true), ToolboxItemFilter("System.Windows.Forms"), Description("Dialog allowing the browsing of all tasks on the specified machine.")]
[Designer("System.ComponentModel.Design.ComponentDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.Drawing.ToolboxBitmap(typeof(TaskEditDialog), "TaskDialog")]
public partial class TaskBrowserDialog :
#if DEBUG
Form
#else
DialogBase
#endif
{
private bool internalSetTS = false;
private string path;
private Type pathType = null;
///
/// Initializes a new instance of the class.
///
public TaskBrowserDialog()
{
InitializeComponent();
smallImages.Images.Add(new Icon(EditorProperties.Resources.tsFolderClosed, 16, 16));
smallImages.Images.Add(new Icon(EditorProperties.Resources.folder, 16, 16));
smallImages.Images.Add(new Icon(EditorProperties.Resources.ts, 16, 16));
AllowFolderSelection = false;
ShowTasks = true;
}
///
/// Gets or sets a value indicating whether folders may be selected.
///
///
/// true if folders may be selected; otherwise, false.
///
[DefaultValue(false), Category("Task Browsing"), Description("Allows task folders to be selected.")]
public bool AllowFolderSelection { get; set; }
///
/// Gets or sets the description.
///
///
/// The description.
///
[Category("Task Browsing"), Description("The string that is displayed above the tree view control in the dialog.")]
public string Description
{
get { return descLabel.Text; }
set { descLabel.Text = value; }
}
///
/// Gets or sets the selected item path. The path may reference either a or a .
///
/// The selected item path.
[DefaultValue(null), Category("Task Browsing"), Description("The path to the task or folder first selected in the dialog or the last one selected by the user.")]
public string SelectedPath
{
get { return path; }
set
{
if (!string.Equals(path, value, StringComparison.InvariantCultureIgnoreCase))
{
path = value;
if (this.treeView.Nodes.Count > 0)
SelectNodeByKey(path);
}
}
}
///
/// Gets the type of the selected path. Value is null if unresolved or not selected.
///
///
/// The type of the selected path.
///
[Browsable(false)]
public Type SelectedPathType
{
get { return pathType; }
}
///
/// Gets or sets a value indicating whether to show tasks along with the folders.
///
///
/// true if tasks are to be shown; otherwise, false.
///
[DefaultValue(true), Category("Task Browsing"), Description("Shows both tasks and folders within the tree view.")]
public bool ShowTasks { get; set; }
///
/// Gets or sets the .
///
/// The task service.
[DefaultValue(null), Category("Task Browsing"), Description("The TaskService for this dialog")]
public TaskService TaskService { get; set; }
private void cancelButton_Click(object sender, EventArgs e)
{
Close();
}
private void okButton_Click(object sender, EventArgs e)
{
if (treeView.SelectedNode != null && treeView.SelectedNode.Tag != null)
{
if (treeView.SelectedNode.Tag is Task)
SelectedPath = (treeView.SelectedNode.Tag as Task).Path;
else
SelectedPath = (treeView.SelectedNode.Tag as TaskFolder).Path;
pathType = treeView.SelectedNode.Tag.GetType();
}
else
{
SelectedPath = null;
pathType = null;
}
Close();
}
private void RefreshList()
{
string curPath = treeView.SelectedNode == null ? this.SelectedPath : treeView.SelectedNode.Name;
this.TaskService.AllowReadOnlyTasks = true;
treeView.Nodes.Clear();
loadingLabel.Visible = true;
treeView.UseWaitCursor = true;
treeView.BeginUpdate();
TreeNode n = treeView.Nodes.Add(@"\", string.Format("Task Scheduler Library ({0})", this.TaskService.TargetServer == null || this.TaskService.TargetServer.Equals(Environment.MachineName, StringComparison.InvariantCultureIgnoreCase) ? "Local" : this.TaskService.TargetServer), 0, 0);
n.Tag = this.TaskService.RootFolder;
n.Expand();
LoadChildren(n);
loadingLabel.Visible = false;
treeView.EndUpdate();
treeView.UseWaitCursor = false;
if (!string.IsNullOrEmpty(curPath))
SelectNodeByKey(curPath);
treeView.Focus();
}
private TreeNode SelectNodeByKey(string key)
{
TreeNode ret = null;
var nodes = treeView.Nodes.Find(key, true);
if (nodes != null && nodes.Length > 0)
{
ret = nodes[0];
treeView.SelectedNode = ret;
}
return ret;
}
private void LoadChildren(TreeNode p)
{
foreach (var item in ((TaskFolder)p.Tag).SubFolders)
{
TreeNode n = p.Nodes.Add(item.Path, item.Name, 1, 1);
n.Tag = item;
LoadChildren(n);
}
if (ShowTasks)
{
foreach (var t in ((TaskFolder)p.Tag).Tasks)
{
TreeNode tn = p.Nodes.Add(t.Path, t.Name, 2, 2);
tn.Tag = t;
}
}
}
private void ResetDescription()
{
var rm = new System.Resources.ResourceManager(this.GetType());
this.Description = rm.GetString("descLabel.Text");
}
private bool ShouldSerializeDescription()
{
var rm = new System.Resources.ResourceManager(this.GetType());
return !string.Equals(this.Description, rm.GetString("descLabel.Text"));
}
private bool ShouldSerializeTaskService()
{
return !internalSetTS;
}
private void TaskBrowserDialog_Load(object sender, EventArgs e)
{
if (this.TaskService == null)
{
TaskService = new TaskService();
internalSetTS = true;
}
RefreshList();
}
private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
okButton.Enabled = treeView.SelectedNode != null;
}
private void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
if (!AllowFolderSelection && e.Node.Tag is TaskFolder)
e.Cancel = true;
}
private void treeView_Resize(object sender, EventArgs e)
{
loadingLabel.SetBounds(treeView.Left + ((treeView.Width - loadingLabel.Width) / 2),
treeView.Top + ((treeView.Height - loadingLabel.Height) / 2),
loadingLabel.Width, loadingLabel.Height);
}
}
}