-
Notifications
You must be signed in to change notification settings - Fork 20
CreatingAndModifyingElements
In this chapter, we're goint to learn how to create elements and modify elements.
The most important is that you should not create or modify elements directly like var class1 = new UMLClass()
or class1.name = "New Name"
because all changes should be supported by undo and redo.
To create a model element, use Factory module.
var Factory = app.getModule("engine/Factory");
Call createModel
function of Factory to create a model element with the following four parameters:
-
id
: ID of Factory function. To see the full ID list, executeFactory.getModelIds()
. -
parent
: A parent element where the created element to be contained. -
field
(optional) : Field name of the parent element (default isownedElements
). -
options
(optional) : An object containing the below options.
-
modelInitializer
: A function to initialize the created model element.
// Get a reference to top-level project
var Repository = app.getModule("core/Repository");
var project = Repository.select("@Project")[0];
// Create a UMLModel element as a child of project
var model1 = Factory.createModel("UMLModel", project);
// Create a UMLClass element as a child of the model
var class1 = Factory.createModel("UMLClass", model1);
// Create a UMLAttribute element and add to the field 'attributes' of the class
var attr1 = Factory.createModel("UMLAttribute", class1, 'attributes');
// Create a UMLClass with options
var options = {
modelInitializer: function (elem) {
elem.name = "MyClass";
elem.isAbstract = true;
}
};
var class2 = Factory.createModel("UMLClass", model1, 'ownedElements', options);
You can see the created elements in Explorer and undo and redo are available for each creation.
Call createDiagram
function of Factory to create a diagram with the following three parameters:
-
id
: ID of Factory function. To see the full ID list, executeFactory.getDiagramIds()
. -
parent
: A parent element where the created diagram to be contained. -
options
(optional) : An object containing the below options.
-
diagramInitializer
: A function to initialize the created diagram.
// Get a reference to top-level project
var Repository = app.getModule("core/Repository");
var project = Repository.select("@Project")[0];
// Create a UMLModel element as a child of project
var model1 = Factory.createModel("UMLModel", project);
// Create a UMLClassDiagram as a child of the model
var diagram1 = Factory.createDiagram("UMLClassDiagram", model1);
// Create a UMLClassDiagram with options
var options = {
diagramInitializer: function (dgm) {
dgm.name = "MyDiagram";
dgm.defaultDiagram = true;
}
};
var diagram2 = Factory.createDiagram("UMLClassDiagram", model1, options);
Call createModelAndView
function of Factory to create a diagram with the following four parameters:
-
id
: ID of Factory function. To see the full ID list, executeFactory.getModelAndViewIds()
. -
parent
: A parent element where the created model element to be contained. -
diagram
: A diagram element where the created view element to be contained. -
options
(optional) : An object containing the below options.
-
modelInitializer
: A function to initialize the created model element. -
viewInitializer
: A function to initialize the created view element. -
x1
,y1
,x2
,y2
: Rectangle coordinate to initialize position and size of the created view element. -
tailView
,headView
: If you try to create a relationship (e.g.UMLAssociation
), the created view element connects these two view elementstailView
andheadView
. -
tailModel
, andheadModel
: If you try to create a relationship, the created model element connects these two model elementstailModel
andheadModel
. -
containerView
: A view element where the created view element to be contained.
The function createModelAndView
returns the created view element, so you need to get the create model element by accessing model
field. (e.g. classView1.model
). Following code will create two classes and a association connecting the two classes.
// Create a UMLClass and UMLClassView
var options1 = {
x1: 100,
y1: 100,
x2: 200,
y2: 200
};
var classView1 = Factory.createModelAndView("UMLClass", diagram1._parent, diagram1, options1);
// Create another UMLClass and UMLClassView
var options2 = {
x1: 400,
y1: 100,
x2: 500,
y2: 200
};
var classView2 = Factory.createModelAndView("UMLClass", diagram1._parent, diagram1, options2);
// Create an association connecting the two classes
var options3 = {
tailView: classView1,
headView: classView2,
tailModel: classView1.model,
headModel: classView2.model
};
var assoView = Factory.createModelAndView("UMLAssociation", diagram1._parent, diagram1, options3);
Call createViewOf
function of Factory to create a view element of an existing model element the following three parameters:
-
model
: A model element referenced by the created view element. -
diagram
: A diagram element where the created view element to be contained. -
options
(optional) : An object containing the below options.
-
viewInitializer
: A function to initialize the created view element. -
x
,y
: Position of the created view element. -
containerView
: A view element where the created view element to be contained.
var options = {
x: 500,
y: 500,
};
Factory.createViewOf(classView1.model, diagram1, options);
You will see the one more class view element at (500, 500).