There are two ways to add/move/delete model elements:
- Use com.nomagic.magicdraw.openapi.uml.ModelElementsManager
- Call direct functions on Elements
The ModelElementsManager is the singleton utility class which checks com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Element editing permissions and the session existence before executing the function. Also, it performs a check, if an element can be added to a parent. If the ModelElementsManager is not used, a programmer must perform these checks in the code explicitly.
Adding element to owner with ModelElementsManager
For adding a new model Element into a model, use the addElement(Element, Element) method provided by the ModelElementsManager.
Project project = ...; Class classA = ...; Package package = ...; // create a new session SessionManager.getInstance().createSession(project, "Add class into package"); try { // add a class into a package ModelElementsManager.getInstance().addElement(classA, package); } catch (ReadOnlyElementException e) { } // apply changes and add a command into the command history. SessionManager.getInstance().closeSession(project);
Adding element to owner using a direct function
Element parent = ...; Class classA = ...; Project project = ...; // create a new session SessionManager.getInstance().createSession(project, "Add class into parent"); if (parent.canAdd(classA)) { classA.setOwner(parent); } // apply changes and add a command into the command history. SessionManager.getInstance().closeSession(project);
Moving element with ModelElementsManager
For moving an existing model Element, use the moveElement(Element, Element) method provided by the ModelElementsManager.
Project project = ...; Class classA = ...; Package package = ...; // create a new session SessionManager.getInstance().createSession(project, "Add class into package"); try { // add a class into a package ModelElementsManager.getInstance().moveElement(classA, package); } catch (ReadOnlyElementException e) { } // apply changes and add a command into the command history. SessionManager.getInstance().closeSession(project);
If a given model element cannot be moved into a given parent, java.lang.IllegalArgumentExceptionis thrown. For example, an Operation cannot be moved into a Package or an Operation cannot be moved into a not locked for editing Class (in the server project). If an element or parent is null, java.lang.IllegalArgumentException also is thrown. If a given element is not editable (read-only), com.nomagic.magicdraw.openapi.uml.ReadOnlyElementException is thrown.
Moving element using a direct function
Element parent = ...; Class classA = ...; Project project = ...; // create a new session SessionManager.getInstance().createSession(project, "Add class into parent"); if (com.nomagic.uml2.ext.jmi.helpers.ModelHelper.canMoveChildInto(parent, classA)) { classA.setOwner(parent); } // apply changes and add a command into the command history. SessionManager.getInstance().closeSession(project);
Removing element with ModelElementsManager
Class classA = ...; Project project = ...; // create a new session SessionManager.getInstance().createSession(project, "Remove class"); try { // remove a class ModelElementsManager.getInstance().removeElement(classA); } catch (ReadOnlyElementException e) { } // apply changes and add a command into the command history. SessionManager.getInstance().closeSession(project);
Removing element using direct call
Class classA = ...; Project project = ...; // create a new session SessionManager.getInstance().createSession(project, "Remove class"); if (classA.isEditable())) { classA.dispose(); } // apply changes and add a command into the command history. SessionManager.getInstance().closeSession(project);
You can find the code examples in <modeling tool installation directory>\openapi\examples\hierarchyremover