I have this block of code that read my project, iterate through every element in a package. I do a check:
if the element is an instanceof IClass, then I draw the class. I was able to draw all the classes.
If the element is an instanceof IAssociation, the draw the association. But I wasn’t able to draw associations. Can anyone help? Thanks.
//
public void createClassDiagram(DiagramManager manager,
IModelElementFactory factory) {
// create and open a new class diagram
IClassDiagramUIModel diagram = (IClassDiagramUIModel) manager
.createDiagram(DiagramManager.DIAGRAM_TYPE_CLASS_DIAGRAM);
manager.openDiagram(diagram);
// create a system boundary and the corresponding UI model
ISystem system = factory.createSystem();
system.setName("System 1");
IShapeUIModel systemShape = ((IShapeUIModel) manager
.createDiagramElement(diagram, system));
systemShape.setBounds(0, 0, 700, 700);
// load project
IProject project = ApplicationManager.instance().getProjectManager()
.getProject();
Iterator iter1 = project
.modelElementIterator(IModelElementFactory.MODEL_TYPE_PACKAGE);
while (iter1.hasNext()) {
IPackage obj1 = (IPackage) iter1.next();
Iterator iter2 = obj1.childIterator();
while (iter2.hasNext()) {
Object obj = iter2.next();
if (obj instanceof IClass) {
IClass theClass = (IClass) obj;
system.addChild(theClass);
IShapeUIModel classShape = ((IShapeUIModel) manager
.createDiagramElement(diagram, theClass));
classShape.setBounds(10, 10, 10, 10);
classShape.setLocation(20, 20);
} else if (obj instanceof IAssociation) {
IAssociation theAssociation = (IAssociation) obj;
system.addChild(theAssociation);
IShapeUIModel associationShape = ((IShapeUIModel) manager
.createDiagramElement(diagram, theAssociation));
associationShape.setBounds(10, 10, 10, 10);
associationShape.setLocation(20, 20);
}
}
}
}