#region
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using CSFrameworkV5.Business.BLL_Permission;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Library.UserControls;
using CSFrameworkV5.Models;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
#endregion
namespace CSFrameworkV5.Library.PermissionForms
{
///
/// 编辑菜单资料
///
public partial class frmEditorMenu : frmEditorBase
{
private TreeListNode _FocusedNode;
private DataTable _MenuData;
private DataTable _MenuPicker;
private RoleActionsView _RoleActionsView;
private UpdateType _UpdateType = UpdateType.None;
private bool Changed;
private frmEditorMenu()
{
InitializeComponent();
}
private void BindingEditors(DataRowView rv)
{
txtFormNamespace.Text =
ConvertEx.ToString(rv.Row[tb_MyMenu.FormNamespace]);
txtMenuCaption.Text =
ConvertEx.ToString(rv.Row[tb_MyMenu.MenuCaption]);
txtMenuName.Text = ConvertEx.ToString(rv.Row[tb_MyMenu.MenuName]);
txtMenuType.SelectedIndex =
GetIndex(ConvertEx.ToString(rv.Row[tb_MyMenu.MenuType]));
txtModuleID.EditValue =
ConvertEx.ToString(rv.Row[tb_MyMenu.ModuleID]);
txtParameters.Text =
ConvertEx.ToString(rv.Row[tb_MyMenu.Parameters]);
txtParentMenuName.EditValue =
ConvertEx.ToString(rv.Row[tb_MyMenu.ParentMenuName]);
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
private void btnOK_Click(object sender, EventArgs e)
{
if (!ValidateInput()) return;
DataRow current = null;
if (_UpdateType == UpdateType.Add)
{
current = _MenuData.NewRow();
_MenuData.Rows.Add(current);
}
if (_UpdateType == UpdateType.Modify)
{
var rv =
(DataRowView)_FocusedNode.TreeList.GetDataRecordByNode(
_FocusedNode);
current = rv.Row;
}
//生成当前菜单的功能点数据。
ActionCreateCustomNameData(pcActions, txtMenuName.Text,
_RoleActionsView.ActionCustomNameData);
if (current != null) FillRow(current);
Changed = true;
Close();
}
public static bool Execute(RoleActionsView roleActionsView,
UpdateType updateType, TreeListNode focusedNode)
{
var form = new frmEditorMenu();
form._MenuData = roleActionsView.MenuData;
form._RoleActionsView = roleActionsView;
form._FocusedNode = focusedNode;
form.InitEditor(updateType, focusedNode);
form.ShowDialog();
return form.Changed;
}
private void FillRow(DataRow current)
{
current[tb_MyMenu.DataSetID] = Loginer.CurrentUser.DBID;
current[tb_MyMenu.FormNamespace] = txtFormNamespace.Text;
current[tb_MyMenu.MenuCaption] = txtMenuCaption.Text;
current[tb_MyMenu.MenuName] = txtMenuName.Text;
current[tb_MyMenu.MenuType] = txtMenuType.Text
.Substring(0, txtMenuType.Text.IndexOf("-") - 1).Trim();
current[tb_MyMenu.ModuleID] = txtModuleID.EditValue;
current[tb_MyMenu.Parameters] = txtParameters.Text;
current[tb_MyMenu.ParentMenuName] = txtParentMenuName.EditValue;
current[tb_MyMenu.Actions] = ActionGetValue(pcActions);
}
private void frmMenuEditor_Load(object sender, EventArgs e)
{
_MenuPicker = new bllPermission().GetMenuPicker(true);
txtModuleID.Properties.DisplayMember =
sys_ModuleFileList.ModuleName;
txtModuleID.Properties.ValueMember = sys_ModuleFileList.ModuleID;
txtModuleID.Properties.DataSource =
new bllPermission().GetModuleData();
OnModuleIDChanged();
}
private int GetIndex(string menuType)
{
if (menuType.Trim() == "") return 0;
for (var i = 0; i < txtMenuType.Properties.Items.Count - 1; i++)
if (txtMenuType.Properties.Items[i].ToStringEx()
.IndexOf(menuType) >= 0)
return i;
return 0;
}
public void InitEditor(UpdateType updateType, TreeListNode focusedNode)
{
_UpdateType = updateType;
var rv =
(DataRowView)focusedNode.TreeList.GetDataRecordByNode(
focusedNode);
ActionInitPanel(pcActions, _RoleActionsView.ActionsData);
ActionShowMenuFuns(pcActions, rv.Row,
_RoleActionsView.ActionCustomNameData);
//新增菜单
if (UpdateType.Add == updateType)
{
txtMenuType.SelectedIndex = 1; //DataForm - 数据窗体菜单
//新增菜单的父级预设为当前结点
if (focusedNode != null)
{
txtModuleID.EditValue = rv.Row[tb_MyMenu.ModuleID];
txtParentMenuName.EditValue = rv.Row[tb_MyMenu.MenuName];
}
}
else
{
BindingEditors(rv);
txtParentMenuName.Enabled = false;
txtModuleID.Enabled = false;
}
var editMode = updateType == UpdateType.Add ||
UpdateType.Modify == updateType;
panelControl1.Enabled = editMode;
pcActions.Enabled = editMode;
btnOK.Enabled = editMode;
btnCancel.Enabled = true; //btnOK.Enabled;
}
private void OnModuleIDChanged()
{
if (_MenuData != null && txtModuleID.EditValue != null)
{
var temp = _MenuData.Copy();
var top = temp.NewRow();
top[tb_MyMenu.MenuName] = "";
top[tb_MyMenu.MenuCaption] = "<顶级菜单>";
temp.Rows.InsertAt(top, 0);
temp.DefaultView.RowFilter = "ModuleID=" +
txtModuleID.EditValue
.ToStringEx() +
" or MenuName=''";
txtParentMenuName.Properties.DisplayMember =
tb_MyMenu.MenuCaption;
txtParentMenuName.Properties.ValueMember = tb_MyMenu.MenuName;
txtParentMenuName.Properties.DataSource = temp.DefaultView;
}
}
private void txtModuleID_EditValueChanged(object sender, EventArgs e)
{
OnModuleIDChanged();
}
private bool ValidateInput()
{
//if (txtModuleID.Text == "")
//{
// Msg.Warning("所属模块不能为空!");
// txtModuleID.Focus();
// return false;
//}
if (txtMenuName.Text == "")
{
Msg.Warning("菜单名称不能为空!");
txtMenuName.Focus();
return false;
}
if (txtMenuCaption.Text == "")
{
Msg.Warning("菜单的标题不能为空!");
txtMenuCaption.Focus();
return false;
}
if (RoleActionsView.IsExistsInCache(_MenuData, txtMenuName.Text,
tb_MyMenu.MenuName, _UpdateType))
{
Msg.Warning("菜单名称已经存在!");
txtMenuName.Focus();
return false;
}
return true;
}
#region 功能点动态面板操作相关的方法。
///
/// 初始化功能权限控制面板
///
public static void ActionInitPanel(PanelControl pcActions,
DataTable actionData)
{
var ROW = 13;
var rowTop = 3;
var colLeft = 2;
var count = 0;
pcActions.BeginInit();
pcActions.Controls.Clear();
foreach (DataRow actionRow in actionData.Rows)
{
var value =
ConvertEx.ToInt(actionRow[tb_MyActions.ActionValue]);
if (value == 0) continue;
var checkEdit = new ucCheckEdit();
checkEdit.Name = "ucCheckEdit_" + value.ToStringEx();
checkEdit.IsChecked = false;
checkEdit.Location = new Point(colLeft, rowTop);
checkEdit.MaximumSize = new Size(2333, 21);
checkEdit.MinimumSize = new Size(29, 21);
checkEdit.Size = new Size(88, 21);
checkEdit.TabIndex = 0;
checkEdit.CheckText =
ConvertEx.ToString(
actionRow[tb_MyActions.ActionName]); //显示预设的名称
checkEdit.Tag = actionRow; //标记
checkEdit.InnerEditor.Properties.ReadOnly = true;
checkEdit.InnerEditor.Enabled = true;
checkEdit.InnerCheckEdit.Enabled = true;
checkEdit.InnerCheckEdit.Properties.ReadOnly = true;
pcActions.Controls.Add(checkEdit);
rowTop += 21 + 2;
count++;
if (count > ROW)
{
colLeft += checkEdit.Width - 8;
rowTop = 3;
count = 0;
}
}
pcActions.EndInit();
}
///
/// 显示菜单的功能点
///
public static void ActionShowMenuFuns(PanelControl pcActions,
DataRow rowMenuData,
DataTable actionCustomNameData)
{
var Auths = ConvertEx.ToInt(rowMenuData[tb_MyMenu.Actions]); //菜单的权限
var menu =
ConvertEx.ToString(rowMenuData[tb_MyMenu.MenuName]); //菜单名称
int value;
DataRow rowAction;
foreach (Control c in pcActions.Controls)
if (c is ucCheckEdit)
{
rowAction = c.Tag as DataRow; //功能点资料行
value = ConvertEx.ToInt(
rowAction[tb_MyActions.ActionValue]); //取功能点的权限值
(c as ucCheckEdit).IsChecked =
(value & Auths) == value; //逻辑“与”运算。
(c as ucCheckEdit).Enabled =
(value & Auths) ==
value; //当前菜单有此功能权限才显示
//显示自定义的功能点名称
var filter = "MenuName='" + menu + "' and TagValue=" +
value.ToStringEx();
var rows = actionCustomNameData.Select(filter);
if (rows.Length > 0)
(c as ucCheckEdit).CheckText =
ConvertEx.ToString(
rows[0][tb_MyFormTagName.TagName]); //取自定义名称
else
(c as ucCheckEdit).CheckText =
ConvertEx.ToString(
rowAction[tb_MyActions.ActionName]); //预设名称
}
}
///
/// 显示菜单的功能点
///
public static int ActionGetValue(PanelControl pcActions)
{
var value = 0;
var i = 0;
DataRow rowAction;
foreach (Control c in pcActions.Controls)
if (c is ucCheckEdit && (c as ucCheckEdit).IsChecked)
{
rowAction = c.Tag as DataRow; //功能点资料行
i = ConvertEx.ToInt(
rowAction[tb_MyActions.ActionValue]); //取功能点的权限值
value = value + i; //累加
}
return value;
}
///
/// 创建功能自定义名称
///
public static void ActionCreateCustomNameData(PanelControl pcActions,
string menuName,
DataTable actionCustomNameData)
{
//取窗体的权限
var auths = 0;
var val = 0;
ucCheckEdit ce;
DataRow row;
string filter;
foreach (Control ctr in pcActions.Controls)
{
if (!(ctr is ucCheckEdit)) continue;
ce = ctr as ucCheckEdit;
row = ce.Tag as DataRow; //功能点的数据行
val = ConvertEx.ToInt(row[tb_MyActions.ActionValue]);
if (ce.IsChecked) auths += val; //累加权限数值
filter = "DataSetID='" + Loginer.CurrentUser.DBID +
"' AND MenuName='" + menuName + "' and TagValue=" +
val.ToStringEx();
var rows = actionCustomNameData.Select(filter);
if (rows.Length > 0) //有自定义功能点名称
{
var tagName =
ConvertEx.ToString(rows[0][tb_MyFormTagName.TagName]);
if (ce.IsChecked &&
tagName.ToUpper() != ce.CheckText.ToUpper())
rows[0][tb_MyFormTagName.TagName] =
ce.CheckText; //修改新功能点名称
if (ce.IsChecked == false) //删除了功能名称
rows[0].Delete();
}
else
{
var authName =
ConvertEx.ToString(row[tb_MyActions.ActionName]);
if (authName.ToUpper() != ce.CheckText.ToUpper())
{
var newrow = actionCustomNameData.NewRow();
newrow[tb_MyFormTagName.DataSetID] =
Loginer.CurrentUser.DBID;
newrow[tb_MyFormTagName.MenuName] = menuName;
newrow[tb_MyFormTagName.TagValue] = val;
newrow[tb_MyFormTagName.TagName] = ce.CheckText; //新的名称
actionCustomNameData.Rows.Add(newrow);
}
}
}
}
#endregion
}
}