Repository navigation
How the Project Manager fills the Object Inspector
When you select a node, the Project Manager creates a small TPersistent wrapper for it and hands that object to the Object Inspector through INTAPropInspServices.SelectObjects (unit PropInspAPI, in source\ToolsAPI). The rows you see are the wrapper’s published properties:
- Rows are published properties of the wrapper.
- Names with spaces (“File Name”, “Custom Build Tool”) come from property editors that override GetName.
- Greyed rows (“Full Path”, “Design Class”, “Form Name”) are properties without a setter. The default property editor marks those read-only.
Let’s look how we control this for a custom node.
The first step is the data class, derived from TPersistent:
type
TCustomNodeStatus = (nsDraft, nsReview, nsApproved);
TCustomNodeData = class(TPersistent)
private
FProjectFileName: string;
FFileName: string;
FTitle: string;
FOwnerName: string;
FPriority: Integer;
FStatus: TCustomNodeStatus;
FCreated: TDateTime;
procedure Changed;
function GetFullPath: string;
procedure SetOwnerName(const Value: string);
procedure SetPriority(Value: Integer);
procedure SetStatus(Value: TCustomNodeStatus);
procedure SetTitle(const Value: string);
published
property Title: string read FTitle write SetTitle;
property OwnerName: string read FOwnerName write SetOwnerName;
property Priority: Integer read FPriority write SetPriority default 0;
property Status: TCustomNodeStatus read FStatus write SetStatus default nsDraft;
property Created: TDateTime read FCreated;
property FullPath: string read GetFullPath;
end;Each setter calls Changed, which marks the project modified so the IDE asks to save it:
procedure TCustomNodeData.SetOwnerName(const Value: string);
begin
if FOwnerName <> Value then
begin
FOwnerName := Value;
Changed;
end;
end;
procedure TCustomNodeData.Changed;
var
Module: IOTAModule;
Project: IOTAProject;
begin
Module := (BorlandIDEServices as IOTAModuleServices).FindModule(FProjectFileName);
if Supports(Module, IOTAProject, Project) then
Project.MarkModified;
end;This is the same technique the IDE uses for its own rows: register property editors for your class and override GetName. For the enum, GetValue/GetValues/SetValue give readable values in the drop-down.
type
TCustomNodeStringProperty = class(TStringProperty)
public
function GetName: string; override;
end;
TCustomNodeStatusProperty = class(TEnumProperty)
public
function GetValue: string; override;
procedure GetValues(Proc: TGetStrProc); override;
procedure SetValue(const Value: string); override;
end;
const
cStatusNames: array[TCustomNodeStatus] of string = ('Draft', 'In Review', 'Approved');
function DisplayName(const PropName: string): string;
begin
if PropName = 'OwnerName' then
Result := 'Owner'
else if PropName = 'FullPath' then
Result := 'Full Path'
else
Result := PropName;
end;
function TCustomNodeStringProperty.GetName: string;
begin
Result := DisplayName(inherited GetName);
end;
function TCustomNodeStatusProperty.GetValue: string;
begin
Result := cStatusNames[TCustomNodeStatus(GetOrdValue)];
end;
procedure TCustomNodeStatusProperty.GetValues(Proc: TGetStrProc);
var
Status: TCustomNodeStatus;
begin
for Status := Low(TCustomNodeStatus) to High(TCustomNodeStatus) do
Proc(cStatusNames[Status]);
end;
procedure TCustomNodeStatusProperty.SetValue(const Value: string);
var
Status: TCustomNodeStatus;
begin
for Status := Low(TCustomNodeStatus) to High(TCustomNodeStatus) do
if SameText(Value, cStatusNames[Status]) then
begin
SetOrdValue(Ord(Status));
Exit;
end;
inherited SetValue(Value);
end;
procedure RegisterCustomNodePropertyEditors;
begin
RegisterPropertyEditor(TypeInfo(string), TCustomNodeData, 'OwnerName', TCustomNodeStringProperty);
RegisterPropertyEditor(TypeInfo(string), TCustomNodeData, 'FullPath', TCustomNodeStringProperty);
RegisterPropertyEditor(TypeInfo(TCustomNodeStatus), TCustomNodeData, 'Status', TCustomNodeStatusProperty);
end;The public way to get a node under a project is a module creator whose GetOwner returns that project. The IDE adds the new file to the project and shows it as a file node directly under the project node. The sample uses its own extension (.cnode) so it can recognise its nodes later.
The creator, trimmed to the methods that matter (the rest return '', False or nil):
type
TCustomNodeCreator = class(TInterfacedObject, IOTACreator, IOTAModuleCreator)
private
FProject: IOTAProject;
FFileName: string;
public
constructor Create(const AProject: IOTAProject; const AFileName: string);
function GetCreatorType: string;
function GetExisting: Boolean;
function GetFileSystem: string;
function GetOwner: IOTAModule;
function GetUnnamed: Boolean;
function GetAncestorName: string;
function GetImplFileName: string;
function GetIntfFileName: string;
function GetFormName: string;
function GetMainForm: Boolean;
function GetShowForm: Boolean;
function GetShowSource: Boolean;
function NewFormFile(const FormIdent, AncestorIdent: string): IOTAFile;
function NewImplSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
function NewIntfSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
procedure FormCreated(const FormEditor: IOTAFormEditor);
end;
function TCustomNodeCreator.GetCreatorType: string;
begin
Result := sText;
end;
function TCustomNodeCreator.GetOwner: IOTAModule;
begin
Result := FProject;
end;
function TCustomNodeCreator.GetImplFileName: string;
begin
Result := FFileName;
end;
function TCustomNodeCreator.GetShowSource: Boolean;
begin
Result := True;
end;
function TCustomNodeCreator.NewImplSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
begin
Result := TOTAFile.Create(Format('Custom node %s' + sLineBreak, [ExtractFileName(FFileName)]));
end;GetImplFileName must be a fully qualified file name. The sample picks CustomNode.cnode next to the .dproj, creates the module, and registers the node’s data:
procedure AddCustomNode(const Project: IOTAProject);
var
FileName: string;
begin
FileName := NewCustomNodeFileName(Project);
(BorlandIDEServices as IOTAModuleServices).CreateModule(TCustomNodeCreator.Create(Project, FileName));
(BorlandIDEServices as IOTAProjectFileStorage).AddNewSection(Project, cCustomNodeSection, False);
CustomNodes.Add(Project.FileName, FileName);
Project.MarkModified;
end;It’s triggered from a Project Manager local-menu item on the project node. The notifier only adds the item when a single project node is selected:
procedure TCustomNodeMenuNotifier.AddMenu(const Project: IOTAProject;
const IdentList: TStrings; const ProjectManagerMenuList: IInterfaceList;
IsMultiSelect: Boolean);
begin
if not IsMultiSelect and (Project <> nil) and (IdentList.IndexOf(sProjectContainer) >= 0) then
ProjectManagerMenuList.Add(TCustomNodeMenuItem.Create as IOTAProjectManagerMenu);
end;
procedure TCustomNodeMenuItem.Execute(const MenuContextList: IInterfaceList);
var
Context: IOTAProjectMenuContext;
begin
if (MenuContextList.Count > 0) and
Supports(MenuContextList[0], IOTAProjectMenuContext, Context) and
(Context.Project <> nil) then
AddCustomNode(Context.Project);
end;TCustomNodeMenuItem is a plain TNotifierObject implementing IOTALocalMenu and IOTAProjectManagerMenu, with Caption “Add Custom Node” and Position pmmpAddNew + 10.

IOTAProjectFileStorage gives you a named XML section in the .dproj. AddNewSection (called in step 3) creates it; from then on, your notifier is called when the project loads, saves and closes:
type
TCustomNodeStorage = class(TInterfacedObject, IOTAProjectFileStorageNotifier)
public
function GetName: string;
procedure ProjectLoaded(const ProjectOrGroup: IOTAModule; const Node: IXMLNode);
procedure CreatingProject(const ProjectOrGroup: IOTAModule);
procedure ProjectSaving(const ProjectOrGroup: IOTAModule; const Node: IXMLNode);
procedure ProjectClosing(const ProjectOrGroup: IOTAModule);
end;
function TCustomNodeStorage.GetName: string;
begin
Result := cCustomNodeSection;
end;
procedure TCustomNodeStorage.ProjectLoaded(const ProjectOrGroup: IOTAModule; const Node: IXMLNode);
begin
CustomNodes.LoadProject(ProjectOrGroup.FileName, Node);
end;
procedure TCustomNodeStorage.ProjectSaving(const ProjectOrGroup: IOTAModule; const Node: IXMLNode);
var
Project: IOTAProject;
begin
if Supports(ProjectOrGroup, IOTAProject, Project) then
CustomNodes.SaveProject(Project, Node);
end;
procedure TCustomNodeStorage.ProjectClosing(const ProjectOrGroup: IOTAModule);
begin
CustomNodes.RemoveProject(ProjectOrGroup.FileName);
end;CustomNodes is the sample’s registry: one list of TCustomNodeData per project, keyed by the node’s file name. Each node is saved as one element. Here’s what I see in the .dproj file when the project shown above is saved:
<CustomNodeOI>
<Node File="CustomNode2.cnode" Title="CustomNode2" Priority="0" Status="nsDraft" Created="2026-10-29T19:45:51.067-07:00"/>
</CustomNodeOI>For projects that were already open when the package was installed, the registry also loads lazily through IOTAProjectFileStorage.GetProjectStorageNode.
Register an IOTASelectionNotifier. When the selection changes, check whether the Project Manager’s current node is one of yours, and if so, select your object instead:
type
TCustomNodeSelectionWatcher = class(TNotifierObject, IOTASelectionNotifier)
private
FPending: Boolean;
public
procedure SelectionChanged(const NewSelection: IOTAPropInspSelection);
procedure UpdateState;
procedure ShowSelectedNode;
end;
function SelectedCustomNode: TCustomNodeData;
var
ProjectManager: IOTAProjectManager;
Project: IOTAProject;
Ident: string;
begin
Result := nil;
if Supports(BorlandIDEServices, IOTAProjectManager, ProjectManager) then
begin
Project := ProjectManager.GetCurrentSelection(Ident);
if (Project <> nil) and SameText(ExtractFileExt(Ident), cCustomNodeExt) then
Result := CustomNodes.Find(Project.FileName, Ident);
end;
end;
procedure TCustomNodeSelectionWatcher.SelectionChanged(const NewSelection: IOTAPropInspSelection);
var
Data: TCustomNodeData;
begin
if FPending or not ProjectManagerFocused then
Exit;
Data := SelectedCustomNode;
if (Data = nil) or (BorlandIDEServices as INTAPropInspServices).ObjectsSelected([Data]) then
Exit;
FPending := True;
TThread.ForceQueue(nil, ShowSelectedNode);
end;
procedure TCustomNodeSelectionWatcher.ShowSelectedNode;
var
Data: TCustomNodeData;
PropInsp: INTAPropInspServices;
begin
FPending := False;
Data := SelectedCustomNode;
PropInsp := BorlandIDEServices as INTAPropInspServices;
if (Data <> nil) and not PropInsp.ObjectsSelected([Data]) then
PropInsp.SelectObjects([Data]);
end;Three things that matter here:
- Queue the call: SelectionChanged runs while the Object Inspector is notifying its listeners and the Project Manager is still in the middle of its own selection. Calling SelectObjects from inside the notification would re-enter both. TThread.ForceQueue runs it right after.
- Only react while the Project Manager has the focus: GetCurrentSelection keeps returning your node after the user clicks a component on a form. Without this check you would take the Object Inspector back from the form designer.
- Keep ownership of your objects: The Object Inspector only holds a pointer. The sample keeps one TCustomNodeData per node for the life of the project, so edits go straight into the data and nothing has to be freed on a selection change.
An API to ask whether the Project Manager is the active view cannot be found, so the sample checks the focused window’s parents. This relies on the Project Manager form’s class name, which is not part of the API. If it ever changes, your node simply shows the standard file rows again:
function ProjectManagerFocused: Boolean;
var
Control: TWinControl;
begin
Control := FindControl(GetFocus);
while Control <> nil do
begin
if Control.ClassNameIs('TProjectManagerForm') then
Exit(True);
Control := Control.Parent;
end;
Result := False;
end;
Register adds the notifiers. On unload, remove them, drop any queued call, and clear the Object Inspector if it still shows one of your objects before freeing them; otherwise the Object Inspector is left pointing at freed memory.
procedure Register;
begin
RegisterCustomNodePropertyEditors;
MenuNotifierIndex := (BorlandIDEServices as IOTAProjectManager).AddMenuItemCreatorNotifier(
TCustomNodeMenuNotifier.Create);
StorageNotifierIndex := (BorlandIDEServices as IOTAProjectFileStorage).AddNotifier(
TCustomNodeStorage.Create);
SelectionWatcher := TCustomNodeSelectionWatcher.Create;
SelectionWatcherIntf := SelectionWatcher;
SelectionNotifierIndex := (BorlandIDEServices as IOTAPropInspServices).AddNotifier(SelectionWatcherIntf);
end;
procedure Unregister;
var
PropInsp: IOTAPropInspServices;
Storage: IOTAProjectFileStorage;
ProjectManager: IOTAProjectManager;
begin
if (SelectionNotifierIndex >= 0) and Supports(BorlandIDEServices, IOTAPropInspServices, PropInsp) then
PropInsp.RemoveNotifier(SelectionNotifierIndex);
if SelectionWatcher <> nil then
TThread.RemoveQueuedEvents(SelectionWatcher.ShowSelectedNode);
SelectionWatcherIntf := nil;
SelectionWatcher := nil;
if (StorageNotifierIndex >= 0) and Supports(BorlandIDEServices, IOTAProjectFileStorage, Storage) then
Storage.RemoveNotifier(StorageNotifierIndex);
if (MenuNotifierIndex >= 0) and Supports(BorlandIDEServices, IOTAProjectManager, ProjectManager) then
ProjectManager.RemoveMenuItemCreatorNotifier(MenuNotifierIndex);
FreeCustomNodes;
end;
initialization
finalization
Unregister;FreeCustomNodes calls INTAPropInspServices.SelectObjects([]) first if one of its objects is still selected.
- The nodes are real project files: They’re written to the .dproj as items, need to exist on disk when the project is saved, and get the file node’s icon and local menu. You can add to that menu with
IOTAProjectMenuItemCreatorNotifier. - The data isn’t attached to the node: The IDE stores your section, but the only link between a node and its data is the file name. If the user renames the node, the link breaks, and the next save drops the data. An
IOTAModuleNotifier.ModuleRenamedon each node’s module would let you re-key it (left as an exercise for…). - Placement is limited: Nodes go under the project (or a folder node in the nested-folder view), or under another entry with
IOTAProject.AddFileWithParent.
package CustomNodeOI;
{$ALIGN 8}
{$ASSERTIONS ON}
{$BOOLEVAL OFF}
{$DEBUGINFO ON}
{$EXTENDEDSYNTAX ON}
{$IMPORTEDDATA ON}
{$IOCHECKS ON}
{$LOCALSYMBOLS ON}
{$LONGSTRINGS ON}
{$OPENSTRINGS ON}
{$OPTIMIZATION OFF}
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
{$REFERENCEINFO ON}
{$SAFEDIVIDE OFF}
{$STACKFRAMES ON}
{$TYPEDADDRESS OFF}
{$VARSTRINGCHECKS ON}
{$WRITEABLECONST OFF}
{$MINENUMSIZE 1}
{$IMAGEBASE $400000}
{$DESCRIPTION 'Custom Project Manager nodes with Object Inspector data'}
{$DESIGNONLY}
{$IMPLICITBUILD OFF}
requires
rtl,
vcl,
xmlrtl,
designide;
contains
CustomNodeOI.Data in 'CustomNodeOI.Data.pas',
CustomNodeOI.Creator in 'CustomNodeOI.Creator.pas',
CustomNodeOI.Menu in 'CustomNodeOI.Menu.pas',
CustomNodeOI.Storage in 'CustomNodeOI.Storage.pas',
CustomNodeOI.Inspector in 'CustomNodeOI.Inspector.pas',
CustomNodeOI.Reg in 'CustomNodeOI.Reg.pas';
end.unit CustomNodeOI.Creator;
interface
uses
System.Classes, System.SysUtils, ToolsAPI;
type
TCustomNodeCreator = class(TInterfacedObject, IOTACreator, IOTAModuleCreator)
private
FProject: IOTAProject;
FFileName: string;
public
constructor Create(const AProject: IOTAProject; const AFileName: string);
function GetCreatorType: string;
function GetExisting: Boolean;
function GetFileSystem: string;
function GetOwner: IOTAModule;
function GetUnnamed: Boolean;
function GetAncestorName: string;
function GetImplFileName: string;
function GetIntfFileName: string;
function GetFormName: string;
function GetMainForm: Boolean;
function GetShowForm: Boolean;
function GetShowSource: Boolean;
function NewFormFile(const FormIdent, AncestorIdent: string): IOTAFile;
function NewImplSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
function NewIntfSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
procedure FormCreated(const FormEditor: IOTAFormEditor);
end;
procedure AddCustomNode(const Project: IOTAProject);
implementation
uses
CustomNodeOI.Data;
function NewCustomNodeFileName(const Project: IOTAProject): string;
var
Dir: string;
I: Integer;
begin
Dir := ExtractFilePath(Project.FileName);
I := 1;
repeat
Result := Dir + Format('CustomNode%d%s', [I, cCustomNodeExt]);
Inc(I);
until not FileExists(Result) and not ProjectContainsFile(Project, Result) and
((BorlandIDEServices as IOTAModuleServices).FindModule(Result) = nil);
end;
procedure AddCustomNode(const Project: IOTAProject);
var
FileName: string;
begin
FileName := NewCustomNodeFileName(Project);
(BorlandIDEServices as IOTAModuleServices).CreateModule(TCustomNodeCreator.Create(Project, FileName));
(BorlandIDEServices as IOTAProjectFileStorage).AddNewSection(Project, cCustomNodeSection, False);
CustomNodes.Add(Project.FileName, FileName);
Project.MarkModified;
end;
constructor TCustomNodeCreator.Create(const AProject: IOTAProject; const AFileName: string);
begin
inherited Create;
FProject := AProject;
FFileName := AFileName;
end;
function TCustomNodeCreator.GetCreatorType: string;
begin
Result := sText;
end;
function TCustomNodeCreator.GetExisting: Boolean;
begin
Result := False;
end;
function TCustomNodeCreator.GetFileSystem: string;
begin
Result := '';
end;
function TCustomNodeCreator.GetOwner: IOTAModule;
begin
Result := FProject;
end;
function TCustomNodeCreator.GetUnnamed: Boolean;
begin
Result := False;
end;
function TCustomNodeCreator.GetAncestorName: string;
begin
Result := '';
end;
function TCustomNodeCreator.GetImplFileName: string;
begin
Result := FFileName;
end;
function TCustomNodeCreator.GetIntfFileName: string;
begin
Result := '';
end;
function TCustomNodeCreator.GetFormName: string;
begin
Result := '';
end;
function TCustomNodeCreator.GetMainForm: Boolean;
begin
Result := False;
end;
function TCustomNodeCreator.GetShowForm: Boolean;
begin
Result := False;
end;
function TCustomNodeCreator.GetShowSource: Boolean;
begin
Result := True;
end;
function TCustomNodeCreator.NewFormFile(const FormIdent, AncestorIdent: string): IOTAFile;
begin
Result := nil;
end;
function TCustomNodeCreator.NewImplSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
begin
Result := TOTAFile.Create(Format('Custom node %s' + sLineBreak, [ExtractFileName(FFileName)]));
end;
function TCustomNodeCreator.NewIntfSource(const ModuleIdent, FormIdent, AncestorIdent: string): IOTAFile;
begin
Result := nil;
end;
procedure TCustomNodeCreator.FormCreated(const FormEditor: IOTAFormEditor);
begin
end;
end.unit CustomNodeOI.Data;
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections, Xml.XMLIntf,
ToolsAPI;
const
cCustomNodeSection = 'CustomNodeOI';
cCustomNodeElement = 'Node';
cCustomNodeExt = '.cnode';
type
TCustomNodeStatus = (nsDraft, nsReview, nsApproved);
TCustomNodeData = class(TPersistent)
private
FProjectFileName: string;
FFileName: string;
FTitle: string;
FOwnerName: string;
FPriority: Integer;
FStatus: TCustomNodeStatus;
FCreated: TDateTime;
procedure Changed;
function GetFullPath: string;
procedure SetOwnerName(const Value: string);
procedure SetPriority(Value: Integer);
procedure SetStatus(Value: TCustomNodeStatus);
procedure SetTitle(const Value: string);
public
constructor Create(const AProjectFileName, AFileName: string);
procedure LoadFromXML(const Node: IXMLNode);
procedure SaveToXML(const Node: IXMLNode);
property FileName: string read FFileName;
published
property Title: string read FTitle write SetTitle;
property OwnerName: string read FOwnerName write SetOwnerName;
property Priority: Integer read FPriority write SetPriority default 0;
property Status: TCustomNodeStatus read FStatus write SetStatus default nsDraft;
property Created: TDateTime read FCreated;
property FullPath: string read GetFullPath;
end;
TCustomNodeList = TObjectList<TCustomNodeData>;
TCustomNodeRegistry = class
private
FProjects: TObjectDictionary<string, TCustomNodeList>;
class function Key(const FileName: string): string; static;
procedure ReleaseNodes(Nodes: TCustomNodeList);
public
constructor Create;
destructor Destroy; override;
function Add(const ProjectFileName, NodeFileName: string): TCustomNodeData;
function Find(const ProjectFileName, NodeFileName: string): TCustomNodeData;
function Nodes(const ProjectFileName: string): TCustomNodeList;
procedure LoadProject(const ProjectFileName: string; const Section: IXMLNode);
procedure SaveProject(const Project: IOTAProject; const Section: IXMLNode);
procedure RemoveProject(const ProjectFileName: string);
procedure Clear;
end;
function CustomNodes: TCustomNodeRegistry;
procedure FreeCustomNodes;
function FindProject(const ProjectFileName: string): IOTAProject;
function ProjectContainsFile(const Project: IOTAProject; const FileName: string): Boolean;
implementation
uses
System.Variants, System.TypInfo, System.DateUtils, PropInspAPI;
var
GCustomNodes: TCustomNodeRegistry;
function CustomNodes: TCustomNodeRegistry;
begin
if GCustomNodes = nil then
GCustomNodes := TCustomNodeRegistry.Create;
Result := GCustomNodes;
end;
procedure FreeCustomNodes;
begin
FreeAndNil(GCustomNodes);
end;
function FindProject(const ProjectFileName: string): IOTAProject;
var
Module: IOTAModule;
begin
Result := nil;
Module := (BorlandIDEServices as IOTAModuleServices).FindModule(ProjectFileName);
if Module <> nil then
Supports(Module, IOTAProject, Result);
end;
function ProjectContainsFile(const Project: IOTAProject; const FileName: string): Boolean;
var
I: Integer;
begin
for I := 0 to Project.GetModuleCount - 1 do
if SameFileName(Project.GetModule(I).FileName, FileName) then
Exit(True);
Result := False;
end;
function NodeFullName(const ProjectFileName, NodeFileName: string): string;
begin
if ExtractFileDrive(NodeFileName) <> '' then
Result := ExpandFileName(NodeFileName)
else
Result := ExpandFileName(ExtractFilePath(ProjectFileName) + NodeFileName);
end;
function AttrValue(const Node: IXMLNode; const Name: string): string;
begin
if Node.HasAttribute(Name) then
Result := VarToStr(Node.Attributes[Name])
else
Result := '';
end;
constructor TCustomNodeData.Create(const AProjectFileName, AFileName: string);
begin
inherited Create;
FProjectFileName := AProjectFileName;
FFileName := ExtractRelativePath(ExtractFilePath(AProjectFileName), AFileName);
FTitle := ChangeFileExt(ExtractFileName(AFileName), '');
FCreated := Now;
end;
procedure TCustomNodeData.Changed;
var
Project: IOTAProject;
begin
Project := FindProject(FProjectFileName);
if Project <> nil then
Project.MarkModified;
end;
function TCustomNodeData.GetFullPath: string;
begin
Result := ExpandFileName(ExtractFilePath(FProjectFileName) + FFileName);
end;
procedure TCustomNodeData.LoadFromXML(const Node: IXMLNode);
var
StatusValue: Integer;
CreatedValue: TDateTime;
begin
FTitle := AttrValue(Node, 'Title');
FOwnerName := AttrValue(Node, 'Owner');
FPriority := StrToIntDef(AttrValue(Node, 'Priority'), 0);
StatusValue := GetEnumValue(TypeInfo(TCustomNodeStatus), AttrValue(Node, 'Status'));
if StatusValue >= 0 then
FStatus := TCustomNodeStatus(StatusValue);
if TryISO8601ToDate(AttrValue(Node, 'Created'), CreatedValue, False) then
FCreated := CreatedValue;
end;
procedure TCustomNodeData.SaveToXML(const Node: IXMLNode);
begin
Node.Attributes['File'] := FFileName;
Node.Attributes['Title'] := FTitle;
Node.Attributes['Owner'] := FOwnerName;
Node.Attributes['Priority'] := IntToStr(FPriority);
Node.Attributes['Status'] := GetEnumName(TypeInfo(TCustomNodeStatus), Ord(FStatus));
Node.Attributes['Created'] := DateToISO8601(FCreated, False);
end;
procedure TCustomNodeData.SetOwnerName(const Value: string);
begin
if FOwnerName <> Value then
begin
FOwnerName := Value;
Changed;
end;
end;
procedure TCustomNodeData.SetPriority(Value: Integer);
begin
if FPriority <> Value then
begin
FPriority := Value;
Changed;
end;
end;
procedure TCustomNodeData.SetStatus(Value: TCustomNodeStatus);
begin
if FStatus <> Value then
begin
FStatus := Value;
Changed;
end;
end;
procedure TCustomNodeData.SetTitle(const Value: string);
begin
if FTitle <> Value then
begin
FTitle := Value;
Changed;
end;
end;
constructor TCustomNodeRegistry.Create;
begin
inherited Create;
FProjects := TObjectDictionary<string, TCustomNodeList>.Create([doOwnsValues]);
end;
destructor TCustomNodeRegistry.Destroy;
begin
Clear;
FProjects.Free;
inherited;
end;
class function TCustomNodeRegistry.Key(const FileName: string): string;
begin
Result := AnsiUpperCase(ExpandFileName(FileName));
end;
procedure TCustomNodeRegistry.ReleaseNodes(Nodes: TCustomNodeList);
var
PropInsp: INTAPropInspServices;
Data: TCustomNodeData;
begin
if Supports(BorlandIDEServices, INTAPropInspServices, PropInsp) then
for Data in Nodes do
if PropInsp.ObjectsSelected([Data]) then
PropInsp.SelectObjects([]);
end;
function TCustomNodeRegistry.Add(const ProjectFileName, NodeFileName: string): TCustomNodeData;
begin
Result := Find(ProjectFileName, NodeFileName);
if Result = nil then
begin
Result := TCustomNodeData.Create(ProjectFileName, NodeFullName(ProjectFileName, NodeFileName));
Nodes(ProjectFileName).Add(Result);
end;
end;
function TCustomNodeRegistry.Find(const ProjectFileName, NodeFileName: string): TCustomNodeData;
var
FullName: string;
Data: TCustomNodeData;
begin
FullName := NodeFullName(ProjectFileName, NodeFileName);
for Data in Nodes(ProjectFileName) do
if SameFileName(Data.FullPath, FullName) then
Exit(Data);
Result := nil;
end;
function TCustomNodeRegistry.Nodes(const ProjectFileName: string): TCustomNodeList;
var
Project: IOTAProject;
Section: IXMLNode;
begin
if not FProjects.TryGetValue(Key(ProjectFileName), Result) then
begin
Result := TCustomNodeList.Create(True);
FProjects.Add(Key(ProjectFileName), Result);
Project := FindProject(ProjectFileName);
if Project <> nil then
begin
Section := (BorlandIDEServices as IOTAProjectFileStorage).GetProjectStorageNode(Project, cCustomNodeSection, False);
if Section <> nil then
LoadProject(ProjectFileName, Section);
end;
end;
end;
procedure TCustomNodeRegistry.LoadProject(const ProjectFileName: string; const Section: IXMLNode);
var
List: TCustomNodeList;
I: Integer;
Child: IXMLNode;
Data: TCustomNodeData;
begin
if not FProjects.TryGetValue(Key(ProjectFileName), List) then
begin
List := TCustomNodeList.Create(True);
FProjects.Add(Key(ProjectFileName), List);
end;
ReleaseNodes(List);
List.Clear;
for I := 0 to Section.ChildNodes.Count - 1 do
begin
Child := Section.ChildNodes[I];
if (Child.NodeName = cCustomNodeElement) and (AttrValue(Child, 'File') <> '') then
begin
Data := TCustomNodeData.Create(ProjectFileName, NodeFullName(ProjectFileName, AttrValue(Child, 'File')));
Data.LoadFromXML(Child);
List.Add(Data);
end;
end;
end;
procedure TCustomNodeRegistry.SaveProject(const Project: IOTAProject; const Section: IXMLNode);
var
Data: TCustomNodeData;
begin
Section.ChildNodes.Clear;
for Data in Nodes(Project.FileName) do
if ProjectContainsFile(Project, Data.FullPath) then
Data.SaveToXML(Section.AddChild(cCustomNodeElement));
end;
procedure TCustomNodeRegistry.RemoveProject(const ProjectFileName: string);
var
List: TCustomNodeList;
begin
if FProjects.TryGetValue(Key(ProjectFileName), List) then
begin
ReleaseNodes(List);
FProjects.Remove(Key(ProjectFileName));
end;
end;
procedure TCustomNodeRegistry.Clear;
var
List: TCustomNodeList;
begin
for List in FProjects.Values do
ReleaseNodes(List);
FProjects.Clear;
end;
end.unit CustomNodeOI.Inspector;
interface
uses
System.Classes, System.SysUtils, ToolsAPI, DesignIntf, DesignEditors, PropInspAPI,
CustomNodeOI.Data;
type
TCustomNodeSelectionWatcher = class(TNotifierObject, IOTASelectionNotifier)
private
FPending: Boolean;
public
procedure SelectionChanged(const NewSelection: IOTAPropInspSelection);
procedure UpdateState;
procedure ShowSelectedNode;
end;
TCustomNodeStringProperty = class(TStringProperty)
public
function GetName: string; override;
end;
TCustomNodeIntegerProperty = class(TIntegerProperty)
public
function GetName: string; override;
end;
TCustomNodeStatusProperty = class(TEnumProperty)
public
function GetName: string; override;
function GetValue: string; override;
procedure GetValues(Proc: TGetStrProc); override;
procedure SetValue(const Value: string); override;
end;
TCustomNodeDateTimeProperty = class(TDateTimeProperty)
public
function GetName: string; override;
end;
function SelectedCustomNode: TCustomNodeData;
procedure RegisterCustomNodePropertyEditors;
implementation
uses
Winapi.Windows, Vcl.Controls;
const
cStatusNames: array[TCustomNodeStatus] of string = ('Draft', 'In Review', 'Approved');
function DisplayName(const PropName: string): string;
begin
if PropName = 'OwnerName' then
Result := 'Owner'
else if PropName = 'FullPath' then
Result := 'Full Path'
else
Result := PropName;
end;
function ProjectManagerFocused: Boolean;
var
Control: TWinControl;
begin
Control := FindControl(GetFocus);
while Control <> nil do
begin
if Control.ClassNameIs('TProjectManagerForm') then
Exit(True);
Control := Control.Parent;
end;
Result := False;
end;
function SelectedCustomNode: TCustomNodeData;
var
ProjectManager: IOTAProjectManager;
Project: IOTAProject;
Ident: string;
begin
Result := nil;
if Supports(BorlandIDEServices, IOTAProjectManager, ProjectManager) then
begin
Project := ProjectManager.GetCurrentSelection(Ident);
if (Project <> nil) and SameText(ExtractFileExt(Ident), cCustomNodeExt) then
Result := CustomNodes.Find(Project.FileName, Ident);
end;
end;
procedure TCustomNodeSelectionWatcher.SelectionChanged(const NewSelection: IOTAPropInspSelection);
var
Data: TCustomNodeData;
begin
if FPending or not ProjectManagerFocused then
Exit;
Data := SelectedCustomNode;
if (Data = nil) or (BorlandIDEServices as INTAPropInspServices).ObjectsSelected([Data]) then
Exit;
FPending := True;
TThread.ForceQueue(nil, ShowSelectedNode);
end;
procedure TCustomNodeSelectionWatcher.UpdateState;
begin
end;
procedure TCustomNodeSelectionWatcher.ShowSelectedNode;
var
Data: TCustomNodeData;
PropInsp: INTAPropInspServices;
begin
FPending := False;
Data := SelectedCustomNode;
PropInsp := BorlandIDEServices as INTAPropInspServices;
if (Data <> nil) and not PropInsp.ObjectsSelected([Data]) then
PropInsp.SelectObjects([Data]);
end;
function TCustomNodeStringProperty.GetName: string;
begin
Result := DisplayName(inherited GetName);
end;
function TCustomNodeIntegerProperty.GetName: string;
begin
Result := DisplayName(inherited GetName);
end;
function TCustomNodeStatusProperty.GetName: string;
begin
Result := DisplayName(inherited GetName);
end;
function TCustomNodeStatusProperty.GetValue: string;
begin
Result := cStatusNames[TCustomNodeStatus(GetOrdValue)];
end;
procedure TCustomNodeStatusProperty.GetValues(Proc: TGetStrProc);
var
Status: TCustomNodeStatus;
begin
for Status := Low(TCustomNodeStatus) to High(TCustomNodeStatus) do
Proc(cStatusNames[Status]);
end;
procedure TCustomNodeStatusProperty.SetValue(const Value: string);
var
Status: TCustomNodeStatus;
begin
for Status := Low(TCustomNodeStatus) to High(TCustomNodeStatus) do
if SameText(Value, cStatusNames[Status]) then
begin
SetOrdValue(Ord(Status));
Exit;
end;
inherited SetValue(Value);
end;
function TCustomNodeDateTimeProperty.GetName: string;
begin
Result := DisplayName(inherited GetName);
end;
procedure RegisterCustomNodePropertyEditors;
begin
RegisterPropertyEditor(TypeInfo(string), TCustomNodeData, 'Title', TCustomNodeStringProperty);
RegisterPropertyEditor(TypeInfo(string), TCustomNodeData, 'OwnerName', TCustomNodeStringProperty);
RegisterPropertyEditor(TypeInfo(string), TCustomNodeData, 'FullPath', TCustomNodeStringProperty);
RegisterPropertyEditor(TypeInfo(Integer), TCustomNodeData, 'Priority', TCustomNodeIntegerProperty);
RegisterPropertyEditor(TypeInfo(TCustomNodeStatus), TCustomNodeData, 'Status', TCustomNodeStatusProperty);
RegisterPropertyEditor(TypeInfo(TDateTime), TCustomNodeData, 'Created', TCustomNodeDateTimeProperty);
end;
end.unit CustomNodeOI.Menu;
interface
uses
System.Classes, System.SysUtils, ToolsAPI;
type
TCustomNodeMenuItem = class(TNotifierObject, IOTALocalMenu, IOTAProjectManagerMenu)
private
FCaption: string;
FEnabled: Boolean;
FHelpContext: Integer;
FName: string;
FParent: string;
FPosition: Integer;
FVerb: string;
public
constructor Create;
function GetCaption: string;
function GetChecked: Boolean;
function GetEnabled: Boolean;
function GetHelpContext: Integer;
function GetName: string;
function GetParent: string;
function GetPosition: Integer;
function GetVerb: string;
procedure SetCaption(const Value: string);
procedure SetChecked(Value: Boolean);
procedure SetEnabled(Value: Boolean);
procedure SetHelpContext(Value: Integer);
procedure SetName(const Value: string);
procedure SetParent(const Value: string);
procedure SetPosition(Value: Integer);
procedure SetVerb(const Value: string);
function GetIsMultiSelectable: Boolean;
procedure SetIsMultiSelectable(Value: Boolean);
procedure Execute(const MenuContextList: IInterfaceList); overload;
function PreExecute(const MenuContextList: IInterfaceList): Boolean;
function PostExecute(const MenuContextList: IInterfaceList): Boolean;
end;
TCustomNodeMenuNotifier = class(TNotifierObject, IOTAProjectMenuItemCreatorNotifier)
public
procedure AddMenu(const Project: IOTAProject; const IdentList: TStrings;
const ProjectManagerMenuList: IInterfaceList; IsMultiSelect: Boolean);
end;
implementation
uses
CustomNodeOI.Creator;
constructor TCustomNodeMenuItem.Create;
begin
inherited Create;
FCaption := 'Add Custom Node';
FEnabled := True;
FName := 'CustomNodeOIAddItem';
FPosition := pmmpAddNew + 10;
FVerb := 'CustomNodeOI.Add';
end;
function TCustomNodeMenuItem.GetCaption: string;
begin
Result := FCaption;
end;
function TCustomNodeMenuItem.GetChecked: Boolean;
begin
Result := False;
end;
function TCustomNodeMenuItem.GetEnabled: Boolean;
begin
Result := FEnabled;
end;
function TCustomNodeMenuItem.GetHelpContext: Integer;
begin
Result := FHelpContext;
end;
function TCustomNodeMenuItem.GetName: string;
begin
Result := FName;
end;
function TCustomNodeMenuItem.GetParent: string;
begin
Result := FParent;
end;
function TCustomNodeMenuItem.GetPosition: Integer;
begin
Result := FPosition;
end;
function TCustomNodeMenuItem.GetVerb: string;
begin
Result := FVerb;
end;
procedure TCustomNodeMenuItem.SetCaption(const Value: string);
begin
FCaption := Value;
end;
procedure TCustomNodeMenuItem.SetChecked(Value: Boolean);
begin
end;
procedure TCustomNodeMenuItem.SetEnabled(Value: Boolean);
begin
FEnabled := Value;
end;
procedure TCustomNodeMenuItem.SetHelpContext(Value: Integer);
begin
FHelpContext := Value;
end;
procedure TCustomNodeMenuItem.SetName(const Value: string);
begin
FName := Value;
end;
procedure TCustomNodeMenuItem.SetParent(const Value: string);
begin
FParent := Value;
end;
procedure TCustomNodeMenuItem.SetPosition(Value: Integer);
begin
FPosition := Value;
end;
procedure TCustomNodeMenuItem.SetVerb(const Value: string);
begin
FVerb := Value;
end;
function TCustomNodeMenuItem.GetIsMultiSelectable: Boolean;
begin
Result := False;
end;
procedure TCustomNodeMenuItem.SetIsMultiSelectable(Value: Boolean);
begin
end;
procedure TCustomNodeMenuItem.Execute(const MenuContextList: IInterfaceList);
var
Context: IOTAProjectMenuContext;
begin
if (MenuContextList.Count > 0) and
Supports(MenuContextList[0], IOTAProjectMenuContext, Context) and
(Context.Project <> nil) then
AddCustomNode(Context.Project);
end;
function TCustomNodeMenuItem.PreExecute(const MenuContextList: IInterfaceList): Boolean;
begin
Result := True;
end;
function TCustomNodeMenuItem.PostExecute(const MenuContextList: IInterfaceList): Boolean;
begin
Result := True;
end;
procedure TCustomNodeMenuNotifier.AddMenu(const Project: IOTAProject;
const IdentList: TStrings; const ProjectManagerMenuList: IInterfaceList;
IsMultiSelect: Boolean);
begin
if not IsMultiSelect and (Project <> nil) and (IdentList.IndexOf(sProjectContainer) >= 0) then
ProjectManagerMenuList.Add(TCustomNodeMenuItem.Create as IOTAProjectManagerMenu);
end;
end.
unit CustomNodeOI.Reg;
interface
procedure Register;
implementation
uses
System.Classes, System.SysUtils, ToolsAPI, PropInspAPI, CustomNodeOI.Data, CustomNodeOI.Menu,
CustomNodeOI.Storage, CustomNodeOI.Inspector;
var
MenuNotifierIndex: Integer = -1;
StorageNotifierIndex: Integer = -1;
SelectionNotifierIndex: Integer = -1;
SelectionWatcher: TCustomNodeSelectionWatcher;
SelectionWatcherIntf: IOTASelectionNotifier;
procedure Register;
begin
RegisterCustomNodePropertyEditors;
MenuNotifierIndex := (BorlandIDEServices as IOTAProjectManager).AddMenuItemCreatorNotifier(
TCustomNodeMenuNotifier.Create);
StorageNotifierIndex := (BorlandIDEServices as IOTAProjectFileStorage).AddNotifier(
TCustomNodeStorage.Create);
SelectionWatcher := TCustomNodeSelectionWatcher.Create;
SelectionWatcherIntf := SelectionWatcher;
SelectionNotifierIndex := (BorlandIDEServices as IOTAPropInspServices).AddNotifier(SelectionWatcherIntf);
end;
procedure Unregister;
var
PropInsp: IOTAPropInspServices;
Storage: IOTAProjectFileStorage;
ProjectManager: IOTAProjectManager;
begin
if (SelectionNotifierIndex >= 0) and Supports(BorlandIDEServices, IOTAPropInspServices, PropInsp) then
PropInsp.RemoveNotifier(SelectionNotifierIndex);
if SelectionWatcher <> nil then
TThread.RemoveQueuedEvents(SelectionWatcher.ShowSelectedNode);
SelectionWatcherIntf := nil;
SelectionWatcher := nil;
if (StorageNotifierIndex >= 0) and Supports(BorlandIDEServices, IOTAProjectFileStorage, Storage) then
Storage.RemoveNotifier(StorageNotifierIndex);
if (MenuNotifierIndex >= 0) and Supports(BorlandIDEServices, IOTAProjectManager, ProjectManager) then
ProjectManager.RemoveMenuItemCreatorNotifier(MenuNotifierIndex);
FreeCustomNodes;
end;
initialization
finalization
Unregister;
end.unit CustomNodeOI.Storage;
interface
uses
System.SysUtils, Xml.XMLIntf, ToolsAPI;
type
TCustomNodeStorage = class(TInterfacedObject, IOTAProjectFileStorageNotifier)
public
function GetName: string;
procedure ProjectLoaded(const ProjectOrGroup: IOTAModule; const Node: IXMLNode);
procedure CreatingProject(const ProjectOrGroup: IOTAModule);
procedure ProjectSaving(const ProjectOrGroup: IOTAModule; const Node: IXMLNode);
procedure ProjectClosing(const ProjectOrGroup: IOTAModule);
end;
implementation
uses
CustomNodeOI.Data;
function TCustomNodeStorage.GetName: string;
begin
Result := cCustomNodeSection;
end;
procedure TCustomNodeStorage.ProjectLoaded(const ProjectOrGroup: IOTAModule; const Node: IXMLNode);
begin
CustomNodes.LoadProject(ProjectOrGroup.FileName, Node);
end;
procedure TCustomNodeStorage.CreatingProject(const ProjectOrGroup: IOTAModule);
begin
end;
procedure TCustomNodeStorage.ProjectSaving(const ProjectOrGroup: IOTAModule; const Node: IXMLNode);
var
Project: IOTAProject;
begin
if Supports(ProjectOrGroup, IOTAProject, Project) then
CustomNodes.SaveProject(Project, Node);
end;
procedure TCustomNodeStorage.ProjectClosing(const ProjectOrGroup: IOTAModule);
begin
CustomNodes.RemoveProject(ProjectOrGroup.FileName);
end;
end.The identifier you pass to TProjectTreeContainer.CreateNewContainer becomes the container’s ident. DPM uses one ident (DPMPackagesContainer) for all its nodes. If you copied that, the ident alone can’t tell your nodes apart, so give each node a unique one: a fixed prefix plus your own key, and never something that could be a file name.
const
cMyNodePrefix = 'MyTool.Node:';
container := TProjectTreeContainer.CreateNewContainer(parentContainer,
displayName, cMyNodePrefix + nodeKey);Unique idents also keep the IDE happy: in a few places it matches sibling containers by ident, so siblings that share one can get mixed up.
IOTAProjectManager.GetCurrentSelection returns the selected container’s ident, plus its project. CreateNewContainer passes the parent’s project to the constructor, so you get the project back for your nodes too:
function SelectedMyNode: TMyNodeData;
var
Project: IOTAProject;
Ident: string;
begin
Result := nil;
Project := (BorlandIDEServices as IOTAProjectManager).GetCurrentSelection(Ident);
if (Project <> nil) and Ident.StartsWith(cMyNodePrefix) then
Result := MyNodes.Find(Project.FileName, Ident);
end;TMyNodeData is your TPersistent with published properties (step 1 of the earlier reply), and MyNodes is your own lookup keyed by project file name and ident.
The rest of step is unchanged from the IOTAModuleCreator approach:
procedure TMyNodeSelectionWatcher.SelectionChanged(const NewSelection: IOTAPropInspSelection);
var
Data: TMyNodeData;
begin
if FPending or not ProjectManagerFocused then
Exit;
Data := SelectedMyNode;
if (Data = nil) or (BorlandIDEServices as INTAPropInspServices).ObjectsSelected([Data]) then
Exit;
FPending := True;
TThread.ForceQueue(nil, ShowSelectedNode);
end;
procedure TMyNodeSelectionWatcher.ShowSelectedNode;
var
Data: TMyNodeData;
PropInsp: INTAPropInspServices;
begin
FPending := False;
Data := SelectedMyNode;
PropInsp := BorlandIDEServices as INTAPropInspServices;
if (Data <> nil) and not PropInsp.ObjectsSelected([Data]) then
PropInsp.SelectObjects([Data]);
end;The same rules apply as before:
- Queue the SelectObjects call. Calling it from inside the notification would re-enter both the Object Inspector and the Project Manager.
- Only react while the Project Manager has the focus. Otherwise you’d take the Object Inspector back from the form designer.
- Keep ownership of your data objects. Before your package unloads, clear the Object Inspector if it still shows one of them.
The property editors (step 2 of the earlier approach) and the IOTAProjectFileStorage section (step 4) work as before, keyed by your ident instead of a file name.
Let’s look at a concrete example:
package DelphinusDPMStyleNode;
{$ALIGN 8}
{$ASSERTIONS ON}
{$BOOLEVAL OFF}
{$DEBUGINFO ON}
{$EXTENDEDSYNTAX ON}
{$IMPORTEDDATA ON}
{$IOCHECKS ON}
{$LOCALSYMBOLS ON}
{$LONGSTRINGS ON}
{$OPENSTRINGS ON}
{$OPTIMIZATION OFF}
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
{$REFERENCEINFO ON}
{$SAFEDIVIDE OFF}
{$STACKFRAMES ON}
{$TYPEDADDRESS OFF}
{$VARSTRINGCHECKS ON}
{$WRITEABLECONST OFF}
{$MINENUMSIZE 1}
{$DESCRIPTION 'Delphinus/DPM-style Project Manager nodes with Object Inspector data (demo)'}
{$DESIGNONLY}
{$IMPLICITBUILD OFF}
requires
rtl,
vcl,
xmlrtl,
designide;
contains
DelphinusDPMStyleNode.Tree in 'DelphinusDPMStyleNode.Tree.pas',
DelphinusDPMStyleNode.Nodes in 'DelphinusDPMStyleNode.Nodes.pas',
DelphinusDPMStyleNode.Reg in 'DelphinusDPMStyleNode.Reg.pas';
end.unit DelphinusDPMStyleNode.Nodes;
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections, System.Generics.Defaults,
Xml.XMLIntf, ToolsAPI, PropInspAPI, DelphinusDPMStyleNode.Tree;
const
cIdentPrefix = 'DelphinusDPMStyleNode:';
cStorageSection = 'DelphinusDPMStyleNode';
cStorageElement = 'Node';
type
TDelphinusDPMNodeData = class(TPersistent)
private
FProjectFileName: string;
FKey: string;
FIdent: string;
FName: string;
FNotes: string;
FPriority: Integer;
procedure Changed;
procedure SetNotes(const Value: string);
procedure SetPriority(Value: Integer);
public
constructor Create(const AProjectFileName, AKey, AName: string);
property ProjectFileName: string read FProjectFileName;
property Key: string read FKey;
published
property Name: string read FName;
property Ident: string read FIdent;
property Notes: string read FNotes write SetNotes;
property Priority: Integer read FPriority write SetPriority default 0;
end;
TDelphinusDPMSavedNode = record
Notes: string;
Priority: Integer;
end;
TDelphinusDPMSavedNodes = TDictionary<string, TDelphinusDPMSavedNode>;
TDelphinusDPMPendingProject = class
private
FFileName: string;
FSaved: TDelphinusDPMSavedNodes;
FAttempts: Integer;
public
constructor Create(const AFileName: string);
destructor Destroy; override;
end;
TDelphinusDPMNodes = class
private
FData: TObjectDictionary<string, TDelphinusDPMNodeData>;
FCategories: TDictionary<string, IGraphLocation>;
FPending: TObjectDictionary<string, TDelphinusDPMPendingProject>;
procedure AddData(const ProjectFileName, Key, Name: string; Saved: TDelphinusDPMSavedNodes);
procedure DropData(const ProjectFileName: string);
procedure AddNodes(const Project: IOTAProject; Saved: TDelphinusDPMSavedNodes);
procedure ProcessPending;
public
constructor Create;
destructor Destroy; override;
procedure AddNodesByUser(const Project: IOTAProject);
procedure RemoveNodesByUser(const Project: IOTAProject);
procedure RemoveNodes(const ProjectFileName: string; RebuildTree: Boolean);
procedure RemoveAll;
function HasNodes(const ProjectFileName: string): Boolean;
function SelectedNodeData: TDelphinusDPMNodeData;
procedure LoadProject(const ProjectFileName: string; const Section: IXMLNode);
procedure SaveProject(const ProjectFileName: string; const Section: IXMLNode);
procedure ProjectClosing(const ProjectFileName: string);
procedure LoadOpenProjects;
end;
TDelphinusDPMNodeSelectionWatcher = class(TNotifierObject, IOTASelectionNotifier)
private
FPending: Boolean;
public
procedure SelectionChanged(const NewSelection: IOTAPropInspSelection);
procedure UpdateState;
procedure ShowSelectedNode;
end;
TDelphinusDPMNodeStorage = class(TInterfacedObject, IOTAProjectFileStorageNotifier)
public
function GetName: string;
procedure ProjectLoaded(const ProjectOrGroup: IOTAModule; const Node: IXMLNode);
procedure CreatingProject(const ProjectOrGroup: IOTAModule);
procedure ProjectSaving(const ProjectOrGroup: IOTAModule; const Node: IXMLNode);
procedure ProjectClosing(const ProjectOrGroup: IOTAModule);
end;
function DelphinusDPMNodes: TDelphinusDPMNodes;
procedure FreeDelphinusDPMNodes;
implementation
uses
Winapi.Windows, System.Variants, Vcl.Controls;
const
cRootKey = 'Root';
cRootName = 'Custom Nodes (demo)';
cChildKeys: array[0..1] of string = ('Alpha', 'Beta');
cRetryDelay = 250;
cMaxAttempts = 40;
var
GDelphinusDPMNodes: TDelphinusDPMNodes;
function DelphinusDPMNodes: TDelphinusDPMNodes;
begin
if GDelphinusDPMNodes = nil then
GDelphinusDPMNodes := TDelphinusDPMNodes.Create;
Result := GDelphinusDPMNodes;
end;
procedure FreeDelphinusDPMNodes;
begin
FreeAndNil(GDelphinusDPMNodes);
end;
function ProjectKey(const ProjectFileName: string): string;
begin
Result := AnsiUpperCase(ExpandFileName(ProjectFileName));
end;
function NodeIdent(const ProjectFileName, Key: string): string;
begin
Result := cIdentPrefix + ProjectKey(ProjectFileName) + ':' + Key;
end;
function FindProject(const ProjectFileName: string): IOTAProject;
var
Module: IOTAModule;
begin
Result := nil;
Module := (BorlandIDEServices as IOTAModuleServices).FindModule(ProjectFileName);
if Module <> nil then
Supports(Module, IOTAProject, Result);
end;
function AttrValue(const Node: IXMLNode; const Name: string): string;
begin
if Node.HasAttribute(Name) then
Result := VarToStr(Node.Attributes[Name])
else
Result := '';
end;
function ProjectManagerFocused: Boolean;
var
Control: TWinControl;
begin
Control := FindControl(GetFocus);
while Control <> nil do
begin
if Control.ClassNameIs('TProjectManagerForm') then
Exit(True);
Control := Control.Parent;
end;
Result := False;
end;
procedure ReleaseFromInspector(Data: TDelphinusDPMNodeData);
var
PropInsp: INTAPropInspServices;
begin
if Supports(BorlandIDEServices, INTAPropInspServices, PropInsp) and PropInsp.ObjectsSelected([Data]) then
PropInsp.SelectObjects([]);
end;
constructor TDelphinusDPMNodeData.Create(const AProjectFileName, AKey, AName: string);
begin
inherited Create;
FProjectFileName := AProjectFileName;
FKey := AKey;
FIdent := NodeIdent(AProjectFileName, AKey);
FName := AName;
end;
procedure TDelphinusDPMNodeData.Changed;
var
Project: IOTAProject;
begin
Project := FindProject(FProjectFileName);
if Project <> nil then
Project.MarkModified;
end;
procedure TDelphinusDPMNodeData.SetNotes(const Value: string);
begin
if FNotes <> Value then
begin
FNotes := Value;
Changed;
end;
end;
procedure TDelphinusDPMNodeData.SetPriority(Value: Integer);
begin
if FPriority <> Value then
begin
FPriority := Value;
Changed;
end;
end;
constructor TDelphinusDPMPendingProject.Create(const AFileName: string);
begin
inherited Create;
FFileName := AFileName;
FSaved := TDelphinusDPMSavedNodes.Create(TIStringComparer.Ordinal);
end;
destructor TDelphinusDPMPendingProject.Destroy;
begin
FSaved.Free;
inherited;
end;
constructor TDelphinusDPMNodes.Create;
begin
inherited Create;
FData := TObjectDictionary<string, TDelphinusDPMNodeData>.Create([doOwnsValues], TIStringComparer.Ordinal);
FCategories := TDictionary<string, IGraphLocation>.Create;
FPending := TObjectDictionary<string, TDelphinusDPMPendingProject>.Create([doOwnsValues]);
end;
destructor TDelphinusDPMNodes.Destroy;
begin
TThread.RemoveQueuedEvents(ProcessPending);
FPending.Clear;
RemoveAll;
FPending.Free;
FCategories.Free;
FData.Free;
inherited;
end;
procedure TDelphinusDPMNodes.AddData(const ProjectFileName, Key, Name: string; Saved: TDelphinusDPMSavedNodes);
var
Data: TDelphinusDPMNodeData;
SavedNode: TDelphinusDPMSavedNode;
begin
Data := TDelphinusDPMNodeData.Create(ProjectFileName, Key, Name);
if (Saved <> nil) and Saved.TryGetValue(Key, SavedNode) then
begin
Data.FNotes := SavedNode.Notes;
Data.FPriority := SavedNode.Priority;
end;
FData.AddOrSetValue(Data.Ident, Data);
end;
procedure TDelphinusDPMNodes.DropData(const ProjectFileName: string);
var
Key: string;
begin
for Key in FData.Keys.ToArray do
if SameFileName(FData[Key].ProjectFileName, ProjectFileName) then
begin
ReleaseFromInspector(FData[Key]);
FData.Remove(Key);
end;
end;
function TDelphinusDPMNodes.HasNodes(const ProjectFileName: string): Boolean;
begin
Result := FCategories.ContainsKey(ProjectKey(ProjectFileName));
end;
procedure TDelphinusDPMNodes.AddNodes(const Project: IOTAProject; Saved: TDelphinusDPMSavedNodes);
var
Tree: TProjectTreeAccess;
ProjectNode: Pointer;
ProjectLocation, Category: IGraphLocation;
Key: string;
begin
if HasNodes(Project.FileName) then
Exit;
Tree := TProjectTreeAccess.Create;
try
ProjectNode := Tree.FindProjectNode(Project.FileName, ProjectLocation);
if ProjectNode = nil then
raise EDelphinusDPMStyleNode.Create('project node not found for ' + ExtractFileName(Project.FileName));
Category := Tree.CreateCategory(ProjectLocation, cRootName, NodeIdent(Project.FileName, cRootKey));
for Key in cChildKeys do
Category.GetChildren.Add(Tree.CreateCategory(Category, Key, NodeIdent(Project.FileName, Key)));
ProjectLocation.GetChildren.Add(Category);
FCategories.Add(ProjectKey(Project.FileName), Category);
AddData(Project.FileName, cRootKey, cRootName, Saved);
for Key in cChildKeys do
AddData(Project.FileName, Key, Key, Saved);
Tree.ResetNode(ProjectNode);
Log('added 3 nodes to ' + ExtractFileName(Project.FileName));
finally
Tree.Free;
end;
end;
procedure TDelphinusDPMNodes.AddNodesByUser(const Project: IOTAProject);
begin
if HasNodes(Project.FileName) then
begin
Log('nodes already added to ' + ExtractFileName(Project.FileName));
Exit;
end;
AddNodes(Project, nil);
(BorlandIDEServices as IOTAProjectFileStorage).AddNewSection(Project, cStorageSection, False);
Project.MarkModified;
end;
procedure TDelphinusDPMNodes.RemoveNodesByUser(const Project: IOTAProject);
begin
RemoveNodes(Project.FileName, True);
Project.MarkModified;
end;
procedure TDelphinusDPMNodes.RemoveNodes(const ProjectFileName: string; RebuildTree: Boolean);
var
Category, ProjectLocation: IGraphLocation;
Tree: TProjectTreeAccess;
ProjectNode: Pointer;
begin
DropData(ProjectFileName);
if not FCategories.TryGetValue(ProjectKey(ProjectFileName), Category) then
Exit;
FCategories.Remove(ProjectKey(ProjectFileName));
if not RebuildTree then
Exit;
Tree := TProjectTreeAccess.Create;
try
ProjectNode := Tree.FindProjectNode(ProjectFileName, ProjectLocation);
if ProjectNode <> nil then
begin
ProjectLocation.GetChildren.Remove(Category);
Tree.ResetNode(ProjectNode);
end;
Log('removed nodes from ' + ExtractFileName(ProjectFileName));
finally
Tree.Free;
end;
end;
procedure TDelphinusDPMNodes.RemoveAll;
var
Key: string;
begin
for Key in FCategories.Keys.ToArray do
try
RemoveNodes(Key, True);
except
on E: Exception do
Log('remove failed: ' + E.Message);
end;
for Key in FData.Keys.ToArray do
ReleaseFromInspector(FData[Key]);
FData.Clear;
end;
function TDelphinusDPMNodes.SelectedNodeData: TDelphinusDPMNodeData;
var
ProjectManager: IOTAProjectManager;
Ident: string;
begin
Result := nil;
if Supports(BorlandIDEServices, IOTAProjectManager, ProjectManager) then
begin
ProjectManager.GetCurrentSelection(Ident);
if Ident.StartsWith(cIdentPrefix) then
FData.TryGetValue(Ident, Result);
end;
end;
procedure TDelphinusDPMNodes.LoadProject(const ProjectFileName: string; const Section: IXMLNode);
var
Pending: TDelphinusDPMPendingProject;
Child: IXMLNode;
SavedNode: TDelphinusDPMSavedNode;
I: Integer;
begin
Pending := TDelphinusDPMPendingProject.Create(ProjectFileName);
for I := 0 to Section.ChildNodes.Count - 1 do
begin
Child := Section.ChildNodes[I];
if (Child.NodeName = cStorageElement) and (AttrValue(Child, 'Key') <> '') then
begin
SavedNode.Notes := AttrValue(Child, 'Notes');
SavedNode.Priority := StrToIntDef(AttrValue(Child, 'Priority'), 0);
Pending.FSaved.AddOrSetValue(AttrValue(Child, 'Key'), SavedNode);
end;
end;
if Pending.FSaved.Count = 0 then
begin
Pending.Free;
Exit;
end;
FPending.AddOrSetValue(ProjectKey(ProjectFileName), Pending);
TThread.RemoveQueuedEvents(ProcessPending);
TThread.ForceQueue(nil, ProcessPending, cRetryDelay);
end;
procedure TDelphinusDPMNodes.ProcessPending;
var
Key: string;
Pending: TDelphinusDPMPendingProject;
Project: IOTAProject;
begin
for Key in FPending.Keys.ToArray do
begin
Pending := FPending[Key];
Project := FindProject(Pending.FFileName);
if Project = nil then
begin
FPending.Remove(Key);
Continue;
end;
try
AddNodes(Project, Pending.FSaved);
Log(Format('restored saved data for %s after %d attempt(s)',
[ExtractFileName(Pending.FFileName), Pending.FAttempts + 1]));
FPending.Remove(Key);
except
on E: Exception do
begin
Inc(Pending.FAttempts);
if Pending.FAttempts >= cMaxAttempts then
begin
Log('could not restore nodes for ' + ExtractFileName(Pending.FFileName) + ': ' + E.Message);
FPending.Remove(Key);
end;
end;
end;
end;
if FPending.Count > 0 then
TThread.ForceQueue(nil, ProcessPending, cRetryDelay);
end;
procedure TDelphinusDPMNodes.SaveProject(const ProjectFileName: string; const Section: IXMLNode);
var
Keys: TArray<string>;
Key: string;
Data: TDelphinusDPMNodeData;
Node: IXMLNode;
begin
Section.ChildNodes.Clear;
Keys := FData.Keys.ToArray;
TArray.Sort<string>(Keys);
for Key in Keys do
begin
Data := FData[Key];
if SameFileName(Data.ProjectFileName, ProjectFileName) then
begin
Node := Section.AddChild(cStorageElement);
Node.Attributes['Key'] := Data.Key;
Node.Attributes['Notes'] := Data.Notes;
Node.Attributes['Priority'] := IntToStr(Data.Priority);
end;
end;
end;
procedure TDelphinusDPMNodes.ProjectClosing(const ProjectFileName: string);
begin
FPending.Remove(ProjectKey(ProjectFileName));
RemoveNodes(ProjectFileName, False);
end;
procedure TDelphinusDPMNodes.LoadOpenProjects;
var
Group: IOTAProjectGroup;
Section: IXMLNode;
I: Integer;
begin
Group := (BorlandIDEServices as IOTAModuleServices).MainProjectGroup;
if Group = nil then
Exit;
for I := 0 to Group.ProjectCount - 1 do
begin
Section := (BorlandIDEServices as IOTAProjectFileStorage).GetProjectStorageNode(Group.Projects[I],
cStorageSection, False);
if Section <> nil then
LoadProject(Group.Projects[I].FileName, Section);
end;
end;
procedure TDelphinusDPMNodeSelectionWatcher.SelectionChanged(const NewSelection: IOTAPropInspSelection);
var
Data: TDelphinusDPMNodeData;
begin
if FPending or not ProjectManagerFocused then
Exit;
Data := DelphinusDPMNodes.SelectedNodeData;
if (Data = nil) or (BorlandIDEServices as INTAPropInspServices).ObjectsSelected([Data]) then
Exit;
FPending := True;
TThread.ForceQueue(nil, ShowSelectedNode);
end;
procedure TDelphinusDPMNodeSelectionWatcher.UpdateState;
begin
end;
procedure TDelphinusDPMNodeSelectionWatcher.ShowSelectedNode;
var
Data: TDelphinusDPMNodeData;
PropInsp: INTAPropInspServices;
begin
FPending := False;
Data := DelphinusDPMNodes.SelectedNodeData;
PropInsp := BorlandIDEServices as INTAPropInspServices;
if (Data <> nil) and not PropInsp.ObjectsSelected([Data]) then
PropInsp.SelectObjects([Data]);
end;
function TDelphinusDPMNodeStorage.GetName: string;
begin
Result := cStorageSection;
end;
procedure TDelphinusDPMNodeStorage.ProjectLoaded(const ProjectOrGroup: IOTAModule; const Node: IXMLNode);
begin
DelphinusDPMNodes.LoadProject(ProjectOrGroup.FileName, Node);
end;
procedure TDelphinusDPMNodeStorage.CreatingProject(const ProjectOrGroup: IOTAModule);
begin
end;
procedure TDelphinusDPMNodeStorage.ProjectSaving(const ProjectOrGroup: IOTAModule; const Node: IXMLNode);
begin
if Supports(ProjectOrGroup, IOTAProject) then
DelphinusDPMNodes.SaveProject(ProjectOrGroup.FileName, Node);
end;
procedure TDelphinusDPMNodeStorage.ProjectClosing(const ProjectOrGroup: IOTAModule);
begin
if GDelphinusDPMNodes <> nil then
GDelphinusDPMNodes.ProjectClosing(ProjectOrGroup.FileName);
end;
end.unit DelphinusDPMStyleNode.Reg;
interface
procedure Register;
implementation
uses
System.Classes, System.SysUtils, ToolsAPI, PropInspAPI, DelphinusDPMStyleNode.Tree, DelphinusDPMStyleNode.Nodes;
type
TDelphinusDPMNodeMenuAction = (maAdd, maRemove);
TDelphinusDPMNodeMenuItem = class(TNotifierObject, IOTALocalMenu, IOTAProjectManagerMenu)
private
FAction: TDelphinusDPMNodeMenuAction;
FCaption: string;
FEnabled: Boolean;
FHelpContext: Integer;
FName: string;
FParent: string;
FPosition: Integer;
FVerb: string;
public
constructor Create(AAction: TDelphinusDPMNodeMenuAction);
function GetCaption: string;
function GetChecked: Boolean;
function GetEnabled: Boolean;
function GetHelpContext: Integer;
function GetName: string;
function GetParent: string;
function GetPosition: Integer;
function GetVerb: string;
procedure SetCaption(const Value: string);
procedure SetChecked(Value: Boolean);
procedure SetEnabled(Value: Boolean);
procedure SetHelpContext(Value: Integer);
procedure SetName(const Value: string);
procedure SetParent(const Value: string);
procedure SetPosition(Value: Integer);
procedure SetVerb(const Value: string);
function GetIsMultiSelectable: Boolean;
procedure SetIsMultiSelectable(Value: Boolean);
procedure Execute(const MenuContextList: IInterfaceList); overload;
function PreExecute(const MenuContextList: IInterfaceList): Boolean;
function PostExecute(const MenuContextList: IInterfaceList): Boolean;
end;
TDelphinusDPMNodeMenuNotifier = class(TNotifierObject, IOTAProjectMenuItemCreatorNotifier)
public
procedure AddMenu(const Project: IOTAProject; const IdentList: TStrings;
const ProjectManagerMenuList: IInterfaceList; IsMultiSelect: Boolean);
end;
var
MenuNotifierIndex: Integer = -1;
SelectionNotifierIndex: Integer = -1;
StorageNotifierIndex: Integer = -1;
SelectionWatcher: TDelphinusDPMNodeSelectionWatcher;
SelectionWatcherIntf: IOTASelectionNotifier;
constructor TDelphinusDPMNodeMenuItem.Create(AAction: TDelphinusDPMNodeMenuAction);
begin
inherited Create;
FAction := AAction;
FEnabled := True;
case AAction of
maAdd:
begin
FCaption := 'Add Delphinus/DPM-style Nodes (demo)';
FName := 'DelphinusDPMStyleNodeAddItem';
FPosition := pmmpAddNew + 20;
FVerb := 'DelphinusDPMStyleNode.Add';
end;
maRemove:
begin
FCaption := 'Remove Delphinus/DPM-style Nodes (demo)';
FName := 'DelphinusDPMStyleNodeRemoveItem';
FPosition := pmmpAddNew + 21;
FVerb := 'DelphinusDPMStyleNode.Remove';
end;
end;
end;
function TDelphinusDPMNodeMenuItem.GetCaption: string;
begin
Result := FCaption;
end;
function TDelphinusDPMNodeMenuItem.GetChecked: Boolean;
begin
Result := False;
end;
function TDelphinusDPMNodeMenuItem.GetEnabled: Boolean;
begin
Result := FEnabled;
end;
function TDelphinusDPMNodeMenuItem.GetHelpContext: Integer;
begin
Result := FHelpContext;
end;
function TDelphinusDPMNodeMenuItem.GetName: string;
begin
Result := FName;
end;
function TDelphinusDPMNodeMenuItem.GetParent: string;
begin
Result := FParent;
end;
function TDelphinusDPMNodeMenuItem.GetPosition: Integer;
begin
Result := FPosition;
end;
function TDelphinusDPMNodeMenuItem.GetVerb: string;
begin
Result := FVerb;
end;
procedure TDelphinusDPMNodeMenuItem.SetCaption(const Value: string);
begin
FCaption := Value;
end;
procedure TDelphinusDPMNodeMenuItem.SetChecked(Value: Boolean);
begin
end;
procedure TDelphinusDPMNodeMenuItem.SetEnabled(Value: Boolean);
begin
FEnabled := Value;
end;
procedure TDelphinusDPMNodeMenuItem.SetHelpContext(Value: Integer);
begin
FHelpContext := Value;
end;
procedure TDelphinusDPMNodeMenuItem.SetName(const Value: string);
begin
FName := Value;
end;
procedure TDelphinusDPMNodeMenuItem.SetParent(const Value: string);
begin
FParent := Value;
end;
procedure TDelphinusDPMNodeMenuItem.SetPosition(Value: Integer);
begin
FPosition := Value;
end;
procedure TDelphinusDPMNodeMenuItem.SetVerb(const Value: string);
begin
FVerb := Value;
end;
function TDelphinusDPMNodeMenuItem.GetIsMultiSelectable: Boolean;
begin
Result := False;
end;
procedure TDelphinusDPMNodeMenuItem.SetIsMultiSelectable(Value: Boolean);
begin
end;
procedure TDelphinusDPMNodeMenuItem.Execute(const MenuContextList: IInterfaceList);
var
Context: IOTAProjectMenuContext;
begin
if (MenuContextList.Count = 0) or not Supports(MenuContextList[0], IOTAProjectMenuContext, Context) or
(Context.Project = nil) then
Exit;
try
case FAction of
maAdd:
DelphinusDPMNodes.AddNodesByUser(Context.Project);
maRemove:
DelphinusDPMNodes.RemoveNodesByUser(Context.Project);
end;
except
on E: Exception do
Log('failed: ' + E.Message);
end;
end;
function TDelphinusDPMNodeMenuItem.PreExecute(const MenuContextList: IInterfaceList): Boolean;
begin
Result := True;
end;
function TDelphinusDPMNodeMenuItem.PostExecute(const MenuContextList: IInterfaceList): Boolean;
begin
Result := True;
end;
procedure TDelphinusDPMNodeMenuNotifier.AddMenu(const Project: IOTAProject; const IdentList: TStrings;
const ProjectManagerMenuList: IInterfaceList; IsMultiSelect: Boolean);
begin
if IsMultiSelect or (Project = nil) or (IdentList.IndexOf(sProjectContainer) < 0) then
Exit;
if DelphinusDPMNodes.HasNodes(Project.FileName) then
ProjectManagerMenuList.Add(TDelphinusDPMNodeMenuItem.Create(maRemove) as IOTAProjectManagerMenu)
else
ProjectManagerMenuList.Add(TDelphinusDPMNodeMenuItem.Create(maAdd) as IOTAProjectManagerMenu);
end;
procedure Register;
begin
MenuNotifierIndex := (BorlandIDEServices as IOTAProjectManager).AddMenuItemCreatorNotifier(
TDelphinusDPMNodeMenuNotifier.Create);
StorageNotifierIndex := (BorlandIDEServices as IOTAProjectFileStorage).AddNotifier(TDelphinusDPMNodeStorage.Create);
SelectionWatcher := TDelphinusDPMNodeSelectionWatcher.Create;
SelectionWatcherIntf := SelectionWatcher;
SelectionNotifierIndex := (BorlandIDEServices as IOTAPropInspServices).AddNotifier(SelectionWatcherIntf);
DelphinusDPMNodes.LoadOpenProjects;
end;
procedure Unregister;
var
PropInsp: IOTAPropInspServices;
Storage: IOTAProjectFileStorage;
ProjectManager: IOTAProjectManager;
begin
if (SelectionNotifierIndex >= 0) and Supports(BorlandIDEServices, IOTAPropInspServices, PropInsp) then
PropInsp.RemoveNotifier(SelectionNotifierIndex);
if SelectionWatcher <> nil then
TThread.RemoveQueuedEvents(SelectionWatcher.ShowSelectedNode);
SelectionWatcherIntf := nil;
SelectionWatcher := nil;
if (StorageNotifierIndex >= 0) and Supports(BorlandIDEServices, IOTAProjectFileStorage, Storage) then
Storage.RemoveNotifier(StorageNotifierIndex);
if (MenuNotifierIndex >= 0) and Supports(BorlandIDEServices, IOTAProjectManager, ProjectManager) then
ProjectManager.RemoveMenuItemCreatorNotifier(MenuNotifierIndex);
FreeDelphinusDPMNodes;
end;
initialization
finalization
Unregister;
end.unit DelphinusDPMStyleNode.Tree;
interface
uses
System.Classes, System.SysUtils, System.Rtti, System.TypInfo;
type
TTriState = (tsNo, tsYes, tsUnknown);
IGraphLocation = interface
['{2EEFC3B0-01CF-11D1-9ADA-0000C0091B45}']
function GetParent: IGraphLocation;
function HasChildren: TTriState;
function GetChildren: IInterfaceList;
end;
EDelphinusDPMStyleNode = class(Exception);
TProjectTreeAccess = class
private
FContext: TRttiContext;
FTree: TComponent;
function FindTreeMethod(const Name: string; NodeParam: Boolean): TRttiMethod;
function CallTree(const Name: string; Node: Pointer; NodeParam: Boolean): TValue;
function FieldInterface(Obj: TObject; const Name: string): IInterface;
public
constructor Create;
function FindProjectNode(const ProjectFileName: string; out Location: IGraphLocation): Pointer;
function CreateCategory(const Parent: IGraphLocation; const DisplayName, Ident: string): IGraphLocation;
procedure ResetNode(Node: Pointer);
end;
procedure Log(const Text: string);
implementation
uses
Vcl.Forms, ToolsAPI;
const
cProjectManagerFormClass = 'TProjectManagerForm';
cProjectTreeName = 'ProjectTree2';
cProjectContainerClass = 'TStdProjectContainer';
cCategoryContainerType = 'Containers.TStdContainerCategory';
type
PNodeDataPrefix = ^TNodeDataPrefix;
TNodeDataPrefix = record
Location: IInterface;
GraphData: IInterface;
end;
procedure Log(const Text: string);
var
Messages: IOTAMessageServices;
begin
if Supports(BorlandIDEServices, IOTAMessageServices, Messages) then
Messages.AddTitleMessage('DelphinusDPMStyleNode: ' + Text);
end;
function InheritsFromName(AClass: TClass; const Name: string): Boolean;
begin
while AClass <> nil do
begin
if AClass.ClassNameIs(Name) then
Exit(True);
AClass := AClass.ClassParent;
end;
Result := False;
end;
function PointerOf(const Value: TValue): Pointer;
begin
if Value.IsEmpty then
Result := nil
else
Result := PPointer(Value.GetReferenceToRawData)^;
end;
constructor TProjectTreeAccess.Create;
var
I: Integer;
begin
inherited Create;
FContext := TRttiContext.Create;
for I := 0 to Screen.FormCount - 1 do
if Screen.Forms[I].ClassNameIs(cProjectManagerFormClass) then
begin
FTree := Screen.Forms[I].FindComponent(cProjectTreeName);
Break;
end;
if FTree = nil then
raise EDelphinusDPMStyleNode.Create('Project Manager tree not found');
end;
function TProjectTreeAccess.FindTreeMethod(const Name: string; NodeParam: Boolean): TRttiMethod;
var
Method: TRttiMethod;
Params: TArray<TRttiParameter>;
begin
for Method in FContext.GetType(FTree.ClassType).GetMethods(Name) do
begin
Params := Method.GetParameters;
if NodeParam then
begin
if (Length(Params) = 1) and (Params[0].ParamType <> nil) and (Params[0].ParamType.TypeKind = tkPointer) then
Exit(Method);
end
else if Length(Params) = 0 then
Exit(Method);
end;
raise EDelphinusDPMStyleNode.CreateFmt('Project tree method %s not found through RTTI', [Name]);
end;
function TProjectTreeAccess.CallTree(const Name: string; Node: Pointer; NodeParam: Boolean): TValue;
var
Method: TRttiMethod;
Arg: TValue;
begin
Method := FindTreeMethod(Name, NodeParam);
if NodeParam then
begin
TValue.Make(@Node, Method.GetParameters[0].ParamType.Handle, Arg);
Result := Method.Invoke(FTree, [Arg]);
end
else
Result := Method.Invoke(FTree, []);
end;
function TProjectTreeAccess.FieldInterface(Obj: TObject; const Name: string): IInterface;
var
Field: TRttiField;
begin
Field := FContext.GetType(Obj.ClassType).GetField(Name);
if Field = nil then
raise EDelphinusDPMStyleNode.CreateFmt('Field %s.%s not found through RTTI', [Obj.ClassName, Name]);
Result := Field.GetValue(Obj).AsInterface;
end;
function TProjectTreeAccess.FindProjectNode(const ProjectFileName: string; out Location: IGraphLocation): Pointer;
var
Data: PNodeDataPrefix;
Obj: TObject;
Project: IOTAProject;
begin
Location := nil;
Result := PointerOf(CallTree('GetFirst', nil, False));
while Result <> nil do
begin
Data := PointerOf(CallTree('GetNodeData', Result, True));
if (Data <> nil) and (Data.Location <> nil) then
begin
Obj := Data.Location as TObject;
if InheritsFromName(Obj.ClassType, cProjectContainerClass) and
Supports(FieldInterface(Obj, 'FProject'), IOTAProject, Project) and
SameFileName(Project.FileName, ProjectFileName) and
Supports(Data.Location, IGraphLocation, Location) then
Exit;
end;
Result := PointerOf(CallTree('GetNext', Result, True));
end;
end;
function TProjectTreeAccess.CreateCategory(const Parent: IGraphLocation; const DisplayName, Ident: string): IGraphLocation;
var
CategoryType: TRttiInstanceType;
Method, Constructor_: TRttiMethod;
Params: TArray<TRttiParameter>;
ParentObj, Category: TObject;
Model, Project, ParentIntf: IInterface;
Args: TArray<TValue>;
AutoExpand: TRttiProperty;
begin
CategoryType := FContext.FindType(cCategoryContainerType) as TRttiInstanceType;
if CategoryType = nil then
raise EDelphinusDPMStyleNode.Create(cCategoryContainerType + ' not found through RTTI');
Constructor_ := nil;
for Method in CategoryType.GetDeclaredMethods do
if Method.IsConstructor and (Length(Method.GetParameters) = 6) then
begin
Constructor_ := Method;
Break;
end;
if Constructor_ = nil then
raise EDelphinusDPMStyleNode.Create(cCategoryContainerType + ' constructor not found through RTTI');
ParentObj := Parent as TObject;
Model := FieldInterface(ParentObj, 'FModelContainer');
Project := FieldInterface(ParentObj, 'FProject');
ParentIntf := Parent;
Params := Constructor_.GetParameters;
SetLength(Args, 6);
TValue.Make(@Model, Params[0].ParamType.Handle, Args[0]);
TValue.Make(@Project, Params[1].ParamType.Handle, Args[1]);
TValue.Make(@ParentIntf, Params[2].ParamType.Handle, Args[2]);
Args[3] := DisplayName;
Args[4] := Ident;
Args[5] := '';
Category := Constructor_.Invoke(CategoryType.MetaclassType, Args).AsObject;
if not Supports(Category, IGraphLocation, Result) then
raise EDelphinusDPMStyleNode.Create('New container does not support IGraphLocation');
AutoExpand := FContext.GetType(Category.ClassType).GetProperty('AutoExpand');
if AutoExpand <> nil then
AutoExpand.SetValue(Category, True);
end;
procedure TProjectTreeAccess.ResetNode(Node: Pointer);
begin
CallTree('ResetNode', Node, True);
end;
end.- Build and install the package
2. Open/Create a VCL project
3. Right-Click on the project menu and add the custom node:
4. If all went well, you should see something like this:
5. When you click on the custom node Alpha or Beta, you should see this in the Object Inspector:
6. When you save, the data is persisted; and when the project is closed and reopened, the data is reloaded.
```xml
```