using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraLayout.Utils;
using DevExpress.Utils;
using DevExpress.XtraTreeList.Nodes;
namespace DPumpHydr.WinFrmUI.Volute
{
public partial class StepTreeListCtrl : XtraUserControl
{
public StepTreeListCtrl()
{
InitializeComponent();
this.layoutControl1.AllowCustomization = false;//AllowCustomizationMenu
this.layoutControl1.OptionsView.UseParentAutoScaleFactor = true;
RegistEvents();
}
///
/// 步骤节点选择改变后事件
///
public event Action AfterStepNodeSelectedChangedEvent;
///
/// 步骤节点选择改变前触发事件
///
public event Func BeforeStepNodeSelectedChangedEvent;
///
/// 选中的节点
///
public TreeStepNode SelectedStepNode
{
get { return _selStepNode; }
private set
{
var selStepNode = _selStepNode;
_selStepNode = value;
if (selStepNode == null)
{
if (_selStepNode != null)
{
if (this.AfterStepNodeSelectedChangedEvent != null)
this.AfterStepNodeSelectedChangedEvent(selStepNode, _selStepNode);
}
}
else if ( !System.Object.ReferenceEquals(selStepNode, _selStepNode))
{
if (this.AfterStepNodeSelectedChangedEvent != null)
this.AfterStepNodeSelectedChangedEvent(selStepNode, _selStepNode);
}
this.treeList1.Refresh();
}
}
private TreeStepNode _selStepNode = null;
public List GetStepSource()
{
return this.treeList1.DataSource as List;
}
///
/// 设置步骤数据源
///
///
public void SetStepSource(List value)
{
this.treeList1.DataSource = value;
this.treeList1.RefreshDataSource();
if (value == null || value.Count < 1)
{
_selStepNode = null;
}
else
{
this.treeList1.ExpandAll();
if (!value.Contains(_selStepNode))
{
var firstStepNode = value.Find(x => x.AllowSelect);
var focusedNode = this.treeList1.FindNodeByKeyID(firstStepNode.ID);
this.treeList1.FocusedNode = focusedNode;
this.SelectedStepNode = firstStepNode;
}
}
}
///
/// 设置步骤数据源
///
///
///
public void SetStepSource(List list,int ID)
{
this.treeList1.DataSource = list;
this.treeList1.RefreshDataSource();
if (list == null || list.Count < 1)
{
_selStepNode = null;
}
else
{
this.treeList1.ExpandAll();
var selStepNode = list.Find(x => x.ID == ID && x.AllowSelect);
if (selStepNode == null)
{
if(!list.Contains(_selStepNode))
selStepNode = list.Find(x => x.AllowSelect);
}
if (selStepNode != null)
{
var focusedNode = this.treeList1.FindNodeByKeyID(selStepNode.ID);
this.treeList1.FocusedNode = focusedNode;
this.SelectedStepNode = selStepNode;
}
}
}
public void ResetStepSource(List list)
{
this.treeList1.DataSource = list;
this.treeList1.RefreshDataSource();
}
#region 私有方法
//注册事件
private void RegistEvents()
{
this.treeList1.GetSelectImage += (sender, e) =>
{
var ID = Convert.ToInt64(e.Node.GetValue("ID"));
var stepNode = GetStepSource().Find(x => x.ID == ID);
if (stepNode == null)
return;
switch (stepNode.DataState)
{
case TreeStepNode.eDataState.错误: e.NodeImageIndex = 0; break;
case TreeStepNode.eDataState.正确: e.NodeImageIndex = 1; break;
case TreeStepNode.eDataState.未知: e.NodeImageIndex = -1; break;
default: break;
}
};
this.treeList1.GetStateImage += (sender, e) =>
{
var ID = Convert.ToInt64(e.Node.GetValue("ID"));
var stepNode = GetStepSource().Find(x => x.ID == ID);
if (stepNode == null)
return;
switch (stepNode.ProgressState)
{
case TreeStepNode.eProgressState.未完成: e.NodeImageIndex = 0; break;
case TreeStepNode.eProgressState.正在进行: e.NodeImageIndex = 1; break;
case TreeStepNode.eProgressState.已完成: e.NodeImageIndex = 2; break;
default: break;
}
};
this.treeList1.BeforeFocusNode += (sender, e) =>
{
var nextStepID = Convert.ToInt64(e.Node.GetValue("ID"));
var nextStepNode = GetStepSource().Find(x => x.ID == nextStepID);
if (!nextStepNode.AllowSelect)
{
e.CanFocus = false;
return;
}
if (_selStepNode != null)
{
if (_selStepNode.ID == nextStepNode.ID)
return;
}
if (this.BeforeStepNodeSelectedChangedEvent != null)
{
TreeStepNode beforeStepNode = null;
if (e.OldNode != null)
{
var beforeStepID = Convert.ToInt64(e.OldNode.GetValue("ID"));
beforeStepNode = GetStepSource().Find(x => x.ID == beforeStepID);
}
if (!this.BeforeStepNodeSelectedChangedEvent(beforeStepNode, nextStepNode))
{
e.CanFocus = false;
return;
}
}
};
this.treeList1.AfterFocusNode += (sender, e) =>
{
var ID = Convert.ToInt64(e.Node.GetValue("ID"));
var nodeModel = GetStepSource().Find(x => x.ID == ID);
this.SelectedStepNode = nodeModel;
};
this.treeList1.AfterExpand += (sender, e) =>
{
if (_selStepNode != null)
{
var node = this.treeList1.FindNodeByKeyID(_selStepNode.ID);
if (e.Node.Nodes.Contains(node))
this.treeList1.FocusedNode = node;
}
};
}
#endregion
#region 公有方法
///
/// 根据步骤ID聚焦节点
///
///
public void SetFocusedNode(long StepNodeID)
{
var treeNode = this.treeList1.FindNodeByKeyID(StepNodeID);
if (treeNode != null)
this.treeList1.FocusedNode = treeNode;
}
///
/// 上一步
///
public void GoPrev()
{
if (_selStepNode == null)
return;
var allCanSelStepNodes = GetStepSource().Where(x => x.AllowSelect).ToList();
var index = allCanSelStepNodes.IndexOf(_selStepNode);
if (index <= 0)
return;
var prevStepNode = allCanSelStepNodes[index - 1];
var prevNode = this.treeList1.FindNodeByKeyID(prevStepNode.ID);
this.treeList1.FocusedNode = prevNode;
}
///
/// 下一步
///
public void GoNext()
{
if (_selStepNode == null)
return;
var allCanSelStepNodes = GetStepSource().Where(x => x.AllowSelect).ToList();
var index = allCanSelStepNodes.IndexOf(_selStepNode);
if (index == allCanSelStepNodes.Count - 1)
{
return;
}
var nextStepNode = allCanSelStepNodes[index + 1];
var nextNode = this.treeList1.FindNodeByKeyID(nextStepNode.ID);
this.treeList1.FocusedNode = nextNode;
}
#endregion
}
}