plantuml/src/net/sourceforge/plantuml/nwdiag/NwDiagram.java

421 lines
13 KiB
Java
Raw Normal View History

2018-11-26 18:46:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2018-11-26 18:46:50 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2018-11-26 18:46:50 +00:00
*
* If you like this project or if you find it useful, you can support us at:
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
2018-11-26 18:46:50 +00:00
*
* 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.nwdiag;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.UmlDiagram;
import net.sourceforge.plantuml.command.CommandExecutionResult;
import net.sourceforge.plantuml.core.DiagramDescription;
import net.sourceforge.plantuml.core.ImageData;
2021-05-23 15:35:13 +00:00
import net.sourceforge.plantuml.core.UmlSource;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.UTranslate;
import net.sourceforge.plantuml.klimt.color.HColor;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.creole.Display;
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.font.FontConfiguration;
import net.sourceforge.plantuml.klimt.font.StringBounder;
import net.sourceforge.plantuml.klimt.geom.HorizontalAlignment;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.geom.XDimension2D;
2023-02-26 18:51:17 +00:00
import net.sourceforge.plantuml.klimt.shape.AbstractTextBlock;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.shape.TextBlock;
import net.sourceforge.plantuml.klimt.shape.TextBlockUtils;
import net.sourceforge.plantuml.klimt.shape.UDrawable;
import net.sourceforge.plantuml.klimt.shape.UEmpty;
import net.sourceforge.plantuml.klimt.sprite.SpriteContainerEmpty;
2021-08-30 17:13:54 +00:00
import net.sourceforge.plantuml.nwdiag.core.NServer;
2021-10-03 21:01:18 +00:00
import net.sourceforge.plantuml.nwdiag.core.NStackable;
2021-08-30 17:13:54 +00:00
import net.sourceforge.plantuml.nwdiag.core.Network;
import net.sourceforge.plantuml.nwdiag.core.NwGroup;
import net.sourceforge.plantuml.nwdiag.next.GridTextBlockDecorated;
2021-08-30 17:13:54 +00:00
import net.sourceforge.plantuml.nwdiag.next.NBar;
2021-06-27 16:50:40 +00:00
import net.sourceforge.plantuml.nwdiag.next.NPlayField;
2023-01-16 19:06:31 +00:00
import net.sourceforge.plantuml.nwdiag.next.NServerDraw;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.skin.UmlDiagramType;
import net.sourceforge.plantuml.style.ClockwiseTopRightBottomLeft;
2021-09-15 21:03:09 +00:00
import net.sourceforge.plantuml.style.SName;
import net.sourceforge.plantuml.style.Style;
import net.sourceforge.plantuml.style.StyleBuilder;
2022-03-01 18:11:51 +00:00
import net.sourceforge.plantuml.style.StyleSignatureBasic;
2018-11-26 18:46:50 +00:00
public class NwDiagram extends UmlDiagram {
private boolean initDone;
2021-08-30 17:13:54 +00:00
private final Map<String, NServer> servers = new LinkedHashMap<>();
private final List<Network> networks = new ArrayList<>();
private final List<NwGroup> groups = new ArrayList<>();
private NwGroup currentGroup = null;
2021-06-27 16:50:40 +00:00
private final NPlayField playField = new NPlayField();
2018-11-26 18:46:50 +00:00
public DiagramDescription getDescription() {
return new DiagramDescription("(Nwdiag)");
}
2022-09-18 17:08:06 +00:00
public NwDiagram(UmlSource source) {
super(source, UmlDiagramType.NWDIAG, null);
2018-11-26 18:46:50 +00:00
}
public void init() {
initDone = true;
}
2021-10-03 21:01:18 +00:00
private Network lastNetwork() {
2022-05-21 09:41:00 +00:00
if (networks.size() == 0)
2018-11-26 18:46:50 +00:00
return null;
2021-10-03 21:01:18 +00:00
2022-05-21 09:41:00 +00:00
return networks.get(networks.size() - 1);
2021-10-03 21:01:18 +00:00
}
private final List<NStackable> stack = new ArrayList<NStackable>();
2018-11-26 18:46:50 +00:00
public CommandExecutionResult openGroup(String name) {
2022-05-21 09:41:00 +00:00
if (initDone == false)
2021-05-23 15:35:13 +00:00
return errorNoInit();
2022-05-21 09:41:00 +00:00
2021-10-03 21:01:18 +00:00
for (NStackable element : stack)
if (element instanceof NwGroup)
return CommandExecutionResult.error("Cannot nest group");
final NwGroup newGroup = new NwGroup(name);
stack.add(0, newGroup);
groups.add(newGroup);
currentGroup = newGroup;
2018-11-26 18:46:50 +00:00
return CommandExecutionResult.ok();
}
public CommandExecutionResult openNetwork(String name) {
2022-05-21 09:41:00 +00:00
if (initDone == false)
2021-05-23 15:35:13 +00:00
return errorNoInit();
2022-05-21 09:41:00 +00:00
2021-10-03 21:01:18 +00:00
for (NStackable element : stack)
if (element instanceof Network)
return CommandExecutionResult.error("Cannot nest network");
final Network network = createNetwork(name);
stack.add(0, network);
return CommandExecutionResult.ok();
}
public CommandExecutionResult closeSomething() {
2022-05-21 09:41:00 +00:00
if (initDone == false)
2021-10-03 21:01:18 +00:00
return errorNoInit();
2022-05-21 09:41:00 +00:00
2021-10-03 21:01:18 +00:00
if (stack.size() > 0)
stack.remove(0);
this.currentGroup = null;
2018-11-26 18:46:50 +00:00
return CommandExecutionResult.ok();
}
2021-08-30 17:13:54 +00:00
private Network createNetwork(String name) {
final Network network = new Network(playField.getLast(), playField.createNewStage(), name);
2020-12-19 21:21:54 +00:00
networks.add(network);
return network;
}
2020-09-19 15:43:24 +00:00
public CommandExecutionResult link(String name1, String name2) {
2022-05-21 09:41:00 +00:00
if (initDone == false)
2021-05-23 15:35:13 +00:00
return errorNoInit();
2022-05-21 09:41:00 +00:00
2023-01-16 19:06:31 +00:00
String existingAddress = "";
2021-10-03 21:01:18 +00:00
final NServer server2;
if (lastNetwork() == null) {
2020-12-19 21:21:54 +00:00
createNetwork(name1);
2023-01-16 19:06:31 +00:00
server2 = NServer.create(name2, getSkinParam());
2020-09-19 15:43:24 +00:00
} else {
2021-10-03 21:01:18 +00:00
final NServer server1 = servers.get(name1);
2023-01-16 19:06:31 +00:00
final NServer previous2 = servers.get(name2);
if (previous2 != null)
existingAddress = previous2.someAddress();
2021-08-30 17:13:54 +00:00
final Network network1 = createNetwork("");
2020-09-19 15:43:24 +00:00
network1.goInvisible();
2023-01-16 19:06:31 +00:00
if (server1 != null) {
final Network someNetwork = server1.someNetwork();
if (someNetwork != null && someNetwork.isVisible() == false && someNetwork.getUp() == null) {
final String tmp = server1.someAddress();
server1.blankSomeAddress();
server1.connectTo(lastNetwork(), tmp);
} else {
server1.connectTo(lastNetwork(), "");
}
}
2022-05-21 09:41:00 +00:00
2023-01-16 19:06:31 +00:00
server2 = new NServer(name2, server1.getBar(), getSkinParam());
2020-09-19 15:43:24 +00:00
}
2021-10-03 21:01:18 +00:00
servers.put(name2, server2);
2023-01-16 19:06:31 +00:00
server2.connectTo(lastNetwork(), existingAddress);
2021-10-03 21:01:18 +00:00
playField.addInPlayfield(server2.getBar());
2021-06-27 16:50:40 +00:00
return CommandExecutionResult.ok();
2020-09-19 15:43:24 +00:00
}
2018-11-26 18:46:50 +00:00
public CommandExecutionResult addElement(String name, String definition) {
2022-05-21 09:41:00 +00:00
if (initDone == false)
2021-05-23 15:35:13 +00:00
return errorNoInit();
2022-05-21 09:41:00 +00:00
2018-11-26 18:46:50 +00:00
if (currentGroup != null) {
2022-05-21 09:41:00 +00:00
if (alreadyInSomeGroup(name))
return CommandExecutionResult.error("Element already in another group.");
2021-06-27 16:50:40 +00:00
currentGroup.addName(name);
2018-11-26 18:46:50 +00:00
}
2022-05-21 09:41:00 +00:00
2021-08-30 17:13:54 +00:00
NServer server = null;
2021-10-03 21:01:18 +00:00
if (lastNetwork() == null) {
2022-05-21 09:41:00 +00:00
if (currentGroup != null)
2021-06-27 16:50:40 +00:00
return CommandExecutionResult.ok();
2022-05-21 09:41:00 +00:00
2021-06-27 16:50:40 +00:00
assert currentGroup == null;
2021-08-30 17:13:54 +00:00
final Network network1 = createNetwork("");
2021-06-27 16:50:40 +00:00
network1.goInvisible();
2023-01-16 19:06:31 +00:00
server = NServer.create(name, getSkinParam());
2021-06-27 16:50:40 +00:00
servers.put(name, server);
server.doNotPrintFirstLink();
2020-09-19 15:43:24 +00:00
} else {
2021-06-27 16:50:40 +00:00
server = servers.get(name);
if (server == null) {
2023-01-16 19:06:31 +00:00
server = NServer.create(name, getSkinParam());
2021-06-27 16:50:40 +00:00
servers.put(name, server);
}
2018-11-26 18:46:50 +00:00
}
2021-10-03 21:01:18 +00:00
final Map<String, String> props = toSet(definition);
server.connectTo(lastNetwork(), props.get("address"));
server.updateProperties(props);
playField.addInPlayfield(server.getBar());
2018-11-26 18:46:50 +00:00
return CommandExecutionResult.ok();
}
2022-05-21 09:41:00 +00:00
private boolean alreadyInSomeGroup(String name) {
2023-01-16 19:06:31 +00:00
for (NwGroup g : groups)
2022-05-21 09:41:00 +00:00
if (g.names().contains(name))
return true;
return false;
}
2021-05-23 15:35:13 +00:00
private CommandExecutionResult errorNoInit() {
return CommandExecutionResult.error("Maybe you forget 'nwdiag {' in your diagram ?");
2018-11-26 18:46:50 +00:00
}
private Map<String, String> toSet(String definition) {
final Map<String, String> result = new HashMap<String, String>();
2022-05-21 09:41:00 +00:00
if (definition == null)
2018-11-26 18:46:50 +00:00
return result;
2022-05-21 09:41:00 +00:00
2018-11-26 18:46:50 +00:00
final Pattern p = Pattern.compile("\\s*(\\w+)\\s*=\\s*(\"([^\"]*)\"|[^\\s,]+)");
final Matcher m = p.matcher(definition);
while (m.find()) {
final String name = m.group(1);
final String value = m.group(3) == null ? m.group(2) : m.group(3);
result.put(name, value);
}
return result;
}
@Override
protected ImageData exportDiagramInternal(OutputStream os, int index, FileFormatOption fileFormatOption)
throws IOException {
2021-05-06 21:23:05 +00:00
return createImageBuilder(fileFormatOption).drawable(getTextBlock()).write(os);
2018-11-26 18:46:50 +00:00
}
2023-02-22 18:43:48 +00:00
@Override
2023-02-26 18:51:17 +00:00
protected TextBlock getTextBlock() {
return new AbstractTextBlock() {
2018-11-26 18:46:50 +00:00
public void drawU(UGraphic ug) {
drawMe(ug);
}
2020-04-26 18:31:41 +00:00
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
2020-04-26 18:31:41 +00:00
return getTotalDimension(stringBounder);
}
2018-11-26 18:46:50 +00:00
};
}
2022-03-01 18:11:51 +00:00
private StyleSignatureBasic getStyleDefinitionNetwork(SName sname) {
return StyleSignatureBasic.of(SName.root, SName.element, SName.nwdiagDiagram, sname);
2021-09-15 21:03:09 +00:00
}
2021-09-19 17:05:24 +00:00
private TextBlock toTextBlockForNetworkName(String name, String s) {
2022-05-21 09:41:00 +00:00
if (s != null)
2018-11-26 18:46:50 +00:00
name += "\\n" + s;
2022-05-21 09:41:00 +00:00
2021-09-15 21:03:09 +00:00
final StyleBuilder styleBuilder = getSkinParam().getCurrentStyleBuilder();
2021-09-19 17:05:24 +00:00
final Style style = getStyleDefinitionNetwork(SName.network).getMergedStyle(styleBuilder);
2022-09-18 17:08:06 +00:00
final FontConfiguration fontConfiguration = style.getFontConfiguration(getSkinParam().getIHtmlColorSet());
2021-09-15 21:03:09 +00:00
return Display.getWithNewlines(name).create(fontConfiguration, HorizontalAlignment.RIGHT,
2018-11-26 18:46:50 +00:00
new SpriteContainerEmpty());
}
2022-09-12 20:08:34 +00:00
private XDimension2D getTotalDimension(StringBounder stringBounder) {
2020-04-26 18:31:41 +00:00
return TextBlockUtils.getMinMax(new UDrawable() {
public void drawU(UGraphic ug) {
drawMe(ug);
}
}, stringBounder, true).getDimension();
}
private final double margin = 5;
2018-11-26 18:46:50 +00:00
private void drawMe(UGraphic ug) {
ug = ug.apply(new UTranslate(margin, margin));
final StringBounder stringBounder = ug.getStringBounder();
double deltaX = 0;
double deltaY = 0;
2021-08-30 17:13:54 +00:00
final GridTextBlockDecorated grid = buildGrid(stringBounder);
2021-08-30 17:13:54 +00:00
2018-11-26 18:46:50 +00:00
for (int i = 0; i < networks.size(); i++) {
2021-08-30 17:13:54 +00:00
final Network current = networks.get(i);
2018-11-26 18:46:50 +00:00
final String address = current.getOwnAdress();
2021-09-19 17:05:24 +00:00
final TextBlock desc = toTextBlockForNetworkName(current.getDisplayName(), address);
2022-09-12 20:08:34 +00:00
final XDimension2D dim = desc.calculateDimension(stringBounder);
2022-05-21 09:41:00 +00:00
if (i == 0)
deltaY = (dim.getHeight() - GridTextBlockDecorated.NETWORK_THIN) / 2;
2022-05-21 09:41:00 +00:00
2018-11-26 18:46:50 +00:00
deltaX = Math.max(deltaX, dim.getWidth());
}
double y = 0;
for (int i = 0; i < networks.size(); i++) {
2021-08-30 17:13:54 +00:00
final Network current = networks.get(i);
2018-11-26 18:46:50 +00:00
final String address = current.getOwnAdress();
2021-09-19 17:05:24 +00:00
final TextBlock desc = toTextBlockForNetworkName(current.getDisplayName(), address);
2022-09-12 20:08:34 +00:00
final XDimension2D dim = desc.calculateDimension(stringBounder);
2018-11-26 18:46:50 +00:00
desc.drawU(ug.apply(new UTranslate(deltaX - dim.getWidth(), y)));
y += grid.lineHeight(stringBounder, i);
}
deltaX += 5;
2021-09-15 21:03:09 +00:00
grid.drawU(ug.apply(new UTranslate(deltaX, deltaY)));
2022-09-12 20:08:34 +00:00
final XDimension2D dimGrid = grid.calculateDimension(stringBounder);
2018-11-26 18:46:50 +00:00
2020-04-26 18:31:41 +00:00
ug.apply(new UTranslate(dimGrid.getWidth() + deltaX + margin, dimGrid.getHeight() + deltaY + margin))
.draw(new UEmpty(1, 1));
2018-11-26 18:46:50 +00:00
}
2021-08-30 17:13:54 +00:00
private Map<Network, String> getLinks(NServer element) {
final Map<Network, String> result = new LinkedHashMap<>();
for (Network network : networks) {
final String s = element.getAdress(network);
2022-05-21 09:41:00 +00:00
if (s != null)
2020-08-25 17:24:17 +00:00
result.put(network, s);
2022-05-21 09:41:00 +00:00
2020-08-25 17:24:17 +00:00
}
return result;
}
private GridTextBlockDecorated buildGrid(StringBounder stringBounder) {
2021-08-30 17:13:54 +00:00
playField.fixGroups(groups, servers.values());
final GridTextBlockDecorated grid = new GridTextBlockDecorated(networks.size(), servers.size(), groups,
2020-12-19 21:21:54 +00:00
networks, getSkinParam());
2020-08-25 17:24:17 +00:00
2021-08-30 17:13:54 +00:00
final Map<NBar, Integer> layout = playField.doLayout();
2020-08-25 17:24:17 +00:00
for (int i = 0; i < networks.size(); i++) {
2021-08-30 17:13:54 +00:00
final Network current = networks.get(i);
for (Map.Entry<String, NServer> ent : servers.entrySet()) {
final NServer server = ent.getValue();
if (server.getMainNetworkNext() == current) {
final Map<Network, String> conns = getLinks(server);
2022-05-21 09:41:00 +00:00
final Integer col = layout.get(server.getBar());
if (col == null)
continue;
2023-01-16 19:06:31 +00:00
double topMargin = NServerDraw.MAGIC;
2021-08-30 17:13:54 +00:00
NwGroup group = getGroupOf(server);
if (group != null)
topMargin += group.getTopHeaderHeight(stringBounder, getSkinParam());
2023-01-16 19:06:31 +00:00
grid.add(i, col, server.getDraw(topMargin, conns, networks, getSkinParam()));
2020-09-19 15:43:24 +00:00
}
2020-08-25 17:24:17 +00:00
}
}
2020-12-19 21:21:54 +00:00
2020-08-25 17:24:17 +00:00
return grid;
}
2021-08-30 17:13:54 +00:00
private NwGroup getGroupOf(NServer server) {
2022-05-21 09:41:00 +00:00
for (NwGroup group : groups)
if (group.contains(server))
2021-08-30 17:13:54 +00:00
return group;
2022-05-21 09:41:00 +00:00
2021-08-30 17:13:54 +00:00
return null;
}
2018-11-26 18:46:50 +00:00
public CommandExecutionResult setProperty(String property, String value) {
2022-05-21 09:41:00 +00:00
if (initDone == false)
2021-05-23 15:35:13 +00:00
return errorNoInit();
2022-05-21 09:41:00 +00:00
if ("address".equalsIgnoreCase(property) && lastNetwork() != null)
2021-10-03 21:01:18 +00:00
lastNetwork().setOwnAdress(value);
2022-05-21 09:41:00 +00:00
if ("width".equalsIgnoreCase(property) && lastNetwork() != null)
2021-10-03 21:01:18 +00:00
lastNetwork().setFullWidth("full".equalsIgnoreCase(value));
2022-05-21 09:41:00 +00:00
2018-11-26 18:46:50 +00:00
if ("color".equalsIgnoreCase(property)) {
2023-01-16 19:06:31 +00:00
final HColor color = value == null ? null : getSkinParam().getIHtmlColorSet().getColorOrWhite(value);
2022-05-21 09:41:00 +00:00
if (currentGroup != null)
2018-11-26 18:46:50 +00:00
currentGroup.setColor(color);
2022-05-21 09:41:00 +00:00
else if (lastNetwork() != null)
2021-10-03 21:01:18 +00:00
lastNetwork().setColor(color);
2022-05-21 09:41:00 +00:00
2018-11-26 18:46:50 +00:00
}
2022-05-21 09:41:00 +00:00
if ("description".equalsIgnoreCase(property))
if (currentGroup == null)
2021-10-03 21:01:18 +00:00
lastNetwork().setDescription(value);
2022-05-21 09:41:00 +00:00
else
2020-12-19 21:21:54 +00:00
currentGroup.setDescription(value);
2022-05-21 09:41:00 +00:00
2018-11-26 18:46:50 +00:00
return CommandExecutionResult.ok();
}
@Override
public ClockwiseTopRightBottomLeft getDefaultMargins() {
return ClockwiseTopRightBottomLeft.none();
}
2018-11-26 18:46:50 +00:00
}