Enterprise Architect scripting with java - add CustomProperty

拥有回忆 提交于 2020-02-02 06:26:06

问题


I want to add a legend to my activity diagram, which will be generate programatically by java and the ea-api. I already know how to create the legend element and show it in the diagram (Type: "Text" und Subtype: 76):

Element legend = elements.AddNew("Color Legend", "Text");
elements.Refresh();

legend.SetSubtype(76);
legend.Update();

//Show in diagram
DiagramObject diagramObject = diagramObjects.AddNew("l=0; r=100; t=0; b=-100;", "");
diagramObjects.Refresh();
// reference the DiagramObject to the before created element
diagramObject.SetElementID(legend.GetElementID());

But this is only an empty legend. So my question is, how to add an CustomProperty to the CustomProperties. My first approach was the following code:

Collection<CustomProperty> customProperties = legend.GetCustomProperties();
CustomProperty cp = customProperties.AddNew("LegendEntryTest", "Back_Ground_Color=2124031;");
customProperties.Refresh();
legend.Update();

But this don't work, the legend is still empty:(

Here is an example-legend:

Regards, Phil

EDIT With the help of Geert Bellekens I had solved my problem. Now I use the repository.Execute(String sqlStmt) method to insert the custom property in the t_xref. The following code is a small example how it work's:

//get elementGUID of legend
String legendGUID = legend.GetElementGUID();

//create the description value for one custom_property
String name="TestColor1";
String color="3381504";
int customPropertyIndex = 0;

String description = "@PROP=@NAME="+name+"@ENDNAME;@TYPE=LEGEND_OBJECTSTYLE@ENDTYPE;@VALU=#Back_Ground_Color#="+color+";#Pen_Color#=16777215;#Pen_Size#=1;#Legend_Type#=LEGEND_OBJECTSTYLE;@ENDVALU;@PRMT="+customPropertyIndex +"@ENDPRMT;@ENDPROP;"

//add description part for the legend
description += "@PROP=@NAME=Legend@ENDNAME;@TYPE=LEGEND_STYLE_SETTINGS@ENDTYPE;@VALU=@ENDVALU;@PRMT=@ENDPRMT;@ENDPROP;"

String sqlInsertStmt="INSERT INTO t_xref "
            + "(" 
                + "Client,"
                + "XrefID,"
                + "Type,"
                + "Name,"
                + "Visibility,"
                + "Partition,"
                + "Supplier,"
                + "Description"
            + ") "
            + " VALUES ("
                +"'"+legendGUID+ "',"
                + "'{"+UUID.randomUUID().toString()+"}',"
                + "'element property',"
                + "'CustomProperties',"
                + "'Public',"
                + "'0',"
                + "'&lt;none&gt;',"
                + "'"+description+"'"
            + ");"
                ;

repository.Execute(sqlInsertStmt);

With java.util.UUID I generate a new GUID for the field XrefID.

By the way: To convert a RGB-color to that which will accept by Enterprise Architect you can use the following formular:

 int colorValue = color.getRed() + (color.getGreen() * 256)
            + (color.getBlue() * 256 * 256);

(RGB Color Model)


回答1:


You can try to add the custom property using code, but I'm pretty sure you'll have to resort to an SQL hack to be able to fill in all the required details. If you check the database you'll find that a all the custom properties of your legend are stored in a single row in the table t_xref. The column Client contains the t_object.ea_guid and the description column contains all the details of your custom properties. I did a small test and this is what was stored in the description (I added newlines for the readability)

@PROP=@NAME=Wit@ENDNAME;@TYPE=LEGEND_OBJECTSTYLE@ENDTYPE;@VALU=#Back_Ground_Color#=16777215;#Pen_Color#=16777215;#Pen_Size#=1;#Legend_Type#=LEGEND_OBJECTSTYLE;@ENDVALU;@PRMT=0@ENDPRMT;@ENDPROP;
@PROP=@NAME=rood@ENDNAME;@TYPE=LEGEND_OBJECTSTYLE@ENDTYPE;@VALU=#Back_Ground_Color#=255;#Pen_Color#=255;#Pen_Size#=1;#Legend_Type#=LEGEND_OBJECTSTYLE;@ENDVALU;@PRMT=1@ENDPRMT;@ENDPROP;
@PROP=@NAME=blauw@ENDNAME;@TYPE=LEGEND_OBJECTSTYLE@ENDTYPE;@VALU=#Back_Ground_Color#=16711680;#Pen_Color#=16711680;#Pen_Size#=1;#Legend_Type#=LEGEND_OBJECTSTYLE;@ENDVALU;@PRMT=2@ENDPRMT;@ENDPROP;
@PROP=@NAME=Legend@ENDNAME;@TYPE=LEGEND_STYLE_SETTINGS@ENDTYPE;@VALU=@ENDVALU;@PRMT=@ENDPRMT;@ENDPROP;

If I where you I would use Repository.Execute() with a dirty SQL insert statement to get the job done.



来源:https://stackoverflow.com/questions/26280146/enterprise-architect-scripting-with-java-add-customproperty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!