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;
|
|
|
|
|
2020-04-26 18:31:41 +00:00
|
|
|
import net.sourceforge.plantuml.AnnotatedWorker;
|
2018-11-26 18:46:50 +00:00
|
|
|
import net.sourceforge.plantuml.ColorParam;
|
|
|
|
import net.sourceforge.plantuml.FileFormatOption;
|
2020-04-26 18:31:41 +00:00
|
|
|
import net.sourceforge.plantuml.ISkinParam;
|
2018-11-26 18:46:50 +00:00
|
|
|
import net.sourceforge.plantuml.Scale;
|
2020-05-07 14:12:08 +00:00
|
|
|
import net.sourceforge.plantuml.SkinParam;
|
2018-11-26 18:46:50 +00:00
|
|
|
import net.sourceforge.plantuml.SpriteContainerEmpty;
|
|
|
|
import net.sourceforge.plantuml.UmlDiagram;
|
|
|
|
import net.sourceforge.plantuml.UmlDiagramType;
|
2020-12-01 21:39:27 +00:00
|
|
|
import net.sourceforge.plantuml.UseStyle;
|
2018-11-26 18:46:50 +00:00
|
|
|
import net.sourceforge.plantuml.command.CommandExecutionResult;
|
|
|
|
import net.sourceforge.plantuml.core.DiagramDescription;
|
|
|
|
import net.sourceforge.plantuml.core.ImageData;
|
|
|
|
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;
|
2020-05-07 14:12:08 +00:00
|
|
|
import net.sourceforge.plantuml.style.ClockwiseTopRightBottomLeft;
|
2020-04-26 18:31:41 +00:00
|
|
|
import net.sourceforge.plantuml.svek.TextBlockBackcolored;
|
2018-11-26 18:46:50 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.ImageBuilder;
|
2020-12-06 21:43:09 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.ImageParameter;
|
2020-04-26 18:31:41 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.MinMax;
|
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.ColorMapperIdentity;
|
|
|
|
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;
|
2020-12-19 21:21:54 +00:00
|
|
|
private final Map<String, Square> squares = new LinkedHashMap<String, Square>();
|
2018-11-26 18:46:50 +00:00
|
|
|
private final List<Network> networks = new ArrayList<Network>();
|
2020-12-19 21:21:54 +00:00
|
|
|
private final List<NwGroup> groups = new ArrayList<NwGroup>();
|
|
|
|
private NwGroup currentGroup = null;
|
2018-11-26 18:46:50 +00:00
|
|
|
|
|
|
|
public DiagramDescription getDescription() {
|
|
|
|
return new DiagramDescription("(Nwdiag)");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public UmlDiagramType getUmlDiagramType() {
|
|
|
|
return UmlDiagramType.NWDIAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void init() {
|
|
|
|
initDone = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Network currentNetwork() {
|
|
|
|
if (networks.size() == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return networks.get(networks.size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public CommandExecutionResult openGroup(String name) {
|
|
|
|
if (initDone == false) {
|
|
|
|
return error();
|
|
|
|
}
|
2020-12-19 21:21: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) {
|
|
|
|
return error();
|
|
|
|
}
|
2020-12-19 21:21:54 +00:00
|
|
|
createNetwork(name);
|
2018-11-26 18:46:50 +00:00
|
|
|
return CommandExecutionResult.ok();
|
|
|
|
}
|
|
|
|
|
2020-12-19 21:21:54 +00:00
|
|
|
private Network createNetwork(String name) {
|
|
|
|
final Network network = new Network(name, networks.size());
|
|
|
|
networks.add(network);
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
|
2020-09-19 15:43:24 +00:00
|
|
|
public CommandExecutionResult link(String name1, String name2) {
|
|
|
|
if (initDone == false) {
|
|
|
|
return error();
|
|
|
|
}
|
|
|
|
if (currentNetwork() == null) {
|
2020-12-19 21:21:54 +00:00
|
|
|
createNetwork(name1);
|
|
|
|
addSquare(null, name2, toSet(null));
|
2020-09-19 15:43:24 +00:00
|
|
|
return CommandExecutionResult.ok();
|
|
|
|
} else {
|
2020-12-19 21:21:54 +00:00
|
|
|
final Square already = squares.get(name1);
|
|
|
|
final Network network1 = createNetwork("");
|
2020-09-19 15:43:24 +00:00
|
|
|
network1.goInvisible();
|
|
|
|
if (already != null) {
|
2020-12-19 21:21:54 +00:00
|
|
|
currentNetwork().addSquare(already, toSet(null));
|
2020-09-19 15:43:24 +00:00
|
|
|
}
|
2020-12-19 21:21:54 +00:00
|
|
|
addSquare(null, name2, toSet(null));
|
2020-09-19 15:43:24 +00:00
|
|
|
return CommandExecutionResult.ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-19 21:21:54 +00:00
|
|
|
private Square addSquare(Square element, String name, Map<String, String> props) {
|
2020-09-19 15:43:24 +00:00
|
|
|
if (element == null) {
|
2020-12-19 21:21:54 +00:00
|
|
|
element = new Square(name, currentNetwork(), this.getSkinParam());
|
|
|
|
squares.put(name, element);
|
2020-09-19 15:43:24 +00:00
|
|
|
}
|
2020-12-19 21:21:54 +00:00
|
|
|
currentNetwork().addSquare(element, props);
|
2020-09-19 15:43:24 +00:00
|
|
|
final String description = props.get("description");
|
|
|
|
if (description != null) {
|
|
|
|
element.setDescription(description);
|
|
|
|
}
|
|
|
|
final String shape = props.get("shape");
|
|
|
|
if (shape != null) {
|
|
|
|
element.setShape(shape);
|
|
|
|
}
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2018-11-26 18:46:50 +00:00
|
|
|
public CommandExecutionResult endSomething() {
|
|
|
|
if (initDone == false) {
|
|
|
|
return error();
|
|
|
|
}
|
|
|
|
this.currentGroup = null;
|
|
|
|
return CommandExecutionResult.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
public CommandExecutionResult addElement(String name, String definition) {
|
|
|
|
if (initDone == false) {
|
|
|
|
return error();
|
|
|
|
}
|
|
|
|
if (currentGroup != null) {
|
|
|
|
currentGroup.addElement(name);
|
|
|
|
}
|
2020-09-19 15:43:24 +00:00
|
|
|
if (currentNetwork() == null) {
|
|
|
|
if (currentGroup == null) {
|
2020-12-19 21:21:54 +00:00
|
|
|
final Network network1 = createNetwork("");
|
2020-09-19 15:43:24 +00:00
|
|
|
network1.goInvisible();
|
2020-12-19 21:21:54 +00:00
|
|
|
final Square first = addSquare(null, name, toSet(definition));
|
2020-09-19 15:43:24 +00:00
|
|
|
first.doNotHaveItsOwnColumn();
|
2018-11-26 18:46:50 +00:00
|
|
|
}
|
2020-09-19 15:43:24 +00:00
|
|
|
} else {
|
2020-12-19 21:21:54 +00:00
|
|
|
final Square element = squares.get(name);
|
|
|
|
addSquare(element, name, toSet(definition));
|
2018-11-26 18:46:50 +00:00
|
|
|
}
|
|
|
|
return CommandExecutionResult.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
private CommandExecutionResult error() {
|
|
|
|
return CommandExecutionResult.error("");
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
final Scale scale = getScale();
|
|
|
|
|
|
|
|
final double dpiFactor = scale == null ? 1 : scale.getScale(100, 100);
|
2020-04-26 18:31:41 +00:00
|
|
|
final ISkinParam skinParam = getSkinParam();
|
2020-05-07 14:12:08 +00:00
|
|
|
final int margin1;
|
|
|
|
final int margin2;
|
2020-12-01 21:39:27 +00:00
|
|
|
if (UseStyle.useBetaStyle()) {
|
2020-05-07 14:12:08 +00:00
|
|
|
margin1 = SkinParam.zeroMargin(0);
|
|
|
|
margin2 = SkinParam.zeroMargin(0);
|
|
|
|
} else {
|
|
|
|
margin1 = 0;
|
|
|
|
margin2 = 0;
|
|
|
|
}
|
2020-12-06 21:43:09 +00:00
|
|
|
final ClockwiseTopRightBottomLeft margins = ClockwiseTopRightBottomLeft.margin1margin2(margin1, margin2);
|
|
|
|
final ImageParameter imageParameter = new ImageParameter(new ColorMapperIdentity(), false, null, dpiFactor, "",
|
|
|
|
"", margins, null);
|
|
|
|
final ImageBuilder imageBuilder = ImageBuilder.build(imageParameter);
|
2020-04-26 18:31:41 +00:00
|
|
|
TextBlock result = getTextBlock();
|
2020-12-06 21:43:09 +00:00
|
|
|
result = new AnnotatedWorker(this, skinParam, fileFormatOption.getDefaultStringBounder(getSkinParam()))
|
|
|
|
.addAdd(result);
|
2018-11-26 18:46:50 +00:00
|
|
|
imageBuilder.setUDrawable(result);
|
|
|
|
|
|
|
|
return imageBuilder.writeImageTOBEMOVED(fileFormatOption, 0, os);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private TextBlock toTextBlock(String name, String s) {
|
|
|
|
if (s != null) {
|
|
|
|
name += "\\n" + s;
|
|
|
|
}
|
|
|
|
return Display.getWithNewlines(name).create(getFontConfiguration(), HorizontalAlignment.RIGHT,
|
|
|
|
new SpriteContainerEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
private FontConfiguration getFontConfiguration() {
|
|
|
|
final UFont font = UFont.serif(11);
|
2020-03-18 10:50:02 +00:00
|
|
|
return new FontConfiguration(font, HColorUtils.BLACK, HColorUtils.BLACK, false);
|
2018-11-26 18:46:50 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2020-08-25 17:24:17 +00:00
|
|
|
final GridTextBlockDecorated grid = buildGrid();
|
2018-11-26 18:46:50 +00:00
|
|
|
|
|
|
|
double deltaX = 0;
|
|
|
|
double deltaY = 0;
|
|
|
|
for (int i = 0; i < networks.size(); i++) {
|
|
|
|
final Network current = networks.get(i);
|
|
|
|
final String address = current.getOwnAdress();
|
|
|
|
final TextBlock desc = toTextBlock(current.getName(), address);
|
|
|
|
final Dimension2D dim = desc.calculateDimension(stringBounder);
|
|
|
|
if (i == 0) {
|
|
|
|
deltaY = (dim.getHeight() - GridTextBlockDecorated.NETWORK_THIN) / 2;
|
|
|
|
}
|
|
|
|
deltaX = Math.max(deltaX, dim.getWidth());
|
|
|
|
}
|
|
|
|
double y = 0;
|
|
|
|
for (int i = 0; i < networks.size(); i++) {
|
|
|
|
final Network current = networks.get(i);
|
|
|
|
final String address = current.getOwnAdress();
|
|
|
|
final TextBlock desc = toTextBlock(current.getName(), address);
|
|
|
|
final Dimension2D dim = desc.calculateDimension(stringBounder);
|
|
|
|
desc.drawU(ug.apply(new UTranslate(deltaX - dim.getWidth(), y)));
|
|
|
|
|
|
|
|
y += grid.lineHeight(stringBounder, i);
|
|
|
|
}
|
|
|
|
deltaX += 5;
|
|
|
|
|
2020-04-19 16:04:39 +00:00
|
|
|
grid.drawU(ug.apply(ColorParam.activityBorder.getDefaultValue())
|
2020-04-26 18:31:41 +00:00
|
|
|
.apply(ColorParam.activityBackground.getDefaultValue().bg()).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
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-19 21:21:54 +00:00
|
|
|
private Map<Network, String> getLinks(Square element) {
|
2020-08-25 17:24:17 +00:00
|
|
|
final Map<Network, String> result = new LinkedHashMap<Network, String>();
|
|
|
|
for (Network network : networks) {
|
|
|
|
final String s = network.getAdress(element);
|
|
|
|
if (s != null) {
|
|
|
|
result.put(network, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private GridTextBlockDecorated buildGrid() {
|
2020-12-19 21:21:54 +00:00
|
|
|
final GridTextBlockDecorated grid = new GridTextBlockDecorated(networks.size(), squares.size(), groups,
|
|
|
|
networks, getSkinParam());
|
2020-08-25 17:24:17 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < networks.size(); i++) {
|
|
|
|
final Network current = networks.get(i);
|
|
|
|
int j = 0;
|
2020-12-19 21:21:54 +00:00
|
|
|
for (Map.Entry<String, Square> ent : squares.entrySet()) {
|
|
|
|
final Square square = ent.getValue();
|
|
|
|
if (square.getMainNetwork() == current) {
|
|
|
|
final Map<Network, String> conns = getLinks(square);
|
|
|
|
grid.add(i, j, square.asTextBlock(conns, networks));
|
2020-08-25 17:24:17 +00:00
|
|
|
}
|
2020-12-19 21:21:54 +00:00
|
|
|
if (square.hasItsOwnColumn()) {
|
2020-09-19 15:43:24 +00:00
|
|
|
j++;
|
|
|
|
}
|
2020-08-25 17:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-19 21:21:54 +00:00
|
|
|
|
|
|
|
grid.checkGroups();
|
|
|
|
|
2020-08-25 17:24:17 +00:00
|
|
|
return grid;
|
|
|
|
}
|
|
|
|
|
2018-11-26 18:46:50 +00:00
|
|
|
public CommandExecutionResult setProperty(String property, String value) {
|
|
|
|
if (initDone == false) {
|
|
|
|
return error();
|
|
|
|
}
|
|
|
|
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)) {
|
2020-12-19 21:21:54 +00:00
|
|
|
final HColor color = NwGroup.colors.getColorIfValid(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)) {
|
|
|
|
if (currentGroup != null) {
|
|
|
|
currentGroup.setDescription(value);
|
|
|
|
}
|
|
|
|
}
|
2018-11-26 18:46:50 +00:00
|
|
|
return CommandExecutionResult.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|