1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-15 14:42:24 +00:00
plantuml/src/net/sourceforge/plantuml/nwdiag/NwDiagram.java

415 lines
13 KiB
Java
Raw Normal View History

2018-11-26 18:46:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, Arnaud Roques
2018-11-26 18:46:50 +00:00
*
* 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.nwdiag;
import java.awt.geom.Dimension2D;
2020-04-26 18:31:41 +00:00
import java.awt.geom.Rectangle2D;
2018-11-26 18:46:50 +00:00
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.ColorParam;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SpriteContainerEmpty;
import net.sourceforge.plantuml.UmlDiagram;
import net.sourceforge.plantuml.UmlDiagramType;
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;
2018-11-26 18:46:50 +00:00
import net.sourceforge.plantuml.cucadiagram.Display;
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
2020-04-26 18:31:41 +00:00
import net.sourceforge.plantuml.graphic.InnerStrategy;
2018-11-26 18:46:50 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
2020-04-26 18:31:41 +00:00
import net.sourceforge.plantuml.graphic.TextBlockUtils;
2018-11-26 18:46:50 +00:00
import net.sourceforge.plantuml.graphic.UDrawable;
2021-08-30 17:13:54 +00:00
import net.sourceforge.plantuml.nwdiag.core.NServer;
import net.sourceforge.plantuml.nwdiag.core.Network;
import net.sourceforge.plantuml.nwdiag.core.NwGroup;
import net.sourceforge.plantuml.nwdiag.next.GridTextBlockDecorated;
import net.sourceforge.plantuml.nwdiag.next.LinkedElement;
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;
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;
import net.sourceforge.plantuml.style.StyleSignature;
2020-04-26 18:31:41 +00:00
import net.sourceforge.plantuml.svek.TextBlockBackcolored;
import net.sourceforge.plantuml.ugraphic.MinMax;
2021-09-15 21:03:09 +00:00
import net.sourceforge.plantuml.ugraphic.UBackground;
2018-11-26 18:46:50 +00:00
import net.sourceforge.plantuml.ugraphic.UEmpty;
import net.sourceforge.plantuml.ugraphic.UFont;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UTranslate;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
import net.sourceforge.plantuml.ugraphic.color.HColorUtils;
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)");
}
2021-05-23 15:35:13 +00:00
public NwDiagram(UmlSource source) {
super(source, UmlDiagramType.NWDIAG);
2018-11-26 18:46:50 +00:00
}
public void init() {
initDone = true;
}
2021-08-30 17:13:54 +00:00
private Network currentNetwork() {
2018-11-26 18:46:50 +00:00
if (networks.size() == 0) {
return null;
}
return networks.get(networks.size() - 1);
}
public CommandExecutionResult openGroup(String name) {
if (initDone == false) {
2021-05-23 15:35:13 +00:00
return errorNoInit();
2018-11-26 18:46:50 +00:00
}
2021-08-30 17:13:54 +00:00
currentGroup = new NwGroup(name, currentNetwork());
2018-11-26 18:46:50 +00:00
groups.add(currentGroup);
return CommandExecutionResult.ok();
}
public CommandExecutionResult openNetwork(String name) {
if (initDone == false) {
2021-05-23 15:35:13 +00:00
return errorNoInit();
2018-11-26 18:46:50 +00:00
}
2020-12-19 21:21:54 +00:00
createNetwork(name);
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) {
if (initDone == false) {
2021-05-23 15:35:13 +00:00
return errorNoInit();
2020-09-19 15:43:24 +00:00
}
2021-08-30 17:13:54 +00:00
final NServer element;
2020-09-19 15:43:24 +00:00
if (currentNetwork() == null) {
2020-12-19 21:21:54 +00:00
createNetwork(name1);
element = new NServer(name2);
2020-09-19 15:43:24 +00:00
} else {
2021-08-30 17:13:54 +00:00
final NServer already = servers.get(name1);
final Network network1 = createNetwork("");
2020-09-19 15:43:24 +00:00
network1.goInvisible();
if (already != null) {
2021-08-30 17:13:54 +00:00
connect(already, toSet(null));
2020-09-19 15:43:24 +00:00
}
element = new NServer(name2);
2020-09-19 15:43:24 +00:00
}
2021-06-27 16:50:40 +00:00
servers.put(name2, element);
addInternal(element, toSet(null));
return CommandExecutionResult.ok();
2020-09-19 15:43:24 +00:00
}
2021-08-30 17:13:54 +00:00
private void addInternal(NServer server, Map<String, String> props) {
connect(server, props);
2020-09-19 15:43:24 +00:00
final String description = props.get("description");
if (description != null) {
2021-06-27 16:50:40 +00:00
server.setDescription(description);
2020-09-19 15:43:24 +00:00
}
final String shape = props.get("shape");
if (shape != null) {
2021-06-27 16:50:40 +00:00
server.setShape(shape);
2020-09-19 15:43:24 +00:00
}
2021-08-30 17:13:54 +00:00
playField.addInPlayfield(server.getBar());
}
private void connect(NServer server, final Map<String, String> props) {
server.connect(currentNetwork(), props);
2018-11-26 18:46:50 +00:00
}
public CommandExecutionResult addElement(String name, String definition) {
if (initDone == false) {
2021-05-23 15:35:13 +00:00
return errorNoInit();
2018-11-26 18:46:50 +00:00
}
if (currentGroup != null) {
2021-06-27 16:50:40 +00:00
currentGroup.addName(name);
2018-11-26 18:46:50 +00:00
}
2021-08-30 17:13:54 +00:00
NServer server = null;
2020-09-19 15:43:24 +00:00
if (currentNetwork() == null) {
2021-06-27 16:50:40 +00:00
if (currentGroup != null) {
return CommandExecutionResult.ok();
2018-11-26 18:46:50 +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();
server = new NServer(name);
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) {
server = new NServer(name);
2021-06-27 16:50:40 +00:00
servers.put(name, server);
}
2018-11-26 18:46:50 +00:00
}
2021-06-27 16:50:40 +00:00
addInternal(server, toSet(definition));
return CommandExecutionResult.ok();
}
public CommandExecutionResult endSomething() {
if (initDone == false) {
return errorNoInit();
}
this.currentGroup = null;
2018-11-26 18:46:50 +00:00
return CommandExecutionResult.ok();
}
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>();
if (definition == null) {
return result;
}
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
}
2020-04-26 18:31:41 +00:00
private TextBlockBackcolored getTextBlock() {
return new TextBlockBackcolored() {
2018-11-26 18:46:50 +00:00
public void drawU(UGraphic ug) {
drawMe(ug);
}
2020-04-26 18:31:41 +00:00
public Rectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
return null;
}
public Dimension2D calculateDimension(StringBounder stringBounder) {
return getTotalDimension(stringBounder);
}
public MinMax getMinMax(StringBounder stringBounder) {
throw new UnsupportedOperationException();
}
public HColor getBackcolor() {
return null;
}
2018-11-26 18:46:50 +00:00
};
}
2021-09-15 21:03:09 +00:00
private StyleSignature getStyleDefinitionNetwork() {
return StyleSignature.of(SName.root, SName.element, SName.nwdiagDiagram, SName.network);
}
2018-11-26 18:46:50 +00:00
private TextBlock toTextBlock(String name, String s) {
if (s != null) {
name += "\\n" + s;
}
2021-09-15 21:03:09 +00:00
final StyleBuilder styleBuilder = getSkinParam().getCurrentStyleBuilder();
final Style style = getStyleDefinitionNetwork().getMergedStyle(styleBuilder);
final FontConfiguration fontConfiguration = style.getFontConfiguration(getSkinParam().getThemeStyle(),
getSkinParam().getIHtmlColorSet());
return Display.getWithNewlines(name).create(fontConfiguration, HorizontalAlignment.RIGHT,
2018-11-26 18:46:50 +00:00
new SpriteContainerEmpty());
}
2020-04-26 18:31:41 +00:00
private Dimension2D getTotalDimension(StringBounder stringBounder) {
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-08-30 17:13:54 +00:00
final TextBlock desc = toTextBlock(current.getDisplayName(), address);
2018-11-26 18:46:50 +00:00
final Dimension2D dim = desc.calculateDimension(stringBounder);
if (i == 0) {
deltaY = (dim.getHeight() - GridTextBlockDecorated.NETWORK_THIN) / 2;
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-08-30 17:13:54 +00:00
final TextBlock desc = toTextBlock(current.getDisplayName(), address);
2018-11-26 18:46:50 +00:00
final Dimension2D dim = desc.calculateDimension(stringBounder);
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)));
2018-11-26 18:46:50 +00:00
final Dimension2D dimGrid = grid.calculateDimension(stringBounder);
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);
2020-08-25 17:24:17 +00:00
if (s != null) {
result.put(network, s);
}
}
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);
final int col = layout.get(server.getBar());
double topMargin = LinkedElement.MAGIC;
2021-08-30 17:13:54 +00:00
NwGroup group = getGroupOf(server);
if (group != null)
topMargin += group.getTopHeaderHeight(stringBounder, getSkinParam());
grid.add(i, col, server.asTextBlock(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) {
for (NwGroup group : groups) {
if (group.contains(server)) {
2021-08-30 17:13:54 +00:00
return group;
}
}
return null;
}
2018-11-26 18:46:50 +00:00
public CommandExecutionResult setProperty(String property, String value) {
if (initDone == false) {
2021-05-23 15:35:13 +00:00
return errorNoInit();
2018-11-26 18:46:50 +00:00
}
if ("address".equalsIgnoreCase(property) && currentNetwork() != null) {
currentNetwork().setOwnAdress(value);
}
2020-09-19 15:43:24 +00:00
if ("width".equalsIgnoreCase(property) && currentNetwork() != null) {
currentNetwork().setFullWidth("full".equalsIgnoreCase(value));
}
2018-11-26 18:46:50 +00:00
if ("color".equalsIgnoreCase(property)) {
2021-05-06 21:23:05 +00:00
final HColor color = value == null ? null
2021-09-15 21:03:09 +00:00
: getSkinParam().getIHtmlColorSet().getColorOrWhite(getSkinParam().getThemeStyle(), value);
2018-11-26 18:46:50 +00:00
if (currentGroup != null) {
currentGroup.setColor(color);
} else if (currentNetwork() != null) {
currentNetwork().setColor(color);
}
}
2020-12-19 21:21:54 +00:00
if ("description".equalsIgnoreCase(property)) {
2021-08-30 17:13:54 +00:00
if (currentGroup == null) {
currentNetwork().setDescription(value);
} else {
2020-12-19 21:21:54 +00:00
currentGroup.setDescription(value);
}
}
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
}