plantuml/src/net/sourceforge/plantuml/sequencediagram/Doll.java

234 lines
8.0 KiB
Java
Raw Normal View History

2015-05-31 18:56:03 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2015-05-31 18:56:03 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2015-05-31 18:56:03 +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
*
2015-05-31 18:56:03 +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.sequencediagram;
import java.util.ArrayList;
import java.util.List;
2022-01-07 18:04:36 +00:00
import java.util.Objects;
2015-05-31 18:56:03 +00:00
import net.sourceforge.plantuml.ISkinParam;
2018-06-25 19:05:58 +00:00
import net.sourceforge.plantuml.PaddingParam;
2022-01-07 18:04:36 +00:00
import net.sourceforge.plantuml.SkinParamBackcolored;
2022-09-12 20:08:34 +00:00
import net.sourceforge.plantuml.awt.geom.XDimension2D;
2015-07-11 09:32:49 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
2015-05-31 18:56:03 +00:00
import net.sourceforge.plantuml.real.Real;
import net.sourceforge.plantuml.sequencediagram.teoz.LivingSpace;
import net.sourceforge.plantuml.sequencediagram.teoz.TileArguments;
import net.sourceforge.plantuml.skin.Area;
2022-01-07 18:04:36 +00:00
import net.sourceforge.plantuml.skin.Component;
import net.sourceforge.plantuml.skin.ComponentType;
2015-05-31 18:56:03 +00:00
import net.sourceforge.plantuml.skin.Context2D;
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.skin.rose.Rose;
2022-01-07 18:04:36 +00:00
import net.sourceforge.plantuml.style.PName;
import net.sourceforge.plantuml.style.Style;
2019-07-14 20:09:26 +00:00
import net.sourceforge.plantuml.style.StyleBuilder;
2022-03-01 18:11:51 +00:00
import net.sourceforge.plantuml.style.StyleSignatureBasic;
2022-01-07 18:04:36 +00:00
import net.sourceforge.plantuml.style.WithStyle;
2015-05-31 18:56:03 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UTranslate;
2022-01-07 18:04:36 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2015-05-31 18:56:03 +00:00
2022-01-07 18:04:36 +00:00
public class Doll implements WithStyle {
2015-05-31 18:56:03 +00:00
2021-05-14 08:42:57 +00:00
final private List<Participant> participants = new ArrayList<>();
2022-01-07 18:04:36 +00:00
final private ParticipantEnglober englober;
final private StyleBuilder styleBuilder;
final private TileArguments tileArguments;
2022-01-04 17:21:17 +00:00
public static Doll createPuma(ParticipantEnglober englober, Participant first, ISkinParam skinParam, Rose skin,
2019-07-14 20:09:26 +00:00
StringBounder stringBounder, StyleBuilder styleBuilder) {
2022-01-07 18:04:36 +00:00
return new Doll(englober, convertFunctionToBeRemoved(skinParam, skin, stringBounder), styleBuilder, first);
2018-06-12 20:50:45 +00:00
}
2022-01-07 18:04:36 +00:00
public static Doll createTeoz(ParticipantEnglober englober, TileArguments tileArguments) {
return new Doll(englober, tileArguments, tileArguments.getSkinParam().getCurrentStyleBuilder(), null);
2015-05-31 18:56:03 +00:00
}
private static TileArguments convertFunctionToBeRemoved(ISkinParam skinParam, Rose skin,
StringBounder stringBounder) {
2022-01-04 17:21:17 +00:00
return new TileArguments(stringBounder, null, skin, skinParam, null);
2015-05-31 18:56:03 +00:00
}
2022-01-07 18:04:36 +00:00
private Doll(ParticipantEnglober englober, TileArguments tileArguments, StyleBuilder styleBuilder,
2022-01-04 17:21:17 +00:00
Participant first) {
2022-01-07 18:04:36 +00:00
this.englober = Objects.requireNonNull(englober);
this.styleBuilder = styleBuilder;
this.tileArguments = Objects.requireNonNull(tileArguments);
2022-10-05 20:32:57 +00:00
if (first != null)
2022-01-07 18:04:36 +00:00
this.participants.add(first);
}
2022-03-01 18:11:51 +00:00
final public StyleSignatureBasic getStyleSignature() {
2022-02-12 17:27:51 +00:00
return ComponentType.ENGLOBER.getStyleSignature();
2022-01-07 18:04:36 +00:00
}
final public Style[] getUsedStyles() {
2022-03-01 18:11:51 +00:00
Style tmp = getStyleSignature().withTOBECHANGED(englober.getStereotype()).getMergedStyle(styleBuilder);
2022-01-07 18:04:36 +00:00
final HColor backColor = englober.getBoxColor();
if (tmp != null)
tmp = tmp.eventuallyOverride(PName.BackGroundColor, backColor);
return new Style[] { tmp };
}
final public ParticipantEnglober getParticipantEnglober() {
return englober;
}
private Component getComponent() {
final ParticipantEnglober englober = getParticipantEnglober();
final ISkinParam s = englober.getBoxColor() == null ? tileArguments.getSkinParam()
: new SkinParamBackcolored(tileArguments.getSkinParam(), englober.getBoxColor());
return tileArguments.getSkin().createComponent(getUsedStyles(), ComponentType.ENGLOBER, null, s,
englober.getTitle());
2015-05-31 18:56:03 +00:00
}
2022-01-07 18:04:36 +00:00
public double getTitlePreferredHeight() {
final Component comp = tileArguments.getSkin().createComponent(getUsedStyles(), ComponentType.ENGLOBER, null,
tileArguments.getSkinParam(), getParticipantEnglober().getTitle());
return comp.getPreferredHeight(tileArguments.getStringBounder());
2022-01-04 17:21:17 +00:00
}
2015-05-31 18:56:03 +00:00
public final Participant getFirst2TOBEPRIVATE() {
return participants.get(0);
}
public final Participant getLast2TOBEPRIVATE() {
return participants.get(participants.size() - 1);
}
2022-01-07 18:04:36 +00:00
private Real getPosA(StringBounder stringBounder) {
return getFirstLivingSpace().getPosA(stringBounder);
2015-05-31 18:56:03 +00:00
}
2022-01-07 18:04:36 +00:00
private Real getPosB(StringBounder stringBounder) {
return getFirstLivingSpace().getPosB(stringBounder);
2015-05-31 18:56:03 +00:00
}
2022-01-07 18:04:36 +00:00
private Real getPosD(StringBounder stringBounder) {
return getLastLivingSpace().getPosD(stringBounder);
2015-05-31 18:56:03 +00:00
}
2022-01-07 18:04:36 +00:00
private Real getPosE(StringBounder stringBounder) {
return getLastLivingSpace().getPosE(stringBounder);
}
private Real getPosAA(StringBounder stringBounder) {
2015-05-31 18:56:03 +00:00
final LivingSpace previous = tileArguments.getLivingSpaces().previous(getFirstLivingSpace());
if (previous == null) {
return tileArguments.getOrigin();
}
2022-01-07 18:04:36 +00:00
return previous.getPosD(stringBounder);
2015-05-31 18:56:03 +00:00
}
private LivingSpace getFirstLivingSpace() {
return tileArguments.getLivingSpace(getFirst2TOBEPRIVATE());
}
private LivingSpace getLastLivingSpace() {
return tileArguments.getLivingSpace(getLast2TOBEPRIVATE());
}
public boolean contains(Participant p) {
return participants.contains(p);
}
2022-01-04 17:21:17 +00:00
public void addParticipant(Participant p) {
2022-01-07 18:04:36 +00:00
participants.add(Objects.requireNonNull(p));
2022-01-04 17:21:17 +00:00
}
2015-05-31 18:56:03 +00:00
@Override
public String toString() {
2022-01-07 18:04:36 +00:00
return "Doll:" + englober.getTitle().toString() + " " + participants;
2018-06-12 20:50:45 +00:00
}
private double getTitleWidth() {
2015-05-31 18:56:03 +00:00
return getComponent().getPreferredWidth(tileArguments.getStringBounder());
}
2022-01-04 17:21:17 +00:00
public void drawMe(UGraphic ug, double height, Context2D context, Doll group) {
2022-01-07 18:04:36 +00:00
final StringBounder stringBounder = ug.getStringBounder();
final double x1 = getPosA(stringBounder).getCurrentValue() - 4;
final double x2 = getPosE(stringBounder).getCurrentValue() + 4;
2015-05-31 18:56:03 +00:00
2022-01-04 17:21:17 +00:00
if (group != null) {
final double titlePreferredHeight = group.getTitlePreferredHeight();
ug = ug.apply(UTranslate.dy(titlePreferredHeight));
height -= titlePreferredHeight;
}
2022-09-12 20:08:34 +00:00
final XDimension2D dim = new XDimension2D(x2 - x1, height);
2015-05-31 18:56:03 +00:00
getComponent().drawU(ug.apply(new UTranslate(x1, 1)), new Area(dim), context);
}
2022-01-07 18:04:36 +00:00
public void addInternalConstraints(StringBounder stringBounder) {
2018-06-12 20:50:45 +00:00
final double titleWidth = getTitleWidth();
2022-01-07 18:04:36 +00:00
final double x1 = getPosB(stringBounder).getCurrentValue();
final double x2 = getPosD(stringBounder).getCurrentValue();
2018-06-12 20:50:45 +00:00
final double actualWidth = x2 - x1;
2022-01-07 18:04:36 +00:00
final double marginX = (titleWidth + 10 - actualWidth) / 2;
if (marginX > 0) {
getFirstLivingSpace().ensureMarginBefore(marginX);
getLastLivingSpace().ensureMarginAfter(marginX);
}
getPosA(stringBounder).ensureBiggerThan(getPosAA(stringBounder).addFixed(10 + padding()));
2022-01-04 17:21:17 +00:00
2022-01-07 18:04:36 +00:00
}
public void addConstraintAfter(StringBounder stringBounder) {
final LivingSpace next = tileArguments.getLivingSpaces().next(getLastLivingSpace());
if (next == null)
return;
2022-01-04 17:21:17 +00:00
2022-01-07 18:04:36 +00:00
next.getPosA(stringBounder).ensureBiggerThan(getPosE(stringBounder).addFixed(20 + 2 * padding()));
2015-05-31 18:56:03 +00:00
}
2018-06-25 19:05:58 +00:00
private double padding() {
return tileArguments.getSkinParam().getPadding(PaddingParam.BOX);
}
2015-07-11 09:32:49 +00:00
public Real getMinX(StringBounder stringBounder) {
2022-01-07 18:04:36 +00:00
return getPosA(stringBounder);
2015-07-11 09:32:49 +00:00
}
public Real getMaxX(StringBounder stringBounder) {
2022-01-07 18:04:36 +00:00
return getPosE(stringBounder).addFixed(10);
2015-07-11 09:32:49 +00:00
}
2015-05-31 18:56:03 +00:00
}