Open API provides the com.nomagic.magicdraw.elementreferenceintext.ElementReferencingInTexts class for creating text with element references, setting, and getting text from elements.

Examples

 You can find the code examples in <modeling_tool_installation_directory>\openapi\examples\elementreferencingintext.

Creating text that refers an element

To create text that refers an element, use:

//creates a text that references an element with default options
ElementReferencingInTexts.createReferencingText(element, "Link");

//creates a text that is updated automatically and is displayed using its representation text. 
//This means the elements representation text will be displayed rather than "Link"
ElementReferencingInTexts.createReferencingText(element, "Link", DisplayMode.REPRESENTATION_TEXT, 
UpdateMode.AUTOMATIC_UPDATE);

//create a referencing text with a provided display mode and with a default update mode
ElementReferencingInTexts.createReferencingText(element, "Link", DisplayMode.REPRESENTATION_TEXT);

Setting text with element references

Some element properties support text with references, e.g., a comment body, constraint specification, opaque behavior body, etc. In order to set the text with element references, you must convert it using com.nomagic.text.TextUtils.toLightHtml(java.lang.String).

Setting text for comment body
//create a text with an element reference
String constructedText = "This text refers an element: " + createReferencingText(elementToRefer);
//convert it to a light html. Light html can be used in a comment body, constraint specification, etc. to show th etext containing references
String textForCommentBody = TextUtils.toLightHtml(constructedText);

Project project = Project.getProject(comment);
SessionManager.getInstance().createSession(project, "Set comment text with reference");
comment.setBody(textForCommentBody);
SessionManager.getInstance().closeSession(project);