Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
From the author of

Custom Components in TAdapterPageProducer

Much of the work of displaying HTML throughout this article has been done by TAdapterPageProducer components, and the components that are embedded within it. However, you'll certainly want to customize the HTML therein beyond the standard code you've seen so far. WebSnap allows you to do this by creating your own components that plug into the TAdapterPageProducer, allowing you to add your own custom HTML to the mix.

Your custom TAdapterPageProducer components must descend from TWebContainedComponent and implement the IWebContent interface. Because all the components must do this, it's a perfect opportunity to use an abstract class as in Listing 2.

Listing 2—Abstract Descendant Class of TWebContainedComponent

type

Tddg6BaseWebSnapComponent = class(TWebContainedComponent, IWebContent)
 protected
  { IWebContent }
  function Content(Options: TWebContentOptions; ParentLayout: TLayout): 
string;
  function GetHTML: string; virtual; abstract;
 end;

This class is implemented like so:

function Tddg6BaseWebSnapComponent.Content(Options: TWebContentOptions;
 ParentLayout: TLayout): string;
var
 Intf: ILayoutWebContent;
begin
 if Supports(ParentLayout, ILayoutWebContent, Intf) then
  Result := Intf.LayoutField(GetHTML, nil)
 else
  Result := GetHTML;
end;

The abstract class implements the Content function only because the GetHTML function is declared as abstract. The Content function basically checks to see whether the containing component is a LayoutGroup. If it is LayoutGroup, the Content function places its content inside the LayoutGroup. Otherwise, Content simply returns the results of GetHTML. Descendant components therefore need only implement the GetHTML function, returning the appropriate HTML code, and they can be registered to work inside a TAdapterPageProducer.

The code on the CD-ROM implements two components that allow you to add HTML content to a TAdapterPageProducer, either as a string or as a file. The code for the Tddg6HTMLCode component is as shown in Listing 3.

Listing 3—Tddg6HTMLCode Component

 Tddg6HTMLCode = class(Tddg6BaseWebSnapComponent)
 private
  FHTML: TStrings;
  procedure SetHTML(const Value: TStrings);
 protected
  function GetHTML: string; override;
 public
  constructor Create(AOwner: TComponent); override;
  destructor Destroy; override;
 published
  property HTML: TStrings read FHTML write SetHTML;
 end;

constructor Tddg6HTMLCode.Create(AOwner: TComponent);
begin
 inherited;
 FHTML := TStringList.Create;
end;

destructor Tddg6HTMLCode.Destroy;
begin
 FHTML.Free;
 inherited;
end;

function Tddg6HTMLCode.GetHTML: string;
begin
 Result := FHTML.Text;
end;

procedure Tddg6HTMLCode.SetHTML(const Value: TStrings);
begin
 FHTML.Assign(Value);
end;

This is a pretty simple class. It merely provides a published property of type TStrings that will take any HTML code and then put it in the TAdapterPageProducer as is. The GetHTML function simply returns the HTML in string form. You can build components to return any HTML code you want to include—images, links, files, and other content. All descendant components have to do is to provide their HTML content in an overridden GetHTML() method. Note that there are supporting registration functions in the unit where the components are implemented. When creating components, be sure to register them in your unit similar to those on the CD-ROM. To use these components, merely install them in a design-time package, and the components will appear in the TAdapterPageProducer's Web Surface Designer (see Figure 2).

Figure 2Figure 2 TAdapterPageProducer components in the Web Surface Designer.

  • + Share This
  • 🔖 Save To Your Account