narsil/Tanshu.Accounts.Helpers/ControlFactory.cs

253 lines
9.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using Tanshu.Accounts.Entities;
using System.Collections;
using Tanshu.Common.KeyboardControl;
namespace Tanshu.Accounts.Helpers
{
public delegate void ButtonClickDelegate(object sender, EventArgs e);
public static class ControlFactory
{
public static void GenerateTables(ref FlowLayoutPanel panel, Point size, int start, IList<FoodTable> inList, ButtonClickDelegate bcDelegate, ButtonClickDelegate pageDelegate)
{
IList<FoodTable> list = new List<FoodTable>();
int pageLength, stop;
PageSetup(panel, start, inList as IList, size, out pageLength, out stop);
if (start != 0)
list.Add(new FoodTable() { FoodTableID = Guid.Empty, Name = "Previous" });
for (int i = start; i < stop; i++)
{
list.Add(inList[i]);
}
if (stop < inList.Count)
list.Add(new FoodTable() { FoodTableID = Guid.Empty, Name = "Next" });
for (int i = 0; i < list.Count; i++)
{
var item = list[i];
Button control;
var status = "";
if (item.FoodTableID == Guid.Empty)
{
var pageLocation = item.Name == "Previous" ? start - pageLength : stop;
control = new Button()
{
Name = string.Format("p{0}", i),
Text = item.Name,
Width = size.X,
Height = size.Y,
Tag = pageLocation,
};
control.Click += new EventHandler(pageDelegate);
status = "paging";
}
else
{
status = item.Status;
control = new Button()
{
Name = string.Format("g{0}", i),
Text = item.Name,
Width = size.X,
Height = size.Y,
Tag = item,
};
control.Click += new EventHandler(bcDelegate);
}
if (status == "printed")
control.BackColor = Color.Green;
else if (status == "running")
control.BackColor = Color.Red;
panel.Controls.Add(control);
}
}
public static void GenerateProducts(ref FlowLayoutPanel panel, Point size, int start, IList<Product> inList, ButtonClickDelegate bcDelegate, ButtonClickDelegate pageDelegate)
{
IList<Product> list = new List<Product>();
int pageLength, stop;
PageSetup(panel, start, inList as IList, size, out pageLength, out stop);
if (start != 0)
list.Add(new Product() { ProductID = Guid.Empty, Name = "Previous" });
for (int i = start; i < stop; i++)
{
list.Add(inList[i]);
}
if (stop < inList.Count)
list.Add(new Product() { ProductID = Guid.Empty, Name = "Next" });
for (var i = 0; i < list.Count; i++)
{
var item = list[i];
Button control;
if (item.ProductID == Guid.Empty)
{
var pageLocation = item.Name == "Previous" ? start - pageLength : stop;
control = new Button()
{
Name = string.Format("p{0}", i),
Text = item.Name,
Width = size.X,
Height = size.Y,
Tag = pageLocation,
};
control.Click += new EventHandler(pageDelegate);
}
else
{
control = new Button()
{
Name = string.Format("p{0}", i),
Text = (item.HasHappyHour ? "H H " : "") + item.FullName,
Width = size.X,
Height = size.Y,
Tag = item,
};
control.Click += new EventHandler(bcDelegate);
if (item.HasHappyHour)
control.BackColor = Color.Yellow;
if (item.IsNotAvailable)
control.BackColor = Color.Red;
}
panel.Controls.Add(control);
}
}
// For Discount Form
public static void GenerateGroups(ref FlowLayoutPanel panel, Point size, IList<string> list, ButtonClickDelegate bcDelegate)
{
panel.Controls.Clear();
for (int i = 0; i < list.Count; i++)
{
var item = list[i];
var control = GetUnselectableCheckbox(i.ToString(), item, size, item, bcDelegate);
panel.Controls.Add(control);
}
}
// For Main Form
public static void GenerateGroups(ref FlowLayoutPanel panel, Point size, int start, IList<ProductGroup> inList, ButtonClickDelegate bcDelegate, ButtonClickDelegate pageDelegate)
{
IList<ProductGroup> list = new List<ProductGroup>();
int pageLength, stop;
PageSetup(panel, start, inList as IList, size, out pageLength, out stop);
if (start != 0)
list.Add(new ProductGroup() { ProductGroupID = Guid.Empty, Name = "Previous" });
for (int i = start; i < stop; i++)
{
list.Add(inList[i]);
}
if (stop < inList.Count)
list.Add(new ProductGroup() { ProductGroupID = Guid.Empty, Name = "Next" });
for (int i = 0; i < list.Count; i++)
{
var item = list[i];
Button control;
if (item.ProductGroupID == Guid.Empty)
{
var pageLocation = item.Name == "Previous" ? start - pageLength : stop;
control = new Button()
{
Name = string.Format("p{0}", i),
Text = item.Name,
Width = size.X,
Height = size.Y,
Tag = pageLocation,
};
control.Click += new EventHandler(pageDelegate);
}
else
{
control = new Button()
{
Name = string.Format("g{0}", i),
Text = item.Name,
Width = size.X,
Height = size.Y,
Tag = item,
};
control.Click += new EventHandler(bcDelegate);
}
panel.Controls.Add(control);
}
}
public static void GenerateModifiers(ref FlowLayoutPanel panel, ref IList<CheckBox> controlList, IList<Modifier> selection, Point size, int count, IList<Modifier> list, ButtonClickDelegate bcDelegate)
{
if (controlList.Count != 0)
{
for (int i = controlList.Count - 1; i >= 0; i--)
{
controlList[i].Dispose();
}
controlList = new List<CheckBox>();
}
if (count > list.Count)
count = list.Count;
for (int i = 0; i < count; i++)
{
var item = list[i];
var control = GetCheckbox(i.ToString(), item.Name, size, item, bcDelegate);
control.Checked = selection.Contains(item);
panel.Controls.Add(control);
controlList.Add(control);
}
}
private static void PageSetup(FlowLayoutPanel panel, int start, ICollection inList, Point size, out int pageLength, out int stop)
{
panel.Controls.Clear();
var marginWidth = panel.Margin.Right + panel.Margin.Left;
var marginHeight = panel.Margin.Top + panel.Margin.Bottom;
pageLength = panel.ClientSize.Height / (marginHeight + size.Y);
pageLength *= panel.ClientSize.Width / (marginWidth + size.X);
if (inList.Count <= pageLength)
{
start = 0;
stop = inList.Count;
}
else
{
pageLength -= 2;
if (start == 0)
pageLength += 1;
stop = start + pageLength;
if (stop > inList.Count)
stop = inList.Count;
}
}
private static UnselectableCheckbox GetUnselectableCheckbox(string name, string text, Point size, object tag, ButtonClickDelegate bcDelegate)
{
var control = new UnselectableCheckbox()
{
Name = name,
Text = text,
Width = size.X,
Height = size.Y,
Tag = tag,
Appearance = Appearance.Button,
};
if (bcDelegate != null)
control.Click += new EventHandler(bcDelegate);
return control;
}
private static CheckBox GetCheckbox(string name, string text, Point size, object tag, ButtonClickDelegate bcDelegate)
{
var control = new CheckBox()
{
Name = name,
Text = text,
Width = size.X,
Height = size.Y,
Tag = tag,
Appearance = Appearance.Button,
};
if (bcDelegate != null)
control.Click += new EventHandler(bcDelegate);
return control;
}
}
}