Edit Models and Presentations

In order to edit models or presentations using API, you need the transaction process. Also an exception will happen if:

  • The project is not registered in the project accessor or
  • target models/presentations you are trying to edit are non-editable

Transaction process

A transaction is a unit of update processing performed on a Astah project.

Use beginTransaction to start transaction.
Use endTransaction to commit the transaction.
This is one update process. undo / redo is done in this processing unit.

Use abortTransaction to cancel a transaction.
If you cancel a transaction, the project returns to the state it was in before the transaction started.
When any exception happened while working on the projects with API, you need to call abortTransaction method, then start again.

ITransactionManager transactionManager = projectAccessor.getTransactionManager();
try {

    transactionManager.beginTransaction();

    // editting

    transactionManager.endTransaction();

} catch (BadTransactionException | Exception e) {

    transactionManager.abortTransaction();

    // processing

}

Edit model

Model Editor

The Model Editor is to used when you edit model with API.
If you edit a model that can have multiple presentations, you can create/edit from each Model Editor.

Editting Model Viewpoint

There are some models that are automatically created when you create presentations. For details, please refer to [Get Diagrams and Presentations].

Model Editors Model elements you can create in the Model Editor
BasicModelEditor Class, Artifact, Association, Association Class, Attribute, Component, Constraint, Copy, Dependency, DeriveReqt, Generalization, Interface, Model, Node, Operation, Package, Parameter, Port, Qualifier, Realization, Refine dependency, Requirement, Satisfy dependency, Subsystem, Tagged value, Template binding, Template parameter, TestCase, Trace dependency, Usage, Verify dependency
UseCaseModelEditor Actor, Usecase, Extension point, Include, Extend
CompositeStructureModelEditor Connector, Realization, Usage
ERModelEditor ER model, ER entity, Domain, ER attribute, Data type, Index of ERAttribute, Identifying relationship, Multi-to-multi relationship, Non-identifying relationship, Subtype

Create a Class under package

// Transaction processing is required
public IClass createClass(ProjectAccessor projectAccessor, IModel parent, String name)
        throws InvalidEditingException {
    IModelEditorFactory modelEditorFactory = projectAccessor.getModelEditorFactory();
    BasicModelEditor basicModelEditor = modelEditorFactory.getBasicModelEditor();
    return basicModelEditor.createClass(parent, name);
}

Create TaggedValues

// Transaction processing is required
public void createTaggedValue(BasicModelEditor modelEditor, IElement element, String tag, String value)
        throws InvalidEditingException {
    modelEditor.createTaggedValue(element, tag, value);
}

Delete Models

// Transaction processing is required
public void deleteElement(BasicModelEditor basicModelEditor, IElement element)
        throws InvalidEditingException {
    basicModelEditor.delete(element);
}

Add Definition

This is an example to add Definition to a Class./

// Transaction processing is required
public void setDefinition(IClass clazz, String definition)
        throws InvalidEditingException {
    clazz.setDefinition(definition);
}

Add Stereotypes

// Transaction processing is required
public void addStereotype(IElement element, String stereotype) throws InvalidEditingException {
    element.addStereotype(stereotype);
}

Edit Presentations

Diagram Editor

You will use DiagramEditor to create, edit and delete presentations.

A diagram type needs to be set for DiagramEditor before you start editing.
When you create a diagram, the diagram type will be automatically set to DiagramEditor. But when you edit any existing diagrams, you need to call DiagramEditor#setDiagram(IDiagram diagram) and set the diagram type.

Editting Presentation Viewpoint

Diagram Editor Editable diagrams
ClassDiagramEditor Class Diagram
UseCaseDiagramEditor UseCase Diagram
StateMachineDiagramEditor StateMachine Diagram
ActivityDiagramEditor Activity Diagram, Flowchart
SequenceDiagramEditor Sequence Diagram
CompositeStructureDiagramEditor Composite Structure Diagram
RequirementDiagramEditor Requirement Diagram
ERDiagramEditor ER Diagram
MindmapEditor Mindmap

Create a Class diagram

// Transaction processing is required
public IClassDiagram createClassDiagram(ClassDiagramEditor editor, INamedElement owner,
        String name) throws InvalidEditingException {
    return editor.createClassDiagram(owner, name);
}

Create a Class Presentation

// Transaction processing is required
public INodePresentation createClassPresentation(ClassDiagramEditor editor, IClass model,
        Point2D location) throws InvalidEditingException {
    return createNodePresentation(editor, model, location);
}

// Transaction processing is required
public INodePresentation createNodePresentation(ClassDiagramEditor editor, IElement model,
        Point2D location) throws InvalidEditingException {
    return editor.createNodePresentation(model, location);
}

Create an Action in Activity Diagram

Here’s an example to create an Action in Activity Diagram. When you create a presentation that requires a Model as 1:1, the model would be automatically created as you create a presentation.

// Transaction processing is required
public INodePresentation createActionPresentation(ActivityDiagramEditor editor, String name,
        Point2D location) throws InvalidEditingException {
    return editor.createAction(name, location);
}

Delete presentations

// Transaction processing is required
public void deletePresentation(DiagramEditor editor, IPresentation presentation)
        throws InvalidEditingException {
    editor.deletePresentation(presentation);
}

Change Property of presentation

To change the presentation such as background color etc…etc, use PresentationPropertyConstants or PresentationPropertyUtil.
For correspondence between model and presentation, please see Supported presentation list.

Presentation Interface

Change Color

// Transaction processing is required
public void changeColor(IPresentation presentation, final String color)
        throws InvalidEditingException {
    presentation.setProperty(Key.FILL_COLOR, color);
}

Relocate

// Transaction processing is required
public void setLocation(INodePresentation presentation, Point2D location) {
        throws InvalidEditingException {
        presentation.setLocation(location);
}