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

372 lines
10 KiB
Java
Raw Normal View History

2011-08-08 17:48:29 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2011-08-08 17:48:29 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://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:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
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;
import java.awt.geom.Point2D;
2015-04-07 18:18:37 +00:00
import java.util.List;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.Dimension2DDouble;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Hideable;
2022-02-17 18:27:32 +00:00
import net.sourceforge.plantuml.StringUtils;
2022-05-04 17:52:00 +00:00
import net.sourceforge.plantuml.awt.geom.Dimension2D;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.EntityPosition;
2020-02-18 21:24:31 +00:00
import net.sourceforge.plantuml.cucadiagram.IGroup;
import net.sourceforge.plantuml.cucadiagram.ILeaf;
import net.sourceforge.plantuml.cucadiagram.entity.EntityImpl;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.posimo.Positionable;
import net.sourceforge.plantuml.svek.image.AbstractEntityImageBorder;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.svek.image.EntityImageDescription;
2019-04-21 20:40:01 +00:00
import net.sourceforge.plantuml.svek.image.EntityImageLollipopInterface;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ugraphic.Shadowable;
import net.sourceforge.plantuml.ugraphic.UPolygon;
2011-08-08 17:48:29 +00:00
2021-02-02 10:12:15 +00:00
public class SvekNode implements Positionable, IShapePseudo, Hideable {
2011-08-08 17:48:29 +00:00
private final ShapeType type;
private final double width;
private final double height;
private final String uid;
private final int color;
private double minX;
private double minY;
2017-04-05 17:37:42 +00:00
private final 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;
public EntityPosition getEntityPosition() {
return entityPosition;
}
2011-08-08 17:48:29 +00:00
private Cluster cluster;
private final boolean top;
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;
}
2020-02-18 21:24:31 +00:00
private final ILeaf leaf;
private final IGroup group;
2021-02-02 10:12:15 +00:00
SvekNode(ILeaf ent, IEntityImage image, ColorSequence colorSequence, StringBounder stringBounder) {
2020-02-18 21:24:31 +00:00
final Dimension2D dim = image.calculateDimension(stringBounder);
this.entityPosition = ent.getEntityPosition();
2011-09-08 10:42:27 +00:00
this.image = image;
2020-02-18 21:24:31 +00:00
this.top = ent.isTop();
this.type = image.getShapeType();
this.width = dim.getWidth();
this.height = dim.getHeight();
2011-08-08 17:48:29 +00:00
this.color = colorSequence.getValue();
this.uid = String.format("sh%04d", color);
2020-02-18 21:24:31 +00:00
this.shield = image.getShield(stringBounder);
2018-05-06 19:59:19 +00:00
if (shield.isZero() == false && type != ShapeType.RECTANGLE && type != ShapeType.RECTANGLE_HTML_FOR_PORTS
&& type != ShapeType.RECTANGLE_WITH_CIRCLE_INSIDE) {
2011-09-07 20:41:58 +00:00
throw new IllegalArgumentException();
}
2020-02-18 21:24:31 +00:00
if (((EntityImpl) ent).getOriginalGroup() == null) {
this.group = null;
this.leaf = ent;
} else {
this.group = ((EntityImpl) ent).getOriginalGroup();
this.leaf = null;
}
2011-08-08 17:48:29 +00:00
}
public final ShapeType getType() {
return type;
}
public final double getWidth() {
return width;
}
public final double getHeight() {
return height;
}
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);
2016-07-04 19:06:50 +00:00
return;
}
2018-05-06 19:59:19 +00:00
if (type == ShapeType.RECTANGLE_WITH_CIRCLE_INSIDE) {
appendHtml(sb);
return;
}
2017-04-05 17:37:42 +00:00
if (type == ShapeType.RECTANGLE && shield.isZero() == false) {
2011-09-07 20:41:58 +00:00
appendHtml(sb);
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);
}
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);
2017-04-05 17:37:42 +00:00
appendTd(sb, 1, shield.getY1());
2011-09-07 20:41:58 +00:00
appendTd(sb);
sb.append("</TR>");
sb.append("<TR>");
2017-04-05 17:37:42 +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>");
2017-04-05 17:37:42 +00:00
appendTd(sb, shield.getX2(), 1);
2011-09-07 20:41:58 +00:00
sb.append("</TR>");
sb.append("<TR>");
appendTd(sb);
2017-04-05 17:37:42 +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) {
2016-07-04 19:06:50 +00:00
if (height <= 0) {
return;
}
sb.append("<TR>");
sb.append("<TD ");
sb.append(" FIXEDSIZE=\"TRUE\" WIDTH=\"" + getWidth() + "\" HEIGHT=\"" + height + "\"");
2020-04-19 16:04:39 +00:00
if (portId != null) {
sb.append(" PORT=\"" + portId + "\"");
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) {
2017-04-05 17:37:42 +00:00
if (type == ShapeType.RECTANGLE && shield.isZero() == false) {
2011-09-07 20:41:58 +00:00
throw new UnsupportedOperationException();
2018-05-06 19:59:19 +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");
2016-07-04 19:06:50 +00:00
} else if (type == ShapeType.RECTANGLE_HTML_FOR_PORTS) {
throw new UnsupportedOperationException();
2015-04-07 18:18:37 +00:00
} else if (type == ShapeType.OCTAGON) {
sb.append("shape=octagon");
2021-02-02 10:12:15 +00:00
} else if (type == ShapeType.HEXAGON) {
sb.append("shape=hexagon");
2011-08-08 17:48:29 +00:00
} else if (type == ShapeType.DIAMOND) {
sb.append("shape=diamond");
} else if (type == ShapeType.CIRCLE) {
sb.append("shape=circle");
} else if (type == ShapeType.CIRCLE_IN_RECT) {
sb.append("shape=circle");
} else if (type == ShapeType.OVAL) {
sb.append("shape=ellipse");
} else if (type == ShapeType.ROUND_RECTANGLE) {
sb.append("shape=rect,style=rounded");
} else {
throw new IllegalStateException(type.toString());
}
}
public final String getUid() {
if (uid == null) {
throw new IllegalStateException();
}
return uid;
}
public final double getMinX() {
return minX;
}
public final double getMinY() {
return minY;
}
public IEntityImage getImage() {
return image;
}
public final boolean isTop() {
return top;
}
public Point2D getPosition() {
return new Point2D.Double(minX, minY);
}
public Dimension2D getSize() {
return new Dimension2DDouble(width, height);
}
2013-12-10 19:36:50 +00:00
public ClusterPosition getClusterPosition() {
return new ClusterPosition(minX, minY, minX + width, minY + height);
}
2011-09-07 20:41:58 +00:00
public boolean isShielded() {
2017-04-05 17:37:42 +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 double getMaxWidthFromLabelForEntryExit(StringBounder stringBounder) {
if (image instanceof AbstractEntityImageBorder) {
final AbstractEntityImageBorder im = (AbstractEntityImageBorder) image;
return im.getMaxWidthFromLabelForEntryExit(stringBounder);
} else {
final Dimension2D dim = image.calculateDimension(stringBounder);
return dim.getWidth();
}
2011-09-08 10:42:27 +00:00
}
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
2021-02-02 10:12:15 +00:00
public void setPolygon(double minX, double minY, List<Point2D.Double> points) {
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
}
2015-05-31 18:56:03 +00:00
public Point2D getPoint2D(double x, double y) {
return new Point2D.Double(minX + x, minY + y);
}
2015-09-28 20:42:17 +00:00
public Point2D projection(Point2D pt, StringBounder stringBounder) {
if (getType() != ShapeType.FOLDER) {
return pt;
}
final ClusterPosition clusterPosition = new ClusterPosition(minX, minY, minX + width, minY + height);
if (clusterPosition.isPointJustUpper(pt)) {
final Dimension2D dimName = ((EntityImageDescription) image).getNameDimension(stringBounder);
if (pt.getX() < minX + dimName.getWidth()) {
return pt;
}
return new Point2D.Double(pt.getX(), pt.getY() + dimName.getHeight() + 4);
}
return pt;
}
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);
}
2011-08-08 17:48:29 +00:00
}