Files
lserp_cs_6.0/插件库/Lskj.Control/Model/MenuStrip/TreeGridViewMenuStrip.cs
T
2026-08-04 13:49:05 +08:00

221 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraTreeList;
using System.Data;
using Lskj.Model;
using DevExpress.XtraTreeList.Nodes;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;
using Lskj.Control.Properties;
using DevExpress.XtraGrid.Views.Grid;
using Lskj.Util;
using DevExpress.XtraGrid.Views.Base;
using Lskj.Business.Impl;
using Lskj.Data;
using DevExpress.XtraTreeList.Columns;
namespace Lskj.Control.Model.MenuStrip
{
public class TreeGridViewMenuStrip : BaseRightMenu
{
private int _oldRowHandler;
private TreeList _treeView;
private MyControl _control;
protected TreeGridControlEx TreeGridControlObj;
public override void InitRightMenus(System.Windows.Forms.Control view, DataTable table, DynamicModel model, MyControl control = null, ContextMenuStrip menu = null)
{
base.InitRightMenus(view, table, model, control, menu);
this.TreeGridControlObj = view as TreeGridControlEx;
if (this.TreeGridControlObj != null)
{
this._treeView = this.TreeGridControlObj.TreeListObj;
this.TreeGridControlObj.TreeListObj.ContextMenuStrip = base.MenuStrip;
}
}
protected override DataRow[] GetSelectedRows()
{
return this.TreeGridControlObj.GetViewFocusedDataRows();
}
protected override DataTable GetDataTable()
{
return this.TreeGridControlObj.GetGridViewDataSource();
}
protected override List<string> HandleRightMenuParams(List<string> paramList, DataRow rowData, DataRow[] rowDatas = null, int actionType = 0)
{
List<string> handleParamList = base.HandleRightMenuParams(paramList, rowData, rowDatas, actionType);
foreach (string item in handleParamList)
{
if (item.StartsWith("{"))
{
string field = item.Replace("{", "").Replace("}", "");
if ("COLUMN_NAME".Equals(field))
{
handleParamList.Add(this._treeView.FocusedColumn.FieldName);
}
else if ("COLUMN_TITLE".Equals(field))
{
handleParamList.Add(this._treeView.FocusedColumn.GetTextCaption());
}
else
{
handleParamList.Add(rowData[field] + "");
}
}
}
return handleParamList;
}
protected override void MenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)
{
DataRow[] mSelectRows = this.TreeGridControlObj.GetViewFocusedDataRows() ?? new DataRow[0];
ContextMenuStrip cmsMenu = (ContextMenuStrip)sender;
cmsMenu.Items.Clear();
cmsMenu.Items.AddRange(this.RightMenuItems.ToArray());
if (mSelectRows.Length > 0)
{
DataRow mSelectRow = mSelectRows[0];
this._treeView.EndCurrentEdit();
this._treeView.ShowEditor();
// 创建QQ、浏览器右键菜单
List<ToolStripMenuItem> mQQArray = new List<ToolStripMenuItem>();
List<ToolStripMenuItem> mBrowerArray = new List<ToolStripMenuItem>();
foreach (TreeListColumn column in this._treeView.Columns)
{
if (!mSelectRow.Table.Columns.Contains(column.Name))
continue;
string rowValue = mSelectRow[column.Name] + "";
if (!string.IsNullOrWhiteSpace(rowValue))
{
GridColumnModel model = column.Tag as GridColumnModel;
if (model == null) continue;
if (model.FieldType == ControlType.LabQQ)
{
ToolStripMenuItem item = new ToolStripMenuItem("(" + column.Caption + ")" + rowValue, Resources.qq);
item.Tag = mSelectRow[column.Name] + "";
item.Click += new EventHandler(QQItemClick);
mQQArray.Add(item);
}
if (model.FieldType == ControlType.LabWWW &&
(rowValue.StartsWith("www.") ||
rowValue.StartsWith("http://") ||
rowValue.StartsWith("https://") ||
rowValue.StartsWith("ftp://") ||
rowValue.StartsWith("file://")))
{
ToolStripMenuItem item = new ToolStripMenuItem("(" + column.Caption + ")" + rowValue, Resources.qq);
item.Tag = mSelectRow[column.Name] + "";
item.Click += new EventHandler(BrowerItemClick);
mBrowerArray.Add(item);
}
}
}
cmsMenu.Items.AddRange(mQQArray.ToArray());
cmsMenu.Items.AddRange(mBrowerArray.ToArray());
}
// 判断右键菜单是否可用
foreach (ToolStripItem cms in cmsMenu.Items)
{
GridRightMenuModel model = cms.Tag as GridRightMenuModel;
if (model != null)
{
if (!string.IsNullOrWhiteSpace(model.MenuCond))
{
try
{
bool condResult = false;
StringBuilder condBuilder = new StringBuilder();
StringBuilder sqlCondBuilder = new StringBuilder();
List<TreeListCell> cells = this._treeView != null ? this._treeView.GetSelectedCells() : null;
DataRow focusedRow = this.TreeGridControlObj.GetViewFocusedDataRow();
DataRow[] conditionRows = mSelectRows;
if (model.AllowNullExec && conditionRows.Length == 0)
{
conditionRows = new DataRow[] { new DataTable().NewRow() };
}
foreach (DataRow selectRow in conditionRows)
{
if (selectRow == null) continue;
string cond = ReplaceHelper.ReplaceUserInfo(model.MenuCond);
cond = RaiseBeforeHandleParamsCallback(model.ParamList, conditionRows, model.MenuCond, selectRow, cond);
if (this._control != null)
cond = _control.ReplaceParentControlValue(cond);
//cond = ReplaceHelper.ReplaceRowParam(selectRow, cond);
cond = ReplaceHelper.ReplaceRowParamEmptyWrapQuote(selectRow, cond);
if (model.Mergeexec && cond.StartsWith("@"))
{
//替换[],替换的是多选行中的数据
cond = ReplaceHelper.ReplaceRowParam(conditionRows, cond.Replace("[", "{").Replace("]", "}"), "", "");
}
if (cells != null && cells.Count > 0 && cond.Contains("{COLUMN_"))
{
TreeListCell cell = cells[0];
string colValue = focusedRow == null || !focusedRow.Table.Columns.Contains(cell.Column.Name) ? "" : focusedRow[cell.Column.Name] + "";
cond = cond.ReplaceColumnParam(cell.Column.Name, cell.Column.Caption, colValue);
}
if (ControlObj != null)
cond = ControlObj.ReplaceParentControlValue(cond);
if (cond.StartsWith("@"))
{
sqlCondBuilder.Append($"({cond.TrimStart('@')}) = '1' and ");
}
else
{
condBuilder.Append($"({cond}) and ");
}
}
string condStr = model.MenuCond;
if (!string.IsNullOrWhiteSpace(sqlCondBuilder.ToString()) || !string.IsNullOrWhiteSpace(condBuilder.ToString()))
{
if (model.MenuCond.StartsWith("@"))
{
condStr = $"@if({sqlCondBuilder.ToString().TrimEnd().TrimEnd('d', 'n', 'a')}) begin select '1' end else begin select '0' end";
}
else
{
condStr = condBuilder.ToString().TrimEnd().TrimEnd('d', 'n', 'a');
}
}
condResult = ValidateCond(condStr, null);
//cms.Enabled = ReplaceHelper.EvalCond(cond);
if (model.ForbiddenDisplay)
{
cms.Visible = condResult;
}
cms.Enabled = condResult;
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(ResourceKeys.SetRightMenuCondFault + "\r\n" + Message);
e.Cancel = true;
break;
}
}
}
}
}
}
}