Pré. | Proc. |
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 '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
Define a Stereotype element in a Profile, and use the special attribute '_image' to specify a Shape Script that adds custom compartments.
Ribbon |
Design > Element > Features > Attributes : [create an attribute named '_image'] > click on the icon in the 'Initial Value' field Settings > Reference Data > UML Types > Stereotypes : (select or specify stereotype) : Shape Script > Assign |
Context Menu |
In diagram, right-click on element | Features | Attributes : [create an attribute named '_image'] | click on in the 'Initial Value' field |
Keyboard Shortcuts |
: [create an attribute named '_image'] > click on the icon in the 'Initial Value' field |
Add custom compartments to elements
This table provides notes on creating Shape Scripts that define custom compartments, and a variety of examples.
Process |
Description |
See also |
---|---|---|
Develop script |
For the selected stereotype, open the Shape Editor. In the script, replace shape main with:
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 RelatedElement Shape Script determines properties of elements that are linked to the current element via connectors. Visibility of each individual custom compartment defined by a Shape Script is controlled using the 'Compartment Visibility' dialog. ChildElement compartments are visible by default and can be hidden using the compartment visibility options, whilst RelatedElement compartments are hidden by default and must be explicitly enabled using the compartment visibility options. Be aware, also, that child elements can be shown in a custom compartment either when they are on the diagram with the parent element or when they are not on the diagram (as in examples 1, 2 and 3). Related elements can only be listed in a compartment if they are NOT on the same diagram (as in examples 4 and 5). |
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:
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 Script 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'
- 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