plantuml/src/net/sourceforge/plantuml/wire/WireDiagram.java

191 lines
6.3 KiB
Java
Raw Normal View History

2020-02-18 21:24:31 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2020-02-18 21:24:31 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2020-02-18 21:24:31 +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
2020-02-18 21:24:31 +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.wire;
import java.io.IOException;
import java.io.OutputStream;
2020-12-19 21:21:54 +00:00
import java.util.ArrayList;
import java.util.List;
2020-02-18 21:24:31 +00:00
2021-01-10 20:52:19 +00:00
import net.sourceforge.plantuml.FileFormat;
2020-02-18 21:24:31 +00:00
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.StringBounder;
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.skin.UmlDiagramType;
2020-02-18 21:24:31 +00:00
public class WireDiagram extends UmlDiagram {
2021-01-10 20:52:19 +00:00
private final WBlock root = new WBlock("", new UTranslate(), 0, 0, null);
2021-05-14 08:42:57 +00:00
private final List<Spot> spots = new ArrayList<>();
private final List<WLinkHorizontal> hlinks = new ArrayList<>();
private final List<WLinkVertical> vlinks = new ArrayList<>();
2020-02-18 21:24:31 +00:00
public DiagramDescription getDescription() {
return new DiagramDescription("Wire Diagram");
}
2022-09-18 17:08:06 +00:00
public WireDiagram(UmlSource source) {
super(source, UmlDiagramType.WIRE, null);
2020-02-18 21:24:31 +00:00
}
@Override
protected ImageData exportDiagramInternal(OutputStream os, int index, FileFormatOption fileFormatOption)
throws IOException {
2022-02-10 18:16:18 +00:00
return createImageBuilder(fileFormatOption).drawable(getTextBlock()).write(os);
2020-02-18 21:24:31 +00:00
}
2023-02-22 18:43:48 +00:00
@Override
protected TextBlock getTextBlock() {
2023-02-26 18:51:17 +00:00
return new AbstractTextBlock() {
2020-02-18 21:24:31 +00:00
public void drawU(UGraphic ug) {
drawMe(ug);
}
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
2020-12-19 21:21:54 +00:00
// return getDrawingElement().calculateDimension(stringBounder);
throw new UnsupportedOperationException();
2020-02-18 21:24:31 +00:00
}
};
}
private void drawMe(UGraphic ug) {
2020-12-19 21:21:54 +00:00
root.drawMe(ug);
2022-02-10 18:16:18 +00:00
for (Spot spot : spots)
2020-12-19 21:21:54 +00:00
spot.drawMe(ug);
2022-02-10 18:16:18 +00:00
for (WLinkHorizontal link : hlinks)
2021-01-10 20:52:19 +00:00
link.drawMe(ug);
2022-02-10 18:16:18 +00:00
for (WLinkVertical link : vlinks)
2020-12-19 21:21:54 +00:00
link.drawMe(ug);
2020-02-18 21:24:31 +00:00
}
2021-01-10 20:52:19 +00:00
public CommandExecutionResult addComponent(String indent, String name, int width, int height, HColor color) {
final int level = computeIndentationLevel(indent);
return this.root.addBlock(level, name, width, height, color);
2020-02-18 21:24:31 +00:00
}
2020-12-19 21:21:54 +00:00
public CommandExecutionResult newColumn(String indent) {
2021-01-10 20:52:19 +00:00
final int level = computeIndentationLevel(indent);
2020-12-19 21:21:54 +00:00
return this.root.newColumn(level);
2020-02-18 21:24:31 +00:00
}
2020-12-19 21:21:54 +00:00
public CommandExecutionResult spot(String name, HColor color, String x, String y) {
final WBlock block = this.root.getBlock(name);
2022-02-10 18:16:18 +00:00
if (block == null)
2020-12-19 21:21:54 +00:00
return CommandExecutionResult.error("No such element " + name);
2022-02-10 18:16:18 +00:00
2020-12-19 21:21:54 +00:00
final Spot spot = new Spot(block, color, x, y);
this.spots.add(spot);
2020-02-18 21:24:31 +00:00
return CommandExecutionResult.ok();
}
2020-12-19 21:21:54 +00:00
public CommandExecutionResult wgoto(String indent, double x, double y) {
2021-01-10 20:52:19 +00:00
final int level = computeIndentationLevel(indent);
2020-12-19 21:21:54 +00:00
return this.root.wgoto(level, x, y);
2020-02-18 21:24:31 +00:00
}
2020-12-19 21:21:54 +00:00
public CommandExecutionResult wmove(String indent, double x, double y) {
2021-01-10 20:52:19 +00:00
final int level = computeIndentationLevel(indent);
2020-12-19 21:21:54 +00:00
return this.root.wmove(level, x, y);
2020-02-18 21:24:31 +00:00
}
2021-01-10 20:52:19 +00:00
public CommandExecutionResult print(String indent, String text) {
final int level = computeIndentationLevel(indent);
final StringBounder stringBounder = FileFormat.PNG.getDefaultStringBounder();
2021-01-10 20:52:19 +00:00
return this.root.print(stringBounder, getSkinParam(), level, text);
}
private int computeIndentationLevel(String indent) {
final int level = indent.replace(" ", "\t").length();
return level;
}
public CommandExecutionResult vlink(String name1, String x1, String y1, String name2, WLinkType type,
WArrowDirection direction, HColor color, Display label) {
2020-12-19 21:21:54 +00:00
final WBlock block1 = this.root.getBlock(name1);
2022-02-10 18:16:18 +00:00
if (block1 == null)
2020-12-19 21:21:54 +00:00
return CommandExecutionResult.error("No such element " + name1);
2022-02-10 18:16:18 +00:00
2020-12-19 21:21:54 +00:00
final WBlock block2 = this.root.getBlock(name2);
2022-02-10 18:16:18 +00:00
if (block2 == null)
2020-12-19 21:21:54 +00:00
return CommandExecutionResult.error("No such element " + name2);
2021-01-10 20:52:19 +00:00
final UTranslate start = block1.getNextOutVertical(x1, y1, type);
final double destination = block2.getAbsolutePosition("0", "0").getDy();
this.vlinks.add(new WLinkVertical(getSkinParam(), start, destination, type, direction, color, label));
return CommandExecutionResult.ok();
}
public CommandExecutionResult hlink(String name1, String x1, String y1, String name2, WLinkType type,
WArrowDirection direction, HColor color, Display label) {
final WBlock block1 = this.root.getBlock(name1);
2022-02-10 18:16:18 +00:00
if (block1 == null)
2021-01-10 20:52:19 +00:00
return CommandExecutionResult.error("No such element " + name1);
2022-02-10 18:16:18 +00:00
2021-01-10 20:52:19 +00:00
final WBlock block2 = this.root.getBlock(name2);
2022-02-10 18:16:18 +00:00
if (block2 == null)
2021-01-10 20:52:19 +00:00
return CommandExecutionResult.error("No such element " + name2);
final UTranslate start = block1.getNextOutHorizontal(x1, y1, type);
final double destination = block2.getAbsolutePosition("0", "0").getDx();
this.hlinks.add(new WLinkHorizontal(getSkinParam(), start, destination, type, direction, color, label));
2020-02-18 21:24:31 +00:00
return CommandExecutionResult.ok();
}
}