Changing the title of Compartments using code

Tags: , , ,
Add comments

When using CompartmentShapes in your DSL you can define the text displayed in the head of each compartment in the DSL designer (there is a property Title for every compartment):

compartmentheadertext

Unfortunately I didn’t find any way to change this text from my source code or by binding it to some DomainProperty. I’m using an abstract base class with the embedded fields that are displayed in this compartment and I have some concrete classes inheriting from the base class. The shapes for all those concrete classes should look nearly the same; the only difference is the compartment header text. I think it is not a good idea to model a separate shape for every concrete class only for having different header texts.

model

After a little bit of research I found an extension point to achieve my aim. The CompartmentShape class contains a GetCompartmentDescriptions() method. To override this method you have to create the CompartmentShape as Double Derived and create a partial class.

I think you should take a look at the implementation of this method in the generated class. Indeed this method is defined in the CompartmentShape class, but it caches the value in a static field. So all shapes (within the same type) share the same CompartmentDescription. Now you can change it before returning it in this method. Just alter the text or do some other funny things like coloring or setting font types.

public override CompartmentDescription[] GetCompartmentDescriptions()
{
    CompartmentDescription[] descriptions;
    descriptions = base.GetCompartmentDescriptions();

    // I know there is only one CompartmentDescription
    // but maybe you should check it first
    CompartmentDescription desc = descriptions[0];

    if (this.ModelElement is ClassA)
        desc.Title = "Fields of A";

    if (this.ModelElement is ClassB)
    {
        desc.Title = "Fields of B";
        desc.TitleFontSettings = new FontSettings {Bold=true};
        desc.CompartmentFillColor = Color.LightBlue;
    }

    return descriptions;
}
shapes 

However there is a little drawback: This method is called only once (at least it seems so) when a shape is created (or the diagram is loaded). After that you cannot change the compartment description. For my problem it doesn’t make any difference. Maybe you can call some method to invalidate the shape and force it to reevaluate the method, but I’m not sure. If somebody knows some working solution for this, please leave a comment.

Comments are closed.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in