Loading and saving of an object structure

 

We now want to allow the object structure to be loaded and saved.

For this purpose, we create a new menu in the main menu:

 

The implementation of the menu item New is quickly realized by setting Product to NoValue:

 

However, loading and saving requires communication between client and server to start a file selection dialog at the client and transport a file from the client to the server and vice versa.

First, you need to enable the TCE Windows Objects type library in the properties of the knowledge base for server and client use:

 

This library currently has no controls but can be used on both the server and the client side.

Here is the code for saving the object structure to the client. The goal of this program code is to call a file selection dialog on the client, and after successfully setting the file name, transport the object tree to a file at the client.

Dim V As Variant, FS As TCEWindows.FileSelect, FN As String

 

FS := Client.CreateObject("TCEWindows.FileSelect")

FS.Filter := Dialog.Filter # "TCE Configuration (*.tce)||*tce"

If FS.ShowSave(MainForm.hwnd) Then

   FN := FS.FileName

   FN &= ".tce" If LCase(Right(FN, 4)) <> ".tce"

   V := Product.ObjectTreeSerialize

   Server.TransactStreamToClient(FN, V)

End If

 

First, three local variables are declared:

Dim V As Variant, FS As TCEWindows.FileSelect, FN As String

 

V is of type Variant. Variant variables can process binary data. A stored object structure consists of such data. FS is a COM object reference of type TCEWindows.FileSelect. It takes an object reference to a server-side or client-side COM object. FN takes the new file name.

FS := Client.CreateObject("TCEWindows.FileSelect")

 

An object of type TCEWindows.FileSelect is created on client. Please do not perform this function on the server side, because the file selection dialog will be displayed later on the desktop of the server.

FS.Filter := Dialog.Filter # "TCE Configuration (*.tce)||*tce"

 

Sets the file filter.

FS.ShowSave(MainForm.hwnd)

 

Opens the Save As Dialog. To ensure that the dialog does not appear detached from the application, the client-side window handle of the main window is passed as a parameter. If successful, the return value is nonzero and FS.FileName contains the full file name.

FN := FS.FileName

 

Applies the file name to the FN variable.

 

FN &= ".tce" If LCase(Right(FN, 4)) <> ".tce"

 

The file name should have the ending .tce.

 

V := Product.ObjectTreeSerialize

 

Stores the object tree of the Product object with all the saveable information in variable V

Server.TransactStreamToClient(FN, V)

 

The contents of variable V are transported from the server to the client, a file is created, and the contents of V are stored there.

 

 

Here is the code for loading an object structure from the client. The goal of this program code is to call a file selection dialog on the client, and after successful selection of the file name, transport the contents of the file to an object tree at the server.

Dim V As Variant, FS As TCEWindows.FileSelect

 

FS := Client.CreateObject("TCEWindows.FileSelect")

FS.Filter := Dialog.Filter #"TCE Configuration (*.tce)||*.tce"

If FS.ShowOpen(MainForm.hwnd) Then

   Client.TransactFileToStream(FS.FileName, V)

   Product := LoadObjectTree(V)

End If

 

First, two local variables are declared:

Dim V As Variant, FS As TCEWindows.FileSelect

 

Variant variables can process binary data. A stored object structure consists of such data. FS is a COM object reference  of type  TCEWindows.FileSelect. It takes an object reference to a server-side or client-side object.

FS := Client.CreateObject("TCEWindows.FileSelect")

 

An object of type TCEWindows.FileSelect is created on client. Please do not perform this function on the server side, because the file selection dialog will then be displayed on the desktop of the server.

FS.Filter := Dialog.Filter #"TCE Configuration (*.tce)||*.tce"

 

Sets the file filter.

FS.ShowOpen(MainForm.hwnd)

 

Opens the Open File dialog. To ensure that the dialog does not appear detached from the application, the client-side window handle of the main window is passed as a parameter. If successful, the return value is nonzero and FS.Name contains the full file name.

Client.TransactFileToStream(FS.FileName, V)

 

The function transports the contents of the client-side file into a server-side variant variable

Product := LoadObjectTree(V)

 

The TCE function loads an object tree from the binary data previously generated with ObjectTreeSerialize into an object.

 

 

(knowledge base Tutorial Stage 14)