Update VirtualStringTree header after a component property has changed - delphi

I would like to create a component named TMyComp.
This component has associated following properties:
property VirtualStringTree: TVirtualStringTree and
property Columns: TMyCompColumns as a collection of items.
The columns from my component are the same with the header columns from associated VirtualStringTree.
What I would like to do, is to redraw at design-time the header text from VirtualStringTree when the caption is updated.
My problem is that I don't know how to trig the procedure RedrawVirtualStringTreeHeader because it's not known by class TMyCompColumns or even TMyCompColumnsItem.
TMyCompColumnsItem = class(TCollectionItem)
private
FCaption: String;
function GetPosition: Integer;
protected
function GetDisplayName: String; override;
procedure SetIndex(Value: Integer);
public
constructor Create(Collection: TCollection); override;
procedure Assign(Source: TPersistent); override;
published
property Caption: String read FCaption write FCaption;
end;
TMyCompColumns= class(TCollection)
private
FOwner: TComponent;
protected
function GetOwner: TPersistent; override;
function GetItem(Index: Integer): TMyCompColumnsItem;
procedure SetItem(Index: Integer; Value: TMyCompColumnsItem);
procedure Update(Item: TMyCompColumnsItem);
public
constructor Create(AOwner: TComponent);
function Add: TMyCompColumnsItem;
property Items[Index: Integer]: TMyCompColumnsItem read GetItem write SetItem;
end;
TMyComp = class(TComponent)
private
FColumns: TMyCompColumns;
FVirtualStringTree: TVirtualStringTree;
procedure SetMyCompColumns(const Value: TMyCompColumns);
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Columns: TMyCompColumns read FColumns write SetMyCompColumns;
property VirtualStringTree: TVirtualStringTree read FVirtualStringTree write FVirtualStringTree;
end;
...
function TMyCompColumnsItem.GetDisplayName: String;
begin
Result:= FCaption;
RedrawVirtualStringTreeHeader; //<--- procedure not recognized!!!
end;
...
procedure TMyCompColumns.Update(Item: TMyCompColumnsItem);
begin
inherited;
//RedrawVirtualStringTreeHeader; ???or here
end;
procedure TMyComp.RedrawVirtualStringTreeHeader;
var
i: Integer;
begin
if Assigned(FVirtualStringTree) then
begin
FVirtualStringTree.Header.Options:= FVirtualStringTree.Header.Options + [hoVisible];
FVirtualStringTree.Header.Columns.Clear;
if FColumns.Count > 0 then
for i := 0 to FColumns.Count-1 do
with FVirtualStringTree.Header.Columns.Add do
begin
Text:= FColumns.Items[i].Caption;
//...
end;
end;
end;

After some searching, this is the answer:
The trigger of RedrawVirtualStringTreeHeader has been done through the FOwner inside of TMyCompColumns class.
procedure TMyCompColumns.Update(Item: TCollectionItem);
begin
inherited;
(FOwner as TMyComp).RedrawVirtualStringTreeHeader;
end;
I updated property Caption: String read FCaption write FCaption with write SetCaption and I add the procedure
procedure TMyCompColumnsItem.SetCaption(const Value: String);
begin
FCaption:= Value;
Changed(False); //---> this will trigger TMyCompColumns.Update
end;
In fact the secret was Changed(False); that trig the Update
Thanks also to open source of TVirtualStringTree component.

Related

Delphi component design - get property from component from subproperty

I'm making a Delphi vcl component, the component class has a 'images' property which lets me select a TImagelist.
The component class also has a subproperty 'buttons' which itself has a imageindex property.
I have written a component editor for the imageindex property so that i can select a image on the buttons from the imagelist; i have done this in other components before but the problem i'm facing now is that i need to get the images property of the base class from the event in the 'buttons' subclass event.
So, the base class of the component has these properties:
property Buttons: TFlexButtons read FButtons write FButtons;
property Images: TCustomImageList read FImages write SetImages;
The buttons class has this property:
property ImageIndex: TImageIndex read FImageIndex write SetImageIndex default -1;
I register a property editor in a seperate unit for the ImageIndex property, in order to pick a image but in this event i need to get the imagelist from the baseclass of the component, how do i get this property from this sub property?
function TImageIndexProperty.GetImageListAt(Index: Integer): TCustomImageList;
var APersistent: TPersistent;
begin
APersistent := GetComponent(Index);
if APersistent is TFlexButton then
Result := ??????????.Images //how do i refer to the images property of the component here?
else
Result := nil;
end;
All classes:
TFlexButton = class(TCollectionItem)
private
FWidth: Word;
FCaption: string;
FHeight: Word;
FImageIndex: TImageIndex;
procedure SetCaption(const Value: string);
procedure SetHeight(const Value: Word);
procedure SetWidth(const Value: Word);
procedure SetImageIndex(const Value: TImageIndex);
public
constructor Create(AOwner: TComponent);
destructor Destroy; override;
published
property Caption: string read FCaption write SetCaption;
property Height: Word read FHeight write SetHeight;
property Width: Word read FWidth write SetWidth;
property ImageIndex: TImageIndex read FImageIndex write SetImageIndex default -1;
end;
TFlexButtons = class(TCollection)
private
function GetItem(Index: Integer): TFlexButton;
public
function Add: TFlexButton;
property Item[index: Integer]: TFlexButton read GetItem;
end;
TFlexButtonGroupBox = class(TcxGroupBox)
private
FDataLink: TFieldDataLink;
FAbout: string;
FAlignment: TAlignment;
FEnabled: Boolean;
FButtons: TFlexButtons;
FImages: TCustomImageList;
FSql: TStrings;
FAutosize: Boolean;
procedure SetAlignment(const Value: TAlignment);
function GetDataField: string;
function GetDataSource: TdataSource;
procedure SetDataField(const Value: string);
procedure SetDataSource(const Value: TdataSource);
procedure DataChange(Sender: TObject);
procedure SetEnabled(const Value: Boolean);
procedure SetImages(const Value: TCustomImageList);
procedure SetSql(const Value: TStrings);
procedure SetAutosize(const Value: Boolean);
protected
public
procedure Loaded; override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property DataField: string read GetDataField write SetDataField;
property DataSource: TdataSource read GetDataSource write SetDataSource;
property Enabled: Boolean read FEnabled write SetEnabled;
property Autosize: Boolean read FAutosize write SetAutosize;
property About: string read FAbout write FAbout;
property Buttons: TFlexButtons read FButtons write FButtons;
property Images: TCustomImageList read FImages write SetImages;
property Alignment: TAlignment read FAlignment write SetAlignment;
property Sql: TStrings read FSql write SetSql;
end;
When exposing a collection at design time, use TOwnedCollection instead of TCollection directly. This facilitates DFM streaming without having to write extra code to enable it.
TCollectionItem has a Collection property, which in turn has an Owner method that TOwnedCollection implements. This way, you can get from a button to its owning group box in code.
Try this:
TFlexButton = class(TCollectionItem)
private
...
public
constructor Create(ACollection: TCollection); override;
end;
TFlexButtonGroupBox = class;
TFlexButtons = class(TOwnedCollection)
private
...
public
constructor Create(AOwner: TFlexButtonGroupBox); reintroduce;
...
end;
TFlexButtonGroupBox = class(TcxGroupBox)
private
...
procedure SetButtons(AValue: TFlexButtons;
public
constructor Create(AOwner: TComponent); override;
...
published
...
property Buttons: TFlexButtons read FButtons write SetButtons;
...
end;
constructor TFlexButton.Create(ACollection: TCollection);
begin
inherited;
...
end;
constructor TFlexButtons.Create(AOwner: TFlexButtonGroupBox);
begin
inherited Create(AOwner, TFlexButton);
...
end;
constructor TFlexButtonGroupBox.Create(AOwner: TComponent);
begin
inherited;
FButtons := TFlexButtons.Create(Self);
...
end;
procedure TFlexButtonGroupBox.SetButtons(AValue: TFlexButtons;
begin
FButtons.Assign(AValue);
end;
function TImageIndexProperty.GetImageListAt(Index: Integer): TCustomImageList;
begin
Result := ((GetComponent(Index) as TFlexButton).Collection.Owner as TFlexButtonGroupBox).Images;
end;

cxFilterControl getting Filtertext inside in a component

I have cxFilterControl what I felt with TCollectionItems (TableCollection - Field(Items). For example... Persons Table -> Person_ID, Age, Name etc...
What I need to do...
To save TcxFilterControl. FilterText to string to work with them after
I can get FilterTextSample: = cxFilterControl1.FilterText easily outside the component but generally the condition is setted by the user. I need the FilterText inside a Component
What I did...
TMySampleComp = class(TComponent, IcxFilterControl)
private
FCriteria: TcxFilterControlCriteria;
function GetPropertiesClassFromFieldType(AFieldType: TFieldType): TcxCustomEditPropertiesClass;
private
function GetCaption(Index: Integer): string;
function GetCount: Integer;
function GetCriteria: TcxFilterCriteria;
function GetItemLink(Index: Integer): TObject;
function GetItemLinkID(Index: Integer): Integer;
function GetItemLinkName(Index: Integer): string;
function GetProperties(Index: Integer): TcxCustomEditProperties;
function GetValueType(Index: Integer): TcxValueTypeClass;
public
property Captions[Index: Integer]: string read GetCaption;
property Criteria: TcxFilterCriteria read GetCriteria;
property ItemLinkNames[Index: Integer]: string read GetItemLinkName;
property ItemLinkIDs[Index: Integer]: Integer read GetItemLinkID;
property ItemLinks[Index: Integer]: TObject read GetItemLink;
property Properties[Index: Integer]: TcxCustomEditProperties read GetProperties;
property ValueTypes[Index: Integer]: TcxValueTypeClass read GetValueType;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
end;
TField = class(TCollectionItem)
private
FEditProperties: TcxCustomEditProperties;
procedure CreateProperties;
procedure DestroyProperties;
procedure RecreateProperties;
procedure SetFieldType(const Value: TFieldType);
protected
procedure SetIndex(Value: Integer); override;
function GetDisplayName: string; override;
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
property EditProperties: TcxCustomEditProperties read FEditProperties;
published
end;
Everything is ok, all the Items can be reach through the TcxFilterControl -> (FilterText). Anyone can give a little advice how canI get these text (for example ... (PERSON_ID >= 12) inside a component? I need a function for it.
What I start:
procedure TdafUniqueSQLGenerate.GetFilterChangeValue(Sender:
TcxFilterCriteria;
const AFilterText: string);
var
vFilterText: string;
I: integer;
J: integer;
begin
if Sender is TcxFilterCriteria then
begin
vFilterText := Sender.FilterText;
vFilterText := StringReplace(vFilterText, '<> NULL', 'is not null', [rfReplaceAll]);
vFilterText := StringReplace(vFilterText, '= NULL', 'is null', [rfReplaceAll]);
end
else vFilterText := '';
if not vFilterText.IsEmpty then
begin
for I := 0 to Tables.Items[i].Fields.Count -1 do
Thanks for the help and sorry for my Eng!

Creating a component with named sub-components?

I need to know the basics behind making a component produce and manage sub-components. I originally tried this by creating a TCollection, and tried to put a name on each TCollectionItem. But I learned it's not that easy as I had hoped.
So now I am going to start this project from scratch again, and I'd like to get it right this time. These sub-components are not visual components, and should not have any display or window, just based off of TComponent. The main component holding these sub-components will also be based off of TComponent. So nothing here is visual at all, and I don't want a little icon on my form (in design time) for each of these sub-components.
I would like to be able to maintain and manage these sub-components in a collection-like fashion. The important thing is that these sub-components should be created, named and added to the form source, just like menu items are for example. This is the whole point of the idea in the first place, if they cannot be named, then this whole idea is kaput.
Oh, another important thing: the main component being the parent of all the sub-components needs to be able to save these sub-components to the DFM file.
EXAMPLE:
Instead of accessing one of these sub items like:
MyForm.MyItems[1].DoSomething();
I would instead like to do something like:
MyForm.MyItem2.DoSomething();
So I do not have to rely on knowing the ID of each sub item.
EDIT:
I felt it a little necessary to include my original code so it can be seen how the original collection works. Here's just the server side collection and collection item stripped from the full unit:
// Command Collections
// Goal: Allow entering pre-set commands with unique Name and ID
// Each command has its own event which is triggered when command is received
// TODO: Name each collection item as a named component in owner form
//Determines how commands are displayed in collection editor in design-time
TJDCmdDisplay = (cdName, cdID, cdCaption, cdIDName, cdIDCaption);
TJDScktSvrCmdEvent = procedure(Sender: TObject; Socket: TJDServerClientSocket;
const Data: TStrings) of object;
TSvrCommands = class(TCollection)
private
fOwner: TPersistent;
fOnUnknownCommand: TJDScktSvrCmdEvent;
fDisplay: TJDCmdDisplay;
function GetItem(Index: Integer): TSvrCommand;
procedure SetItem(Index: Integer; Value: TSvrCommand);
procedure SetDisplay(const Value: TJDCmdDisplay);
protected
function GetOwner: TPersistent; override;
public
constructor Create(AOwner: TPersistent);
destructor Destroy;
procedure DoCommand(const Socket: TJDServerClientSocket;
const Cmd: Integer; const Data: TStrings);
function Add: TSvrCommand;
property Items[Index: Integer]: TSvrCommand read GetItem write SetItem;
published
property Display: TJDCmdDisplay read fDisplay write SetDisplay;
property OnUnknownCommand: TJDScktSvrCmdEvent
read fOnUnknownCommand write fOnUnknownCommand;
end;
TSvrCommand = class(TCollectionItem)
private
fID: Integer;
fOnCommand: TJDScktSvrCmdEvent;
fName: String;
fParamCount: Integer;
fCollection: TSvrCommands;
fCaption: String;
procedure SetID(Value: Integer);
procedure SetName(Value: String);
procedure SetCaption(const Value: String);
protected
function GetDisplayName: String; override;
public
procedure Assign(Source: TPersistent); override;
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
published
property ID: Integer read fID write SetID;
property Name: String read fName write SetName;
property Caption: String read fCaption write SetCaption;
property ParamCount: Integer read fParamCount write fParamCount;
property OnCommand: TJDScktSvrCmdEvent read fOnCommand write fOnCommand;
end;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
{ TSvrCommands }
function TSvrCommands.Add: TSvrCommand;
begin
Result:= inherited Add as TSvrCommand;
end;
constructor TSvrCommands.Create(AOwner: TPersistent);
begin
inherited Create(TSvrCommand);
Self.fOwner:= AOwner;
end;
destructor TSvrCommands.Destroy;
begin
inherited Destroy;
end;
procedure TSvrCommands.DoCommand(const Socket: TJDServerClientSocket;
const Cmd: Integer; const Data: TStrings);
var
X: Integer;
C: TSvrCommand;
F: Bool;
begin
F:= False;
for X:= 0 to Self.Count - 1 do begin
C:= GetItem(X);
if C.ID = Cmd then begin
F:= True;
try
if assigned(C.fOnCommand) then
C.fOnCommand(Self, Socket, Data);
except
on e: exception do begin
raise Exception.Create(
'Failed to execute command '+IntToStr(Cmd)+': '+#10+e.Message);
end;
end;
Break;
end;
end;
if not F then begin
//Command not found
end;
end;
function TSvrCommands.GetItem(Index: Integer): TSvrCommand;
begin
Result:= TSvrCommand(inherited GetItem(Index));
end;
function TSvrCommands.GetOwner: TPersistent;
begin
Result:= fOwner;
end;
procedure TSvrCommands.SetDisplay(const Value: TJDCmdDisplay);
begin
fDisplay := Value;
end;
procedure TSvrCommands.SetItem(Index: Integer; Value: TSvrCommand);
begin
inherited SetItem(Index, Value);
end;
{ TSvrCommand }
procedure TSvrCommand.Assign(Source: TPersistent);
begin
inherited;
end;
constructor TSvrCommand.Create(Collection: TCollection);
begin
inherited Create(Collection);
fCollection:= TSvrCommands(Collection);
end;
destructor TSvrCommand.Destroy;
begin
inherited Destroy;
end;
function TSvrCommand.GetDisplayName: String;
begin
case Self.fCollection.fDisplay of
cdName: begin
Result:= fName;
end;
cdID: begin
Result:= '['+IntToStr(fID)+']';
end;
cdCaption: begin
Result:= fCaption;
end;
cdIDName: begin
Result:= '['+IntToStr(fID)+'] '+fName;
end;
cdIDCaption: begin
Result:= '['+IntToStr(fID)+'] '+fCaption;
end;
end;
end;
procedure TSvrCommand.SetCaption(const Value: String);
begin
fCaption := Value;
end;
procedure TSvrCommand.SetID(Value: Integer);
begin
fID:= Value;
end;
procedure TSvrCommand.SetName(Value: String);
begin
fName:= Value;
end;
This Thread helped me creating something as we discussed yesterday. I took the package posted there and modified it a bit. Here is the source:
TestComponents.pas
unit TestComponents;
interface
uses
Classes;
type
TParentComponent = class;
TChildComponent = class(TComponent)
private
FParent: TParentComponent;
procedure SetParent(const Value: TParentComponent);
protected
procedure SetParentComponent(AParent: TComponent); override;
public
destructor Destroy; override;
function GetParentComponent: TComponent; override;
function HasParent: Boolean; override;
property Parent: TParentComponent read FParent write SetParent;
end;
TParentComponent = class(TComponent)
private
FChilds: TList;
protected
procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Childs: TList read FChilds;
end;
implementation
{ TChildComponent }
destructor TChildComponent.Destroy;
begin
Parent := nil;
inherited;
end;
function TChildComponent.GetParentComponent: TComponent;
begin
Result := FParent;
end;
function TChildComponent.HasParent: Boolean;
begin
Result := Assigned(FParent);
end;
procedure TChildComponent.SetParent(const Value: TParentComponent);
begin
if FParent <> Value then
begin
if Assigned(FParent) then
FParent.FChilds.Remove(Self);
FParent := Value;
if Assigned(FParent) then
FParent.FChilds.Add(Self);
end;
end;
procedure TChildComponent.SetParentComponent(AParent: TComponent);
begin
if AParent is TParentComponent then
SetParent(AParent as TParentComponent);
end;
{ TParentComponent }
constructor TParentComponent.Create(AOwner: TComponent);
begin
inherited;
FChilds := TList.Create;
end;
destructor TParentComponent.Destroy;
var
I: Integer;
begin
for I := 0 to FChilds.Count - 1 do
FChilds[0].Free;
FChilds.Free;
inherited;
end;
procedure TParentComponent.GetChildren(Proc: TGetChildProc; Root: TComponent);
var
i: Integer;
begin
for i := 0 to FChilds.Count - 1 do
Proc(TComponent(FChilds[i]));
end;
end.
TestComponentsReg.pas
unit TestComponentsReg;
interface
uses
Classes,
DesignEditors,
DesignIntf,
TestComponents;
type
TParentComponentEditor = class(TComponentEditor)
procedure ExecuteVerb(Index: Integer); override;
function GetVerb(Index: Integer): string; override;
function GetVerbCount: Integer; override;
end;
procedure Register;
implementation
uses
ColnEdit;
type
TChildComponentCollectionItem = class(TCollectionItem)
private
FChildComponent: TChildComponent;
function GetName: string;
procedure SetName(const Value: string);
protected
property ChildComponent: TChildComponent read FChildComponent write FChildComponent;
function GetDisplayName: string; override;
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
published
property Name: string read GetName write SetName;
end;
TChildComponentCollection = class(TOwnedCollection)
private
FDesigner: IDesigner;
public
property Designer: IDesigner read FDesigner write FDesigner;
end;
procedure Register;
begin
RegisterClass(TChildComponent);
RegisterNoIcon([TChildComponent]);
RegisterComponents('Test', [TParentComponent]);
RegisterComponentEditor(TParentComponent, TParentComponentEditor);
end;
{ TParentComponentEditor }
procedure TParentComponentEditor.ExecuteVerb(Index: Integer);
var
LCollection: TChildComponentCollection;
i: Integer;
begin
LCollection := TChildComponentCollection.Create(Component, TChildComponentCollectionItem);
LCollection.Designer := Designer;
for i := 0 to TParentComponent(Component).Childs.Count - 1 do
with TChildComponentCollectionItem.Create(nil) do
begin
ChildComponent := TChildComponent(TParentComponent(Component).Childs[i]);
Collection := LCollection;
end;
ShowCollectionEditorClass(Designer, TCollectionEditor, Component, LCollection, 'Childs');
end;
function TParentComponentEditor.GetVerb(Index: Integer): string;
begin
Result := 'Edit Childs...';
end;
function TParentComponentEditor.GetVerbCount: Integer;
begin
Result := 1;
end;
{ TChildComponentCollectionItem }
constructor TChildComponentCollectionItem.Create(Collection: TCollection);
begin
inherited;
if Assigned(Collection) then
begin
FChildComponent := TChildComponent.Create(TComponent(TOwnedCollection(Collection).Owner).Owner);
FChildComponent.Name := TChildComponentCollection(Collection).Designer.UniqueName(TChildComponent.ClassName);
FChildComponent.Parent := TParentComponent(TComponent(TOwnedCollection(Collection).Owner));
end;
end;
destructor TChildComponentCollectionItem.Destroy;
begin
FChildComponent.Free;
inherited;
end;
function TChildComponentCollectionItem.GetDisplayName: string;
begin
Result := FChildComponent.Name;
end;
function TChildComponentCollectionItem.GetName: string;
begin
Result := FChildComponent.Name;
end;
procedure TChildComponentCollectionItem.SetName(const Value: string);
begin
FChildComponent.Name := Value;
end;
end.
The most important thing is the RegisterNoIcon which prevents showing the component on the form when you create it. The overridden methods in TChildComponent are causing them to be nested inside the TParentComponent.
Edit: I added a temporary collection to edit the items in the built-in TCollectionEditor instead of having to write an own one. The only disadvantage is that the TChildComponentCollectionItem has to publish every property that TChildComponent has published to be able to edit them inside the OI.
Use the TComponent.SetSubComponent routine:
type
TComponent1 = class(TComponent)
private
FSubComponent: TComponent;
procedure SetSubComponent(Value: TComponent);
public
constructor Create(AOwner: TComponent); override;
published
property SubComponent: TComponent read FSubComponent write SetSubComponent;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TComponent1]);
end;
{ TComponent1 }
constructor TComponent1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FSubComponent := TComponent.Create(Self); // Nót AOwner as owner here !!
FSubComponent.Name := 'MyName';
FSubComponent.SetSubComponent(True);
end;
procedure TComponent1.SetSubComponent(Value: TComponent);
begin
FSubComponent.Assign(Value);
end;
I now understand this sub component would be part of a collection item. In that case: no difference, use this method.
Implement TCollectionItem.GetDisplayName to "name" the collection items.
And concerning the collection: when this is a published property, the collection will automatically be named as the property name.
Be careful to implement GetOwner when you create properties of TPersistent.

TCollectionItem not initializing default property values

I've been fighting this crazy problem for hours and have gotten nowhere. I have this problem in two completely different projects using a TCollection. When a new collection item is added, I need to initialize the values of that item. However, they're not defaulting at all. I'm even setting them in two completely different places, in the item's constructor, and in the collection's add function - neither of them are working. I can set the values once the items are there, but I need to set default values. I've done collections in the past and never had this problem, I must be missing something here...
unit JDGrids;
interface
uses
Classes, Windows, SysUtils, Grids, StrUtils;
type
TJDGridCol = class;
TJDGridCols = class;
TJDGridCols = class(TCollection)
private
fOnEvent: TNotifyEvent;
private
fOwner: TComponent;
procedure DoEvent;
property OnEvent: TNotifyEvent read fOnEvent write fOnEvent;
protected
function GetItem(Index: Integer): TJDGridCol;
procedure SetItem(Index: Integer; Value: TJDGridCol);
function GetOwner: TPersistent; override;
public
constructor Create(AOwner: TComponent);
destructor Destroy; override;
function Add: TJDGridCol;
procedure Assign(Source: TPersistent); override;
procedure Clear;
procedure Delete(Index: Integer);
property Items[Index: Integer]: TJDGridCol read GetItem write SetItem; default;
end;
TJDGridCol = class(TCollectionItem)
private
fOwner: TComponent;
fWidth: Integer;
fTitle: String;
fCols: TJDGridCols;
fOnEvent: TNotifyEvent;
fVisible: Bool;
procedure SetTitle(const Value: String);
procedure SetWidth(const Value: Integer);
procedure DoEvent;
property OnEvent: TNotifyEvent read fOnEvent write fOnEvent;
procedure SetVisible(const Value: Bool);
protected
function GetDisplayName: String; override;
public
constructor Create(AOwner: TJDGridCols);
destructor Destroy; override;
published
property Title: String read fTitle write SetTitle;
property Width: Integer read fWidth write SetWidth;
property Visible: Bool read fVisible write SetVisible;
end;
implementation
{ TJDGridCols }
constructor TJDGridCols.Create(AOwner: TComponent);
begin
inherited Create(TJDGridCol);
fOwner:= AOwner;
end;
destructor TJDGridCols.Destroy;
begin
inherited Destroy;
end;
function TJDGridCols.Add: TJDGridCol;
begin
Result:= TJDGridCol(inherited Add);
Result.fCols:= Self;
Result.fTitle:= 'Column '+IntToStr(Result.ID);
Result.fWidth:= 30;
Result.fVisible:= True;
DoEvent;
end;
procedure TJDGridCols.Assign(Source: TPersistent);
begin
inherited Assign(Source);
DoEvent;
end;
procedure TJDGridCols.Clear;
begin
inherited Clear;
DoEvent;
end;
procedure TJDGridCols.Delete(Index: Integer);
begin
inherited Delete(Index);
DoEvent;
end;
function TJDGridCols.GetItem(Index: Integer): TJDGridCol;
begin
Result:= TJDGridCol(inherited Items[Index]);
end;
function TJDGridCols.GetOwner: TPersistent;
begin
Result:= fOwner;
end;
procedure TJDGridCols.SetItem(Index: Integer; Value: TJDGridCol);
begin
inherited Items[Index]:= Value;
DoEvent;
end;
procedure TJDGridCols.DoEvent;
begin
if assigned(fOnEvent) then fOnEvent(Self);
end;
{ TJDGridCol }
constructor TJDGridCol.Create(AOwner: TJDGridCols);
begin
inherited Create(AOwner);
fOwner:= AOwner.fOwner;
fCols:= AOwner;
fTitle:= 'Column '+IntToStr(ID);
fWidth:= 30;
fVisible:= True;
end;
destructor TJDGridCol.Destroy;
begin
inherited Destroy;
end;
procedure TJDGridCol.DoEvent;
begin
if assigned(fOnEvent) then fOnEvent(Self);
end;
function TJDGridCol.GetDisplayName: String;
begin
Result:= fTitle;
end;
procedure TJDGridCol.SetTitle(const Value: String);
begin
fTitle:= Value;
DoEvent;
end;
procedure TJDGridCol.SetVisible(const Value: Bool);
begin
fVisible := Value;
DoEvent;
end;
procedure TJDGridCol.SetWidth(const Value: Integer);
begin
fWidth := Value;
DoEvent;
end;
end.
You are not overriding the constructor of TCollection, so TCollection.Add() cannot call your constructor. That is why you needed to have Add() set the default values.
Even then, you are setting default property values during construction, but you are not specifying those same default values in your property declarations. You need to do so for DFM streaming to work correctly.
You should also use TOwnedCollection instead of using TCollection directly. Let TOwnedCollection manage the Owner for you.
Try this:
unit JDGrids;
interface
uses
Classes, Windows, SysUtils, Grids, StrUtils;
type
TJDGridCol = class;
TJDGridCols = class(TOwnedCollection)
private
fOnEvent: TNotifyEvent;
private
procedure DoEvent;
property OnEvent: TNotifyEvent read fOnEvent write fOnEvent;
protected
function GetItem(Index: Integer): TJDGridCol;
procedure SetItem(Index: Integer; Value: TJDGridCol);
public
constructor Create(AOwner: TComponent); reintroduce;
destructor Destroy; override;
function Add: TJDGridCol; reintroduce;
procedure Assign(Source: TPersistent); override;
procedure Clear; reintroduce;
procedure Delete(Index: Integer); reintroduce;
property Items[Index: Integer]: TJDGridCol read GetItem write SetItem; default;
end;
TJDGridCol = class(TCollectionItem)
private
fWidth: Integer;
fTitle: String;
fOnEvent: TNotifyEvent;
fVisible: Bool;
procedure SetTitle(const Value: String);
procedure SetWidth(const Value: Integer);
procedure DoEvent;
procedure SetVisible(const Value: Bool);
property OnEvent: TNotifyEvent read fOnEvent write fOnEvent;
protected
function GetDisplayName: String; override;
function GetCols: TJDGridCols;
function GetOwner: TComponent;
public
constructor Create(AOwner: TCollection); override;
destructor Destroy; override;
published
property Title: String read fTitle write SetTitle;
property Width: Integer read fWidth write SetWidth default 30;
property Visible: Bool read fVisible write SetVisible default True;
end;
implementation
{ TJDGridCols }
constructor TJDGridCols.Create(AOwner: TComponent);
begin
inherited Create(AOwner, TJDGridCol);
end;
destructor TJDGridCols.Destroy;
begin
inherited Destroy;
end;
function TJDGridCols.Add: TJDGridCol;
begin
Result := TJDGridCol(inherited Add);
DoEvent;
end;
procedure TJDGridCols.Assign(Source: TPersistent);
begin
inherited Assign(Source);
DoEvent;
end;
procedure TJDGridCols.Clear;
begin
inherited Clear;
DoEvent;
end;
procedure TJDGridCols.Delete(Index: Integer);
begin
inherited Delete(Index);
DoEvent;
end;
function TJDGridCols.GetItem(Index: Integer): TJDGridCol;
begin
Result:= TJDGridCol(inherited Items[Index]);
end;
procedure TJDGridCols.SetItem(Index: Integer; Value: TJDGridCol);
begin
inherited SetItems(Index, Value);
DoEvent;
end;
procedure TJDGridCols.DoEvent;
begin
if Assigned(fOnEvent) then fOnEvent(Self);
end;
{ TJDGridCol }
constructor TJDGridCol.Create(AOwner: TCollection);
begin
inherited Create(AOwner);
fTitle := 'Column ' + IntToStr(ID);
fWidth := 30;
fVisible := True;
end;
destructor TJDGridCol.Destroy;
begin
inherited Destroy;
end;
procedure TJDGridCol.DoEvent;
begin
if Assigned(fOnEvent) then fOnEvent(Self);
end;
function TJDGridCol.GetDisplayName: String;
begin
Result := fTitle;
end;
function TJDGridCol.GetCols: TJDGridCols;
begin
Result := Collection as TJDGridCols;
end;
function TJDGridCol.GetOwner: TComponent;
begin
Result := GetCols.GetOwner as TComponent;
end;
procedure TJDGridCol.SetTitle(const Value: String);
begin
if fTitle <> Value then
begin
fTitle := Value;
DoEvent;
end;
end;
procedure TJDGridCol.SetVisible(const Value: Bool);
begin
if fVisible <> Value then
begin
fVisible := Value;
DoEvent;
end;
end;
procedure TJDGridCol.SetWidth(const Value: Integer);
begin
if fWidth <> Value then
begin
fWidth := Value;
DoEvent;
end;
end;
end.

Collection editor does not open for a TCollection property in a TPersistent property

I've got my custom collection property which is working great when it is a direct member of my component.
But I want to move the collection property to a TPersistent propery within my component. And now comes the problem, it doesn't work: double clicking on the collection property in the object inspector normally opens the collection editor, but it does not anymore.
Fist of all - what should I pass to the contructor of the TPersistent property?
TMyCollection = class(TCollection)
constructor Create(AOwner: TComponent); // TMyCollection constuctor
...
I can't pass Self, so should I pass my persistent owner?
constructor TMyPersistent.Create(AOwner: TComponent);
begin
inherited Create;
fOwner := AOwner;
fMyCollection := TMyCollection.Create(AOwner); // hmmm... doesn't make sense
end;
I think I'm missing something. If more code is needed just please comment this post.
A TCollection's constructor does not need a TComponent, but a TCollectionItemClass.
Your collection now being a member of a TPersistent property instead of being a direct member of the component makes no difference for the constructor.
Update
What dóes differ is the ownership, but then at the TPersistent level, which should be managed by a correct implementation of GetOwner:
GetOwner returns the owner of an object. GetOwner is used by the GetNamePath method to find the owner of a persistent object. GetNamePath and GetOwner are introduced in TPersistent so descendants such as collections can appear in the Object Inspector.
You have to tell the IDE that your TCollection property is owned by the TPersistent property, which in turn is owned by the component.
The tutorial you are using has several errors regarding this implementation:
The owner of the collection is declared as TComponent, which should be TPersistent,
GetOwner is not implemented for the TPersistent property class, and
The fix shown at the end of the tutorial, stating that the TPersistent property should inherit from TComponent instead, is plain wrong; or more nicely said: is rather a workaround for not implementing GetOwner.
This is how it should look like:
unit MyComponent;
interface
uses
Classes, SysUtils;
type
TMyCollectionItem = class(TCollectionItem)
private
FStringProp: String;
protected
function GetDisplayName: String; override;
public
procedure Assign(Source: TPersistent); override;
published
property StringProp: String read FStringProp write FStringProp;
end;
TMyCollection = class(TCollection)
private
FOwner: TPersistent;
function GetItem(Index: Integer): TMyCollectionItem;
procedure SetItem(Index: Integer; Value: TMyCollectionItem);
protected
function GetOwner: TPersistent; override;
public
constructor Create(AOwner: TPersistent);
function Add: TMyCollectionItem;
function Insert(Index: Integer): TMyCollectionItem;
property Items[Index: Integer]: TMyCollectionItem read GetItem
write SetItem;
end;
TMyPersistent = class(TPersistent)
private
FOwner: TPersistent;
FCollectionProp: TMyCollection;
procedure SetCollectionProp(Value: TMyCollection);
protected
function GetOwner: TPersistent; override;
public
procedure Assign(Source: TPersistent); override;
constructor Create(AOwner: TPersistent);
destructor Destroy; override;
published
property CollectionProp: TMyCollection read FCollectionProp
write SetCollectionProp;
end;
TMyComponent = class(TComponent)
private
FPersistentProp: TMyPersistent;
procedure SetPersistentProp(Value: TMyPersistent);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property PersistentProp: TMyPersistent read FPersistentProp
write SetPersistentProp;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMyComponent]);
end;
{ TMyCollectionItem }
procedure TMyCollectionItem.Assign(Source: TPersistent);
begin
if Source is TMyCollectionItem then
FStringProp := TMyCollectionItem(Source).FStringProp
else
inherited Assign(Source);
end;
function TMyCollectionItem.GetDisplayName: String;
begin
Result := Format('Item %d',[Index]);
end;
{ TMyCollection }
function TMyCollection.Add: TMyCollectionItem;
begin
Result := TMyCollectionItem(inherited Add);
end;
constructor TMyCollection.Create(AOwner: TPersistent);
begin
inherited Create(TMyCollectionItem);
FOwner := AOwner;
end;
function TMyCollection.GetItem(Index: Integer): TMyCollectionItem;
begin
Result := TMyCollectionItem(inherited GetItem(Index));
end;
function TMyCollection.GetOwner: TPersistent;
begin
Result := FOwner;
end;
function TMyCollection.Insert(Index: Integer): TMyCollectionItem;
begin
Result := TMyCollectionItem(inherited Insert(Index));
end;
procedure TMyCollection.SetItem(Index: Integer; Value: TMyCollectionItem);
begin
inherited SetItem(Index, Value);
end;
{ TMyPersistent }
procedure TMyPersistent.Assign(Source: TPersistent);
begin
if Source is TMyPersistent then
CollectionProp := TMyPersistent(Source).FCollectionProp
else
inherited Assign(Source);
end;
constructor TMyPersistent.Create(AOwner: TPersistent);
begin
inherited Create;
FOwner := AOwner;
FCollectionProp := TMyCollection.Create(Self);
end;
destructor TMyPersistent.Destroy;
begin
FCollectionProp.Free;
inherited Destroy;
end;
function TMyPersistent.GetOwner: TPersistent;
begin
Result := FOwner;
end;
procedure TMyPersistent.SetCollectionProp(Value: TMyCollection);
begin
FCollectionProp.Assign(Value);
end;
{ TMyComponent }
constructor TMyComponent.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FPersistentProp := TMyPersistent.Create(Self);
end;
destructor TMyComponent.Destroy;
begin
FPersistentProp.Free;
inherited Destroy;
end;
procedure TMyComponent.SetPersistentProp(Value: TMyPersistent);
begin
FPersistentProp.Assign(Value);
end;
end.
But may I say that you can also inherit from TOwnedCollection, which makes the use and the declaration of TMyCollection much simpler:
TMyCollection = class(TOwnedCollection)
private
function GetItem(Index: Integer): TMyCollectionItem;
procedure SetItem(Index: Integer; Value: TMyCollectionItem);
public
function Add: TMyCollectionItem;
function Insert(Index: Integer): TMyCollectionItem;
property Items[Index: Integer]: TMyCollectionItem read GetItem
write SetItem;
end;

Resources