How can i change the color of a camunda activity from java?

from a java project that only receives 2 parameters (the path of the .bpmn file and the id of the activity), I need to change the color of the activity, is there a way to do this modification?

Hi everyone!

Together with my partner @JHON_ALEXANDER_ROA_R , we are working on a project to trace BPMN models to external information systems, where we need to customize the visual aspects (in this case, the color) of the activities in the BPMN model using Java.

We have implemented a code that uses the Camunda API to get the BpmnShape from the activity ID (source code in this link:

package org.example;

import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.instance.Activity;
import org.camunda.bpm.model.bpmn.instance.FlowElement;
import org.camunda.bpm.model.bpmn.instance.bpmndi.BpmnShape;

import java.io.File;
import java.util.Collection;

public class ColorBPMN {
    public static void main(String[] args) {
        // Cargar el modelo BPMN desde un archivo (reemplazar con la ruta correcta)
        File bpmnFile = new File("C:\\Users\\danno\\Documents\\MsgFoundation\\diagram_1.bpmn");
        BpmnModelInstance modelInstance = Bpmn.readModelFromFile(bpmnFile);

        // ID de la actividad que estás buscando
        String targetActivityId = "Activity_1euaoi5";

        // Obtener todos los elementos de flujo del modelo
        Collection<FlowElement> flowElements = modelInstance.getModelElementsByType(FlowElement.class);

        // Buscar la actividad con el ID especĂ­fico
        Activity targetActivity = null;
        for (FlowElement flowElement : flowElements) {
            if (flowElement instanceof Activity && flowElement.getId().equals(targetActivityId)) {
                targetActivity = (Activity) flowElement;
                break;
            }
        }

        // Verificar si se encontrĂł la actividad
        if (targetActivity != null) {
            System.out.println("La actividad con el ID " + targetActivityId + " existe en el modelo BPMN.");

            // Obtener la forma asociada a la actividad
            BpmnShape bpmnShape = modelInstance.getModelElementById(targetActivityId + "_di");

            // Verificar si se encontrĂł la BpmnShape
            if (bpmnShape != null) {
                System.out.println("BPMN Shape encontrado para la actividad con ID " + targetActivityId);

                // Actualizar la BpmnShape con atributos adicionales
                bpmnShape.setAttributeValue("bioc:stroke", "#205022");
                bpmnShape.setAttributeValue("bioc:fill", "#c8e6c9");
                bpmnShape.setAttributeValue("color:background-color", "#c8e6c9");
                bpmnShape.setAttributeValue("color:border-color", "#205022");

            } else {
                System.out.println("BPMN Shape no encontrado para la actividad con ID " + targetActivityId);
            }
        } else {
            System.out.println("La actividad con el ID " + targetActivityId + " no se encontrĂł en el modelo BPMN.");
        }
    }
}

The program finds the BpmnShape but at the moment of assigning the attributes that give color to that activity we get an error. The error we get is:

Exception in thread “main” org.w3c.dom.DOMException**: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.**

The attributes that we need in addition to the bpmnShape are:

// Actualizar la BpmnShape con atributos adicionales
bpmnShape.setAttributeValue("bioc:stroke", "#205022");
bpmnShape.setAttributeValue("bioc:fill", "#c8e6c9");
bpmnShape.setAttributeValue("color:background-color", "#c8e6c9");
bpmnShape.setAttributeValue("color:border-color", "#205022");

We would be grateful if someone could help us with this problem :pray:.

@Niall @mary_grace @BerndRuecker @hassang

When I look at a BPMN diagram that has never had a color, it has a Namespace block like:

<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" 
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" 
xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" 
xmlns:di="http://www.omg.org/spec/DD/20100524/DI" 
xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1l4ma4y" 
targetNamespace="http://bpmn.io/schema/bpmn" 
exporter="Camunda Modeler" exporterVersion="4.9.0" 
modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">

However, once I add color to it
the Namespace block looks like:

<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" 
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" 
xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" 
xmlns:di="http://www.omg.org/spec/DD/20100524/DI" 
xmlns:bioc="http://bpmn.io/schema/bpmn/biocolor/1.0" 
xmlns:color="http://www.omg.org/spec/BPMN/non-normative/color/1.0" 
xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1l4ma4y" 
targetNamespace="http://bpmn.io/schema/bpmn" 
exporter="Camunda Modeler" exporterVersion="4.9.0" 
modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">

I’m guessing you’re getting the namespace error because your initial definition is missing the bioc and color namespace definitions… Try adding these

xmlns:bioc="http://bpmn.io/schema/bpmn/biocolor/1.0" 
xmlns:color="http://www.omg.org/spec/BPMN/non-normative/color/1.0" 

To your XML DOM before you try to set the namespaced attributes…

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.