关于环境链,以下是一个例子,把从全局环境的所有环境信息都显示到一个TreeView上
========================================
function GetBizObjectDesc(AObject: TBizObject): String;
var
s: String;
begin
if AObject is TOperatorPosition then
begin
Result := SysUtils.Format('部门:%s 岗位:%s', [(AObject as TOperatorPosition).DeptID, (AObject as TOperatorPosition).PositionID]);
Result := Result + ':TOperatorPosition';
Exit;
end;
if AObject is TFlowControl then
begin
Result := SysUtils.Format('流程:%s 环节:%s', [(AObject as TFlowControl).Proc.DisplayName, (AObject as TFlowControl).CurrentProcUnit.DisplayName]);
Result := Result + ':TFlowControl';
Exit;
end;
try
s := ObjectHelper.ToString(ObjectHelper.GetPropertyValue(AObject, 'ID',[]));
except
try
s := ObjectHelper.ToString(ObjectHelper.GetPropertyValue(AObject, 'Name',[]));
except
s := '(unknown)';
end;
end;
try
s := s+' '+ObjectHelper.ToString(ObjectHelper.GetPropertyValue(AObject, 'DisplayName',[]));
except
end;
try
Result := s+':'+ObjectHelper.GetType(AObject).Name;
except
Result := s+'unknown type)';
end;
end;
function GetPolicyDesc(APolicy: TBizObjectPolicy): String;
begin
Result := APolicy.Name+':'+APolicy.Kind+' '+APolicy.Description;
end;
function GetComponentDesc(AComponent: TComponent): String;
begin
try
Result := AComponent.Name+':'+ObjectHelper.GetType(AComponent).Name;
except
Result := AComponent.Name+'unknown type)';
end;
end;
procedure AddSubContext(ATreeView: TTreeView; ANode: TTreeNode; AContext: TContext);
var
i: Integer;
node, pnode: TTreeNode;
begin
if AContext.ChildCount=0 then Exit;
pnode := ATreeView.Items.AddChild(ANode, '环境');
for i := 0 to AContext.ChildCount-1 do
begin
node := ATreeView.Items.AddChildObject(pnode, GetBizObjectDesc(AContext.Children.Owner), AContext.Children.Owner);
ParseContext(ATreeView, node, AContext.Children);
end;
end;
procedure AddPolicies(ATreeView: TTreeView; ANode: TTreeNode; AObject: TBizObject);
var
i: Integer;
pnode: TTreeNode;
begin
if AObject.PolicyCount=0 then Exit;
pnode := ATreeView.Items.AddChild(ANode, '策略');
for i:=0 to AObject.PolicyCount-1 do
begin
ATreeView.Items.AddChildObject(pnode, GetPolicyDesc(AObject.Policies), AObject.Policies);
end;
end;
type
TMyContext = class(TContext)
private
function GetMyObject(Index: Integer): TBizObject;
function GetObjectCount: Integer;
public
property BizObjPool[Index: Longint]: TBizObject read GetMyObject;
property BizObjPoolCount: Longint read GetObjectCount;
end;
function TMyContext.GetMyObject(Index: Integer): TBizObject;
begin
Result := BizObjectPool[Index];
end;
function TMyContext.GetObjectCount: Integer;
begin
Result := BizObjectPoolCount;
end;
procedure AddComponent(ATreeView: TTreeView; ANode: TTreeNode; AComp: TComponent);
var
i: Integer;
node, pNode: TTreeNode;
begin
if (AComp.ComponentCount=0) and ((not (AComp is TFunc)) or ((AComp as TFunc).MainForm=nil)) then Exit;
pnode := ATreeView.Items.AddChild(ANode, '组件');
for i:=0 to AComp.ComponentCount-1 do
begin
node := ATreeView.Items.AddChildObject(pnode, GetComponentDesc(AComp.Components), AComp.Components);
AddComponent(ATreeView, node, AComp.Components);
end;
if (AComp is TFunc) and ((AComp as TFunc).MainForm<>nil) then
begin
node := ATreeView.Items.AddChildObject(pnode, GetComponentDesc((AComp as TFunc).MainForm), (AComp as TFunc).MainForm);
AddComponent(ATreeView, node, (AComp as TFunc).MainForm);
end;
end;
procedure AddBizObject(ATreeView: TTreeView; ANode: TTreeNode; AContext: TMyContext);
var
i: Integer;
node, pnode: TTreeNode;
begin
if AContext.BizObjPoolCount=0 then Exit;
pnode := ATreeView.Items.AddChild(ANode, '缓冲池');
for i:=0 to AContext.BizObjPoolCount-1 do
begin
node := ATreeView.Items.AddChildObject(pnode, GetBizObjectDesc(AContext.BizObjPool), AContext.BizObjPool);
AddComponent(ATreeView, node, AContext.BizObjPool);
end;
end;
procedure ParseContext(ATreeView: TTreeView; ANode: TTreeNode; AContext: TContext);
var
node: TTreeNode;
i: Integer;
begin
AddSubContext(ATreeView, ANode, AContext);
AddPolicies(ATreeView, ANode, AContext.Owner);
AddBizObject(ATreeView, ANode, TMyContext(AContext));
AddComponent(ATreeView, ANode, AContext.Owner);
end;
procedure TMainForm.Button3Click(Sender: TObject);
var
ctx: TContext;
node: TTreeNode;
begin
tv.Items.Clear;
ctx := Context.GetParentContext(BizSys.IL_GLOBAL);
node := tv.Items.AddChildObjectFirst(nil, '全局', ctx);
ParseContext(tv, node, ctx);
end; |