2010. december 18., szombat

A Simple Property Editor (2)


Problem/Question/Abstract:

Sometimes, when you create components, you want to make it easier for the developers to use them. This is the time to develop a property editor.

Answer:

INTRODUCTION

In this article I will give you a short introduction to property editor development. This property editor developed here will simlpy allow you to edit string and TCaption properties in a better way, allowing you to add line breaks to strings.

There are two reasons for this property editor. First it is great to add line breaks into the labels caption, second it is fairly simple, therefore a good start for developing a property editor.

STEPS IN CREATING A PROPERTY EDITOR

First a short list of considerations when creating a property editor.

How should the property editor support the developer?
Which components/properties/data types should the editor support?
When do you have enough time to write it? :)

How do we support the developer?

Well, as I have written before, we will give the developer a simple way of adding line breaks to. The form we will create with the Delphi form designer, jsut as we do always. We add a public procedure to it, that will take the old value, load it into a memo field, show the form and return the either new value or the old if the user has not confirmed the changes.

Which components/properties/data are supported?

We will support all components and properties of the types string and TCaption.

DESIGING THE FORM

Start Delphi and close all open files. Create a new form and name it frmStringEditor. Add a memo field to the form and name it mmoStringProperty. Now we need to buttons, one for "OK" and one for "Cancel." Thats all for the design part. Make it fit "nicely." Add event handlers to the to button click procedures!

We will add one public procedure that will accomplish the form show and decide, whether the property is changed or not.

function Edit(var Data: string): Boolean;

The remaining code comes a little later.

CREATING THE PROPERTY EDITOR CLASS

All Property Editors have to be a descendend of the TPropertyEditor class. In our case we will descend from the TStringProperty class, that itself descends from the one previously named.

There are two function we need to override. GetAttributes to tell Delphi that we provide a dialog to manipulate the property. Edit is the function called when the developer calls for the property editor dialog.

TOurStringProperty = class(TStringProperty)
public
  function GetAttributes: TPropertyAttributes; override;
  procedure Edit; override;
end;

The remaining code comes a little later, too.

REGISTERING THE PROPERTY EDITOR

We will install the property editor just like we install components, therefore we have to provide the Register property. In the body we will add a call to the RegisterPropertyEditor function. This function takes four parameters.

Information about the property type handled by the editor
The component/control class this editor is for (nil for all)
The property this editor is for ('' for all)
The property editor class itself

AND NOW THE WHOLE CODE

I have placed this all into one unit developed on Delphi 5 and tested with Delphi 6 Evaluation version. You will need at least the Professional Editions to get it working. Earlier versions of Delphi should work just fine. Cannot test on them, sorry.

This unit assumes that you saved your form under the name of uStringEditor.

unit uStringEditor;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, TypInfo
{$IFNDEF VER140}
  , DsgnIntf
{$ELSE}
  , DesignEditors, DesignIntf
{$ENDIF}
  ;

type
  TfrmStringEditor = class(TForm)
    mmoStringProperty: TMemo;
    btnOK: TButton;
    btnCancel: TButton;
    procedure btnOKClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
  private
  public
    function Edit(var Data: string): Boolean;
  end;

  TOurStringProperty = class(TStringProperty)
  public
    function GetAttributes: TPropertyAttributes; override;
    procedure Edit; override;
  end;

procedure Register;

implementation

{$R *.DFM}

procedure Register;
begin
  RegisterPropertyEditor(TypeInfo(TCaption), nil, '', TOurStringProperty);
  RegisterPropertyEditor(TypeInfo(string), nil, '', TOurStringProperty);
end;

function EditConnectionString(
  Component: TComponent; PropInfo: PPropInfo
  ): Boolean;
var
  Str: string;
begin
  Result := False;
  with TfrmStringEditor.Create(Application) do
  try
    Caption := Format('%s.%s string editor', [Component.Name, PropInfo^.Name]);
    Str := GetStrProp(Component, PropInfo);
    if Edit(Str) then
    begin
      SetStrProp(Component, PropInfo, Str);
      Result := True;
    end;
  finally
    Free;
  end;
end;

{ TOurStringProperty }

procedure TOurStringProperty.Edit;
begin
  if EditConnectionString(GetComponent(0) as TComponent, GetPropInfo)
    then
    Modified;
end;

function TOurStringProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog];
end;

{ TfrmStringEditor }

procedure TfrmStringEditor.btnCancelClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TfrmStringEditor.btnOKClick(Sender: TObject);
begin
  ModalResult := mrOk;
end;

function TfrmStringEditor.Edit(var Data: string): Boolean;
begin
  mmoStringProperty.Text := Data;
  if ShowModal = mrOK then
  begin
    Result := Data <> mmoStringProperty.Text;
    Data := mmoStringProperty.Text;
  end
  else
  begin
    Result := False;
  end;
end;

end.

INSTALLING IT

Go to the menu Component | Install Component..., select the your file from the disk and press "OK." After compiling and saving the package you are finished. You may have to restart Delphi for the changes to take place.

Nincsenek megjegyzések:

Megjegyzés küldése