Custom diagram painters

The Open API provides a way to add your own custom diagram painters for painting some additional stuff on the diagram canvas. A good sample would be some highlighting in the diagram.

The painter can be added only into the opened diagram's com.nomagic.magicdraw.uml.symbols.DiagramSurface. Only the opened diagram has DiagramSurface. A closed diagram returns null.

Example of the code:

Application.getInstance().addProjectEventListener(new ProjectEventListenerAdapter()
{
public void projectOpened(Project project)
{
project.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent evt)
{
if(evt.getPropertyName().equals(Project.DIAGRAM_OPENED))
{
DiagramPresentationElement diagram = Application.getInstance().getProject().getActiveDiagram();
diagram.getDiagramSurface().addPainter(new DiagramSurfacePainter()
{
public void paint(Graphics g, DiagramPresentationElement diagram)
{
g.setColor(Color.BLUE);
List symbols = diagram.getPresentationElements();
for (int i = 0; i < symbols.size(); i++)
{
PresentationElement o = (PresentationElement)symbols.get(i);
if( o instanceof ShapeElement)
{
Rectangle bounds = o.getBounds();
bounds.grow(5,5);
((Graphics2D)g).draw(bounds);
}
 
}
 
};
 
});
 
}
 
}
 
});
 
}
 
});
    

You can find the code examples in <programinstallation directory>\openapi\examples\customdiagrampainter