I have a class extending ShapeRenderer and overriding the draw class so that I can create a custom shape for specific model objects. I am trying to access adornments associated with a given model element so that the custom shape being generated can be affected by legends. The snippet I created below should work but relies on importing the internal com.nomagic.magicdraw.adornment.Adornment and com.nomagic.magicdraw.adornment.AdornmentAnnotation packages which is not optimal. Is there a more appropriate way to access legend adornment information that uses the public API?
@Override
public void draw(Graphics g, PresentationElement presentationElement) {
/* Draw normal elements with standard renderer */
if (!presentationElement.getElement().getHumanName().equals("TestShape")) {
super.draw(g, presentationElement);
return;
}
Color penColor = Color.BLACK; // default
/* Calculate Pen Color from properties */
Property property = presentationElement.getPropertyManager().getProperty(PropertyID.PEN_COLOR);
if (property != null && property.getValue() instanceof Color) {
penColor = (Color) property.getValue();
}
/* Calculate Pen Color from annotations */
Project currProject = Application.getInstance().getProject();
AnnotationManager am = AnnotationManager.getInstance(currProject);
// Collect all annotations associated with current element
List annotations = new ArrayList<>();
for (Object target : am.getAnnotatedTargets(AnnotationSubset.ALL)) {
if (target instanceof DiagramAnnotationTarget) {
DiagramAnnotationTarget dat = (DiagramAnnotationTarget) target;
if (dat.getElement().equals(presentationElement.getElement())) {
for (Annotation annotation: am.getAnnotations(dat)) {
if (annotation instanceof AdornmentAnnotation) {
annotations.add((AdornmentAnnotation) annotation);
}
}
}
}
}
// Sort annotations by priority
annotations.sort((a1, a2) -> Integer.compare(a1.getPriority(), a2.getPriority()));
for (AdornmentAnnotation annotation : annotations) {
if (annotation.getAdornment().getPenColor() != null) {
penColor = annotation.getAdornment().getPenColor();
}
}
/* Draw custom shape */
Rectangle bounds = presentationElement.getBounds();
int diameter = Math.min(bounds.width, bounds.height);
g.setColor(penColor);
g.drawOval(bounds.x, bounds.y, diameter, diameter);
} 