Mirror

Application Settings (article 2) (Views: 100)


Problem/Question/Abstract:

Managing Application Settings

Answer:

Introduction

In part 1 of this article, we looked at how we can automate the handling of application settings using a base object, TGXAppSettings. In part 2, we are going to look at using an object dataset to interface the application settings object to data aware controls.

The Object Dataset

Now that we have created our TRichEditSettings object, we need to somehow enable the end user of our application to manipulate the properties of TRichEditSettings. A first crack at doing this might look something like this:

procedure TForm1.LoadSettings;
begin
  cbWordWrap.Checked := Settings.WordWrap;
  edFontName.Text := Settings.FontName;
  edFontSize.Text := IntToStr(Settings.FontSize);
end;

procedure TForm1.SaveSettings;
begin
  Settings.WordWrap := cbWordWrap.Checked;
  Settings.FontName := edFontName.Text;
  Settings.FontSize := StrToInt(edFontSize.Text);
end;

This approach works fine when there is only a few properties to deal with, but in a real application there could be hundreds of properties and this approach will get very tedious, very quickly. Fortunately, due to architectural changes made in Delphi 3, we can do something about this.

In Delphi 1 and 2, data access was tightly bound to the BDE. Starting with Delphi 3, Borland abstracted the TDataset class thereby allowing anyone to create a provider of data. Since Delphi 3 was released lot of vendors have taken advantage of this by creating TDataset descendants that enable developers to access various datasources such as SQL Server, Interbase, Dbase without having to go through the BDE. I know what your asking yourself, how does this benefit us in this case?

Simple really. What we are going to do is create a TDataset descendant that treats objects as if they were a database. The object itself can be considered the "Table" while the properties will become the "Fields". By doing this we can directly connect the properties of our application settings object to data aware controls and thereby eliminate the work of transferring the information manually to visual controls and back again.

Using the Object Dataset

Now explaining how to write a complete dataset descendant is far beyond the scope of this article. Instead, I'm going to focus on how to use the object dataset I've written in the context of application settings.

The first thing we need to do is create an options form. Here a the picture of the one included in the sample code.



At the bottom of the form are three non-visual components. The leftmost component is the TRichEditSettings component, the middle component is the TGXObjectDataset and the right component is a standard TDataSource component.

Once we have dropped these components on the form, we need to hook them up. Set the GXObjectDataset's Component property to RichEditSettings1. You can create persistent field objects at this time if you desire, but generally there is no need to. In order to better show how the RichEditSettings properties become fields, I have included a picture below of the fields editor showing the persistent fields generated by the GXObjectDataset when connected to a TRichEditSettings component.



Next, connect the datasource to the GXObjectDataset. Finally, go through the data aware components and set the datasource and datafield property. Remember, every property of TRichEditSettings will appear as a field.

Now that this has been completed, we can write the code to show the options dialog from the main form. It's very straightforward.

procedure TMainForm.acOptionsExecute(Sender: TObject);
var
  Dlg: TfmOptions;
begin
  Dlg := TfmOptions.Create(Self);
  try
    Dlg.RichEditSettings1.Assign(RichEditSettings);
    Dlg.GXObjectDataset1.Open;
    if Dlg.ShowModal = mrOK then
    begin
      if (Dlg.GXObjectDataset1.State in dsEditModes) then
        Dlg.GXObjectDataset1.Post;
      RichEditSettings.Assign(Dlg.RichEditSettings1);
      RichEditSettings.UpdateSettings(Editor);
      SelectionChange(Editor);
    end;
  finally
    Dlg.Free;
  end;
end;

In the above code, we first create the options dialog. We then assign the current RichEditSettings object of the main form to the dialog's RichEditSettings. This means that the user is working on a copy. Next we open the GXObjectDataset on the dialog so that when the dialog is shown, the user will be able to edit the various values. After this, we then show the dialog modally.

If the user clicked the OK button, we check to see if the GXObjectDataset is in edit mode, and if so, post the changes. This will cause the underlying properties of the RichEditSettings component of the dialog to be updated with the user changes. We then assign the option dialog's RichEditSettings to the main forms RichEditSettings in order to capture the users changes. Finally, we update the richedit control with the new changes.

Conclusion

Well, this was a short Part 2 but I hope you have seen that managing application settings can be easy instead of being a chore.

Code

Download the code from this article here. Please be sure to read Install.txt included in the zip file before opening the project in Delphi.

<< Back to main page