⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tiofp documentation - a worked example of using the tiopf.htm

📁 tiOPF 面向对象的数据库持久层持久层开发的框架
💻 HTM
📖 第 1 页 / 共 5 页
字号:
Double click each action and create an OnClick event handler. Wire up the 
actions to the tool buttons and main menu items so the form looks like the one 
shown below. Run and test.</P>
<P>Add the single command Close to the aCloseExecute event (to close the form 
and shut down the application.)</P>
<P><IMG height=142 
src="tiOFP Documentation - A worked example of using the tiOPF_files/6_AWorkedExampleOfUsingTheTIOPF_clip_image001_0004.gif" 
width=302> </P>
<H2>Add a TtiTreeView</H2>
<P>Go to the TechInsite tab on the component pallet and add a TtiTreeView, which 
we shall name TV. Set its HasChildForms property to true, and SplitterVisible 
property to true. You can set it’s align property to alClient, but I prefer to 
do this in the form’s constructor because is leaves the form less cluttered at 
design time.</P>
<P>While we are in the form’s constructor, set the tree view’s Data property to 
point to the single instance of the TContactMgr. The form’s constructor will 
look like this:</P><PRE>procedure TFormMain.FormCreate(Sender: TObject);
begin
  aClose.ShortCut := ShortCut(VK_F4, [ssAlt]);
  TV.Align := alClient ;
  TV.SplitterPos := 110 ;
  TV.Data := gContactMgr ;
end;</PRE>
<P>Next, we will configure the tree view to display the necessary nodes of the 
object hierarchy, and hide the nodes we do not want to display. Click on the 
tree view’s DataMappings property and add three mappings to the collection 
editor. Set the mappings DataClass properties to TContactMgr, TPeople and 
TPerson. You will end up with a collection editor that looks like this:</P>
<P><IMG height=126 
src="tiOFP Documentation - A worked example of using the tiOPF_files/6_AWorkedExampleOfUsingTheTIOPF_clip_image001_0005.gif" 
width=223> </P>
<P>Run the application and you should see a tree view displaying the three 
levels of the contact manager class hierarchy: TContactMgr, TPeople and TPerson 
as shown below:</P>
<P><IMG height=148 
src="tiOFP Documentation - A worked example of using the tiOPF_files/6_AWorkedExampleOfUsingTheTIOPF_clip_image002_0001.jpg" 
width=302></P>
<P>Next, we shall add a form to display the details of the TPerson on the right 
hand side of the tree view.</P>
<H2>Add a form to display the currently selected node on the TtiTreeView</H2>
<P>Add a new form to the project and save it with the file name 
FContactMgr_Person. (Make sure that it is not added to the projects list of auto 
create forms.) Give the form the name FormContactMgrPerson then add two 
TtiSplitterPanel(s), one inside the other, and three TLabel(s) to the form. 
Arrange the as shown below and set their Anchors properties so the panels resize 
as expected when the form is resized. (Note, you can change the properties of 
the TtiSplitterPanel by double clicking.)</P>
<P><IMG height=238 
src="tiOFP Documentation - A worked example of using the tiOPF_files/6_AWorkedExampleOfUsingTheTIOPF_clip_image001_0006.gif" 
width=272></P>
<P>The TtiTreeView can have a form associated with each data type it will 
display. When a node is selected, if it is registered against a form the form 
will be shown to the right hand side of the tree view. The tree view controls 
the form using RTTI so it must have a standard interface as shown in the code 
stub below, which can be found in the unit tiTreeView.pas.</P><PRE>{
// This stub of code defines the interface a child form
// must have if it is to be displayed as in the right
// hand pane of the TtiTreeView
TMyChildForm = class(TForm)
private
  FData: TPersistent; // Can be any TPersistent descendant
  FTreeNode: TTreeNode;
  function GetValid: boolean;
  procedure SetData(const Value: TPersistent);
published
  property Data : TPersistent read FData write SetData ;
  property Valid : boolean read GetValid ;
  property TreeNode : TTreeNode read FTreeNode write FTreeNode ;
public
end;
}</PRE>
<P>Paste this code into the interface section of the newly create form and 
change the type of FData from TPersistent to TPerson. Add ContactMgr_BOM and 
ComCtlrs to the unit’s uses clause then press Ctlr+Shift+C so Delphi will create 
the implementation of the unit. (ComCtrls.pas is required because we have added 
a reference to a TTreeNode, which is defined in this unit.)</P>
<P>Add a TtiSplitterPanel to the form so we will be able to identify if when it 
is displayed by the tree view. Arrange the TtiSplitterPanel so its sides are 
about 8 pixels in from each edge, and then set its top, left, bottom and right 
anchors to true. Double click the TtiSplitterPanel and set its SplitterAlignment 
property to horizontal. Next, add a stub of implementation to the SetData and 
Valid methods as shown below:</P><PRE>function TFormContactMgrPerson.GetValid: boolean;
begin
  result := true ;
end;

procedure TFormContactMgrPerson.SetData(const Value: TPerson);
begin
  FData := Value ;
end;</PRE>
<P>Go to the application&amp;'s main form and in the OnCreate event, add the 
line: TV.RegisterChildForm( TPerson, TFormContactMgrPerson ). This will 
associate the form we have just created with the TPerson class. The 
implementation of TformMain.OnCreate now looks like this:</P><PRE>procedure TFormMain.FormCreate(Sender: TObject);
begin
  aClose.ShortCut := ShortCut(VK_F4, [ssAlt]);
  TV.Align := alClient ;
  TV.SplitterPos := 110 ;
  // This line associates the TPerson class with
  // TFormContactMgrPerson for display and editing
  TV.RegisterChildForm( TPerson, TFormContactMgrPerson ) ;
  TV.Data := gContactMgr ;
end;</PRE>
<P>Add FContactMgr_Person and ContactMgr_BOM to the main form’s uses clause then 
compile and test the application; you should see the form we created appear when 
a TPerson is selected. There should be no form visible on the right hand side of 
the tree view when a node that is not a TPerson is selected. The application 
should look this when it is running:</P>
<P><IMG height=247 
src="tiOFP Documentation - A worked example of using the tiOPF_files/6_AWorkedExampleOfUsingTheTIOPF_clip_image002_0002.jpg" 
width=341></P>
<P>Next we will add some TPersistent aware controls to display the data 
associated with a TPerson.</P>
<H2>Setup a form to display all the details of a TPerson</H2>
<P>We must display five 'flat' properties including FirstName, LastName, 
Initials, Title and Notes. We will display the firs three with TtiPerAwareEdit 
controls, the title with a TtiPerAwareComboBox and Notes with a TtiPerAwareMemo. 
Add these controls to the form, then go to the SetData method and connect up the 
controls to the data property like this:</P><PRE>procedure TFormContactMgrPerson.SetData(const Value: TPerson);
begin
  FData := Value ;
  paeFirstName.LinkToData( FData, 'FirstName' ) ;
  paeLastName.LinkToData( FData, 'LastName' ) ;
  paeInitials.LinkToData( FData, 'Initials' ) ;
  paeTitle.LinkToData( FData, 'Title' ) ;
  paeNotes.LinkToData( FData, 'Notes' ) ;
end;</PRE>
<P>Run the application and when you select a person note in the tree view, you 
should see this:</P>
<P><IMG height=258 
src="tiOFP Documentation - A worked example of using the tiOPF_files/6_AWorkedExampleOfUsingTheTIOPF_clip_image002_0003.jpg" 
width=408></P>
<P>The next step is to add two TtiListView(s) and connect them up to display the 
TAdrs(s) and TEAdrs(s). Add the TtiListView(s) and call them lvAdrs and lvEAdrs. 
Set their all their anchors properties to true connect them up to the TPerson in 
the SetData method like this:</P><PRE>procedure TFormContactMgrPerson.SetData(const Value: TPerson);
begin
  // Snip
  if FData = nil then
  begin
    lvAdrs.Data := nil ;
    lvEAdrs.Data := nil ;
    Exit ; //==&gt;
  end ;
  lvAdrs.Data := FData.AdrsList.List ;
  lvEAdrs.Data := FData.EAdrsList.List ;
end;</PRE>
<P>Select lvEAdrs and click the ListColumns property in the property editor. The 
ListColumn collection editor will open. Add two collection items and set their 
FieldName and DisplayLabel properties as below:</P>
<TABLE cellSpacing=0 cellPadding=0>
  <TBODY>
  <TR class=Normal>
    <TD vAlign=top width=127>
      <P align=center><STRONG>Collection Item </STRONG></P></TD>
    <TD vAlign=top width=108>
      <P><STRONG>Field Name </STRONG></P></TD>
    <TD vAlign=top width=138>
      <P><STRONG>Display Label </STRONG></P></TD></TR>
  <TR class=Normal>
    <TD vAlign=top width=127>
      <P align=center>0 </P></TD>
    <TD vAlign=top width=108>
      <P>AdrsType </P></TD>
    <TD vAlign=top width=138>
      <P>Address type </P></TD></TR>
  <TR class=Normal>
    <TD vAlign=top width=127>
      <P align=center>1 </P></TD>
    <TD vAlign=top width=108>
      <P>Caption </P></TD>
    <TD vAlign=top width=138>
      <P>Text </P></TD></TR></TBODY></TABLE>
<P>The collection editor for the ListColumns property, along with the property 
editor for collection item 0 are shown below:</P>
<P><IMG height=193 
src="tiOFP Documentation - A worked example of using the tiOPF_files/6_AWorkedExampleOfUsingTheTIOPF_clip_image001_0007.gif" 
width=372> </P>
<P>Repeat the process for lvAdrs and enter the values shown below:</P>
<P><IMG height=293 
src="tiOFP Documentation - A worked example of using the tiOPF_files/6_AWorkedExampleOfUsingTheTIOPF_clip_image002_0004.jpg" 
width=426></P>
<P>Now that we have created the BOM, and build a GUI to display the data, we can 
create an Interbase database to save the objects to, and then write the Visitors 
to manage the mapping of objects to tables and properties to columns. After 
that, we will add edit and delete features to the GUI, and implement Visitors to 
save the objects.</P>
<H2>Write the SQL create script for the database</H2>
<P>In the section on database schema and object - database mapping, we 
identified three persistent objects that require three tables to store their 
information. Remind yourself of the way we decided to map objects to tables and 
properties to columns. The SQL schema to implement this will be written next, 
but first we must create an Interbase database, and then connect to it using a 
tool for running adhoc SQL statements.</P>
<H2>Create the Interbase database</H2>
<P>Make sure the Interbase server is running, and then load WinISQL (or what 
ever tool you prefer for creating an Interbase database) the dialog to create a 
database in WinISQL is shown below:</P>
<P><IMG height=270 
src="tiOFP Documentation - A worked example of using the tiOPF_files/6_AWorkedExampleOfUsingTheTIOPF_clip_image002_0005.jpg" 
width=209></P>
<P>This will create an Interbase database called ContactMgr.gdb in the directory 
C:\ContactMgr\</P>
<H2>Compile the tiSQLEditor</H2>
<P>Add the application C:\TechInsite\SupportApps\tiSQLManager\tiSQLEditor.dpr to 
the project group. Check the command line parameters are entered as shown 
below:</P>
<P><IMG height=220 
src="tiOFP Documentation - A worked example of using the tiOPF_files/6_AWorkedExampleOfUsingTheTIOPF_clip_image002_0006.jpg" 
width=354></P>
<P>The command line parameters have the following meanings:</P>
<TABLE cellSpacing=0 cellPadding=0>
  <TBODY>
  <TR class=Normal>
    <TD vAlign=top width=121>
      <P align=center><STRONG>Parameter switch </STRONG></P></TD>
    <TD vAlign=top width=198>
      <P><STRONG>Value </STRONG></P></TD>
    <TD vAlign=top width=150>
      <P><STRONG>Meaning </STRONG></P></TD></TR>
  <TR class=Normal>
    <TD vAlign=top width=121>
      <P align=center>-pl</P></TD>
    <TD vAlign=top width=198>
      <P>IBX</P></TD>
    <TD vAlign=top width=150>
      <P>Persistence layer name</P></TD></TR>
  <TR class=Normal>
    <TD vAlign=top width=121>
      <P align=center>-d</P></TD>
    <TD vAlign=top width=198>
      <P>C:\ContactMgr\ContactMgr.gdb</P></TD>
    <TD vAlign=top width=150>
      <P>Database name</P></TD></TR>
  <TR class=Normal>
    <TD vAlign=top width=121>
      <P align=center>-u</P></TD>
    <TD vAlign=top width=198>
      <P>SYSDBA</P></TD>
    <TD vAlign=top width=150>
      <P>User name</P></TD></TR>
  <TR class=Normal>
    <TD vAlign=top width=121>
      <P align=center>-p</P></TD>
    <TD vAlign=top width=198>
      <P>masterkey</P></TD>
    <TD vAlign=top width=150>
      <P>Password</P></TD></TR></TBODY></TABLE>
<P>The tiSQLEditor window should show like the one shown below:</P>
<P><IMG height=285 
src="tiOFP Documentation - A worked example of using the tiOPF_files/6_AWorkedExampleOfUsingTheTIOPF_clip_image002_0008.jpg" 
width=402> </P>
<P>Enter the SQL create script shown below, then save it to 
C:\ContactMgr\ContactMgr_DDL.SQL</P><PRE>/*****************************************************************
  Create the contact manager database
*/

create table Next_OID
  ( oid integer not null);

create table Person
( oid integer not null,
  First_Name varchar( 60 ),
  Last_Name varchar( 60 ),
  title varchar( 10 ),
  initials varchar( 10 ),
  notes varchar( 256 ),
  primary key ( oid )) ;

create table adrs
( oid integer not null,
  owner_oid integer not null,
  adrs_type varchar( 20 ),
  lines varchar( 180 ),
  suburb varchar( 30 ),
  state varchar( 30 ),
  pcode varchar( 20 ),
  country varchar( 30 ),
  primary key( oid )) ;

alter table adrs
  add foreign key ( owner_oid ) references Person( OID ) ;

create table EAdrs
( oid integer not null,
  owner_oid integer not null,
  eadrs_type varchar( 20 ),
  text varchar( 60 ),
  primary key( oid )) ;

alter table EAdrs
  add foreign key ( owner_oid ) references Person( OID ) ;

commit ;</PRE>
<P>Note the first table created NEXT_OID. This is required by all databases the 
framework uses. It provides a central point for handing out Object Identifiers 
(OIDs). Without this table you will not be able to create any new objects.</P>
<P>You can run this script using either one of the tools that come with 
Interbase, or by selecting each statement, one at the time in the tiSQLEditor 
then clicking the run button. (This is my preferred approach for small 
scripts)</P>
<H2>Insert some test data</H2>
<P>Create another script in the tiSQLEditor and save it as 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -