Software Development Kit
to the Delphi-Win32 and Free Pascal compilers
Home > Wiki > PhonebookProject/Article1

PhonebookProject/Article1

english (en) | português (pt)



This is the first in a series of articles showing the steps needed to create a working application with PressObjects.

This first article shows how to create the project, fix dependencies, and create the visual interface.

Contents

Pre requirements

  • Delphi 5 or higher, or Lazarus
  • Recent version of PressObjects
  • Component of connection to the database that PressObjects support. So far only DOA, FBLib, IBX, SQLdb, UIB or Zeos with PostgreSQL, Firebird, Interbase or Oracle.
  • FastMM (optional)

Installation

Unpack the PressObjects and configure the project or the IDE so that the folders {press}\Source\Core e {press}\Source\Brokers can be found by the compiler. On Delphi, put the folders in the Library Path of IDE on Project | Options | Directories | Search Path of the project. On Lazarus, put the folders on field Project | Compiler Options | Other Unit Files. Make also the installation of the connection component, if not done, at least indicate the path of units. The PressObjects not require the installation of component for connection, need only find their units, so the application can be compiled.

Create the following tree for the project's directory (suggestion):

+-{phonebook}
  +-Source
    +-Core
    | +-Main
    | | +-vcl5 (or your Delphi version)
    | | +-lcl
    | +-Custom
    | | +-vcl5
    | | +-lcl
    | +-Contact
    | | +-vcl5
    | | +-lcl
    +-Metadata
    | +-Firebird
    | +-Oracle
    | +-PostgreSQL
    +-Projects
      +-D5 (or your Delphi version)
      +-Laz

Project

Create a new project, inform the path to locate the PressObjects (if necessary, see details on Installation area) and rename the main form to MainForm.

Give a File | Save All to save the project files:

  • {phonebook}\Source\Core\Main\vcl5\MainFrm.pas for the main form
  • {phonebook}\Source\Projects\D5\Agenda.dpr for the main file of the project

Use the folder name and extension for project files according to IDE utilized. If you are going to use FastMM, change the .dpr and include the unit FastMM4 before all other units.

None of the forms will have code, except by one. Create a method for MainForm OnKeyDown event, and implement:

...
implementation

uses
  Dialogs, Clipbrd, PressOPF;

procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key = Ord('9')) and (Shift = [ssShift, ssAlt]) then
  begin
    Clipboard.AsText := PressOPFService.CreateDatabaseStatement;
    ShowMessage('Database metadata copied to Clipboard');
  end;
end;

end.

The combination of keys Shift+Alt+9 will copy the metadata, for database creation, to the clipboard. Set Form KeyPreview property to True for this event work.

Create a new unit (only the .pas, without form), write to {phonebook}\Source\Core\Main\Brokers.pas and put the following content:

unit Brokers;

interface

implementation

uses
  PressSubject,
  PressAttributes,
  PressUIBBroker,  // or the broker for your connection component
  PressMessages_en;

initialization
  PressModel.ClassIdStorageName := 'ModelClasses';
  PressModel.DefaultGeneratorName := 'gen_phonebook';
  PressModel.DefaultKeyType := TPressInteger;

end.

PressUIBBroker is the unit that registers the connector framework to the OPF. You can use more than one connector on the same project and choose which of them will use through the configuration file (which will be addressed in the third article).

Give a build all the project to verify that all dependencies were found. Maybe an error occurs on the compilation of unit MainFrm. Make a choice between cleaning the header of the interface area, or remove Dialogs from implementation area.

When the compilation is successful at this point, probably no more problems will occur, related to dependencies of the PressObjects at any point of the project.

Forms

Create the forms for the phonebook with the following names and folders. Note that the examples use the folder vcl5, use the folder corresponding to your Delphi version, or lcl, if building project in Lazarus. Within the parentheses are the name of the form and what form it inherits, where applicable.

CustomEditForm

  • {phonebook}\Source\Core\Custom\vcl5\CustomEditFrm.pas (CustomEditForm)

Create at least one Save button (SaveButton) and a Cancel (CancelButton).

Remove some excesses from the form, such as the public and private area of the class, and the variable that point to the instance. They are not necessary.

At the end of the unit, insert:

uses
  PressVCLBroker, CustomMVP;  // or the PressLCLBroker if you use the Lazarus

initialization
  PressVCLForm(TCustomEditPresenter, TCustomEditForm);  // or PressLCLForm
  // if you use the Lazarus, keep the code {$I...} generated by IDE

end.

This piece of code is used to register the form in the framework MVP.

CustomQueryForm

  • {phonebook}\Source\Core\Custom\vcl\CustomQueryFrm.pas (CustomQueryForm)

Create this form with a TStringGrid (QueryStringGrid) and a Search button (SearchButton). Attention to the names of components, they must be identical to those given in brackets. It is through this name that the MVP will find them later.

Add the record in MVP, the same way as was done with the form CustomEditForm, however pointing to the classes TCustomQueryPresenter and TCustomQueryForm.

ContactEditForm

  • {phonebook}\Source\Core\Contact\vcl\ContactEditFrm.pas (ContactEditForm - CustomEditForm)

Attention to the inheritance of CustomEditForm. Besides the buttons inherited, also place a TEdit with Label for Name (NameEdit), Address (AddressEdit), and a TComboBox for the City (CityComboBox) and a TStringGrid for phones (PhonesStringGrid). Set FocusControl property of their respective controls for Labels. Do not use components that have Label, use TEdit, otherwise the MVP framework does not recognize (however you can create a view to use it in your project).

Always observe the name of the components, is necessary declare them properly to the mapping in the unit that controls the MVP, that will be written later.

Do not forget to register the Form in the MVP framework, referencing the classes TContactEditPresenter and TContactEditForm, and using the unit ContactMVP:

uses
  PressVCLBroker, ContactMVP;  // or PressLCLBroker if you use the Lazarus.

initialization
  PressVCLForm(TContactEditPresenter, TContactEditForm);  // or PressLCLForm
  // if you use the Lazarus, keep the code {$I...} generated by IDE

end.

ContactQueryForm

  • {phonebook}\Source\Core\Contact\vcl\ContactQueryFrm.pas (ContactQueryForm - CustomQueryFrm)

Place a TEdit to enter the contact name to be searched (NomeEdit) and a TComboBox to filter by city (CidadeComboBox). Create also the Labels and always observed the name of the components to avoid errors at runtime.

Register the form at the end of the unit, identical to the form ContactEditForm, however referencing the classes TContatoQueryPresenter and TContatoQueryForm.

ContactPhoneEditForm

  • {phonebook}\Source\Core\Contact\vcl\ContactPhoneEditFrm.pas (ContactPhoneEditForm - CustomEditFrm)

Place a TComboBox for the type (TPhoneTypeComboBox), a TEdit for the number (NumberEdit).

The registry of the form refers the classes TContatoFoneEditPresenter and TContatoFoneEditForm.

Remember that none of the Forms needs the private and public areas, neither the variable that receives the address of the instance. The unit ContactFoneEditForm is so:

unit ContactPhoneEditFrm;

interface

uses
  CustomEditFrm, Classes, Controls, StdCtrls;

type
  TContactPhoneEditForm = class(TCustomEditForm)
    TPhoneTypeLabel: TLabel;
    TPhoneTypeComboBox: TComboBox;
    NumberLabel: TLabel;
    NumberEdit: TEdit;
  end;

implementation

{$R *.DFM}

uses
  PressVCLBroker, ContactMVP;  // or PressLCLBroker if you use the Lazarus

initialization
  PressVCLForm(TContactPhoneEditPresenter, TContactPhoneEditForm);  // or PressLCLForm
  // if you use the Lazarus, keep the code {$I...} generated by IDE

end.

CityEditForm

  • {phonebook}\Source\Core\Contact\vcl\CityEditFrm.pas (CityEditForm - CustomEditFrm)

Place a TEdit to name (NameEdit) and another for the state (StateEdit) City. The registry of the form refers the classes TCityEditPresenter e TCityEditForm.

CityQueryForm

  • {phonebook}\Source\Core\Contact\vcl\CityQueryFrm.pas (CityQueryForm - CustomQueryFrm)

Place a TEdit to name (NameEdit) of the city to search, and register the form at the end of the unit referencing the classes TCidadeQueryPresenter and TCidadeQueryForm.

MainForm

Finally create four buttons in the MainForm: New Contact (NewContactButton), Search Contact (SearchContactButton), Search City (SearchCityButton) and Close (CloseButton).

The visual part is ready. The next post will present how to write the logic (objects of business and MVP), create a DB, configure the connector, and finally test the application.

Comments

Usually the order of building an application is:

  • Project and MainForm
  • Unit Brokers
  • Business classes
  • MVP Classes
  • Forms
  • DB and configuration of the connector

This article placed the forms before business and MVP classes. The most familiar parts, for a Delphi/Lazarus programmer, was presented first. The next article will present the construction of these classes.

At this point you can not compile the project, because forms depend of the MVP classes, which depend on business classes. The project can be compiled again at the end of the second article.