1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-01 08:00:48 +00:00
plantuml/src/net/sourceforge/plantuml/sequencediagram/Englober.java

262 lines
8.5 KiB
Java
Raw Normal View History

2015-05-31 18:56:03 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, 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;
2021-05-09 21:14:40 +00:00
import java.util.Objects;
2015-05-31 18:56:03 +00:00
import net.sourceforge.plantuml.Dimension2DDouble;
import net.sourceforge.plantuml.ISkinParam;
2018-06-25 19:05:58 +00:00
import net.sourceforge.plantuml.PaddingParam;
2015-05-31 18:56:03 +00:00
import net.sourceforge.plantuml.SkinParamBackcolored;
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.real.RealUtils;
import net.sourceforge.plantuml.sequencediagram.teoz.LivingSpace;
import net.sourceforge.plantuml.sequencediagram.teoz.TileArguments;
import net.sourceforge.plantuml.skin.Area;
import net.sourceforge.plantuml.skin.Component;
import net.sourceforge.plantuml.skin.ComponentType;
import net.sourceforge.plantuml.skin.Context2D;
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.skin.rose.Rose;
2019-07-14 20:09:26 +00:00
import net.sourceforge.plantuml.style.PName;
import net.sourceforge.plantuml.style.Style;
import net.sourceforge.plantuml.style.StyleBuilder;
2019-08-26 17:07:21 +00:00
import net.sourceforge.plantuml.style.StyleSignature;
2019-07-14 20:09:26 +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;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2015-05-31 18:56:03 +00:00
2019-07-14 20:09:26 +00:00
public class Englober implements WithStyle {
2015-05-31 18:56:03 +00:00
final private ParticipantEnglober participantEnglober;
2021-05-14 08:42:57 +00:00
final private List<Participant> participants = new ArrayList<>();
2015-05-31 18:56:03 +00:00
final private TileArguments tileArguments;
2019-07-14 20:09:26 +00:00
final private StyleBuilder styleBuilder;
2015-05-31 18:56:03 +00:00
final private Real core1;
final private Real core2;
2018-06-12 20:50:45 +00:00
final private boolean isTeoz;
private double marginX = 0;
2015-05-31 18:56:03 +00:00
2019-08-26 17:07:21 +00:00
public StyleSignature getDefaultStyleDefinition() {
2019-07-14 20:09:26 +00:00
return ComponentType.ENGLOBER.getDefaultStyleDefinition();
}
public Style[] getUsedStyles() {
2019-09-14 18:12:04 +00:00
Style tmp = getDefaultStyleDefinition().with(participantEnglober.getStereotype()).getMergedStyle(styleBuilder);
2020-03-18 10:50:02 +00:00
final HColor backColor = participantEnglober.getBoxColor();
2019-07-14 20:09:26 +00:00
if (tmp != null) {
tmp = tmp.eventuallyOverride(PName.BackGroundColor, backColor);
}
return new Style[] { tmp };
}
2019-03-01 22:16:29 +00:00
public static Englober createPuma(ParticipantEnglober englober, Participant first, ISkinParam skinParam, Rose skin,
2019-07-14 20:09:26 +00:00
StringBounder stringBounder, StyleBuilder styleBuilder) {
return new Englober(englober, first, convertFunctionToBeRemoved(skinParam, skin, stringBounder), false,
styleBuilder);
2018-06-12 20:50:45 +00:00
}
public static Englober createTeoz(ParticipantEnglober participantEnglober, Participant first,
2019-07-14 20:09:26 +00:00
TileArguments tileArguments, StyleBuilder styleBuilder) {
return new Englober(participantEnglober, first, tileArguments, true, styleBuilder);
2015-05-31 18:56:03 +00:00
}
2019-03-01 22:16:29 +00:00
private static TileArguments convertFunctionToBeRemoved(ISkinParam skinParam, Rose skin, StringBounder stringBounder) {
2016-08-25 20:45:37 +00:00
final TileArguments result = new TileArguments(stringBounder, null, skin, skinParam, null);
2015-05-31 18:56:03 +00:00
return result;
}
2018-06-12 20:50:45 +00:00
private Englober(ParticipantEnglober participantEnglober, Participant first, TileArguments tileArguments,
2019-07-14 20:09:26 +00:00
boolean isTeoz, StyleBuilder styleBuilder) {
this.styleBuilder = styleBuilder;
2018-06-12 20:50:45 +00:00
this.isTeoz = isTeoz;
2015-05-31 18:56:03 +00:00
this.participantEnglober = participantEnglober;
this.participants.add(first);
2021-05-14 08:42:57 +00:00
this.tileArguments = Objects.requireNonNull(tileArguments);
2015-05-31 18:56:03 +00:00
final double preferredWidth = getPreferredWidth();
if (tileArguments.getLivingSpaces() == null) {
this.core1 = null;
this.core2 = null;
} else {
this.core1 = getMiddle().addFixed(-preferredWidth / 2);
this.core2 = getMiddle().addFixed(preferredWidth / 2);
}
}
public final Participant getFirst2TOBEPRIVATE() {
return participants.get(0);
}
public final Participant getLast2TOBEPRIVATE() {
return participants.get(participants.size() - 1);
}
private Real getMiddle() {
return RealUtils.middle(getPosB(), getPosD());
}
private Real getPosB() {
return getFirstLivingSpace().getPosB();
}
private Real getPosD() {
return getLastLivingSpace().getPosD(tileArguments.getStringBounder());
}
private Real getPosAA() {
final LivingSpace previous = tileArguments.getLivingSpaces().previous(getFirstLivingSpace());
if (previous == null) {
return tileArguments.getOrigin();
}
return previous.getPosD(tileArguments.getStringBounder());
}
private Real getPosZZ() {
final LivingSpace next = tileArguments.getLivingSpaces().next(getLastLivingSpace());
if (next == null) {
2015-07-11 09:32:49 +00:00
// return tileArguments.getMaxAbsolute();
return null;
2015-05-31 18:56:03 +00:00
}
return next.getPosB();
}
private LivingSpace getFirstLivingSpace() {
return tileArguments.getLivingSpace(getFirst2TOBEPRIVATE());
}
private LivingSpace getLastLivingSpace() {
return tileArguments.getLivingSpace(getLast2TOBEPRIVATE());
}
private Component getComponent() {
final ParticipantEnglober englober = getParticipantEnglober();
final ISkinParam s = englober.getBoxColor() == null ? tileArguments.getSkinParam() : new SkinParamBackcolored(
tileArguments.getSkinParam(), englober.getBoxColor());
2019-07-14 20:09:26 +00:00
return tileArguments.getSkin().createComponent(getUsedStyles(), ComponentType.ENGLOBER, null, s,
englober.getTitle());
2015-05-31 18:56:03 +00:00
}
public final ParticipantEnglober getParticipantEnglober() {
return participantEnglober;
}
public boolean contains(Participant p) {
return participants.contains(p);
}
public void add(Participant p) {
if (participants.contains(p)) {
throw new IllegalArgumentException();
}
participants.add(p);
}
@Override
public String toString() {
return "ParticipantEngloberContexted:" + participantEnglober.getTitle().toString() + " " + participants;
}
private double getPreferredWidth() {
2018-06-12 20:50:45 +00:00
if (isTeoz) {
return 10;
}
return getTitleWidth();
}
private double getTitleWidth() {
2015-05-31 18:56:03 +00:00
return getComponent().getPreferredWidth(tileArguments.getStringBounder());
}
public double getPreferredHeight() {
2019-07-14 20:09:26 +00:00
final Component comp = tileArguments.getSkin().createComponent(getUsedStyles(), ComponentType.ENGLOBER, null,
2015-05-31 18:56:03 +00:00
tileArguments.getSkinParam(), getParticipantEnglober().getTitle());
return comp.getPreferredHeight(tileArguments.getStringBounder());
}
public void drawEnglober(UGraphic ug, double height, Context2D context) {
final double x1 = getX1().getCurrentValue() - 4;
final double x2 = getX2().getCurrentValue() + 4;
final Dimension2DDouble dim = new Dimension2DDouble(x2 - x1, height);
getComponent().drawU(ug.apply(new UTranslate(x1, 1)), new Area(dim), context);
}
private Real getX2() {
2018-06-12 20:50:45 +00:00
return RealUtils.max(getPosD(), core2).addFixed(marginX);
2015-05-31 18:56:03 +00:00
}
private Real getX1() {
2018-06-12 20:50:45 +00:00
return RealUtils.min(getPosB(), core1).addFixed(-marginX);
2015-05-31 18:56:03 +00:00
}
public void addInternalConstraints() {
2018-06-12 20:50:45 +00:00
assert isTeoz;
final double titleWidth = getTitleWidth();
final double x1 = getX1().getCurrentValue();
final double x2 = getX2().getCurrentValue();
final double actualWidth = x2 - x1;
if (titleWidth > actualWidth + 20) {
this.marginX = (titleWidth - actualWidth - 20) / 2;
}
2018-06-25 19:05:58 +00:00
getX1().ensureBiggerThan(getPosAA().addFixed(10 + padding()));
2015-07-11 09:32:49 +00:00
final Real posZZ = getPosZZ();
2018-06-25 19:05:58 +00:00
final Real limit = getX2().addFixed(10 + padding());
2015-07-11 09:32:49 +00:00
if (posZZ != null) {
posZZ.ensureBiggerThan(limit);
}
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-05-31 18:56:03 +00:00
public void addConstraintAfter(Englober current) {
2018-06-25 19:05:58 +00:00
current.getX1().ensureBiggerThan(getX2().addFixed(10 + 2 * padding()));
2015-05-31 18:56:03 +00:00
}
2015-07-11 09:32:49 +00:00
public Real getMinX(StringBounder stringBounder) {
return getX1();
}
public Real getMaxX(StringBounder stringBounder) {
2015-11-01 18:37:20 +00:00
return getX2().addFixed(10);
2015-07-11 09:32:49 +00:00
}
2015-05-31 18:56:03 +00:00
}