For example, suppose you want to assign a TStringList to each node of a graph, and you want also to save/load or copy/paste these strings as well as the objects.
The following piece of code illustrates how the above example can be implemented:
- Code: Select all
procedure TForm1.SimpleGraph1ObjectInitInstance(Graph: TSimpleGraph;
GraphObject: TGraphObject);
begin
// If graph object being created is a node...
if GraphObject.IsNode then
begin
// We create a new StringList and assign it to the Data property
// of the graph object.
GraphObject.Data := TStringList.Create;
// We also specify that this graph object has some custom data to
// stream out.
GraphObject.HasCustomData := True;
end;
end;
procedure TForm1.SimpleGraph1ObjectRead(Graph: TSimpleGraph;
GraphObject: TGraphObject; Stream: TStream);
begin
// If Data property of graph object is nill...
if not Assigned(GraphObject.Data) then
// The graph object is just created, and is loading its property
// settings from the stream. Therefore we should create an
// instance of StringList for it.
GraphObject.Data := TStringList.Create;
// We load StringList from the stream provided as parameter
// of the event handler.
TStringList(GraphObject.Data).LoadFromStream(Stream);
end;
procedure TForm1.SimpleGraph1ObjectWrite(Graph: TSimpleGraph;
GraphObject: TGraphObject; Stream: TStream);
begin
// We save StringList in to the stream provided as parameter
// of the event handler.
TStringList(GraphObject.Data).SaveToStream(Stream);
end;
procedure TForm1.SimpleGraph1ObjectRemove(Graph: TSimpleGraph;
GraphObject: TGraphObject);
begin
// if Data property is not nil...
if Assigned(GraphObject.Data) then
begin
// We release the StringList instance
TStringList(GraphObject.Data).Free;
// And, clean up the graph object
GraphObject.Data := nil;
end;
end;
In the above code, assumed there is no other application to share (via file or clipboard) graph objects with it. A practical code should identify its own data and ignore not supported ones. This can be done by writing a unique signature on the stream, and checking the signature before loading data.