Posts Tagged ‘Autoforms’

ECO : Autoforms and Form Factories

I’ve been in New Zealand this week doing some Borland Developer Day’s events. Mornings we’ve been focusing on ECO, afternoons we’ve been focusing on the new MDA features in the next version of Together.

Anyway, during the ECO session in Wellington, I was talking about the AutoForms capabilities, and how you could register your own AutoForms for specific types. This is a really useful feature, as you can use the default AutoForm during early parts of your development, so that you can focus on getting the model developed, and then when ready, you can create custom forms to be used to view and edit your objects. Registering them with the AutoForm service means that you don’t have to change any of the code in your app that display’s your AutoForm, it will automatically use the new form you have registered.

After the session, one of the guys in the audience who’d been using ECO for awhile came up to me asked how to do this. To be honest, I thought I’d already blogged about this. Apparently not, so I had a few hours to kill in the Qantas lounge in Christchurch while I waited for my flight home, so I thought I’d write something up. You can grab the sample code here.

A few things first. If you haven’t seen the default AutoForm capabilities, download the source for this project and open it in Delphi 2005 Architect. Bring up Winform1, and note that I’ve set the ECOAutoForm property on the grdAllPeople DataGrid to True. Now I can simply double-click on a row in my grid, and an autoform will be created.  This is probably the simplest way to get ECO to display an AutoForm for an object.

NB: If you can’t see an ECOAutoForm property in the DataGrid’s on your form, you probably need to add an ECOAutoFormExtender component to the form. This will insert some ECO-specific properties into the DataGrid.

Also click on Button3, which is the button labeled "Show  Autoform" next to the upper grid. I’ve also set it to display an AutoForm for the currently selected Person in the grid. I’ve enabled this by setting it’s ECOListAction property to ShowAutoForm, and setting its BindingContext to my grdAllPeople grid, and its RootHandle property to the ExpressionHandle holding the collection of Person objects, in this case, ehAllPeople. This is probably the second simplest way to display an AutoForm for an object.

If you want to display an autoform for an ECO object via some other method (e.g., via a context menu, etc), then the following code (Which is also in the Click event of Button4 in the sample project) will achieve the desired result:

procedure TWinForm1.Button4_Click(sender: System.Object; e: System.EventArgs);
  autoContainer: IAutoContainer;
  if Assigned(cmhSelectedAppointment.Element) then
    autoContainer := AutoContainerService.Instance.CreateContainer(
    if Assigned(autoContainer) then

Firstly, we’re checking that an Appointment object has been selected in our grid, by seeing if the Element attribute of our CurrencyManagerHandle is assigned. Then, we’re using the AutoContainerService’s CreateContainer method to get back a reference to an IAutoContainer for the selected Appointment. Lastly, provided we got back a reference, we’re casting it as a Form and calling show to display it.

Running the project should show that clicking the button to invoke this code has the same result as double clicking in the grid.

Personally any time I want to display a form in my ECO apps, I use this code. Yes, it’s longer than just creating an instance of a custom form and assigning the ECOSpace and the ECO object you want to edit. However, bear with me for a minute and hopefully you’ll see the advantage.

Open the fAppointmentAutoForm unit and have a look at the TAppointmentForm it contains. I designed this form like I would design any other ECO Winforms. File | New | Other…ECO Enabled Windows Form, drop my controls down. set the ReferenceHandle’s ECOSpaceType and StaticValueTypeName properties, set up my databinding, etc. To convert this form into an ECO AutoForm, I have to do a few things:

1. Extend your form class so that it implements the IAutoContainer interface. You’ll need to add the Borland.Eco.AutoContainers and the Borland.Eco.ObjectRepresentation namespaces to your interface uses clause. Use Ctrl-Space from within your class declaration to declare the BuildControls, HookUpGUI and set_ECOSpace methods, and extend the declaration of your EcoSpace property so that has a setter method (the aforementioned set_EcoSpace).

2. Ctrl-Shift-C from inside the form classes declaration to stub out the implementations of these new methods. The code within them looks like the following:

procedure TAppointmentForm.set_EcoSpace(value: EcoSpace);
  if value <> FEcoSpace then
    FEcoSpace := value;
procedure TAppointmentForm.HookUpGUI(ecoSpace: EcoSpace; element: IElement);
  FEcoSpace := ecoSpace;
  RHRoot.EcoSpace := self.EcoSpace;
procedure TAppointmentForm.BuildControls(element: IElement;
autoContainerArgs: AutoContainerArgs);
// don’t need to do anything, unless you intend writing a generic
// autoform

Most of these should be fairly self explanatory. The BuildControls method is empty, however if you wanted to dynamically create controls on your AutoForm, this is where you’d do it.

3. So, now our AutoForm is complete, however we need to register it with the AutoFormService. To do ths, we define a simple Factory class. In the same unit, you’ll see this under my form declaration. Its delcaration looks like this:

AppointmentAutoContainerFactory = class(System.Object, IAutoContainerFactory)
  function get_AutoContainer: IAutoContainer;
  function Matches(modelElement: IModelElement): Boolean;

Its implementation looks like this:

function AppointmentAutoContainerFactory.get_AutoContainer: IAutoContainer;
  Result := TAppointmentForm.Create(nil);
function AppointmentAutoContainerFactory.Matches(modelElement: IModelElement): Boolean;
  Result := modelElement.Name.Equals(Appointment.ClassName);

You may need to add the Borland.Eco.UmlRt namespace to your Interface uses clause. The get_AutoContainer method is where we actually create an instance of our form. The Matches method will be called by the ECO Framework, passing in a reference to the model element for which it is trying to create an AutoForm. Your job in this method is to decide if your form will handle this class, in this case I’m checking whether it represents an Appointment, and if so returning true.

4. Lastly, I need to register my Factory with the AutoForm service, by adding the following code to the initialization section of the unit:


That’s it. Now when you run your app, you should notice that when you request an autoform for an Appointment object you’ll get back our new form. This is regardless of how you request it (i.e.. double clicking in your grid, using the ECOListAction on your button, using the AutoForm code, ECOSpaceDebugger etc). You don’t need to change any of your forms, you can make the change in one place and the rest of your app will follow. Way cool!

In the sample code, the initialization section code is commented out, so you can easily see the before and after effect.


Technorati Tags: Delphi,