Skip to content

Commit 184cdf6

Browse files
author
Tihomir Surdilovic
authored
Merge pull request #64 from tsurdilo/diagramupdate2
disable diagram legend by default
2 parents 1defc45 + 487ac92 commit 184cdf6

File tree

6 files changed

+34
-6
lines changed

6 files changed

+34
-6
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,19 @@ String diagramSVG = workflowDiagram.getSvgDiagram();
254254
`diagramSVG` includes the diagram SVG source which you can then decide to save to a file,
255255
print, or process further.
256256

257-
Here are some generated diagrams from the specification examples:
257+
By default the diagram legend is now shown. If you want to enable it you can do:
258+
259+
``` java
260+
Workflow workflow = Workflow.fromSource(source);
261+
262+
WorkflowDiagram workflowDiagram = new WorkflowDiagramImpl();
263+
workflowDiagram.setWorkflow(workflow)
264+
.showLegend(true);
265+
266+
String diagramSVG = workflowDiagram.getSvgDiagram();
267+
```
268+
269+
Here are some generated diagrams from the specification examples (with legend enabled):
258270

259271
1. [Job Monitoring Example](https://github.com/serverlessworkflow/specification/blob/master/examples/examples.md#Monitor-Job-Example)
260272
<p align="center">

api/src/main/java/io/serverlessworkflow/api/interfaces/WorkflowDiagram.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ public interface WorkflowDiagram {
2626
WorkflowDiagram setSource(String source);
2727

2828
String getSvgDiagram() throws Exception;
29+
30+
WorkflowDiagram showLegend(boolean showLegend);
2931
}

diagram/src/main/java/io/serverlessworkflow/diagram/WorkflowDiagramImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class WorkflowDiagramImpl implements WorkflowDiagram {
3434

3535
private String source;
3636
private Workflow workflow;
37+
private boolean showLegend = false;
3738

3839
@Override
3940
public WorkflowDiagram setWorkflow(Workflow workflow) {
@@ -46,7 +47,6 @@ public WorkflowDiagram setWorkflow(Workflow workflow) {
4647
public WorkflowDiagram setSource(String source) {
4748
this.source = source;
4849
this.workflow = Workflow.fromSource(source);
49-
5050
return this;
5151
}
5252

@@ -55,10 +55,16 @@ public String getSvgDiagram() throws Exception {
5555
if(workflow == null) {
5656
throw new IllegalAccessException("Unable to get diagram - no workflow set.");
5757
}
58-
SourceStringReader reader = new SourceStringReader(WorkflowToPlantuml.convert(workflow));
58+
SourceStringReader reader = new SourceStringReader(WorkflowToPlantuml.convert(workflow, showLegend));
5959
final ByteArrayOutputStream os = new ByteArrayOutputStream();
6060
String desc = reader.generateImage(os, new FileFormatOption(FileFormat.SVG));
6161
os.close();
6262
return new String(os.toByteArray(), Charset.forName("UTF-8"));
6363
}
64+
65+
@Override
66+
public WorkflowDiagram showLegend(boolean showLegend) {
67+
this.showLegend = showLegend;
68+
return this;
69+
}
6470
}

diagram/src/main/java/io/serverlessworkflow/diagram/model/WorkflowDiagramModel.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ public class WorkflowDiagramModel {
3636
private List<ModelStateDef> modelStateDefs = new ArrayList<>();
3737
private List<ModelState> modelStates = new ArrayList<>();
3838
private List<ModelConnection> modelConnections = new ArrayList<>();
39+
private boolean showLegend;
3940

40-
public WorkflowDiagramModel(Workflow workflow) {
41+
public WorkflowDiagramModel(Workflow workflow, boolean showLegend) {
4142
this.workflow = workflow;
43+
this.showLegend = showLegend;
4244
inspect(workflow);
4345
}
4446

@@ -409,4 +411,8 @@ public List<ModelStateDef> getModelStateDefs() {
409411
public void setModelStateDefs(List<ModelStateDef> modelStateDefs) {
410412
this.modelStateDefs = modelStateDefs;
411413
}
414+
415+
public boolean getShowLegend() {
416+
return showLegend;
417+
}
412418
}

diagram/src/main/java/io/serverlessworkflow/diagram/utils/WorkflowToPlantuml.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import org.thymeleaf.context.Context;
2424

2525
public class WorkflowToPlantuml {
26-
public static String convert(Workflow workflow) {
26+
public static String convert(Workflow workflow, boolean showLegend) {
2727
TemplateEngine plantUmlTemplateEngine = ThymeleafConfig.templateEngine;
2828
Context context = new Context();
29-
context.setVariable("diagram", new WorkflowDiagramModel(workflow));
29+
context.setVariable("diagram", new WorkflowDiagramModel(workflow, showLegend));
3030

3131
return plantUmlTemplateEngine.process("workflow-template", context);
3232
}

diagram/src/main/resources/templates/plantuml/workflow-template.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ state "[(${diagram.title})]" as workflow << workflow >> {
3636

3737
}
3838

39+
[# th:if="${diagram.showLegend}" ]
3940
legend center
4041
State Types and Border Colors:
4142
| Event | Operation | Switch | Delay | Parallel | SubFlow | Inject | ForEach | CallBack |
4243
|<#7fe5f0>|<#bada55>|<#92a0f2>|<#b83b5e>|<#6a2c70>|<#87753c>|<#1e5f74>|<#931a25>|<#ffcb8e>|
4344
endlegend
45+
[/]
4446

4547
@enduml

0 commit comments

Comments
 (0)