2020-11-21 17:33:24 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
|
|
|
* (C) Copyright 2009-2020, Arnaud Roques
|
|
|
|
*
|
|
|
|
* Project Info: http://plantuml.com
|
|
|
|
*
|
|
|
|
* If you like this project or if you find it useful, you can support us at:
|
|
|
|
*
|
|
|
|
* http://plantuml.com/patreon (only 1$ per month!)
|
|
|
|
* http://plantuml.com/paypal
|
|
|
|
*
|
|
|
|
* This file is part of PlantUML.
|
|
|
|
*
|
|
|
|
* PlantUML is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PlantUML distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Original Author: Arnaud Roques
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package net.sourceforge.plantuml.jsondiagram;
|
|
|
|
|
|
|
|
import java.awt.geom.Dimension2D;
|
|
|
|
import java.util.ArrayList;
|
2020-12-01 21:39:27 +00:00
|
|
|
import java.util.Collections;
|
2020-11-21 17:33:24 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.sourceforge.plantuml.Dimension2DDouble;
|
|
|
|
import net.sourceforge.plantuml.ISkinParam;
|
2021-03-04 18:11:16 +00:00
|
|
|
import net.sourceforge.plantuml.LineBreakStrategy;
|
2020-12-01 21:39:27 +00:00
|
|
|
import net.sourceforge.plantuml.creole.CreoleMode;
|
2020-11-21 17:33:24 +00:00
|
|
|
import net.sourceforge.plantuml.cucadiagram.Display;
|
|
|
|
import net.sourceforge.plantuml.graphic.AbstractTextBlock;
|
|
|
|
import net.sourceforge.plantuml.graphic.FontConfiguration;
|
|
|
|
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
|
|
|
|
import net.sourceforge.plantuml.graphic.StringBounder;
|
|
|
|
import net.sourceforge.plantuml.graphic.TextBlock;
|
|
|
|
import net.sourceforge.plantuml.graphic.TextBlockUtils;
|
2020-12-01 21:39:27 +00:00
|
|
|
import net.sourceforge.plantuml.json.JsonArray;
|
|
|
|
import net.sourceforge.plantuml.json.JsonObject;
|
|
|
|
import net.sourceforge.plantuml.json.JsonObject.Member;
|
|
|
|
import net.sourceforge.plantuml.json.JsonValue;
|
2021-03-04 18:11:16 +00:00
|
|
|
import net.sourceforge.plantuml.style.PName;
|
2020-11-21 17:33:24 +00:00
|
|
|
import net.sourceforge.plantuml.style.SName;
|
|
|
|
import net.sourceforge.plantuml.style.Style;
|
2020-12-01 21:39:27 +00:00
|
|
|
import net.sourceforge.plantuml.svek.TextBlockBackcolored;
|
2020-11-21 17:33:24 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.UGraphic;
|
|
|
|
import net.sourceforge.plantuml.ugraphic.ULine;
|
2020-12-01 21:39:27 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.URectangle;
|
2020-11-21 17:33:24 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.UTranslate;
|
2020-12-01 21:39:27 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.color.HColor;
|
2020-11-21 17:33:24 +00:00
|
|
|
|
|
|
|
//See TextBlockMap
|
2020-12-01 21:39:27 +00:00
|
|
|
public class TextBlockJson extends AbstractTextBlock implements TextBlockBackcolored {
|
|
|
|
|
2021-05-14 08:42:57 +00:00
|
|
|
private final List<Line> lines = new ArrayList<>();
|
2020-11-21 17:33:24 +00:00
|
|
|
|
2021-03-04 18:11:16 +00:00
|
|
|
private final Style style;
|
2021-03-07 12:23:24 +00:00
|
|
|
private final Style styleHightlight;
|
2020-11-21 17:33:24 +00:00
|
|
|
private final ISkinParam skinParam;
|
|
|
|
private double totalWidth;
|
2020-12-01 21:39:27 +00:00
|
|
|
private final JsonValue root;
|
|
|
|
|
|
|
|
static class Line {
|
|
|
|
final TextBlock b1;
|
|
|
|
final TextBlock b2;
|
|
|
|
final boolean highlighted;
|
|
|
|
|
|
|
|
Line(TextBlock b1, TextBlock b2, boolean highlighted) {
|
|
|
|
this.b1 = b1;
|
|
|
|
this.b2 = b2;
|
|
|
|
this.highlighted = highlighted;
|
|
|
|
}
|
|
|
|
|
|
|
|
Line(TextBlock b1, boolean highlighted) {
|
|
|
|
this(b1, null, highlighted);
|
|
|
|
}
|
2020-11-21 17:33:24 +00:00
|
|
|
|
2020-12-01 21:39:27 +00:00
|
|
|
double getHeightOfRow(StringBounder stringBounder) {
|
|
|
|
final double height = b1.calculateDimension(stringBounder).getHeight();
|
|
|
|
if (b2 == null) {
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
return Math.max(height, b2.calculateDimension(stringBounder).getHeight());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-06 21:23:05 +00:00
|
|
|
private HColor getBackColor() {
|
|
|
|
return styleHightlight.value(PName.BackGroundColor).asColor(skinParam.getThemeStyle(),
|
|
|
|
skinParam.getIHtmlColorSet());
|
2021-03-07 12:23:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public TextBlockJson(ISkinParam skinParam, JsonValue root, List<String> allHighlighteds, Style style,
|
|
|
|
Style styleHightlight) {
|
|
|
|
this.styleHightlight = styleHightlight;
|
2020-11-21 17:33:24 +00:00
|
|
|
this.skinParam = skinParam;
|
2021-03-04 18:11:16 +00:00
|
|
|
this.style = style;
|
2020-12-01 21:39:27 +00:00
|
|
|
this.root = root;
|
|
|
|
if (root instanceof JsonObject)
|
|
|
|
for (Member member : (JsonObject) root) {
|
|
|
|
final String key = member.getName();
|
|
|
|
final String value = getShortString(member.getValue());
|
|
|
|
|
2021-03-07 12:23:24 +00:00
|
|
|
final boolean highlighted = isHighlighted(key, allHighlighteds);
|
|
|
|
final TextBlock block1 = getTextBlock(getRightStyle(highlighted), key);
|
|
|
|
final TextBlock block2 = getTextBlock(getRightStyle(highlighted), value);
|
|
|
|
this.lines.add(new Line(block1, block2, highlighted));
|
2020-12-01 21:39:27 +00:00
|
|
|
}
|
|
|
|
if (root instanceof JsonArray) {
|
|
|
|
int i = 0;
|
|
|
|
for (JsonValue value : (JsonArray) root) {
|
2021-03-07 12:23:24 +00:00
|
|
|
final boolean highlighted = isHighlighted("" + i, allHighlighteds);
|
|
|
|
final TextBlock block2 = getTextBlock(getRightStyle(highlighted), getShortString(value));
|
|
|
|
this.lines.add(new Line(block2, highlighted));
|
2020-12-01 21:39:27 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 12:23:24 +00:00
|
|
|
private Style getRightStyle(boolean highlighted) {
|
|
|
|
if (highlighted) {
|
|
|
|
return styleHightlight;
|
|
|
|
}
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:39:27 +00:00
|
|
|
private boolean isHighlighted(String key, List<String> highlighted) {
|
|
|
|
for (String tmp : highlighted) {
|
|
|
|
if (tmp.trim().equals("\"" + key + "\"")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int size() {
|
|
|
|
int size = 0;
|
|
|
|
if (root instanceof JsonObject) {
|
|
|
|
for (Member member : (JsonObject) root)
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
if (root instanceof JsonArray) {
|
|
|
|
for (JsonValue value : (JsonArray) root)
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getShortString(JsonValue value) {
|
|
|
|
if (value.isString()) {
|
|
|
|
return value.asString();
|
|
|
|
}
|
2020-12-14 18:31:06 +00:00
|
|
|
if (value.isNull()) {
|
|
|
|
return "<U+2400>";
|
|
|
|
// return "<U+2205> null";
|
|
|
|
}
|
|
|
|
if (value.isNumber()) {
|
2020-12-01 21:39:27 +00:00
|
|
|
return value.toString();
|
2020-11-21 17:33:24 +00:00
|
|
|
}
|
2020-12-14 18:31:06 +00:00
|
|
|
if (value.isBoolean()) {
|
|
|
|
if (value.isTrue()) {
|
|
|
|
return "<U+2611> true";
|
|
|
|
} else {
|
|
|
|
return "<U+2610> false";
|
|
|
|
}
|
|
|
|
}
|
2020-12-01 21:39:27 +00:00
|
|
|
return " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<JsonValue> children() {
|
2021-05-14 08:42:57 +00:00
|
|
|
final List<JsonValue> result = new ArrayList<>();
|
2020-12-01 21:39:27 +00:00
|
|
|
if (root instanceof JsonObject) {
|
|
|
|
for (Member member : (JsonObject) root) {
|
|
|
|
final JsonValue value = member.getValue();
|
|
|
|
if (value instanceof JsonObject || value instanceof JsonArray) {
|
|
|
|
result.add(value);
|
|
|
|
} else {
|
|
|
|
result.add(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (root instanceof JsonArray) {
|
|
|
|
for (JsonValue value : (JsonArray) root) {
|
|
|
|
if (value instanceof JsonObject || value instanceof JsonArray) {
|
|
|
|
result.add(value);
|
|
|
|
} else {
|
|
|
|
result.add(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Collections.unmodifiableList(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> keys() {
|
2021-05-14 08:42:57 +00:00
|
|
|
final List<String> result = new ArrayList<>();
|
2020-12-01 21:39:27 +00:00
|
|
|
if (root instanceof JsonObject) {
|
|
|
|
for (Member member : (JsonObject) root) {
|
|
|
|
final String key = member.getName();
|
|
|
|
result.add(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (root instanceof JsonArray) {
|
|
|
|
int i = 0;
|
|
|
|
for (JsonValue value : (JsonArray) root) {
|
|
|
|
result.add("" + i);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Collections.unmodifiableList(result);
|
2020-11-21 17:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Dimension2D calculateDimension(StringBounder stringBounder) {
|
|
|
|
return new Dimension2DDouble(getWidthColA(stringBounder) + getWidthColB(stringBounder),
|
|
|
|
getTotalHeight(stringBounder));
|
|
|
|
}
|
|
|
|
|
2020-12-06 21:43:09 +00:00
|
|
|
public double getWidthColA(StringBounder stringBounder) {
|
2020-12-01 21:39:27 +00:00
|
|
|
double width = 0;
|
|
|
|
for (Line line : lines) {
|
|
|
|
width = Math.max(width, line.b1.calculateDimension(stringBounder).getWidth());
|
|
|
|
}
|
|
|
|
return width;
|
2020-11-21 17:33:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 21:43:09 +00:00
|
|
|
public double getWidthColB(StringBounder stringBounder) {
|
2020-11-21 17:33:24 +00:00
|
|
|
double width = 0;
|
2020-12-01 21:39:27 +00:00
|
|
|
for (Line line : lines) {
|
|
|
|
if (line.b2 != null) {
|
|
|
|
width = Math.max(width, line.b2.calculateDimension(stringBounder).getWidth());
|
|
|
|
}
|
2020-11-21 17:33:24 +00:00
|
|
|
}
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2021-03-04 18:11:16 +00:00
|
|
|
public void drawU(final UGraphic ug) {
|
2020-11-21 17:33:24 +00:00
|
|
|
final StringBounder stringBounder = ug.getStringBounder();
|
|
|
|
|
|
|
|
final Dimension2D fullDim = calculateDimension(stringBounder);
|
2020-12-01 21:39:27 +00:00
|
|
|
double trueWidth = Math.max(fullDim.getWidth(), totalWidth);
|
2020-11-21 17:33:24 +00:00
|
|
|
final double widthColA = getWidthColA(stringBounder);
|
2021-03-04 18:11:16 +00:00
|
|
|
final double widthColB = getWidthColB(stringBounder);
|
2020-12-01 21:39:27 +00:00
|
|
|
|
2020-11-21 17:33:24 +00:00
|
|
|
double y = 0;
|
2021-05-06 21:23:05 +00:00
|
|
|
final UGraphic ugNode = style.applyStrokeAndLineColor(ug, skinParam.getIHtmlColorSet(),
|
|
|
|
skinParam.getThemeStyle());
|
2020-12-01 21:39:27 +00:00
|
|
|
for (Line line : lines) {
|
2021-03-04 18:11:16 +00:00
|
|
|
final double heightOfRow = line.getHeightOfRow(stringBounder);
|
|
|
|
y += heightOfRow;
|
|
|
|
}
|
|
|
|
if (y == 0)
|
|
|
|
y = 15;
|
|
|
|
if (trueWidth == 0)
|
|
|
|
trueWidth = 30;
|
|
|
|
|
|
|
|
final double round = style.value(PName.RoundCorner).asDouble();
|
|
|
|
final URectangle fullNodeRectangle = new URectangle(trueWidth, y).rounded(round);
|
2021-05-06 21:23:05 +00:00
|
|
|
final HColor backColor = style.value(PName.BackGroundColor).asColor(skinParam.getThemeStyle(),
|
|
|
|
skinParam.getIHtmlColorSet());
|
2021-03-04 18:11:16 +00:00
|
|
|
ugNode.apply(backColor.bg()).apply(backColor).draw(fullNodeRectangle);
|
|
|
|
|
|
|
|
final Style styleSeparator = style.getSignature().add(SName.separator)
|
|
|
|
.getMergedStyle(skinParam.getCurrentStyleBuilder());
|
2021-05-06 21:23:05 +00:00
|
|
|
final UGraphic ugSeparator = styleSeparator.applyStrokeAndLineColor(ug, skinParam.getIHtmlColorSet(),
|
|
|
|
skinParam.getThemeStyle());
|
2021-03-04 18:11:16 +00:00
|
|
|
|
|
|
|
y = 0;
|
|
|
|
for (Line line : lines) {
|
|
|
|
final UGraphic ugline = ugSeparator.apply(UTranslate.dy(y));
|
2020-12-01 21:39:27 +00:00
|
|
|
final double heightOfRow = line.getHeightOfRow(stringBounder);
|
|
|
|
if (line.highlighted) {
|
|
|
|
final URectangle back = new URectangle(trueWidth - 2, heightOfRow).rounded(4);
|
2021-05-06 21:23:05 +00:00
|
|
|
ugline.apply(getBackColor()).apply(getBackColor().bg()).apply(new UTranslate(1.5, 0)).draw(back);
|
2020-12-01 21:39:27 +00:00
|
|
|
}
|
2020-11-21 17:33:24 +00:00
|
|
|
|
2020-12-01 21:39:27 +00:00
|
|
|
if (y > 0)
|
|
|
|
ugline.draw(ULine.hline(trueWidth));
|
|
|
|
|
2021-03-04 18:11:16 +00:00
|
|
|
final HorizontalAlignment horizontalAlignment = style.getHorizontalAlignment();
|
|
|
|
horizontalAlignment.draw(ugline, line.b1, 0, widthColA);
|
2020-12-01 21:39:27 +00:00
|
|
|
|
|
|
|
if (line.b2 != null) {
|
2021-03-04 18:11:16 +00:00
|
|
|
final UGraphic uglineColB = ugline.apply(UTranslate.dx(widthColA));
|
|
|
|
horizontalAlignment.draw(uglineColB, line.b2, 0, widthColB);
|
|
|
|
uglineColB.draw(ULine.vline(heightOfRow));
|
2020-12-01 21:39:27 +00:00
|
|
|
}
|
2020-11-21 17:33:24 +00:00
|
|
|
|
|
|
|
y += heightOfRow;
|
|
|
|
}
|
2021-03-04 18:11:16 +00:00
|
|
|
ugNode.draw(fullNodeRectangle);
|
2020-12-01 21:39:27 +00:00
|
|
|
|
2020-11-21 17:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private double getTotalHeight(StringBounder stringBounder) {
|
|
|
|
double height = 0;
|
2020-12-01 21:39:27 +00:00
|
|
|
for (Line line : lines) {
|
|
|
|
height += line.getHeightOfRow(stringBounder);
|
2020-11-21 17:33:24 +00:00
|
|
|
}
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2021-03-07 12:23:24 +00:00
|
|
|
private TextBlock getTextBlock(Style style, String key) {
|
2020-11-21 17:33:24 +00:00
|
|
|
final Display display = Display.getWithNewlines(key);
|
2021-05-06 21:23:05 +00:00
|
|
|
final FontConfiguration fontConfiguration = style.getFontConfiguration(skinParam.getThemeStyle(),
|
|
|
|
skinParam.getIHtmlColorSet());
|
2021-03-04 18:11:16 +00:00
|
|
|
final LineBreakStrategy wrap = style.wrapWidth();
|
|
|
|
final HorizontalAlignment horizontalAlignment = style.getHorizontalAlignment();
|
|
|
|
TextBlock result = display.create0(fontConfiguration, horizontalAlignment, skinParam, wrap,
|
|
|
|
CreoleMode.NO_CREOLE, null, null);
|
2020-11-21 17:33:24 +00:00
|
|
|
result = TextBlockUtils.withMargin(result, 5, 2);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTotalWidth(double totalWidth) {
|
|
|
|
this.totalWidth = totalWidth;
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:39:27 +00:00
|
|
|
public HColor getBackcolor() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-21 17:33:24 +00:00
|
|
|
}
|