plantuml gets styles

This commit is contained in:
2026-06-12 08:42:54 +02:00
parent 5ebfbbbbdf
commit 9aa7e42960
24 changed files with 1188 additions and 426 deletions

View File

@@ -5,6 +5,9 @@ import click.kamil.springstatemachineexporter.model.Transition;
import click.kamil.springstatemachineexporter.model.TransitionType;
import click.kamil.springstatemachineexporter.ast.common.StringUtils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -17,22 +20,19 @@ import java.util.stream.Collectors;
public class PlantUml implements StateMachineExporter {
private final List<String> choiceColors = List.of("FF6347", "4682B4", "32CD32", "FFD700", "6A5ACD", "FF69B4");
private String getColor(Transition t, String source, Map<String, String> choiceStateColorMap) {
if (t.getType() == null)
return "000000";
return switch (t.getType()) {
case CHOICE -> choiceStateColorMap.getOrDefault(source, "000000");
case EXTERNAL -> "1E90FF";
case INTERNAL -> "32CD32";
case LOCAL -> "FFA500";
case JUNCTION -> "FF69B4";
case JOIN -> "8A2BE2";
case FORK -> "20B2AA";
};
}
private final String LAMBDA = "λ";
private String loadBaseStyle() {
try (InputStream is = getClass().getResourceAsStream("/plantuml/default-style.puml")) {
if (is == null) return "";
return new BufferedReader(new InputStreamReader(is))
.lines()
.collect(Collectors.joining("\n"));
} catch (Exception e) {
return "";
}
}
@Override
public String export(String name,
List<Transition> transitions,
@@ -42,9 +42,21 @@ public class PlantUml implements StateMachineExporter {
StringBuilder sb = new StringBuilder();
sb.append("@startuml\n");
sb.append("skinparam state {\n");
sb.append(" BackgroundColor<<choice>> LightYellow\n");
sb.append("}\n\n");
String style = loadBaseStyle();
StringBuilder choiceColorStyles = new StringBuilder();
StringBuilder hideChoiceColors = new StringBuilder();
for (int i = 0; i < choiceColors.size(); i++) {
choiceColorStyles.append(" .choice_color_").append(i)
.append(" { LineColor #").append(choiceColors.get(i)).append(" }\n");
hideChoiceColors.append("hide <<choice_color_").append(i).append(">> stereotype\n");
}
style = style.replace("/* CHOICE_COLORS_PLACEHOLDER */", choiceColorStyles.toString());
style = style.replace("/* HIDE_CHOICE_COLORS_PLACEHOLDER */", hideChoiceColors.toString());
sb.append(style).append("\n");
for (String start : startStates) {
sb.append("[*] --> ").append(simplify(start)).append("\n");
@@ -60,12 +72,13 @@ public class PlantUml implements StateMachineExporter {
}
}
Map<String, String> choiceStateColorMap = new HashMap<>();
Map<String, String> choiceStateClassMap = new HashMap<>();
int colorIndex = 0;
for (String state : statesWithChoice) {
String className = "choice_color_" + (colorIndex % choiceColors.size());
sb.append("state ").append(state).append(" <<choice>>\n");
choiceStateColorMap.put(state, choiceColors.get(colorIndex % choiceColors.size()));
choiceStateClassMap.put(state, className);
colorIndex++;
}
@@ -80,6 +93,7 @@ public class PlantUml implements StateMachineExporter {
sb.append("\n");
Set<String> usedEventStereotypes = new HashSet<>();
for (Transition t : transitions) {
if (t.getSourceStates() == null || t.getSourceStates().isEmpty())
continue;
@@ -104,8 +118,15 @@ public class PlantUml implements StateMachineExporter {
String target = simplify(rawTarget);
String transitionSource = source;
String color = getColor(t, source, choiceStateColorMap);
sb.append(transitionSource).append(" -[#").append(color).append(",bold]-> ").append(target);
String styleClass = getStyleClass(t, source, choiceStateClassMap);
String color = getLegacyColor(t, source, choiceStateClassMap);
sb.append(transitionSource).append(" -[#").append(color).append(",bold]-> ").append(target).append(" <<").append(styleClass).append(">>");
if (t.getEvent() != null && !t.getEvent().isBlank()) {
String eventClass = "e_" + normalize(t.getEvent());
sb.append(" <<").append(eventClass).append(">>");
usedEventStereotypes.add(eventClass);
}
String label = buildLabel(t, useLambdaGuards);
if (!label.isEmpty()) {
@@ -116,6 +137,10 @@ public class PlantUml implements StateMachineExporter {
}
}
for (String eventClass : usedEventStereotypes) {
sb.append("hide <<").append(eventClass).append(">> stereotype\n");
}
sb.append("\n");
for (String end : endStates) {
sb.append(simplify(end)).append(" --> [*]\n");
@@ -125,6 +150,46 @@ public class PlantUml implements StateMachineExporter {
return sb.toString();
}
private String normalize(String name) {
if (name == null) return "";
return name.replaceAll("[^a-zA-Z0-9]", "_");
}
private String getLegacyColor(Transition t, String source, Map<String, String> choiceStateClassMap) {
if (t.getType() == null)
return "000000";
return switch (t.getType()) {
case CHOICE -> {
String className = choiceStateClassMap.get(source);
if (className != null && className.startsWith("choice_color_")) {
int index = Integer.parseInt(className.substring("choice_color_".length()));
yield choiceColors.get(index % choiceColors.size());
}
yield "000000";
}
case EXTERNAL -> "1E90FF";
case INTERNAL -> "32CD32";
case LOCAL -> "FFA500";
case JUNCTION -> "FF69B4";
case JOIN -> "8A2BE2";
case FORK -> "20B2AA";
};
}
private String getStyleClass(Transition t, String source, Map<String, String> choiceStateClassMap) {
if (t.getType() == null)
return "default";
return switch (t.getType()) {
case CHOICE -> choiceStateClassMap.getOrDefault(source, "choice_type");
case EXTERNAL -> "external";
case INTERNAL -> "internal";
case LOCAL -> "local";
case JUNCTION -> "junction";
case JOIN -> "join";
case FORK -> "fork";
};
}
@Override
public String getFileExtension() {
return ".puml";