CEL_Payroll/Payroll.Controls/CustomControls/frmCustomBasic.cs
2024-09-17 14:30:13 +06:00

611 lines
23 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Ease.CoreV35;
using Ease.CoreV35.Model;
using Payroll.BO;
namespace Payroll.Controls
{
public partial class frmCustomBasic : Form
{
#region Decleration
private ControlManager _oManager = null;
private string[] _columnWidth = null;
private ObjectsTemplate<ObjectTemplate> _oCollectionBase = null;
private System.Reflection.PropertyInfo cpInfo;
private Hashtable _Code = null;
#endregion
public frmCustomBasic()
{
InitializeComponent();
}
#region Show Dialog
public void CopyCollList<T>(List<T> source) where T : ObjectTemplate
{
_oCollectionBase = new ObjectsTemplate<ObjectTemplate>();
foreach (var item in source)
_oCollectionBase.Add(item);
}
public int[] ShowDialog(ControlManager oManager)
{
try
{
this.Cursor = Cursors.WaitCursor;
_oManager = oManager;
_columnWidth = null;
if (_oManager.IsMultipleSelection == false)
{
lvwItems.Columns.Clear();
}
if (_oManager.ControlType == EnumCustomBasicControlType.ListView)
{
lvwItems.Visible = true;
tvwItems.Visible = false;
lvwItems.CheckBoxes = _oManager.IsMultipleSelection;
RefreshList();
}
else
{
lvwItems.Visible = false;
tvwItems.Visible = true;
tvwItems.CheckBoxes = _oManager.IsMultipleSelection;
_Code = new Hashtable();
RefreshTree();
}
this.Text = _oManager.PickerCaption;
this.Cursor = Cursors.Default;
this.ShowDialog();
}
catch (Exception ex)
{
this.Cursor = Cursors.Default;
MessageBox.Show("Failed to load." + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.Cursor = Cursors.Default;
return _oManager.SelectedIDs;
}
#endregion
#region refresh List
private void RefreshList()
{
BuildListViewHeader();
if (_oManager.Parameters.Contains("ID,") == false)
_oManager.Parameters = "ID," + _oManager.Parameters;
string[] sProperties = _oManager.Parameters.Split(',');
if (sProperties.Length < 1)
{
throw new Exception("At least One Property need to be sent");
}
System.Collections.IEnumerator oEnumerator = _oCollectionBase.GetEnumerator();
while (oEnumerator.MoveNext())
{
ListViewItem oItem = new ListViewItem();
for (int n = 0; n < sProperties.Length; n++)
{
System.Reflection.PropertyInfo oInfo = oEnumerator.Current.GetType().GetProperty(sProperties[n]);
object oValue = oInfo.GetValue(oEnumerator.Current, null);
try
{
if (n == 0)
{
oItem.Tag = ((ID)oValue).Integer;
if (_oManager.SelectedIDs != null)
{
for (int nn = 0; nn < _oManager.SelectedIDs.Length; nn++)
{
if (((ID)oValue).Integer == _oManager.SelectedIDs[nn])
oItem.Checked = true;
}
}
}
else if (n == 1)
{
if (_oManager.IsMultipleSelection == false)
oItem.Text = oValue.ToString();
else
oItem.SubItems.Add(oValue.ToString());
}
else
{
oItem.SubItems.Add(oValue.ToString());
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
if (oItem != null) { lvwItems.Items.Add(oItem); }
}
if (lvwItems.CheckedItems.Count == lvwItems.Items.Count)
{
btnSelectAll.Text = "Deselect &All";
}
else
{
btnSelectAll.Text = "Select &All";
}
lblTotal.Text = "Total Records :: " + lvwItems.Items.Count.ToString();
}
#endregion
#region Refresh Tree
private void tvwItems_AfterSelect(object sender, TreeViewEventArgs e)
{
if (tvwItems.SelectedNode.Nodes.Count > 0)
return;
this.Cursor = Cursors.WaitCursor;
ExpendNode(tvwItems.SelectedNode);
this.Cursor = Cursors.Default;
}
private void ExpendNode(TreeNode oNode)
{
if (oNode.Nodes.Count > 0)
return;
System.Collections.IEnumerator oEnumerator = _oCollectionBase.GetEnumerator();
object cValue = null;
if (_oManager.ChildPropertyName.Trim() != string.Empty)
{
while (oEnumerator.MoveNext())
{
System.Reflection.PropertyInfo oInfo = oEnumerator.Current.GetType().GetProperty("ID");
object oValue = oInfo.GetValue(oEnumerator.Current, null);
if (((ObjectTemplate)oNode.Tag).ID == ((ID)oValue))
{
cValue = cpInfo.GetValue(oEnumerator.Current, null);
}
}
if (cValue != null)
AddChildsToNode(oNode, (ObjectsTemplate<ObjectTemplate>)cValue);
}
}
private void RefreshTree()
{
tvwItems.Nodes.Clear();
string[] sProperties = ("ID," + _oManager.Parameters).Split(',');
if (sProperties.Length < 1)
{
throw new Exception("At least One Property needs to be sent");
}
System.Collections.IEnumerator oEnumerator = _oCollectionBase.GetEnumerator();
while (oEnumerator.MoveNext())
{
TreeNode oItem = new TreeNode();
for (int n = 0; n < sProperties.Length; n++)
{
System.Reflection.PropertyInfo oInfo = oEnumerator.Current.GetType().GetProperty(sProperties[n]);
object oValue = oInfo.GetValue(oEnumerator.Current, null);
try
{
if (n == 0)
{
oItem.Tag = oEnumerator.Current;
if (_oManager.SelectedIDs != null)
{
for (int nn = 0; nn < _oManager.SelectedIDs.Length; nn++)
{
if (((ID)oValue).Integer == _oManager.SelectedIDs[nn])
oItem.Checked = true;
}
}
}
else if (n == 1)
{
oItem.Text = oValue.ToString();
_Code[((ObjectTemplate)oItem.Tag).ID] = oItem.Text;
}
else
{
oItem.Text = oValue.ToString();
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
if (oItem != null) { tvwItems.Nodes.Add(oItem); }
if (!(_oManager.IsMultipleSelection))
{
if (_oManager.SelectedIDs != null && _oManager.SelectedIDs.Length > 0 && oItem.Checked)
{
tvwItems.SelectedNode = oItem;
if (tvwItems.SelectedNode.Parent != null)
tvwItems.SelectedNode.Parent.Expand();
}
}
cpInfo = oEnumerator.Current.GetType().GetProperty(_oManager.ChildPropertyName);
if (!((_oManager.IsMultipleSelection)) && (_oManager.SelectedIDs != null && _oManager.SelectedIDs.Length > 0))
{
if (_oManager.ChildPropertyName.Trim() != string.Empty)
{
System.Reflection.PropertyInfo cInfo = oEnumerator.Current.GetType().GetProperty(_oManager.ChildPropertyName);
object cValue = cInfo.GetValue(oEnumerator.Current, null);
if (cValue != null)
{
AddChildsToNode(oItem, (ObjectsTemplate<ObjectTemplate>)cValue);
}
}
}
}
btnSelectAll.Text = "Select &All";
lblTotal.Text = "Total Records :: " + tvwItems.Nodes.Count.ToString();
}
private void AddChildsToNode<T>(TreeNode parentNode, ObjectsTemplate<T> childs)where T:ObjectTemplate
{
System.Collections.IEnumerator oEnumerator = childs.GetEnumerator();
string[] sProperties = ("ID," + _oManager.Parameters).Split(',');
while (oEnumerator.MoveNext())
{
TreeNode oItem = new TreeNode();
for (int n = 0; n < sProperties.Length; n++)
{
System.Reflection.PropertyInfo oInfo = oEnumerator.Current.GetType().GetProperty(sProperties[n]);
//if(_oCollectionBase.GetIndex( ((ObjectTemplate)oEnumerator.Current).ID)<0)
// _oCollectionBase.Add((ObjectTemplate)oEnumerator.Current);
object oValue = oInfo.GetValue(oEnumerator.Current, null);
try
{
if (n == 0)
{
oItem.Tag = oEnumerator.Current;
if (_oManager.SelectedIDs != null)
{
for (int nn = 0; nn < _oManager.SelectedIDs.Length; nn++)
{
if (((ID)oValue).Integer == _oManager.SelectedIDs[nn])
oItem.Checked = true;
}
}
}
else if (n == 1)
{
oItem.Text = oValue.ToString();
_Code[((ObjectTemplate)oItem.Tag).ID] = oItem.Text;
}
else
{
oItem.Text = oValue.ToString();
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
throw new ServiceException(exp.Message);
}
}
if (oItem != null) { parentNode.Nodes.Add(oItem); }
if (!(_oManager.IsMultipleSelection))
{
if (_oManager.SelectedIDs != null && _oManager.SelectedIDs.Length > 0 && oItem.Checked)
{
tvwItems.SelectedNode = oItem;
}
}
System.Reflection.PropertyInfo cInfo = oEnumerator.Current.GetType().GetProperty(_oManager.ChildPropertyName);
object cValue = cInfo.GetValue(oEnumerator.Current, null);
if (cValue != null)
AddChildsToNode(oItem, (ObjectsTemplate<ObjectTemplate>)cValue);
}
}
#endregion
#region Bulid ListView Header
private void BuildListViewHeader()
{
lvwItems.Columns.Clear();
string[] sValues = _oManager.Caption.Split(',');
if (_oManager.IsMultipleSelection)
lvwItems.Columns.Add("...", 25);
for (int nIndex = 0; nIndex < sValues.Length; nIndex++)
{
if (_columnWidth != null && _columnWidth.Length == sValues.Length && Convert.ToDouble(_columnWidth[nIndex]) > 0)
lvwItems.Columns.Add(sValues[nIndex], Convert.ToInt32(_columnWidth[nIndex]), HorizontalAlignment.Left);
else
{
if (nIndex == 0)
lvwItems.Columns.Add(sValues[nIndex], 100, HorizontalAlignment.Left);
else
lvwItems.Columns.Add(sValues[nIndex], 200, HorizontalAlignment.Left);
}
}
}
#endregion
private void btnSelectAll_Click(object sender, EventArgs e)
{
bool bCheck = false;
if (btnSelectAll.Text == "Select &All")
{
bCheck = true;
btnSelectAll.Text = "Deselect &All";
}
else
{
bCheck = false;
btnSelectAll.Text = "Select &All";
}
if (_oManager.ControlType == EnumCustomBasicControlType.ListView)
{
foreach (ListViewItem oItem in lvwItems.Items)
{
oItem.Checked = bCheck;
}
}
else
{
foreach (TreeNode node in tvwItems.Nodes)
{
recure(node, bCheck);
}
}
}
private void recure(TreeNode node, bool bCheck)
{
node.Checked = bCheck;
foreach (TreeNode child in node.Nodes)
{
recure(child, bCheck);
}
}
#region BuildString
private void BuildString()
{
_oManager.SelectedValues = "";
foreach (ListViewItem oItem in lvwItems.CheckedItems)
{
if (_oManager.SelectedValues == "")
{
if (oItem.SubItems.Count > 0)
{
_oManager.SelectedValues = oItem.SubItems[2].Text;
}
}
else
{
if (oItem.SubItems.Count > 0)
{
_oManager.SelectedValues = _oManager.SelectedValues + "; " + oItem.SubItems[2].Text;
}
}
}
}
private string BuildString(TreeNodeCollection nodes)
{
string sValues = "";
int[] nn = new int[1];
foreach (TreeNode oNode in nodes)
{
if (oNode.Checked)
{
_oManager.SelectedObjects.Add((ObjectTemplate)oNode.Tag);
if (sValues == "")
{
sValues = ((ObjectTemplate)oNode.Tag).ID.ToString();
if (_oManager.SelectedValues == "")
{
_oManager.SelectedValues = (oNode.Text);
}
else
{
_oManager.SelectedValues = _oManager.SelectedValues + ", " + (oNode.Text);
}
}
else
{
sValues = sValues + "," + ((ObjectTemplate)oNode.Tag).ID.ToString();
if (_oManager.SelectedValues == "")
{
_oManager.SelectedValues = (oNode.Text);
}
else
{
_oManager.SelectedValues = _oManager.SelectedValues + ", " + (oNode.Text);
}
}
}
if (oNode.Nodes.Count > 0)
{
string sTemp = BuildString(oNode.Nodes);
if (sTemp != "")
{
if (sValues.Trim() != "")
{
sValues = sValues + "," + sTemp;
}
else
{
sValues = sTemp;
}
}
}
}
return sValues;
}
#endregion
private void btnOK_Click(object sender, EventArgs e)
{
if (_oManager.ControlType == EnumCustomBasicControlType.ListView)
{
#region List View
if (_oManager.IsMultipleSelection)
{
if (lvwItems.CheckedItems.Count > 0)
{
_oManager.SelectedIDs = new int[lvwItems.CheckedItems.Count];
_oManager.SelectedValues = "";
_oManager.SelectedObjects = new ObjectsTemplate<ObjectTemplate>();
int nIndex = 0;
foreach (ListViewItem oItem in lvwItems.CheckedItems)
{
_oManager.SelectedIDs[nIndex] = Convert.ToInt32(oItem.Tag);
_oManager.SelectedObjects.Add(_oCollectionBase.GetItem(ID.FromInteger(_oManager.SelectedIDs[nIndex])));
nIndex++;
}
BuildString();
}
else
{
this.Close();
}
}
else
{
if (lvwItems.SelectedItems.Count > 0)
{
_oManager.SelectedIDs = new int[1];
_oManager.SelectedIDs[0] = (int)lvwItems.SelectedItems[0].Tag;
_oManager.SelectedValues = lvwItems.SelectedItems[0].SubItems[1].Text;
_oManager.SelectedCode = lvwItems.SelectedItems[0].SubItems[0].Text;
_oManager.SelectedDescription = lvwItems.SelectedItems[0].SubItems[1].Text;
_oManager.SelectedObjects.Add(_oCollectionBase.GetItem(ID.FromInteger(_oManager.SelectedIDs[0])));
}
else
{
this.Close();
}
}
#endregion List View
}
else if (_oManager.ControlType == EnumCustomBasicControlType.TreeView)
{
_oManager.SelectedValues = "";
string sValues = string.Empty;
if (_oManager.IsMultipleSelection == false)
{
sValues = ((ObjectTemplate)tvwItems.SelectedNode.Tag).ID.Integer.ToString();
_oManager.SelectedValues = tvwItems.SelectedNode.Text;//((ObjectTemplate)tvwItems.SelectedNode.Tag).ID.Integer.ToString();
_oManager.SelectedCode = _Code[((ObjectTemplate)tvwItems.SelectedNode.Tag).ID].ToString();
_oManager.SelectedDescription = tvwItems.SelectedNode.Text;
_oManager.SelectedObjects.Add((ObjectTemplate)tvwItems.SelectedNode.Tag);
}
else
{
sValues = BuildString(tvwItems.Nodes);
}
_oManager.SelectedIDs = null;
if (sValues.Trim() != "")
{
string[] sVal = sValues.Split(',');
_oManager.SelectedIDs = new int[sVal.Length];
for (int i = 0; i < sVal.Length; i++)
{
try
{
_oManager.SelectedIDs[i] = Convert.ToInt32(sVal[i]);
// _oManager.SelectedObjects.Add(_oCollectionBase.GetItem(_oManager.SelectedIDs[i]));
}
catch
{
throw new ServiceException("Invalid ID Custing");
}
}
}
}
this.Close();
}
private void tvwItems_AfterCheck(object sender, TreeViewEventArgs e)
{
tvwItems.SelectedNode = (TreeNode)e.Node;
foreach (TreeNode node in e.Node.Nodes)
{
recure(node, e.Node.Checked);
}
}
private void lvwItems_ColumnClick(object sender, ColumnClickEventArgs e)
{
//try
//{
// //object[] param = new object[2];
// //param[0] = _masterForm.Listviewcloumns[e.Column].PropertyName;
// //param[1] = System.Windows.Forms.SortOrder.Descending;
// //System.Reflection.MethodInfo methodInfoDel = (_masterForm.SourceCollection.GetType()).GetMethod("Sort");
// //if (methodInfoDel != null)
// //methodInfoDel.Invoke(_masterForm.SourceCollection, param);
// //RefreshListview();
// ListViewSorter Sorter = new ListViewSorter();
// lvwBEMF.ListViewItemSorter = Sorter;
// if (!(lvwBEMF.ListViewItemSorter is ListViewSorter))
// return;
// Sorter = (ListViewSorter)lvwBEMF.ListViewItemSorter;
// if (Sorter.LastSort == e.Column)
// {
// if (lvwBEMF.Sorting == SortOrder.Ascending)
// lvwBEMF.Sorting = SortOrder.Descending;
// else
// lvwBEMF.Sorting = SortOrder.Ascending;
// }
// else
// {
// lvwBEMF.Sorting = SortOrder.Descending;
// }
// Sorter.ByColumn = e.Column;
// lvwBEMF.Sort();
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}
}
private void btnClose_Click(object sender, EventArgs e)
{
}
private void tvwItems_DoubleClick(object sender, EventArgs e)
{
if (!_oManager.IsMultipleSelection)
btnOK_Click(null, null);
}
private void lvwItems_DoubleClick(object sender, EventArgs e)
{
if (!_oManager.IsMultipleSelection)
btnOK_Click(null, null);
}
}
}