plantuml/src/net/sourceforge/plantuml/svek/SvekNode.java

491 lines
14 KiB
Java
Raw Normal View History

2011-08-08 17:48:29 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2011-08-08 17:48:29 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2011-08-08 17:48:29 +00:00
*
2017-03-15 19:13: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
2017-03-15 19:13:31 +00:00
*
2011-08-08 17:48:29 +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
2013-12-10 19:36:50 +00:00
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
2011-08-08 17:48:29 +00:00
* 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.svek;
2015-04-07 18:18:37 +00:00
import java.util.List;
2011-08-08 17:48:29 +00:00
2022-12-17 11:10:05 +00:00
import net.sourceforge.plantuml.StringUtils;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.abel.Entity;
import net.sourceforge.plantuml.abel.EntityPosition;
import net.sourceforge.plantuml.abel.Hideable;
import net.sourceforge.plantuml.abel.Together;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.Shadowable;
2023-02-26 18:51:17 +00:00
import net.sourceforge.plantuml.klimt.UTranslate;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.font.StringBounder;
2023-02-26 18:51:17 +00:00
import net.sourceforge.plantuml.klimt.geom.MagneticBorder;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.geom.Positionable;
2023-02-26 18:51:17 +00:00
import net.sourceforge.plantuml.klimt.geom.RectangleArea;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.geom.XDimension2D;
import net.sourceforge.plantuml.klimt.geom.XPoint2D;
import net.sourceforge.plantuml.klimt.shape.UPolygon;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.svek.image.EntityImageLollipopInterface;
2022-09-01 17:40:58 +00:00
import net.sourceforge.plantuml.svek.image.EntityImagePort;
import net.sourceforge.plantuml.svek.image.EntityImageStateBorder;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.Direction;
2011-08-08 17:48:29 +00:00
2022-09-01 17:40:58 +00:00
public class SvekNode implements Positionable, Hideable {
2011-08-08 17:48:29 +00:00
private final ShapeType type;
2022-09-12 20:08:34 +00:00
private XDimension2D dimImage;
2011-08-08 17:48:29 +00:00
private final String uid;
private final int color;
private double minX;
private double minY;
2022-08-26 16:00:28 +00:00
private Margins shield;
2011-08-08 17:48:29 +00:00
2013-12-10 19:36:50 +00:00
private final EntityPosition entityPosition;
private final IEntityImage image;
2022-08-26 16:00:28 +00:00
private final StringBounder stringBounder;
2013-12-10 19:36:50 +00:00
public EntityPosition getEntityPosition() {
return entityPosition;
}
2011-08-08 17:48:29 +00:00
private Cluster cluster;
public final Cluster getCluster() {
return cluster;
}
public final void setCluster(Cluster cluster) {
this.cluster = cluster;
}
2015-09-28 20:42:17 +00:00
@Override
public String toString() {
return super.toString() + " " + image + " " + type;
}
2023-02-06 21:04:53 +00:00
private final Entity leaf;
2023-01-13 17:36:46 +00:00
public final Together getTogether() {
if (leaf == null)
return null;
return leaf.getTogether();
}
2020-02-18 21:24:31 +00:00
2023-02-06 21:04:53 +00:00
SvekNode(Entity ent, IEntityImage image, ColorSequence colorSequence, StringBounder stringBounder) {
2022-08-26 16:00:28 +00:00
this.stringBounder = stringBounder;
2020-02-18 21:24:31 +00:00
this.entityPosition = ent.getEntityPosition();
2011-09-08 10:42:27 +00:00
this.image = image;
2020-02-18 21:24:31 +00:00
this.type = image.getShapeType();
2022-08-26 16:00:28 +00:00
2011-08-08 17:48:29 +00:00
this.color = colorSequence.getValue();
this.uid = String.format("sh%04d", color);
this.leaf = ent;
2011-08-08 17:48:29 +00:00
}
2022-09-12 20:08:34 +00:00
private XDimension2D getDimImage() {
2022-08-26 16:00:28 +00:00
if (dimImage == null)
this.dimImage = image.calculateDimension(stringBounder);
return dimImage;
}
2011-08-08 17:48:29 +00:00
public final ShapeType getType() {
return type;
}
public final double getWidth() {
2022-08-26 16:00:28 +00:00
return getDimImage().getWidth();
2011-08-08 17:48:29 +00:00
}
public final double getHeight() {
2022-08-26 16:00:28 +00:00
return getDimImage().getHeight();
2011-08-08 17:48:29 +00:00
}
2016-08-25 20:45:37 +00:00
public void appendShape(StringBuilder sb, StringBounder stringBounder) {
2016-07-04 19:06:50 +00:00
if (type == ShapeType.RECTANGLE_HTML_FOR_PORTS) {
2016-08-25 20:45:37 +00:00
appendLabelHtmlSpecialForLink(sb, stringBounder);
2022-09-06 08:49:43 +00:00
SvekUtils.println(sb);
2016-07-04 19:06:50 +00:00
return;
}
if (type == ShapeType.RECTANGLE_PORT) {
appendLabelHtmlSpecialForPort(sb, stringBounder);
2022-09-06 08:49:43 +00:00
SvekUtils.println(sb);
return;
}
2018-05-06 19:59:19 +00:00
if (type == ShapeType.RECTANGLE_WITH_CIRCLE_INSIDE) {
appendHtml(sb);
2022-09-06 08:49:43 +00:00
SvekUtils.println(sb);
2018-05-06 19:59:19 +00:00
return;
}
2022-08-26 16:00:28 +00:00
if (type == ShapeType.RECTANGLE && shield().isZero() == false) {
2011-09-07 20:41:58 +00:00
appendHtml(sb);
2022-09-06 08:49:43 +00:00
SvekUtils.println(sb);
2011-09-07 20:41:58 +00:00
return;
}
2011-08-08 17:48:29 +00:00
sb.append(uid);
sb.append(" [");
appendShapeInternal(sb);
sb.append(",");
sb.append("label=\"\"");
sb.append(",");
sb.append("width=" + SvekUtils.pixelToInches(getWidth()));
sb.append(",");
sb.append("height=" + SvekUtils.pixelToInches(getHeight()));
sb.append(",");
2022-02-17 18:27:32 +00:00
sb.append("color=\"" + StringUtils.sharp000000(color) + "\"");
2011-08-08 17:48:29 +00:00
sb.append("];");
SvekUtils.println(sb);
}
2022-09-01 17:40:58 +00:00
private double getMaxWidthFromLabelForEntryExit(StringBounder stringBounder) {
if (image instanceof EntityImagePort) {
final EntityImagePort im = (EntityImagePort) image;
return im.getMaxWidthFromLabelForEntryExit(stringBounder);
}
if (image instanceof EntityImageStateBorder) {
final EntityImageStateBorder im = (EntityImageStateBorder) image;
return im.getMaxWidthFromLabelForEntryExit(stringBounder);
}
throw new UnsupportedOperationException();
}
private void appendLabelHtmlSpecialForPort(StringBuilder sb, StringBounder stringBounder) {
final int width1 = (int) getWidth();
final int width2 = (int) getMaxWidthFromLabelForEntryExit(stringBounder);
if (width2 > 40)
appendLabelHtmlSpecialForPortHtml(sb, stringBounder, width2 - 40);
else
appendLabelHtmlSpecialForPortBasic(sb, stringBounder);
}
private void appendLabelHtmlSpecialForPortHtml(StringBuilder sb, StringBounder stringBounder, int fullWidth) {
if (fullWidth < 10)
fullWidth = 10;
sb.append(uid);
sb.append(" [");
sb.append("shape=plaintext");
sb.append(",");
sb.append("label=<");
sb.append("<TABLE BORDER=\"0\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">");
sb.append("<TR><TD WIDTH=\"" + fullWidth + "\" HEIGHT=\"1\" COLSPAN=\"3\"></TD></TR>");
sb.append("<TR><TD></TD><TD FIXEDSIZE=\"TRUE\" PORT=\"P\" BORDER=\"1\" COLOR=\""
+ StringUtils.sharp000000(color) + "\" WIDTH=\"" + (int) getWidth() + "\" HEIGHT=\"" + (int) getHeight()
+ "\"></TD><TD></TD></TR>");
sb.append("<TR><TD WIDTH=\"" + fullWidth + "\" HEIGHT=\"1\" COLSPAN=\"3\"></TD></TR>");
sb.append("</TABLE>");
sb.append(">];");
}
private void appendLabelHtmlSpecialForPortBasic(StringBuilder sb, StringBounder stringBounder) {
sb.append(uid);
sb.append(" [");
sb.append("shape=rect");
sb.append(",");
sb.append("label=\"\"");
sb.append(",");
sb.append("width=" + SvekUtils.pixelToInches(getWidth()));
sb.append(",");
sb.append("height=" + SvekUtils.pixelToInches(getHeight()));
sb.append(",");
sb.append("color=\"" + StringUtils.sharp000000(color) + "\"");
sb.append("];");
}
2011-08-08 17:48:29 +00:00
2022-08-26 16:00:28 +00:00
private Margins shield() {
if (shield == null) {
this.shield = image.getShield(stringBounder);
if (shield.isZero() == false && type != ShapeType.RECTANGLE && type != ShapeType.RECTANGLE_HTML_FOR_PORTS
&& type != ShapeType.RECTANGLE_WITH_CIRCLE_INSIDE)
throw new IllegalStateException();
}
return shield;
}
2011-09-07 20:41:58 +00:00
private void appendHtml(StringBuilder sb) {
sb.append(uid);
sb.append(" [");
sb.append("shape=plaintext,");
sb.append("label=<");
appendLabelHtml(sb);
sb.append(">");
sb.append("];");
SvekUtils.println(sb);
}
private void appendLabelHtml(StringBuilder sb) {
2013-12-10 19:36:50 +00:00
// Log.println("shield=" + shield);
2011-09-07 20:41:58 +00:00
sb.append("<TABLE BORDER=\"0\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">");
sb.append("<TR>");
appendTd(sb);
2022-08-26 16:00:28 +00:00
appendTd(sb, 1, shield().getY1());
2011-09-07 20:41:58 +00:00
appendTd(sb);
sb.append("</TR>");
sb.append("<TR>");
2022-08-26 16:00:28 +00:00
appendTd(sb, shield().getX1(), 1);
2022-02-17 18:27:32 +00:00
sb.append("<TD BGCOLOR=\"" + StringUtils.sharp000000(color) + "\"");
2011-09-07 20:41:58 +00:00
sb.append(" FIXEDSIZE=\"TRUE\" WIDTH=\"" + getWidth() + "\" HEIGHT=\"" + getHeight() + "\"");
sb.append(" PORT=\"h\">");
sb.append("</TD>");
2022-08-26 16:00:28 +00:00
appendTd(sb, shield().getX2(), 1);
2011-09-07 20:41:58 +00:00
sb.append("</TR>");
sb.append("<TR>");
appendTd(sb);
2022-08-26 16:00:28 +00:00
appendTd(sb, 1, shield().getY2());
2011-09-07 20:41:58 +00:00
appendTd(sb);
sb.append("</TR>");
sb.append("</TABLE>");
}
2016-08-25 20:45:37 +00:00
private void appendLabelHtmlSpecialForLink(StringBuilder sb, StringBounder stringBounder) {
final Ports ports = ((WithPorts) this.image).getPorts(stringBounder);
2016-07-04 19:06:50 +00:00
sb.append(uid);
sb.append(" [");
sb.append("shape=plaintext,");
// sb.append("color=\"" + StringUtils.getAsHtml(color) + "\",");
sb.append("label=<");
2022-02-17 18:27:32 +00:00
sb.append("<TABLE BGCOLOR=\"" + StringUtils.sharp000000(color)
2016-07-04 19:06:50 +00:00
+ "\" BORDER=\"0\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\">");
double position = 0;
for (PortGeometry geom : ports.getAllPortGeometry()) {
final String portId = geom.getId();
2016-07-04 19:06:50 +00:00
final double missing = geom.getPosition() - position;
appendTr(sb, null, missing);
2020-04-19 16:04:39 +00:00
appendTr(sb, portId, geom.getHeight());
2016-07-04 19:06:50 +00:00
position = geom.getLastY();
}
appendTr(sb, null, getHeight() - position);
sb.append("</TABLE>");
sb.append(">");
sb.append("];");
SvekUtils.println(sb);
}
2020-04-19 16:04:39 +00:00
private void appendTr(StringBuilder sb, String portId, double height) {
2022-08-24 16:46:33 +00:00
if (height <= 0)
2016-07-04 19:06:50 +00:00
return;
2022-08-24 16:46:33 +00:00
2016-07-04 19:06:50 +00:00
sb.append("<TR>");
sb.append("<TD ");
sb.append(" FIXEDSIZE=\"TRUE\" WIDTH=\"" + getWidth() + "\" HEIGHT=\"" + height + "\"");
2022-08-24 16:46:33 +00:00
if (portId != null)
2020-04-19 16:04:39 +00:00
sb.append(" PORT=\"" + portId + "\"");
2022-08-24 16:46:33 +00:00
2016-07-04 19:06:50 +00:00
sb.append(">");
sb.append("</TD>");
sb.append("</TR>");
}
2017-04-05 17:37:42 +00:00
private void appendTd(StringBuilder sb, double w, double h) {
2011-09-07 20:41:58 +00:00
sb.append("<TD");
sb.append(" FIXEDSIZE=\"TRUE\" WIDTH=\"" + w + "\" HEIGHT=\"" + h + "\"");
sb.append(">");
sb.append("</TD>");
}
private void appendTd(StringBuilder sb) {
sb.append("<TD>");
sb.append("</TD>");
}
2011-08-08 17:48:29 +00:00
private void appendShapeInternal(StringBuilder sb) {
2022-08-26 16:00:28 +00:00
if (type == ShapeType.RECTANGLE && shield().isZero() == false)
2011-09-07 20:41:58 +00:00
throw new UnsupportedOperationException();
2022-08-24 16:46:33 +00:00
else if (type == ShapeType.RECTANGLE || type == ShapeType.RECTANGLE_WITH_CIRCLE_INSIDE
|| type == ShapeType.FOLDER)
2011-08-08 17:48:29 +00:00
sb.append("shape=rect");
2022-08-24 16:46:33 +00:00
else if (type == ShapeType.RECTANGLE_HTML_FOR_PORTS)
2016-07-04 19:06:50 +00:00
throw new UnsupportedOperationException();
2022-08-24 16:46:33 +00:00
else if (type == ShapeType.OCTAGON)
2015-04-07 18:18:37 +00:00
sb.append("shape=octagon");
2022-08-24 16:46:33 +00:00
else if (type == ShapeType.HEXAGON)
2021-02-02 10:12:15 +00:00
sb.append("shape=hexagon");
2022-08-24 16:46:33 +00:00
else if (type == ShapeType.DIAMOND)
2011-08-08 17:48:29 +00:00
sb.append("shape=diamond");
2022-08-24 16:46:33 +00:00
else if (type == ShapeType.CIRCLE)
2011-08-08 17:48:29 +00:00
sb.append("shape=circle");
2022-08-24 16:46:33 +00:00
else if (type == ShapeType.OVAL)
2011-08-08 17:48:29 +00:00
sb.append("shape=ellipse");
2022-08-24 16:46:33 +00:00
else if (type == ShapeType.ROUND_RECTANGLE)
2011-08-08 17:48:29 +00:00
sb.append("shape=rect,style=rounded");
2022-08-24 16:46:33 +00:00
else
2011-08-08 17:48:29 +00:00
throw new IllegalStateException(type.toString());
2022-08-24 16:46:33 +00:00
2011-08-08 17:48:29 +00:00
}
public final String getUid() {
2022-08-24 16:46:33 +00:00
if (uid == null)
2011-08-08 17:48:29 +00:00
throw new IllegalStateException();
2022-08-24 16:46:33 +00:00
2011-08-08 17:48:29 +00:00
return uid;
}
public final double getMinX() {
return minX;
}
public final double getMinY() {
return minY;
}
public IEntityImage getImage() {
return image;
}
2022-09-12 20:08:34 +00:00
public XPoint2D getPosition() {
return new XPoint2D(minX, minY);
2011-08-08 17:48:29 +00:00
}
2022-09-12 20:08:34 +00:00
public XDimension2D getSize() {
2022-08-26 16:00:28 +00:00
return getDimImage();
2011-08-08 17:48:29 +00:00
}
2023-02-26 18:51:17 +00:00
public RectangleArea getRectangleArea() {
return new RectangleArea(minX, minY, minX + getWidth(), minY + getHeight());
2013-12-10 19:36:50 +00:00
}
2011-09-07 20:41:58 +00:00
public boolean isShielded() {
2022-08-26 16:00:28 +00:00
return shield().isZero() == false;
2011-09-07 20:41:58 +00:00
}
2011-09-08 10:42:27 +00:00
public void moveSvek(double deltaX, double deltaY) {
this.minX += deltaX;
this.minY += deltaY;
}
2013-12-10 19:36:50 +00:00
public boolean isHidden() {
return image.isHidden();
2011-09-08 10:42:27 +00:00
}
2013-12-10 19:36:50 +00:00
2021-02-02 10:12:15 +00:00
private Shadowable polygon;
2015-04-07 18:18:37 +00:00
2022-09-12 20:08:34 +00:00
public void setPolygon(double minX, double minY, List<XPoint2D> points) {
2021-02-02 10:12:15 +00:00
this.polygon = new UPolygon(points).translate(-minX, -minY);
2015-04-07 18:18:37 +00:00
}
2021-02-02 10:12:15 +00:00
public Shadowable getPolygon() {
return polygon;
2015-04-07 18:18:37 +00:00
}
2022-09-12 20:08:34 +00:00
public XPoint2D getPoint2D(double x, double y) {
return new XPoint2D(minX + x, minY + y);
2015-05-31 18:56:03 +00:00
}
2018-12-22 11:11:40 +00:00
public double getOverscanX(StringBounder stringBounder) {
return image.getOverscanX(stringBounder);
}
2019-04-21 20:40:01 +00:00
public void addImpact(double angle) {
((EntityImageLollipopInterface) image).addImpact(angle);
}
2022-08-26 16:00:28 +00:00
public void drawKals(UGraphic ug) {
2023-02-06 21:04:53 +00:00
if (leaf instanceof Entity == false)
2022-08-26 16:00:28 +00:00
return;
2023-02-26 18:51:17 +00:00
drawList(ug, leaf.getKals(Direction.DOWN));
drawList(ug, leaf.getKals(Direction.UP));
drawList(ug, leaf.getKals(Direction.LEFT));
drawList(ug, leaf.getKals(Direction.RIGHT));
2022-08-26 16:00:28 +00:00
}
public void fixOverlap() {
2023-02-06 21:04:53 +00:00
if (leaf instanceof Entity == false)
2022-08-26 16:00:28 +00:00
return;
2023-02-26 18:51:17 +00:00
fixHoverlap(leaf.getKals(Direction.DOWN));
fixHoverlap(leaf.getKals(Direction.UP));
2022-08-26 16:00:28 +00:00
}
private void fixHoverlap(final List<Kal> list) {
final LineOfSegments los = new LineOfSegments();
for (Kal kal : list)
los.addSegment(kal.getX1(), kal.getX2());
final double[] res = los.solveOverlaps();
for (int i = 0; i < list.size(); i++) {
final Kal kal = list.get(i);
final double diff = res[i] - kal.getX1();
kal.moveX(diff);
}
}
private void drawList(UGraphic ug, final List<Kal> list) {
for (Kal kal : list)
kal.drawU(ug);
}
2023-02-26 18:51:17 +00:00
// public XPoint2D projection(XPoint2D pt, StringBounder stringBounder) {
// if (getType() != ShapeType.FOLDER)
// return pt;
//
//// final ClusterPosition clusterPosition = new ClusterPosition(minX, minY, minX + getWidth(), minY + getHeight());
//// if (clusterPosition.isPointJustUpper(pt)) {
//// final XDimension2D dimName = ((EntityImageDescription) image).getNameDimension(stringBounder);
//// if (pt.getX() < minX + dimName.getWidth())
//// return pt;
////
//// return new XPoint2D(pt.getX(), pt.getY() + dimName.getHeight() + 4);
//// }
// return pt;
//}
public MagneticBorder getMagneticBorder() {
return new MagneticBorder() {
public UTranslate getForceAt(StringBounder stringBounder, XPoint2D position) {
final MagneticBorder orig = image.getMagneticBorder();
return orig.getForceAt(stringBounder, position.move(-minX, -minY));
}
};
// return image.getMagneticBorder();
// if (getType() != ShapeType.FOLDER)
// return new MagneticBorderNone();
//
// return new MagneticBorder() {
// @Override
// public UTranslate getForceAt(XPoint2D pt) {
// if ((pt.getX() >= minX && pt.getX() <= minX + getWidth() && pt.getY() <= minY)) {
// final XDimension2D dimName = ((EntityImageDescription) image).getNameDimension(stringBounder);
// if (pt.getX() < minX + dimName.getWidth())
// return new UTranslate();
//
// return new UTranslate(0, dimName.getHeight() + 4);
// }
// return new UTranslate();
// }
// };
}
2011-08-08 17:48:29 +00:00
}