Add Custom Compartments to Element

When you display an element on a diagram in normal, rectangular format, it is possible to show a number of compartments within that frame to reveal added characteristics such as Attributes, Operations and Notes, using the diagram Properties and element Feature and Compartment Visibility dialogs. If you want to reveal other added characteristics, such as related elements or Ports and Parts, you can use a Shape Script to add custom compartments to the diagram display of the element. You would usually add this Shape Script to a Stereotype element in a Profile.

Having created a custom compartment, you can add a linked Note to the element to display the content of the compartment, as you can for the other features of the element.

Access    Profile Stereotype element: ( F9 ) > General | Initial Value: browse    or
Settings | UML Types > Stereotypes (specify stereotype): Shape Script, Assign

Add custom compartments to elements

Process

Description

See also

Develop script

For the selected stereotype, open the Shape Editor.

In the script, replace shape main  with:

shape ChildElement or
shape RelatedElement
 

You can keep shape main if you prefer, to adjust some properties of the main element (such as color); however, the main shape then requires a call to DrawNativeShape() in order to work correctly.

At this point, you can use the HasProperty query method to search child or related elements for specific properties (such as stereotypes) to be displayed in compartments. A Related Element Shape Script determines properties of elements that are linked to the current element via connectors.

Examples of Shape Scripts that define custom compartments are provided below.

 

Shape Editor

 

Drawing Methods

Query Methods

Display Element/Connector Properties

 

Attach Linked Note

You can use one of two methods to create a linked Note to display custom compartment contents:

Method 1 (the element is currently displaying custom compartments) - highlight the related or child element name in the custom compartment, then right-click on it and select the Create Linked Note menu option; the custom compartment is automatically closed, and the linked Note added to the diagram listing all element names in that compartment
 
Method 2 (the element is not necessarily showing custom compartments) - drag a Note element from the Common page of the Diagram Toolbox and link it to the element containing the custom compartment with a Notelink connector
 
Right-click on the connector and select the Link this note to an element feature menu option, to display the Link note to element feature dialog; click on the drop-down arrow in the Feature Type field and click on the name of the custom compartment, such as Properties, then click on the OK button
 
The contents of that compartment are displayed in the Note
 

In Method 2, if the compartment is displayed the method will NOT hide the compartment. It is recommended that you use this method if the compartment is already hidden.

Any changes you make to the list of elements in the compartment, or their names, are immediately reflected in the Note to maintain the accuracy of the displayed information.

 

 

Script Example 1: Add compartment without adjusting the parent element

 

  //Add compartments for Child elements.

  shape ChildElement

  {

       //Check if a child element has the property stereotype, if so set

       //the compartment name to Properties.

        if(HasProperty("stereotype", "property"))

        {

                SetCompartmentName("Properties");

        }

 

       //Check if the child element has a public scope and if so add the + 

       //symbol to the child compartment.

        if(HasProperty("scope", "public"))

        {

                AppendCompartmentText("+");

        }

 

       //Add the child elements name to the child compartment.

        AppendCompartmentText("#NAME#");

  }

 

The Shape Script checks all child elements to see if they have a stereotype of property. If this stereotype is found, the SetCompartmentName function sets a compartment called Properties.

The script then checks whether the child element has a public scope and, if it does, appends the + symbol.

Finally, the AppendCompartmentText function adds the child element's name to the compartment.

If a compartment has already been declared by SetCompartmentName, any additional child elements that fall under the same compartment are automatically added to it without having to declare a new compartment name (that is, all child elements with the stereotype property end up in the Properties compartment).

 

 

Script Example 2: Adjust the color of the parent element and add child compartments

 

  //Shape main affects the parent

  shape main

  {

        //Set the color of the parent element to red

        setfillcolor(255,0,0);

        //draw the parents native shape

        drawnativeshape();

  }

 

  //Shape ChildElement adds Child Compartments to the parent.

  shape ChildElement

  {

         if(HasProperty("stereotype", "part"))

         {

                SetCompartmentName("Parts");

          }

         else if(HasProperty("stereotype", "mystereotype"))

         {

               SetCompartmentName("My Stereotype");

          }

 

          AppendCompartmentText("#NAME#");

  }

 

The shape main  section sets the color of the main element to red and adds child compartments based upon stereotyped child elements.

The script checks whether a child element has either the stereotype value "part" or "mystereotype" applied to it.  If there are multiple child elements, having a combination of "part" and "mystereotype" stereotypes, two compartments are created called Parts and My Stereotype.

In order to display the compartments, AppendCompartmentText must be called to insert content into the compartment.

Values passed to SetCompartmentName and AppendCompartmentText can not contain new line characters.

 

 

Script Example 3: Only list child element in compartment if it is not already visible on the diagram

 

  shape ChildElement

  {

 //Check if the child element is on the diagram or not.

 if(hasproperty("IsVisible", "false"))

 {

         //Create a compartment for parts.

         if(hasproperty("type", "part"))

         {

                 SetCompartmentName("Parts");

         }

         //Create a compartment for ports.

         else if(hasproperty("type", "port"))

         {

                 SetCompartmentName("Ports");

         }

 

         //Add child element name to compartment.

         AppendCompartmentText("#NAME#");

 }

 

  }

 

This script adds custom compartments for Port and Part elements that belong to the current element but that are not visible on the current diagram.

The IsVisible property returns true if the child element is already visible on the diagram, false if the child element is not visible.

This can be used to prevent the child element from being listed in the custom compartment if it is already visible on the diagram, avoiding display of redundant information.

 

 

Script Example 4: Display  elements that are the target of a Dependency connector from the element that owns the shape script

 

  shape RelatedElement

  {

 //Check if the current connector we are processing has a

 //dependency type.

 if(HasProperty("Connector.Type", "Dependency"))

 {

         //Check if the element we are currently checking is

         //the target of the current connector.

         if(HasProperty("Element.IsTarget"))

         {

                 //Set the compartment Name

                 SetCompartmentName("dependsOn");

                 if(HasProperty("Element.Stereotype", ""))

                 {

                 }

                 else

                 {

                 AppendCompartmentText("«#Element.Stereotype#» ");

                 }

                 AppendCompartmentText("#Element.Name#");

         }

 }

  }

 

With this script, if a Class1 has a stereotype with the 'RelatedElement' Shape Script and Class1 is the source of a Dependency connector to the target Class2, then the name Class2 is displayed in a compartment of Class 1, called dependsOn.

 

 

Script Example 5: Display a list of Realized Interfaces within a compartment on an element

 

  shape RelatedElement

  {

 //Check if the current connector being processed is a Realization

 if(HasProperty("Connector.Type", "Realization"))

 {

         //Only display this compartment if the related element we 

         //are checking is the target of the connector that has this

         //shape scripts element as the source

         if(HasProperty("Element.IsTarget"))

         {

                 //If the element is an interface, display it in

                 //'realizedInterfaces' compartment

                 if(HasProperty("Element.Type", "Interface"))

                 {

                         SetCompartmentName("realizedInterfaces");

                         AppendCompartmentText("#Element.Name#");

                 }

         }

 }

  }

 

If an element Class 1 has this Shape Script and is the source of a Realization connector to an element Interface 1, the name Interface 1 is displayed in the realizedInterfaces compartment of Class 1.

 

 

Notes

If you use punctuation within a compartment name, it is stripped out when the script is saved; for example, Ports, Parts and Properties becomes Ports Parts and Properties
Visibility of each individual custom compartment defined by a shape script can be controlled using the Feature and Compartment Visibility dialog
The 'RelatedElement' Shape Scripts have extended capabilities to check both a connector and the element on the other end of the connector; they are applied only to an element and are solely used to retrieve information to be displayed within a compartment of that element

Learn more