plantuml/src/net/sourceforge/plantuml/svek/image/EntityImageDescription.java

330 lines
12 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
2017-03-15 19:13:31 +00:00
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2013-12-10 19:36:50 +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
* Modified by : Arno Peterson
*
2013-12-10 19:36:50 +00:00
*
*/
package net.sourceforge.plantuml.svek.image;
2022-02-16 18:59:44 +00:00
import java.util.Collection;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
2013-12-10 19:36:50 +00:00
2019-03-29 22:14:07 +00:00
import net.sourceforge.plantuml.Guillemet;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ISkinParam;
import net.sourceforge.plantuml.Url;
2022-09-12 20:08:34 +00:00
import net.sourceforge.plantuml.awt.geom.XDimension2D;
2022-11-07 19:27:11 +00:00
import net.sourceforge.plantuml.baraye.IEntity;
import net.sourceforge.plantuml.baraye.ILeaf;
2020-12-14 18:31:06 +00:00
import net.sourceforge.plantuml.cucadiagram.BodyFactory;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.Display;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.cucadiagram.EntityPortion;
2017-05-10 19:51:15 +00:00
import net.sourceforge.plantuml.cucadiagram.Link;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.cucadiagram.PortionShower;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.cucadiagram.Stereotype;
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.SymbolContext;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.graphic.TextBlockUtils;
import net.sourceforge.plantuml.graphic.USymbol;
2022-02-17 18:27:32 +00:00
import net.sourceforge.plantuml.graphic.USymbols;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.graphic.color.ColorType;
2017-12-11 21:02:10 +00:00
import net.sourceforge.plantuml.graphic.color.Colors;
import net.sourceforge.plantuml.style.PName;
import net.sourceforge.plantuml.style.SName;
import net.sourceforge.plantuml.style.Style;
2022-03-01 18:11:51 +00:00
import net.sourceforge.plantuml.style.StyleSignatureBasic;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.svek.AbstractEntityImage;
2021-02-02 10:12:15 +00:00
import net.sourceforge.plantuml.svek.Bibliotekon;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.svek.Margins;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.svek.ShapeType;
2021-03-07 12:23:24 +00:00
import net.sourceforge.plantuml.svek.SvekNode;
2021-02-02 10:12:15 +00:00
import net.sourceforge.plantuml.ugraphic.Shadowable;
2016-07-25 19:25:28 +00:00
import net.sourceforge.plantuml.ugraphic.UComment;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
2020-10-12 20:56:58 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphicStencil;
2021-03-07 12:23:24 +00:00
import net.sourceforge.plantuml.ugraphic.UGroupType;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.UStroke;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.ugraphic.UTranslate;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.utils.MathUtils;
2013-12-10 19:36:50 +00:00
2015-07-11 09:32:49 +00:00
public class EntityImageDescription extends AbstractEntityImage {
2013-12-10 19:36:50 +00:00
2015-09-28 20:42:17 +00:00
private final ShapeType shapeType;
2013-12-10 19:36:50 +00:00
final private Url url;
2015-04-07 18:18:37 +00:00
private final TextBlock asSmall;
2013-12-10 19:36:50 +00:00
2015-09-28 20:42:17 +00:00
private final TextBlock name;
2017-04-05 17:37:42 +00:00
private final TextBlock desc;
private TextBlock stereo;
private final boolean hideText;
2017-05-10 19:51:15 +00:00
private final Collection<Link> links;
2017-06-05 11:27:21 +00:00
private final boolean useRankSame;
2018-10-21 19:44:14 +00:00
private final boolean fixCircleLabelOverlapping;
2021-02-02 10:12:15 +00:00
private final Bibliotekon bibliotekon;
private final SymbolContext ctx;
2015-09-28 20:42:17 +00:00
2021-03-07 12:23:24 +00:00
public EntityImageDescription(ILeaf entity, ISkinParam skinParam2, PortionShower portionShower,
Collection<Link> links, SName styleName, Bibliotekon bibliotekon) {
2021-11-15 18:27:27 +00:00
super(entity, entity.getColors().mute(skinParam2));
2020-09-30 20:57:58 +00:00
this.useRankSame = getSkinParam().useRankSame();
2021-02-02 10:12:15 +00:00
this.bibliotekon = bibliotekon;
2020-09-30 20:57:58 +00:00
this.fixCircleLabelOverlapping = getSkinParam().fixCircleLabelOverlapping();
2018-01-28 22:08:15 +00:00
2017-05-10 19:51:15 +00:00
this.links = links;
2017-04-05 17:37:42 +00:00
USymbol symbol = getUSymbol(entity);
2022-02-17 18:27:32 +00:00
if (symbol == USymbols.FOLDER)
2018-05-06 19:59:19 +00:00
this.shapeType = ShapeType.FOLDER;
2022-02-17 18:27:32 +00:00
else if (symbol == USymbols.HEXAGON)
2021-02-02 10:12:15 +00:00
this.shapeType = ShapeType.HEXAGON;
2022-02-17 18:27:32 +00:00
else if (symbol == USymbols.INTERFACE)
2020-09-30 20:57:58 +00:00
this.shapeType = getSkinParam().fixCircleLabelOverlapping() ? ShapeType.RECTANGLE_WITH_CIRCLE_INSIDE
2018-08-26 12:09:50 +00:00
: ShapeType.RECTANGLE;
2022-02-01 20:21:45 +00:00
else
2018-05-06 19:59:19 +00:00
this.shapeType = ShapeType.RECTANGLE;
2022-02-01 20:21:45 +00:00
2022-02-17 18:27:32 +00:00
this.hideText = symbol == USymbols.INTERFACE;
2013-12-10 19:36:50 +00:00
2015-04-07 18:18:37 +00:00
this.url = entity.getUrl99();
2013-12-10 19:36:50 +00:00
2021-11-15 18:27:27 +00:00
final Colors colors = entity.getColors();
2022-04-10 19:24:55 +00:00
2022-09-15 17:24:26 +00:00
final StyleSignatureBasic tmp = StyleSignatureBasic.of(SName.root, SName.element, styleName, symbol.getSName(),
SName.title);
2022-04-10 19:24:55 +00:00
final Stereotype stereotype = entity.getStereotype();
2022-09-15 17:24:26 +00:00
final Style styleTitle = tmp.withTOBECHANGED(stereotype).getMergedStyle(getSkinParam().getCurrentStyleBuilder())
2022-04-10 19:24:55 +00:00
.eventuallyOverride(colors);
final Style styleStereo = tmp.forStereotypeItself(stereotype)
.getMergedStyle(getSkinParam().getCurrentStyleBuilder());
2022-10-18 20:57:44 +00:00
final Style style = StyleSignatureBasic.of(SName.root, SName.element, styleName, symbol.getSName())
.withTOBECHANGED(stereotype).getMergedStyle(getSkinParam().getCurrentStyleBuilder())
.eventuallyOverride(colors);
2022-09-18 17:08:06 +00:00
final HColor forecolor = styleTitle.value(PName.LineColor).asColor(getSkinParam().getIHtmlColorSet());
2022-05-21 09:41:00 +00:00
HColor backcolor = colors.getColor(ColorType.BACK);
2022-04-10 19:24:55 +00:00
if (backcolor == null)
2022-09-18 17:08:06 +00:00
backcolor = styleTitle.value(PName.BackGroundColor).asColor(getSkinParam().getIHtmlColorSet());
2022-04-10 19:24:55 +00:00
2022-09-15 17:24:26 +00:00
final double roundCorner = styleTitle.value(PName.RoundCorner).asDouble();
final double diagonalCorner = styleTitle.value(PName.DiagonalCorner).asDouble();
final double deltaShadow = styleTitle.value(PName.Shadowing).asDouble();
final UStroke stroke = styleTitle.getStroke(colors);
2022-09-18 17:08:06 +00:00
final FontConfiguration fcTitle = styleTitle.getFontConfiguration(getSkinParam().getIHtmlColorSet());
final FontConfiguration fcStereo = styleStereo.getFontConfiguration(getSkinParam().getIHtmlColorSet());
2022-09-15 17:24:26 +00:00
final HorizontalAlignment defaultAlign = styleTitle.getHorizontalAlignment();
2017-04-05 17:37:42 +00:00
2016-12-21 22:10:29 +00:00
assert getStereo() == stereotype;
2020-10-12 20:56:58 +00:00
2021-02-02 10:12:15 +00:00
ctx = new SymbolContext(backcolor, forecolor).withStroke(stroke).withShadow(deltaShadow).withCorner(roundCorner,
diagonalCorner);
2015-04-07 18:18:37 +00:00
2020-09-30 20:57:58 +00:00
final Display codeDisplay = Display.getWithNewlines(entity.getCodeGetName());
2022-11-04 17:36:03 +00:00
if ((entity.getDisplay().equalsLike(codeDisplay) && symbol.getSName() == SName.package_)
|| entity.getDisplay().isWhite())
2022-10-18 20:57:44 +00:00
desc = TextBlockUtils.empty(style.value(PName.MinimumWidth).asDouble(), 0);
else
desc = BodyFactory.create3(entity.getDisplay(), getSkinParam(), defaultAlign, fcTitle,
2023-01-09 19:13:37 +00:00
style.wrapWidth(), styleTitle);
2020-09-30 20:57:58 +00:00
2017-04-05 17:37:42 +00:00
stereo = TextBlockUtils.empty(0, 0);
2015-04-07 18:18:37 +00:00
2022-02-01 20:21:45 +00:00
if (stereotype != null && stereotype.getSprite(getSkinParam()) != null)
2019-03-01 22:16:29 +00:00
stereo = stereotype.getSprite(getSkinParam());
2022-02-01 20:21:45 +00:00
else if (stereotype != null && stereotype.getLabel(Guillemet.DOUBLE_COMPARATOR) != null
&& portionShower.showPortion(EntityPortion.STEREOTYPE, entity))
2020-12-01 21:39:27 +00:00
stereo = Display.getWithNewlines(stereotype.getLabel(getSkinParam().guillemet())).create(fcStereo,
2015-09-28 20:42:17 +00:00
HorizontalAlignment.CENTER, getSkinParam());
2013-12-10 19:36:50 +00:00
2021-03-07 12:23:24 +00:00
name = BodyFactory.create2(getSkinParam().getDefaultTextAlignment(HorizontalAlignment.CENTER), codeDisplay,
2022-09-15 17:24:26 +00:00
getSkinParam(), stereotype, entity, styleTitle);
2015-07-11 09:32:49 +00:00
2022-02-01 20:21:45 +00:00
if (hideText)
asSmall = symbol.asSmall(TextBlockUtils.empty(0, 0), TextBlockUtils.empty(0, 0), TextBlockUtils.empty(0, 0),
2021-03-07 12:23:24 +00:00
ctx, getSkinParam().getStereotypeAlignment());
2022-02-01 20:21:45 +00:00
else
2021-03-07 12:23:24 +00:00
asSmall = symbol.asSmall(name, desc, stereo, ctx, getSkinParam().getStereotypeAlignment());
2022-02-01 20:21:45 +00:00
2017-04-05 17:37:42 +00:00
}
private USymbol getUSymbol(ILeaf entity) {
2020-06-21 20:31:45 +00:00
final USymbol result = entity.getUSymbol() == null ? getSkinParam().componentStyle().toUSymbol()
: entity.getUSymbol();
2021-05-14 08:42:57 +00:00
return Objects.requireNonNull(result);
2013-12-10 19:36:50 +00:00
}
2022-09-12 20:08:34 +00:00
public XDimension2D getNameDimension(StringBounder stringBounder) {
2022-02-01 20:21:45 +00:00
if (hideText)
2022-09-12 20:08:34 +00:00
return new XDimension2D(0, 0);
2022-02-01 20:21:45 +00:00
2015-09-28 20:42:17 +00:00
return name.calculateDimension(stringBounder);
}
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
2015-04-07 18:18:37 +00:00
return asSmall.calculateDimension(stringBounder);
2013-12-10 19:36:50 +00:00
}
2017-04-05 17:37:42 +00:00
@Override
public Margins getShield(StringBounder stringBounder) {
2022-02-01 20:21:45 +00:00
if (hideText == false)
2017-12-11 21:02:10 +00:00
return Margins.NONE;
2022-02-01 20:21:45 +00:00
if (isThereADoubleLink((ILeaf) getEntity(), links))
2018-05-01 17:26:04 +00:00
return Margins.NONE;
2022-02-01 20:21:45 +00:00
if (fixCircleLabelOverlapping == false && hasSomeHorizontalLinkVisible((ILeaf) getEntity(), links))
2017-12-11 21:02:10 +00:00
return Margins.NONE;
2022-02-01 20:21:45 +00:00
if (hasSomeHorizontalLinkDoubleDecorated((ILeaf) getEntity(), links))
2017-12-11 21:02:10 +00:00
return Margins.NONE;
2022-02-01 20:21:45 +00:00
2022-09-12 20:08:34 +00:00
final XDimension2D dimStereo = stereo.calculateDimension(stringBounder);
final XDimension2D dimDesc = desc.calculateDimension(stringBounder);
final XDimension2D dimSmall = asSmall.calculateDimension(stringBounder);
2017-12-11 21:02:10 +00:00
final double x = Math.max(dimStereo.getWidth(), dimDesc.getWidth());
double suppX = x - dimSmall.getWidth();
2022-02-01 20:21:45 +00:00
if (suppX < 1)
2017-12-11 21:02:10 +00:00
suppX = 1;
2022-02-01 20:21:45 +00:00
2017-12-11 21:02:10 +00:00
final double y = MathUtils.max(1, dimDesc.getHeight(), dimStereo.getHeight());
return new Margins(suppX / 2, suppX / 2, y, y);
2017-04-05 17:37:42 +00:00
}
2017-06-05 11:27:21 +00:00
2018-05-01 17:26:04 +00:00
private boolean hasSomeHorizontalLinkVisible(ILeaf leaf, Collection<Link> links) {
2022-02-01 20:21:45 +00:00
for (Link link : links)
if (link.getLength() == 1 && link.contains(leaf) && link.isInvis() == false)
2017-05-10 19:51:15 +00:00
return true;
2022-02-01 20:21:45 +00:00
2017-05-10 19:51:15 +00:00
return false;
}
2018-05-01 17:26:04 +00:00
private boolean isThereADoubleLink(ILeaf leaf, Collection<Link> links) {
2021-05-14 08:42:57 +00:00
final Set<IEntity> others = new HashSet<>();
2018-05-01 17:26:04 +00:00
for (Link link : links) {
if (link.contains(leaf)) {
final IEntity other = link.getOther(leaf);
final boolean changed = others.add(other);
2022-02-01 20:21:45 +00:00
if (changed == false)
2018-05-01 17:26:04 +00:00
return true;
2022-02-01 20:21:45 +00:00
2018-05-01 17:26:04 +00:00
}
}
return false;
}
2017-12-11 21:02:10 +00:00
private boolean hasSomeHorizontalLinkDoubleDecorated(ILeaf leaf, Collection<Link> links) {
2022-02-01 20:21:45 +00:00
for (Link link : links)
if (link.getLength() == 1 && link.contains(leaf) && link.getType().isDoubleDecorated())
2017-12-11 21:02:10 +00:00
return true;
2022-02-01 20:21:45 +00:00
2017-12-11 21:02:10 +00:00
return false;
}
2013-12-10 19:36:50 +00:00
final public void drawU(UGraphic ug) {
2019-12-10 21:45:49 +00:00
ug.draw(new UComment("entity " + getEntity().getCodeGetName()));
2022-02-16 18:59:44 +00:00
final Map<UGroupType, String> typeIDent = new EnumMap<>(UGroupType.class);
typeIDent.put(UGroupType.CLASS, "elem " + getEntity().getCode() + " selected");
typeIDent.put(UGroupType.ID, "elem_" + getEntity().getCode());
ug.startGroup(typeIDent);
2022-02-01 20:21:45 +00:00
if (url != null)
2013-12-10 19:36:50 +00:00
ug.startUrl(url);
2022-02-01 20:21:45 +00:00
if (shapeType == ShapeType.HEXAGON)
2021-02-02 10:12:15 +00:00
drawHexagon(ctx.apply(ug));
2022-02-01 20:21:45 +00:00
2015-04-07 18:18:37 +00:00
asSmall.drawU(ug);
2021-02-02 10:12:15 +00:00
2017-04-05 17:37:42 +00:00
if (hideText) {
final double space = 8;
2022-09-12 20:08:34 +00:00
final XDimension2D dimSmall = asSmall.calculateDimension(ug.getStringBounder());
final XDimension2D dimDesc = desc.calculateDimension(ug.getStringBounder());
2018-12-22 11:11:40 +00:00
final double posx1 = (dimSmall.getWidth() - dimDesc.getWidth()) / 2;
2020-12-01 21:39:27 +00:00
2020-10-12 20:56:58 +00:00
UGraphic ugDesc = ug.apply(new UTranslate(posx1, space + dimSmall.getHeight()));
ugDesc = UGraphicStencil.create(ugDesc, dimDesc);
desc.drawU(ugDesc);
2020-12-01 21:39:27 +00:00
2022-09-12 20:08:34 +00:00
final XDimension2D dimStereo = stereo.calculateDimension(ug.getStringBounder());
2018-12-22 11:11:40 +00:00
final double posx2 = (dimSmall.getWidth() - dimStereo.getWidth()) / 2;
stereo.drawU(ug.apply(new UTranslate(posx2, -space - dimStereo.getHeight())));
2017-04-05 17:37:42 +00:00
}
2015-04-07 18:18:37 +00:00
2022-02-01 20:21:45 +00:00
if (url != null)
2020-05-17 21:15:50 +00:00
ug.closeUrl();
ug.closeGroup();
2013-12-10 19:36:50 +00:00
}
2021-02-02 10:12:15 +00:00
private void drawHexagon(UGraphic ug) {
2022-02-01 20:21:45 +00:00
if (bibliotekon == null)
2021-02-02 10:12:15 +00:00
throw new IllegalStateException();
2022-02-01 20:21:45 +00:00
2021-02-02 10:12:15 +00:00
final SvekNode node = bibliotekon.getNode(getEntity());
final Shadowable hexagon = node.getPolygon();
2021-11-01 14:47:30 +00:00
if (hexagon != null) {
hexagon.setDeltaShadow(ctx.getDeltaShadow());
ug.draw(hexagon);
}
2021-02-02 10:12:15 +00:00
}
2013-12-10 19:36:50 +00:00
public ShapeType getShapeType() {
2015-09-28 20:42:17 +00:00
return shapeType;
2013-12-10 19:36:50 +00:00
}
2018-12-22 11:11:40 +00:00
@Override
public double getOverscanX(StringBounder stringBounder) {
if (hideText) {
2022-09-12 20:08:34 +00:00
final XDimension2D dimSmall = asSmall.calculateDimension(stringBounder);
final XDimension2D dimDesc = desc.calculateDimension(stringBounder);
final XDimension2D dimStereo = stereo.calculateDimension(stringBounder);
2018-12-22 11:11:40 +00:00
final double posx1 = (dimSmall.getWidth() - dimDesc.getWidth()) / 2;
final double posx2 = (dimSmall.getWidth() - dimStereo.getWidth()) / 2;
return MathUtils.max(-posx1, -posx2, 0);
}
return 0;
}
2013-12-10 19:36:50 +00:00
}