![]() |
Web Development »
Applications & Tools »
CodeProject Tools
Intermediate
License: The Code Project Open License (CPOL)
CM_ConfigBuilder 1.2g: Visual Studio 6/Visual Studio 2005/Visual Studio 2008 Code Generator for Application Settings Graphic ManagementBy Stefano BasiliCM_ConfigBuilder generates and compiles the required files to manage your application's settings/preferences and to store/retrieve them in XML format. |
VB, VC6, VC7.NET 1.0, Win2K, WinXP, MFC, ATL, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
| CM_Configbuilder Version 1.2G | |
| 1. | Fixed issue on Visual C++ 6.0 Project Generator |
| 2. | Fixed issue on Visual C++ 6.0 Test Project Template |
About two years ago, I began to be bored every time my customers asked me, "Can you make this parameter configurable?" Every time I make a parameter configurable, I need to write code to perform the following actions: save the parameter to a file, retrieve the parameter from a file, set up some graphic control to allow the user to change its value (such as text box or some other input control), and validate values set by the user. So, I decided to create a tool that could make all these operations for me.
The first step was to create a tool to describe the structure of the parameters I wanted in my application. The second step was to add the capability to describe how the user could edit the data. The third step was to build a library containing all the needed code. All these wishes led me to write CM_ConfigBuilder. CM_ConfigBuilder generates all the code needed to manage my data structure and to make it persistent in an XML file. The GUI generated by CM_ConfigBuilder shows a tree view containing all the classes of the hierarchy:

Here follows a brief tutorial that shows how to create a simple configuration.
Let's create a simple configuration made of a root class with three attributes. To add members to the class, right click on the tree view and select the item you want to add. In this example, we will add three members:
By selecting a member (in the tree view), we will see a set of tabs in the right pane. The first tab ("global") contains the name of the member, its caption (used in the GUI), and the "persistence mode":
The persistence mode can be set to the following values:
CM_ConfigBuilder allows definition of members' data types: string, long, Boolean, double, date, time. By default, the data type of members is set to "string" (the C++ generator uses a CString derived class called cmString):
Member validation currently supports a "Static Enum" and "Custom Validation."
| Static Enum | |||||||||||||||||||||
When we select a "Static Enum" validation, CM_ConfigBuilder generates:
Fields with "Static Enum" validation will be edited by ComboBox controls. The column "Default Values" can be used to specify which default value will be displayed for uninitialized fields. The button "Clear Default" will clear the entire column: the generator will set the initial field value at 0. So, we can set up a member variable long, string or double and validate it as an enum value. Let's have an example: We need to declare a class member representing a sampling rate (let's call it
Cm_ConfigBuilder generates the following code: typedef enum enSampleRate
{
enSampleRate_Low = 1,
enSampleRate_Medium = 5,
enSampleRate_High = 10
}
enSampleRate;
void SetSampleRate(enSampleRate newValue);
enSampleRate GetSampleRate() const;
double GetSampleRate_Value() const;
// this methods returns 10.55 when sampleRate is enSampleRate_Low
In this way, it is easy to make comparisons ( |
|||||||||||||||||||||
| Custom Validation | |||||||||||||||||||||
|
CM_ConfigBuilder can generate an interface. This interface contains a validation function for each member using Custom Validation. Every class member can have one for each class of the project. The Validation process has been associated with a set of possible results:
If you enable Custom Validation for a class member, you will get the following function: //
// VB Sample
//
Implements ItestRootValidator
Private Sub ItestRootValidator_MEMBERNAME_OnLostFocus(_
parent As CLASS, value As DATATYPE)
' if you implement this interface you could change value
' call SetMemberValidationStatus (enum memberId, validationStatus)
' to indicate the icon to display
parent.SetMemberValidationStatus enAttributeId_MEMBERNAME, _
enValidationResult_Error
' call SetMemberValidationMsg (enum memberId, _
' displayDefaultMsg as Boolean, Msg As String) to indicate the
' message to display
' if you set displayDefaultMsg to True the message set in
' figure above will be shown, if you set displayDefaultMsg to
' False you can provide the message to be displayed
parent.SetMemberValidationStatus enAttributeId_MEMBERNAME, _
False, "The value you entered is wrong"
End Sub
The following image shows a possible use of Custom Member Validation: ![]() |
Another thing to do is set the "GUI mode." We can decide if a member is visible or hidden.
For each class member, CM_ConfiguBuilder will generate a Get/Set couple of methods. If the member is set to visible and the generator is configured to generate the GUI needed to edit the configuration, the member will be shown and will be editable if the check box "Read only" is not checked.
To configure a single class, we have to click on the corresponding node in the tree view. The first tab ("global") contains general information about the class: its name, its caption (shown in the treeview of the generated GUI) and its container caption. The container caption is used when the cardinality of the class is Array or Map (see sample 1 in CM_ConfigBuilder_samples.zip).
The tab "GUI" allows us to define how we want to see the attributes. Currently, only "Category list" and "Grid" are supported. CategoryList allows the class attributes to be grouped, as shown below. The configuration panel has two tabs: the first is a preview of the generated panel, while the second tab allows editing of the panel structure.


Grid style allows arrays of classes to be shown in a single table, where each row represents a single class instance. Currently, CM_ConfigBuilder provides the following customizations:
Starting from version 1.2c, "category list" (implemented by component cmOptionList) and "grid" (implemented by component cmTable) have a new style (soon customizable).
Sample of cmOptionList (style1):
Sample of cmTable (style1):
The tab "Cardinality" allows us to say, "This class is not a single instance." We can decide if we want to create a class array or a class map. These settings are shown in sample 1 (see sample 1 in CM_ConfigBuilder_samples.zip). P.S. this version does not support min/max size of array or map.
The tab "Tree Icons" allows us to specify which icons will be shown in the tree view. For classes not "single instance" (see Cardinality), it is possible to specify which icons will be shown on the container node.
If you need to implement your specific validation procedure for a class, you'll need to set up the panel shown above. For each class, you can select which kind of validation you want:
SetData method on MainDialog. This allows you to perform a first validation. GetFocus validation method: this method (if enabled) will be called each time the class is about to be shown. LostFocus validation method: this method (if enabled) will be called each time the class is about to be hidden (and a new class is displayed). You can also decide if you want to display the default messages (defined in the Default Class settings) or if you want to provide class-specific messages. For classes with cardinality Map or Array, you can have a Container Class Validation Interface. You can configure it as you would configure the Class Validation Interface and have some more methods:
OnBeforeItemAdd: called when the user is trying to add a new item to the class Container (Array or Map) OnBeforeItemDelete: called when the user is trying to delete an item from the class Container (Array or Map) Each class has two additional methods:
SetClassValidationStatus: allows you to indicate the validation icon shown in the tree view node tool tip associated with the class. If you select enValidationResult_Error, the tree view item text will be painted in red. SetClassValidationMsg: allows you to indicate the validation message shown in the tree view node tool tip associated with the class.
CM_ConfigBuilder allows you to customize the dialog called "Main Dialog":

After the Custom Data Structure has been defined, we can reach the options via selecting the menu "Code Generation >> Options..." By checking "Enable C++ code generation," we are able to set up the C++ library generation:
| CfgSample_D.lib | library for debug version |
| CfgSample_D.dll | library for debug version |
| CfgSample_DU.lib | library for Unicode debug version |
| CfgSample_DU.dll | library for Unicode debug version |
| CfgSample_R.lib | library for release version |
| CfgSample_R.dll | library for release version |
| CfgSample_RU.lib | library for Unicode release version |
| CfgSample_RU.dll | library for Unicode release version |
Note on Visual Studio 2005: If you need to use these libraries with Visual Studio 2005, you just have to open the generated project (*.dsw) with Visual Studio 2005 and compile it.
ATL generation allows Visual Basic users to access the class hierarchy. By checking the "enable ATL code generation," we are able to set up the ActiveX ATL library generation:
Choosing menu "Code generation >> Go," we get the generator dialog. Pressing the button "Generate," we finally get what we want! For C++ and ATL generators, we can define some post-build steps:
After generation and compilation are done, we should see the following directory structure:
The structure shows a "global include folder" that contains all of the include files needed to integrate the library into our applications. Obviously, include files are not needed for ATL components. The structure shows a "global lib folder" that contains all the *.lib and *.dll files generated by the build process.
To integrate the generated library into our applications, we need to follow these steps:
The following code shows how to use the classes:
//
// C++ sample
//
Cfg_Root root;
Cfg_CfgManagerDlg dlg;
UINT ret;
ret = root.LoadFromXmlFile("mySettings.xml");
if (!ret)
{
// file not found: set defaults
//
root.SetMember1("default value");
root.SetMember2(10000);
root.SetMember3(true);
}
dlg.SetData(root);
ret = dlg.DoModal(); // display modal dialog
if (ret == IDOK) {
root = dlg.GetData();
root.SaveToXmlFile("mySettings.xml");
}
// embedded dialog sample
//
BOOL CMyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
Cfg_Root root;
Cfg_CfgManagerDlg dlg;
UINT ret;
ret = root.LoadFromXmlFile("mySettings.xml");
dlg_.CreateAndEmbed(this, CRect(0, 0, 100, 100));
// dlg_ should be declared as class member
dlg_.SetData(root);
}
From version 1.2b, every single class dialog can be embedded in your application. Every dialog (generated for classes and their container) has a method: CreateAndEmbed. To embed a dialog in your dialogs, you have just to:
OnInitDialog handler, call the CreateAndEmbed method and pass to the embedded dialog the variable you want to edit using the method SetData. OnOK() event handler), call StoreData on the embedded dialog. //
// C++ sample (how to embed a single class)
//
// header file
//
Cfg_SampleClassDlg dlg_;
Cfg_SampleClass data_;
// implementation file
//
BOOL CMyClass::OnInitDialog()
{
CDialog::OnInitDialog();
dlg_.CreateAndEmbed(this, CRect(0, 0, 200, 200));
dlg_.SetData(&data_);
}
void CMyClass::OnOK()
{
dlg_.StoreData(); // this updates data_
data_.SaveToXmlFile(...) // if you need to store data_
}
You can get something like this (sorry... it's in Italian):
To integrate the ActiveX component with Visual Basic, you have just to select the menu "Project >> References ..." and choose library "CM_ConfigBuilder Sample2 Type Library 1.0":
The following code shows how to use the classes from VB:
' vb sample
'
Dim dlg As New CFGSAMPLE_Lib.MainDialog
Dim cfg As New CFGSAMPLE_Lib.Root
Dim ret As Boolean
ret = cfg.Load("mySettings.xml")
If Not ret Then
cfg.Member1 = "default value"
cfg.Member2 = 10000
cfg.Member3 = True
End If
ret = dlg.Display(cfg)
If ret Then
cfg.Save "mySettings.xml"
End If
Many resources used to create CM_ConfigBuilder came from CodeProject. I'd like to thank:
MFCGrid component. CEnTabControl. CAMSEdit validating class. CTreePropSheet. CPathSplit. CStringUtils. CXSBrowseFolder. CCoolButton. CVMBitmap. cmTreeCtrl: faster icon drawing InfoArea ShowInfoArea cmOptionList. cmTable. cmString derived from CString). You can contact me by leaving a message in the discussion area below. For any problems, do not hesitate to send me your project and a brief description of the problem. I'll reply ASAP!
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 12 Feb 2008 Editor: Sean Ewington |
Copyright 2004 by Stefano Basili Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |