Coloring Execution specification in Sequence Diagram using Script

スクリーンショット 2019-05-22 14.08.40

Recently, we’ve received the following question from our user, and I’d like to share the solution we provided to him.

Q: How do I set the default color for the lifeline activity?
I want to set this globally as well as per project. When I create a new activity, as seen below, the color of the activity is white (or no color).

 

Answer1: Cannot set the default color

Default colors for most of the objects can be set in [Tool] – [System Properties] – [Default Item Color], however Execution Specification in Sequence Diagram isn’t one of them at the moment.

system-properties 

So we wrote a script to add colors to them that he can run using this [Script Editor].

var infNamespace = 'com.change_vision.jude.api.inf'
var ISequenceDiagram = Java.type(infNamespace + '.model.ISequenceDiagram');
var Key = Java.type(infNamespace + '.presentation.PresentationPropertyConstants.Key');
var INodePresentation = Java.type(infNamespace + '.presentation.INodePresentation');

// ------------------------------------------------------
// Please set the new color as you like.
var COLOR = "#fc3768";
// ------------------------------------------------------

run();

function run() {
    var currentSequenceDiagram = getCurrentSequenceDiagram();
    if (currentSequenceDiagram == null) {
        print("error : Please open the sequence diagram.");
        return;
    }
    var presentations = getPresentations(currentSequenceDiagram);
    var chengeColorPresentations = getExecutionSpecificationPresentations(presentations);
    setColor(chengeColorPresentations, COLOR);
}

function getCurrentSequenceDiagram() {
    try{
        var viewManager = astah.getViewManager();
        var diagramViewManager = viewManager.getDiagramViewManager();
        var currentDiagram = diagramViewManager.getCurrentDiagram();
        if(currentDiagram instanceof ISequenceDiagram) {
            return currentDiagram;
        }
        return null;
    } catch(e) {
        return null;
    }
}

function getPresentations(currentDiagram) {
    try {
        return currentDiagram.getPresentations();
    } catch (e) {
        return new IPresentation[0];
    }
}

function getExecutionSpecificationPresentations(presentations) {
    var executionSpecificationPresentations = [];
    for (var i in presentations) {
        var presentation = presentations[i];
        if (presentation == null || !(presentation instanceof INodePresentation)) {
            continue;
        }
        var model = presentation.getModel();
        if (model != null) {
            continue;
        }
        var parentPresentation = presentation.getParent();
        if (parentPresentation == null) {
            continue;
        }
        executionSpecificationPresentations.push(presentation);
    }
    return executionSpecificationPresentations;
}

function setColor(presentations, color) {
    try {
        astah.getTransactionManager().beginTransaction();
        for(var i in presentations) {
            var presentation = presentations[i];
            presentation.setProperty(Key.FILL_COLOR, color);
        }
        astah.getTransactionManager().endTransaction();
        print("info: Changed the color of " + presentations.length + " elements.");
    } catch (e) {
        astah.getTransactionManager().abortTransaction();
        print("error : Could not change element color.");
        print(e);
    }
}

スクリーンショット 2019-05-22 14.08.40

Answer2. How to run this Script

  1. Go to [Tools] – [Script Editor].
    script-editor.png
  2.  

  3. A script window opens.
    script-editor2.png
  4.  

  5. Paste the script in the script editor and hit the [Run] button – the right triangle one.
    Sequence-color
  6.  

  7. The color is added to all the Execution Specifications.
    done.png

スクリーンショット 2019-05-22 14.08.40

Let’s use more Scripts!

This Script Editor unleashes Astah’s ability. You can add any customized features using API and perform through this script editor. There are many sample scripts that you can copy and paste and use right away.

スクリーンショット 2019-05-22 14.08.40

If you are using Astah Community

You can use this script editor by installing the Plug-in.  However, the API for Astah Community is limited to read-function only, so you cannot add/edit models. If you’d like to try out the whole capabilities, download the free trial of Astah Pro or UML.
astah-pro   astah-uml

スクリーンショット 2019-05-22 14.08.40

Related links

スクリーンショット 2019-05-22 14.08.40

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s