Cluster.java
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2024, Arnaud Roques
*
* Project Info: https://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://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
* Contribution : Hisashi Miyashita
*
*
*/
package net.sourceforge.plantuml.svek;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.UnaryOperator;
import net.atmp.CucaDiagram;
import net.sourceforge.plantuml.abel.CucaNote;
import net.sourceforge.plantuml.abel.Entity;
import net.sourceforge.plantuml.abel.EntityPosition;
import net.sourceforge.plantuml.abel.GroupType;
import net.sourceforge.plantuml.abel.Together;
import net.sourceforge.plantuml.core.DiagramType;
import net.sourceforge.plantuml.decoration.symbol.USymbol;
import net.sourceforge.plantuml.decoration.symbol.USymbols;
import net.sourceforge.plantuml.dot.GraphvizVersion;
import net.sourceforge.plantuml.klimt.UChange;
import net.sourceforge.plantuml.klimt.UGroup;
import net.sourceforge.plantuml.klimt.UGroupType;
import net.sourceforge.plantuml.klimt.UShape;
import net.sourceforge.plantuml.klimt.UStroke;
import net.sourceforge.plantuml.klimt.UTranslate;
import net.sourceforge.plantuml.klimt.color.ColorMapper;
import net.sourceforge.plantuml.klimt.color.ColorType;
import net.sourceforge.plantuml.klimt.color.Colors;
import net.sourceforge.plantuml.klimt.color.HColor;
import net.sourceforge.plantuml.klimt.color.HColorSet;
import net.sourceforge.plantuml.klimt.color.HColors;
import net.sourceforge.plantuml.klimt.drawing.LimitFinder;
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
import net.sourceforge.plantuml.klimt.drawing.UGraphicNo;
import net.sourceforge.plantuml.klimt.font.StringBounder;
import net.sourceforge.plantuml.klimt.geom.HorizontalAlignment;
import net.sourceforge.plantuml.klimt.geom.MagneticBorder;
import net.sourceforge.plantuml.klimt.geom.MagneticBorderNone;
import net.sourceforge.plantuml.klimt.geom.Moveable;
import net.sourceforge.plantuml.klimt.geom.RectangleArea;
import net.sourceforge.plantuml.klimt.geom.XDimension2D;
import net.sourceforge.plantuml.klimt.geom.XPoint2D;
import net.sourceforge.plantuml.klimt.shape.TextBlock;
import net.sourceforge.plantuml.klimt.shape.UComment;
import net.sourceforge.plantuml.klimt.shape.UEmpty;
import net.sourceforge.plantuml.klimt.shape.UImage;
import net.sourceforge.plantuml.klimt.shape.UImageSvg;
import net.sourceforge.plantuml.klimt.shape.ULine;
import net.sourceforge.plantuml.klimt.shape.UText;
import net.sourceforge.plantuml.skin.AlignmentParam;
import net.sourceforge.plantuml.skin.PragmaKey;
import net.sourceforge.plantuml.stereo.Stereotype;
import net.sourceforge.plantuml.style.ISkinParam;
import net.sourceforge.plantuml.style.PName;
import net.sourceforge.plantuml.style.SName;
import net.sourceforge.plantuml.style.Style;
import net.sourceforge.plantuml.style.StyleBuilder;
import net.sourceforge.plantuml.style.StyleQueries;
import net.sourceforge.plantuml.style.StyleQuery;
import net.sourceforge.plantuml.svek.image.EntityImageNoteLink;
import net.sourceforge.plantuml.svek.image.EntityImageState;
import net.sourceforge.plantuml.svek.image.EntityImageStateCommon;
import net.sourceforge.plantuml.url.Url;
import net.sourceforge.plantuml.utils.LineLocation;
import net.sourceforge.plantuml.utils.MyCollections;
import net.sourceforge.plantuml.utils.Position;
public class Cluster implements Moveable {
// /* private */ static final String RANK_SAME = "same";
/* private */ static final String RANK_SOURCE = "source";
/* private */ static final String RANK_SINK = "sink";
public final static String CENTER_ID = "za";
private final Cluster parentCluster;
private final Entity group;
private final List<SvekNode> nodes = new ArrayList<>();
private final List<Cluster> children = new ArrayList<>();
private final int color;
private final int colorTitle;
private final int colorNoteTop;
private final int colorNoteBottom;
private final ISkinParam skinParam;
protected final CucaDiagram diagram;
private ClusterHeader clusterHeader;
private XPoint2D xyTitle;
private XPoint2D xyNoteTop;
private XPoint2D xyNoteBottom;
private RectangleArea rectangleArea;
private final LineLocation location;
public void moveDelta(double deltaX, double deltaY) {
if (this.xyNoteTop != null)
this.xyNoteTop = this.xyNoteTop.move(deltaX, deltaY);
if (this.xyNoteBottom != null)
this.xyNoteBottom = this.xyNoteBottom.move(deltaX, deltaY);
if (this.xyTitle != null)
this.xyTitle = this.xyTitle.move(deltaX, deltaY);
if (this.rectangleArea != null)
this.rectangleArea = this.rectangleArea.move(deltaX, deltaY);
}
private Set<EntityPosition> entityPositionsExceptNormal() {
final Set<EntityPosition> result = EnumSet.<EntityPosition>noneOf(EntityPosition.class);
for (SvekNode sh : nodes)
if (sh.getEntityPosition() != EntityPosition.NORMAL)
result.add(sh.getEntityPosition());
return MyCollections.unmodifiableSet(result);
}
public Cluster(LineLocation location, CucaDiagram diagram, ColorSequence colorSequence, Entity root) {
this(location, diagram, null, colorSequence, root);
}
private Cluster(LineLocation location, CucaDiagram diagram, Cluster parentCluster, ColorSequence colorSequence,
Entity group) {
if (group == null)
throw new IllegalStateException();
this.location = location;
this.parentCluster = parentCluster;
this.group = group;
this.diagram = diagram;
this.color = colorSequence.getValue();
this.colorTitle = colorSequence.getValue();
this.colorNoteTop = colorSequence.getValue();
this.colorNoteBottom = colorSequence.getValue();
this.skinParam = group.getColors().mute(diagram.getSkinParam());
}
@Override
public String toString() {
return super.toString() + " " + group;
}
public final Cluster getParentCluster() {
return parentCluster;
}
public void addNode(SvekNode node) {
this.nodes.add(Objects.requireNonNull(node));
node.setCluster(this);
}
public final List<SvekNode> getNodes() {
return MyCollections.unmodifiableList(nodes);
}
public final List<SvekNode> getNodes(EnumSet<EntityPosition> position) {
final List<SvekNode> result = new ArrayList<>();
for (SvekNode node : nodes)
if (position.contains(node.getEntityPosition()))
result.add(node);
return MyCollections.unmodifiableList(result);
}
private List<SvekNode> getNodesOrderedTop(Collection<SvekEdge> lines) {
final List<SvekNode> firsts = new ArrayList<>();
final Map<String, SvekNode> shs = new HashMap<String, SvekNode>();
for (final Iterator<SvekNode> it = nodes.iterator(); it.hasNext();) {
final SvekNode node = it.next();
shs.put(node.getUid(), node);
}
for (SvekEdge l : lines)
if (l.isInverted()) {
final SvekNode sh = shs.get(l.getStartUidPrefix());
if (sh != null && isNormalPosition(sh))
firsts.add(0, sh);
}
return firsts;
}
private boolean isNormalPosition(final SvekNode sh) {
return sh.getEntityPosition() == EntityPosition.NORMAL;
}
private List<SvekNode> getNodesOrderedWithoutTop(Collection<SvekEdge> lines) {
final List<SvekNode> all = new ArrayList<>(nodes);
final Map<String, SvekNode> shs = new HashMap<String, SvekNode>();
for (final Iterator<SvekNode> it = all.iterator(); it.hasNext();) {
final SvekNode sh = it.next();
if (isNormalPosition(sh) == false) {
it.remove();
continue;
}
shs.put(sh.getUid(), sh);
}
for (SvekEdge l : lines)
if (l.isInverted()) {
final SvekNode sh = shs.get(l.getStartUidPrefix());
if (sh != null)
all.remove(sh);
}
return all;
}
public final List<Cluster> getChildren() {
return MyCollections.unmodifiableList(children);
}
public Cluster createChild(LineLocation location, ClusterHeader clusterHeader, ColorSequence colorSequence,
Entity g) {
final Cluster child = new Cluster(location, diagram, this, colorSequence, g);
child.clusterHeader = clusterHeader;
this.children.add(child);
return child;
}
public final Set<Entity> getGroups() {
return Collections.singleton(group);
}
final Entity getGroup() {
return group;
}
public final int getTitleAndAttributeWidth() {
final double minimumWidth = getStyle().value(PName.MinimumWidth).asDouble();
return Math.max(clusterHeader.getTitleAndAttributeWidth(), (int) Math.ceil(minimumWidth));
}
public final int getTitleAndAttributeHeight() {
return clusterHeader.getTitleAndAttributeHeight();
}
public RectangleArea getRectangleArea() {
return rectangleArea;
}
public void setTitlePosition(XPoint2D pos) {
this.xyTitle = pos;
}
public void setNoteTopPosition(XPoint2D pos) {
this.xyNoteTop = pos;
}
public void setNoteBottomPosition(XPoint2D pos) {
this.xyNoteBottom = pos;
}
static public StyleQuery getDefaultStyleDefinition(SName diagramStyleName, USymbol symbol,
GroupType groupType) {
if (diagramStyleName == SName.stateDiagram)
return StyleQueries.STATEDIAG_STATE_GROUP;
if (symbol != null)
return StyleQueries.GROUP.add(diagramStyleName).addSNames(symbol.getSNames());
if (groupType == GroupType.PACKAGE)
return StyleQueries.PACKAGE_GROUP.add(diagramStyleName);
return StyleQueries.GROUP.add(diagramStyleName);
}
public void drawU(UGraphic ug) {
if (group.isHidden())
return;
if (diagram.getPragma().isTrue(PragmaKey.KERMOR)) {
if (xyNoteTop != null)
getCucaNote(Position.TOP).drawU(ug.apply(UTranslate.point(xyNoteTop)));
if (xyNoteBottom != null)
getCucaNote(Position.BOTTOM).drawU(ug.apply(UTranslate.point(xyNoteBottom)));
}
final String fullName = group.getName();
if (fullName.startsWith("##") == false)
ug.draw(new UComment("cluster " + fullName));
// ug.startGroup(Collections.singletonMap(UGroupType.ID, "cluster_" +
// fullName));
final UGroup uGroup = new UGroup(group.getLocation());
uGroup.put(UGroupType.CLASS, "cluster");
uGroup.put(UGroupType.ID, "cluster_" + fullName);
uGroup.put(UGroupType.DATA_ENTITY, group.getName());
uGroup.put(UGroupType.DATA_UID, group.getUid());
uGroup.put(UGroupType.DATA_QUALIFIED_NAME, group.getQuark().getQualifiedName());
ug.startGroup(uGroup);
final Url url = group.getUrl99();
if (url != null)
ug.startUrl(url);
try {
fitToEntryExitPoints(ug.getStringBounder());
drawFrameAndHeader(ug, UnaryOperator.identity());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (url != null)
ug.closeUrl();
ug.closeGroup();
}
}
/**
* Draws the frame and the header of the cluster. This is the only place that
* decides how a cluster looks, so drawU() and getHeaderAreas() cannot drift
* apart: each header block (title, stereotype, state description) goes
* through <code>header</code> before it is drawn, which lets
* getHeaderAreas() measure exactly what drawU() paints.
*/
private void drawFrameAndHeader(UGraphic ug, UnaryOperator<TextBlock> header) {
final DiagramType diagramType = diagram.getDiagramType();
final Style style = getStyle();
final double shadowing = style.getShadowing();
HColor borderColor;
if (group.getColors().getColor(ColorType.LINE) != null)
borderColor = group.getColors().getColor(ColorType.LINE);
else
borderColor = style.value(PName.LineColor).asColor(skinParam.getIHtmlColorSet());
double rounded = style.value(PName.RoundCorner).asDouble();
if (skinParam.strictUmlStyle())
rounded = 0;
final double diagonalCorner = style.value(PName.DiagonalCorner).asDouble();
final TextBlock title = header.apply(clusterHeader.getTitle());
if (skinParam.useSwimlanes(diagramType)) {
drawSwinLinesState(ug, borderColor, title);
return;
}
final boolean isState = diagramType == DiagramType.STATE;
if (isState && group.getUSymbol() == null) {
drawUState(ug, rounded, shadowing, title, header.apply(group.getStateDescription(skinParam)));
return;
}
PackageStyle packageStyle = group.getPackageStyle();
if (packageStyle == null)
packageStyle = skinParam.packageStyle();
final UStroke stroke = getStrokeInternal(group, style);
HColor backColor = getBackColor(style);
backColor = getBackColor(backColor, group.getStereotype(), diagramType.getStyleName(), group.getUSymbol(),
skinParam.getCurrentStyleBuilder(), skinParam.getIHtmlColorSet(), group.getGroupType());
final ClusterDecoration decoration = new ClusterDecoration(packageStyle, group.getUSymbol(), title,
header.apply(clusterHeader.getStereo()), rectangleArea, stroke);
// final HorizontalAlignment horizontalAlignment = skinParam
// .getHorizontalAlignment(AlignmentParam.packageTitleAlignment, null, false, null);
final HorizontalAlignment horizontalAlignment = clusterHeader.getTitleHorizontalAlignment();
decoration.drawU(ug, backColor, borderColor, shadowing, rounded, horizontalAlignment,
skinParam.getStereotypeAlignment(), diagonalCorner);
}
private Style getStyle() {
final DiagramType diagramType = diagram.getDiagramType();
final USymbol uSymbol = group.getUSymbol() == null ? USymbols.PACKAGE : group.getUSymbol();
final Style style = skinParam.getCurrentStyleBuilder().getMergedStyle(getDefaultStyleDefinition(
diagramType.getStyleName(), uSymbol, group.getGroupType()).withStereotype(group.getStereotype()));
return style;
}
EntityImageNoteLink getCucaNote(Position position) {
final List<CucaNote> notes = getGroup().getNotes(position);
if (notes.size() == 0)
return null;
final CucaNote note = notes.get(0);
return new EntityImageNoteLink(note.getDisplay(), note.getColors(), skinParam,
skinParam.getCurrentStyleBuilder());
}
static public UStroke getStrokeInternal(Entity group, Style style) {
final Colors colors = group.getColors();
if (colors.getSpecificLineStroke() != null)
return colors.getSpecificLineStroke();
return style.getStroke();
}
/**
* Moves the frame onto the entry and exit points that sit on its border.
* Drawing does this as well. A caller that needs the final frame before
* anything is drawn can ask for it here.
*/
public void fitToEntryExitPoints(StringBounder stringBounder) {
if (entityPositionsExceptNormal().size() > 0)
manageEntryExitPoint(stringBounder);
}
void manageEntryExitPoint(StringBounder stringBounder) {
final Collection<RectangleArea> insides = new ArrayList<>();
final List<XPoint2D> points = new ArrayList<>();
for (SvekNode sh : nodes)
if (isNormalPosition(sh))
insides.add(sh.getRectangleArea());
else
points.add(sh.getRectangleArea().getPointCenter());
for (Cluster in : children)
if (in.getRectangleArea() == null)
System.err.println("Frontier null for " + in);
else
insides.add(in.getRectangleArea());
final FrontierCalculator frontierCalculator = new FrontierCalculator(getRectangleArea(), insides, points,
skinParam.getRankdir());
if (getTitleAndAttributeWidth() > 0 && getTitleAndAttributeHeight() > 0)
frontierCalculator.ensureMinWidth(getTitleAndAttributeWidth() + 10);
this.rectangleArea = frontierCalculator.getSuggestedPosition();
final double widthTitle = clusterHeader.getTitle().calculateDimension(stringBounder).getWidth();
final double minX = rectangleArea.getMinX();
final double minY = rectangleArea.getMinY();
this.xyTitle = new XPoint2D(minX + ((rectangleArea.getWidth() - widthTitle) / 2), minY + IEntityImage.MARGIN);
}
private void drawSwinLinesState(UGraphic ug, HColor borderColor, TextBlock title) {
if (xyTitle != null)
title.drawU(ug.apply(UTranslate.dx(xyTitle.x)));
final ULine line = ULine.vline(rectangleArea.getHeight());
ug = ug.apply(borderColor);
ug.apply(UTranslate.dx(rectangleArea.getMinX())).draw(line);
ug.apply(UTranslate.dx(rectangleArea.getMaxX())).draw(line);
}
// GroupPngMakerState
private void drawUState(UGraphic ug, double rounded, double shadowing, TextBlock title, TextBlock attribute) {
HColor borderColor = group.getColors().getColor(ColorType.LINE);
final StyleBuilder styleBuilder = skinParam.getCurrentStyleBuilder();
final HColorSet colorSet = skinParam.getIHtmlColorSet();
if (borderColor == null) {
borderColor = EntityImageStateCommon.getStyleState(group.getStereotype(), styleBuilder)
.value(PName.LineColor).asColor(colorSet);
}
HColor northBackcolor = group.getColors().getColor(ColorType.BACK);
HColor centerBackColor = northBackcolor;
HColor southBackcolor = northBackcolor;
if (northBackcolor == null) {
northBackcolor = styleBuilder
.getMergedStyle(
EntityImageStateCommon.STYLE.add(SName.name).withStereotype(group.getStereotype()))
.value(PName.BackGroundColor).asColor(colorSet);
centerBackColor = styleBuilder.getMergedStyle(EntityImageStateCommon.STYLE.add(SName.description)
.withStereotype(group.getStereotype())).value(PName.BackGroundColor).asColor(colorSet);
southBackcolor = styleBuilder
.getMergedStyle(
EntityImageStateCommon.STYLE.add(SName.body).withStereotype(group.getStereotype()))
.value(PName.BackGroundColor).asColor(colorSet);
}
final double attributeHeight = attribute.calculateDimension(ug.getStringBounder()).getHeight();
if (rectangleArea.getDimension().getWidth() == 0) {
System.err.println("Cluster::drawUState issue");
return;
}
UStroke stroke = group.getColors().getSpecificLineStroke();
if (stroke == null) {
final StyleBuilder styleBuilder4 = skinParam.getCurrentStyleBuilder();
stroke = EntityImageStateCommon.getStyleState(group.getStereotype(), styleBuilder4).getStroke();
}
final double descriptionHeight = attributeHeight + (attributeHeight > 0 ? IEntityImage.MARGIN : 0);
final double nameHeight = title.calculateDimension(ug.getStringBounder()).getHeight() + IEntityImage.MARGIN;
final RoundedContainer r = new RoundedContainer(borderColor, rectangleArea.getDimension(), nameHeight,
descriptionHeight, northBackcolor, centerBackColor, southBackcolor, stroke, rounded, shadowing);
r.drawU(ug.apply(rectangleArea.getPosition()));
if (xyTitle != null)
clusterHeader.getTitleHorizontalAlignment().draw(
ug.apply(new UTranslate(rectangleArea.getMinX(), xyTitle.getY())), title,
IEntityImage.MARGIN, 0, rectangleArea.getWidth());
if (attributeHeight > 0)
attribute.drawU(ug.apply(new UTranslate(rectangleArea.getMinX() + IEntityImage.MARGIN,
rectangleArea.getMinY() + nameHeight + IEntityImage.MARGIN / 2.0)));
final Stereotype stereotype = group.getStereotype();
final boolean withSymbol = stereotype != null && stereotype.isWithOOSymbol();
if (withSymbol)
EntityImageState.drawSymbol(ug.apply(borderColor), rectangleArea.getMaxX(), rectangleArea.getMaxY());
}
public void setPosition(XPoint2D min, XPoint2D max) {
this.rectangleArea = RectangleArea.build(min, max);
}
public boolean printCluster1(StringBuilder sb, Collection<SvekEdge> lines, StringBounder stringBounder) {
final List<SvekNode> tmp = getNodesOrderedTop(lines);
if (tmp.size() == 0)
return false;
// A node wrapped for a horizontal link is declared by printCluster2.
final HorizontalLinkOrder horizontalLinkOrder = HorizontalLinkOrder.of(this, lines, skinParam.getRankdir());
for (SvekNode node : tmp)
if (horizontalLinkOrder.isWrapped(node) == false)
node.appendShape(sb, stringBounder);
return true;
}
private int togetherCounter = 0;
private void printTogether(Together together, Collection<Together> otherTogethers, StringBuilder sb,
List<SvekNode> nodesOrderedWithoutTop, StringBounder stringBounder, Collection<SvekEdge> lines,
DotMode dotMode, GraphvizVersion graphvizVersion, DiagramType type) {
sb.append("subgraph " + getClusterId() + "t" + togetherCounter + " {\n");
for (SvekNode node : nodesOrderedWithoutTop)
if (node.getTogether() == together)
node.appendShape(sb, stringBounder);
for (Cluster child : children)
if (child.group.getTogether() == together)
child.printInternal(sb, lines, stringBounder, dotMode, graphvizVersion, type);
for (Together otherTogether : otherTogethers)
if (otherTogether.getParent() == together)
printTogether(otherTogether, otherTogethers, sb, nodesOrderedWithoutTop, stringBounder, lines, dotMode,
graphvizVersion, type);
sb.append("}\n");
togetherCounter++;
}
public SvekNode printCluster2(StringBuilder sb, Collection<SvekEdge> lines, StringBounder stringBounder,
DotMode dotMode, GraphvizVersion graphvizVersion, DiagramType type) {
SvekNode added = null;
final Collection<Together> togethers = new LinkedHashSet<>();
final List<SvekNode> nodesOrderedWithoutTop = getNodesOrderedWithoutTop(lines);
final HorizontalLinkOrder horizontalLinkOrder = HorizontalLinkOrder.of(this, lines, skinParam.getRankdir());
for (SvekNode node : nodesOrderedWithoutTop) {
final Together together = node.getTogether();
if (together != null)
addTogetherWithParents(togethers, together);
else if (horizontalLinkOrder.isWrapped(node) == false)
node.appendShape(sb, stringBounder);
added = node;
}
for (Cluster child : children)
if (child.group.getTogether() != null)
addTogetherWithParents(togethers, child.group.getTogether());
for (Together together : togethers)
if (together.getParent() == null)
printTogether(together, togethers, sb, nodesOrderedWithoutTop, stringBounder, lines, dotMode,
graphvizVersion, type);
if (skinParam.useRankSame() && dotMode != DotMode.NO_LEFT_RIGHT_AND_XLABEL
&& graphvizVersion.ignoreHorizontalLinks() == false)
appendRankSame(sb, lines);
if (horizontalLinkOrder.isEmpty()) {
for (Cluster child : children)
if (child.group.getTogether() == null)
child.printInternal(sb, lines, stringBounder, dotMode, graphvizVersion, type);
} else {
printInHorizontalLinkOrder(sb, horizontalLinkOrder, lines, stringBounder, dotMode, graphvizVersion,
type);
}
return added;
}
private int horizontalCounter = 0;
private void printInHorizontalLinkOrder(StringBuilder sb, HorizontalLinkOrder horizontalLinkOrder,
Collection<SvekEdge> lines, StringBounder stringBounder, DotMode dotMode, GraphvizVersion graphvizVersion,
DiagramType type) {
for (Object item : horizontalLinkOrder.getSequence())
if (item instanceof Cluster) {
((Cluster) item).printInternal(sb, lines, stringBounder, dotMode, graphvizVersion, type);
} else {
sb.append("subgraph " + getClusterId() + "h" + horizontalCounter + " {label=\"\";\n");
((SvekNode) item).appendShape(sb, stringBounder);
sb.append("}\n");
horizontalCounter++;
}
}
private static void addTogetherWithParents(Collection<Together> collection, Together together) {
Together t = together;
while (t != null) {
collection.add(t);
t = t.getParent();
}
}
public void printCluster3_forKermor(StringBuilder sb, Collection<SvekEdge> lines, StringBounder stringBounder,
DotMode dotMode, GraphvizVersion graphvizVersion, DiagramType type) {
final List<SvekNode> tmp = getNodes(EntityPosition.getNormals());
if (tmp.size() == 0) {
sb.append(getClusterId() + "empty [shape=point,label=\"\"];");
SvekUtils.println(sb);
} else {
for (SvekNode node : tmp)
node.appendShape(sb, stringBounder);
}
for (Cluster child : getChildren())
child.printInternal(sb, lines, stringBounder, dotMode, graphvizVersion, type);
}
private void printInternal(StringBuilder sb, Collection<SvekEdge> lines, StringBounder stringBounder,
DotMode dotMode, GraphvizVersion graphvizVersion, DiagramType type) {
new ClusterDotString(this, skinParam).printInternal(sb, lines, stringBounder, dotMode, graphvizVersion, type);
}
private void appendRankSame(StringBuilder sb, Collection<SvekEdge> lines) {
for (String same : getRankSame(lines)) {
sb.append(same);
SvekUtils.println(sb);
}
}
private Set<String> getRankSame(Collection<SvekEdge> lines) {
final Set<String> rankSame = new HashSet<>();
for (SvekEdge l : lines) {
if (l.hasEntryPoint())
continue;
final String startUid = l.getStartUidPrefix();
final String endUid = l.getEndUidPrefix();
if (isInCluster(startUid) && isInCluster(endUid)) {
final String same = l.rankSame();
if (same != null)
rankSame.add(same);
}
}
return rankSame;
}
private boolean isInCluster(String uid) {
for (SvekNode node : nodes)
if (node.getUid().equals(uid))
return true;
return false;
}
public String getClusterId() {
return "cluster" + color;
}
static String getSpecialPointId(Entity group) {
return CENTER_ID + group.getUid();
}
String getMinPoint(DiagramType type) {
if (skinParam.useSwimlanes(type))
return "minPoint" + color;
return null;
}
String getMaxPoint(DiagramType type) {
if (skinParam.useSwimlanes(type))
return "maxPoint" + color;
return null;
}
public boolean isLabel() {
return getTitleAndAttributeHeight() > 0 || getTitleAndAttributeWidth() > 0;
}
int getColor() {
return color;
}
int getTitleColor() {
return colorTitle;
}
private final HColor getBackColor(Style style) {
if (group.isRoot())
return null;
final HColor result = group.getColors().getColor(ColorType.BACK);
if (result != null)
return result;
return style.value(PName.BackGroundColor).asColor(skinParam.getIHtmlColorSet());
}
boolean isClusterOf(Entity ent) {
if (ent.isGroup() == false)
return false;
return group == ent;
}
public static HColor getBackColor(HColor backColor, Stereotype stereotype, SName styleName, USymbol symbol,
StyleBuilder styleBuilder, HColorSet colorSet, GroupType groupType) {
final Style style = styleBuilder.getMergedStyle(getDefaultStyleDefinition(styleName, symbol, groupType));
if (backColor == null)
backColor = style.value(PName.BackGroundColor).asColor(colorSet);
if (backColor == null || backColor.equals(HColors.transparent()))
backColor = HColors.transparent();
return backColor;
}
public final int getColorNoteTop() {
return colorNoteTop;
}
public final int getColorNoteBottom() {
return colorNoteBottom;
}
public XDimension2D getTitleDimension(StringBounder stringBounder) {
return clusterHeader.getTitle().calculateDimension(stringBounder);
}
/**
* The areas of the title, the stereotype and the state description, for
* those that paint something. The frame itself is not part of them.
*/
public List<RectangleArea> getHeaderAreas(StringBounder stringBounder) {
final List<RectangleArea> result = new ArrayList<>();
if (rectangleArea == null)
return result;
final HeaderFinder finder = new HeaderFinder(stringBounder, UTranslate.none(), result,
new AtomicReference<LimitFinder>());
// The frame is already fitted to its entry and exit points: only replay the drawing.
try {
drawFrameAndHeader(finder, finder::probe);
} catch (UnsupportedOperationException e) {
// A package style without a symbol draws no frame and no header either.
}
return result;
}
// Follows the UTranslate of a drawing to learn where the blocks it has
// been given paint something, without painting anything itself.
private static final class HeaderFinder extends UGraphicNo {
private final List<RectangleArea> areas;
// The painted bounds of the block being probed. Empty outside of one,
// so that the frame of the cluster is not recorded.
private final AtomicReference<LimitFinder> probing;
HeaderFinder(StringBounder stringBounder, UTranslate translate, List<RectangleArea> areas,
AtomicReference<LimitFinder> probing) {
super(stringBounder, translate);
this.areas = areas;
this.probing = probing;
}
@Override
public UGraphic apply(UChange change) {
if (change instanceof UTranslate)
return new HeaderFinder(getStringBounder(), getTranslate().compose((UTranslate) change), areas, probing);
return this;
}
// A raster sprite asks for it before it draws its image.
@Override
public ColorMapper getColorMapper() {
return ColorMapper.IDENTITY;
}
// An embedded diagram asks whether it is drawn as SVG.
@Override
public boolean matchesProperty(String propertyName) {
return getStringBounder().matchesProperty(propertyName);
}
// Blank lines and hidden blocks have a size, but they paint nothing.
@Override
public void draw(UShape shape) {
final LimitFinder limits = probing.get();
if (limits == null || shape instanceof UEmpty || shape instanceof UComment
|| (shape instanceof UText && ((UText) shape).getText().trim().isEmpty()))
return;
final UGraphic at = limits.apply(getTranslate());
if (shape instanceof UImage) {
// The dimensions of a UImage are its last pixel coordinates.
final UImage image = (UImage) shape;
at.draw(new UEmpty(image.getWidth() + 1, image.getHeight() + 1));
} else if (shape instanceof UImageSvg) {
final UImageSvg image = (UImageSvg) shape;
if (image.getWidth() > 0 && image.getHeight() > 0)
at.draw(new UEmpty(image.getWidth(), image.getHeight()));
} else {
try {
at.draw(shape);
} catch (UnsupportedOperationException e) {
// A shape without known bounds cannot be routed around.
}
}
}
TextBlock probe(final TextBlock block) {
return new TextBlock() {
@Override
public XDimension2D calculateDimension(StringBounder stringBounder) {
return block.calculateDimension(stringBounder);
}
@Override
public void drawU(UGraphic ug) {
final LimitFinder limits = LimitFinder.create(ug.getStringBounder(), false);
probing.set(limits);
try {
block.drawU(ug);
} finally {
probing.set(null);
}
// The raw limits tell a block that painted nothing from a point at the origin.
if (limits.getMinX() <= limits.getMaxX() && limits.getMinY() <= limits.getMaxY())
areas.add(new RectangleArea(limits.getMinX(), limits.getMinY(), limits.getMaxX(),
limits.getMaxY()));
}
};
}
}
public MagneticBorder getMagneticBorder() {
if (group.getUSymbol() == null && group.getGroupType() != GroupType.PACKAGE)
return new MagneticBorderNone();
final USymbol uSymbol = group.getUSymbol();
PackageStyle packageStyle = group.getPackageStyle();
if (packageStyle == null)
packageStyle = skinParam.packageStyle();
final DiagramType diagramType = DiagramType.CLASS;
final Style style = skinParam.getCurrentStyleBuilder().getMergedStyle(getDefaultStyleDefinition(
diagramType.getStyleName(), uSymbol, group.getGroupType()).withStereotype(group.getStereotype()));
final UStroke stroke = getStrokeInternal(group, style);
final ClusterDecoration decoration = new ClusterDecoration(packageStyle, group.getUSymbol(),
clusterHeader.getTitle(), clusterHeader.getStereo(), rectangleArea, stroke);
final TextBlock textBlock = decoration.getTextBlock(HColors.BLACK, HColors.BLACK, 0, 0,
skinParam.getHorizontalAlignment(AlignmentParam.packageTitleAlignment, null, false, null),
skinParam.getStereotypeAlignment(), 0);
final MagneticBorder orig = textBlock.getMagneticBorder();
return new MagneticBorder() {
public UTranslate getForceAt(StringBounder stringBounder, XPoint2D position) {
return orig.getForceAt(stringBounder,
position.move(-rectangleArea.getMinX(), -rectangleArea.getMinY()));
}
};
}
}