using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using IReaper.CourseData;
using IReaper.FileData;
using IReaper.Command;
using System.Threading;
namespace IReaper.CommonGui{
public partial class FileViewToolStrip : ToolStrip
{
public FileViewToolStrip()
{
InitializeComponent();
}
public FileViewToolStrip(IContainer container)
{
container.Add(this);
InitializeComponent();
CustomInit();
}
private void CustomInit()
{
Core.CoreData.PropertyChanged += new PropertyChangedEventHandler(CoreComponent_PropertyChanged);
CourseFileData.RLChanged += new EventHandler(CourseFileData_LifetimeChanged);
// Load strip image
// Pause
this.toolStripBPause.Image = global::IReaper.Properties.Resources.Stop;
this.toolStripBPauseAll.Image = global::IReaper.Properties.Resources.StopAll;
// Remove
this.toolStripBRemove.Image = global::IReaper.Properties.Resources.Clear;
this.toolStripBRemoveAll.Image = global::IReaper.Properties.Resources.ClearAll;
// Run
this.toolStripBStart.Image = global::IReaper.Properties.Resources.Run;
this.toolStripBStartAll.Image = global::IReaper.Properties.Resources.RunAll;
// Load button enable
UpdateButton();
}
void CourseFileData_LifetimeChanged(object sender, EventArgs e)
{
UpdateButton();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CoreComponent_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
/*
*
*/
if (e.PropertyName == CoreDataType.CurrentSelectedFiles.ToString() || e.PropertyName == CoreDataType.CurrentViewedFiles.ToString())
{
UpdateButton();
}
}
/// <summary>
/// Update single file operation buttons stats
/// </summary>
private void UpdateButton()
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ThreadStart(UpdateButton));
return;
}
IList<CourseFileData> files = Core.CoreData[CoreDataType.CurrentSelectedFiles] as IList<CourseFileData>;
bool canStart, canPause, canRemove;
this.CheckButtonStatus(files, out canPause, out canStart, out canRemove);
files = Core.CoreData[CoreDataType.CurrentViewedFiles] as IList<CourseFileData>;
bool canStartAll, canPauseAll, canRemoveAll;
this.CheckButtonStatus(files, out canPauseAll, out canStartAll, out canRemoveAll);
this.BeginInvoke(new System.Threading.ThreadStart(delegate()
{
this.toolStripBPause.Enabled = canPause;
this.toolStripBStart.Enabled = canStart;
this.toolStripBRemove.Enabled = canRemove;
this.toolStripBPauseAll.Enabled = canPauseAll;
this.toolStripBStartAll.Enabled = canStartAll;
this.toolStripBRemoveAll.Enabled = canRemoveAll;
}));
}
/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <param name="canPause"></param>
/// <param name="canStart"></param>
/// <param name="canRemove"></param>
private void CheckButtonStatus(IList<CourseFileData> data, out bool canPause, out bool canStart, out bool canRemove)
{
canPause = false;
canStart = false;
canRemove = false;
if (data == null)
{
return;
}
for (int i = 0; i < data.Count; i++)
{
CourseFileData file = data[i];
if (file.RunState == RunningStatue.Running ||
file.RunState == RunningStatue.RunQueued ||
file.RunState == RunningStatue.RunRequest)
canPause |= true;
if (file.RunState == RunningStatue.Created ||
file.RunState == RunningStatue.Error ||
file.RunState == RunningStatue.Stopped)
canStart |= true;
}
canRemove = canStart;//
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PauseDownload(object sender, EventArgs e)
{
CourseFileDataCollection files = this.GetPredictedFiles(sender);
CommandBase command = CommandManager.GetCommand(CommandFamily.Command_PauseDownload);
command.ExecuteAsync(this, files);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void StartDownload(object sender, EventArgs e)
{
CourseFileDataCollection files = this.GetPredictedFiles(sender);
CommandBase command = CommandManager.GetCommand(CommandFamily.Command_DownloadFile);
command.ExecuteAsync(this, files);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RemoveDownload(object sender, EventArgs e)
{
CourseFileDataCollection files = this.GetPredictedFiles(sender);
CommandBase command = CommandManager.GetCommand(CommandFamily.Command_RemoveFile);
command.ExecuteAsync(this,files,RemoveFileTaskTypeEnum.Incompleted);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <returns></returns>
private CourseFileDataCollection GetPredictedFiles(object sender)
{
ToolStripItem item = sender as ToolStripItem;
string tagMessage;
CourseFileDataCollection files = null;
if (item == null)
{
tagMessage = null;
}
else
{
tagMessage = item.Tag as string;
}
if (tagMessage == "All")
{
files = ((CourseFileDataCollection)Core.CoreData[CoreDataType.CurrentViewedFiles]);
}
else
{
files = ((CourseFileDataCollection)Core.CoreData[CoreDataType.CurrentSelectedFiles]);
}
return files;
}
}
}
|