plantuml/src/net/sourceforge/plantuml/sequencediagram/graphic/DrawableSet.java

459 lines
15 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2023-02-22 18:43:48 +00:00
* (C) Copyright 2009-2024, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2023-02-22 18:43:48 +00:00
* Project Info: https://plantuml.com
2010-11-15 20:35:36 +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:
*
2023-02-22 18:43:48 +00:00
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
2017-03-15 19:13:31 +00:00
*
2010-11-15 20:35:36 +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
2010-11-15 20:35:36 +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.sequencediagram.graphic;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
2021-05-09 21:14:40 +00:00
import java.util.Objects;
2010-11-15 20:35:36 +00:00
import java.util.Set;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.UClip;
import net.sourceforge.plantuml.klimt.UStroke;
import net.sourceforge.plantuml.klimt.UTranslate;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.drawing.UGraphic;
import net.sourceforge.plantuml.klimt.drawing.txt.UGraphicTxt;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.klimt.font.StringBounder;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.geom.XDimension2D;
2023-02-26 18:51:17 +00:00
import net.sourceforge.plantuml.klimt.shape.AbstractTextBlock;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.klimt.shape.TextBlock;
2022-01-04 17:21:17 +00:00
import net.sourceforge.plantuml.sequencediagram.Doll;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.sequencediagram.Event;
import net.sourceforge.plantuml.sequencediagram.Newpage;
import net.sourceforge.plantuml.sequencediagram.Participant;
import net.sourceforge.plantuml.sequencediagram.ParticipantEnglober;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.skin.Area;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.skin.Component;
import net.sourceforge.plantuml.skin.ComponentType;
import net.sourceforge.plantuml.skin.Context2D;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.skin.LineParam;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.skin.SimpleContext2D;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.skin.SkinParamBackcolored;
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.skin.rose.Rose;
2023-02-22 18:43:48 +00:00
import net.sourceforge.plantuml.style.ISkinParam;
2023-02-02 17:59:43 +00:00
import net.sourceforge.plantuml.url.Url;
2010-11-15 20:35:36 +00:00
2015-04-07 18:18:37 +00:00
public class DrawableSet {
2010-11-15 20:35:36 +00:00
private final Map<Participant, LivingParticipantBox> participants = new LinkedHashMap<Participant, LivingParticipantBox>();
private final Map<Event, GraphicalElement> events = new HashMap<Event, GraphicalElement>();
2011-02-14 11:56:34 +00:00
private final Map<Participant, ParticipantEnglober> participantEnglobers2 = new LinkedHashMap<Participant, ParticipantEnglober>();
2021-05-14 08:42:57 +00:00
private final List<Event> eventsList = new ArrayList<>();
2019-03-01 22:16:29 +00:00
private final Rose skin;
2010-11-15 20:35:36 +00:00
private final ISkinParam skinParam;
2022-09-12 20:08:34 +00:00
private XDimension2D dimension;
2010-11-15 20:35:36 +00:00
private double topStartingY;
2019-03-01 22:16:29 +00:00
DrawableSet(Rose skin, ISkinParam skinParam) {
2021-05-14 08:42:57 +00:00
this.skin = Objects.requireNonNull(skin);
this.skinParam = Objects.requireNonNull(skinParam);
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
public ParticipantBox getVeryfirst() {
return participants.values().iterator().next().getParticipantBox();
}
2019-03-01 22:16:29 +00:00
public final Rose getSkin() {
2010-11-15 20:35:36 +00:00
return skin;
}
public final ISkinParam getSkinParam() {
return skinParam;
}
public Collection<Event> getAllEvents() {
return Collections.unmodifiableCollection(eventsList);
}
public Set<Participant> getAllParticipants() {
return Collections.unmodifiableSet(participants.keySet());
}
public Collection<LivingParticipantBox> getAllLivingParticipantBox() {
return Collections.unmodifiableCollection(participants.values());
}
public Collection<GraphicalElement> getAllGraphicalElements() {
2021-05-14 08:42:57 +00:00
final Collection<GraphicalElement> result = new ArrayList<>();
for (Event ev : eventsList)
2010-11-15 20:35:36 +00:00
result.add(events.get(ev));
2010-11-15 20:35:36 +00:00
return Collections.unmodifiableCollection(result);
}
public LivingParticipantBox getLivingParticipantBox(Participant p) {
return participants.get(p);
}
public GraphicalElement getEvent(Event ev) {
return events.get(ev);
}
public double getHeadHeight(StringBounder stringBounder) {
double r = 0;
for (Participant p : participants.keySet()) {
final double y = getHeadAndEngloberHeight(p, stringBounder);
r = Math.max(r, y);
}
return r;
}
public double getHeadAndEngloberHeight(Participant p, StringBounder stringBounder) {
final LivingParticipantBox box = participants.get(p);
final double height = box.getParticipantBox().getHeadHeight(stringBounder);
2022-01-04 17:21:17 +00:00
final Doll doll = getParticipantEnglober(p, stringBounder);
if (doll == null)
2010-11-15 20:35:36 +00:00
return height;
final Component comp = skin.createComponent(doll.getUsedStyles(), ComponentType.ENGLOBER, null, skinParam,
doll.getParticipantEnglober().getTitle());
2010-11-15 20:35:36 +00:00
final double heightEnglober = comp.getPreferredHeight(stringBounder);
return height + heightEnglober;
}
2022-01-04 17:21:17 +00:00
public List<Doll> getExistingParticipantEnglober(StringBounder stringBounder) {
final List<Doll> result = new ArrayList<>();
Doll pending = null;
2011-02-14 11:56:34 +00:00
for (Map.Entry<Participant, ParticipantEnglober> ent : participantEnglobers2.entrySet()) {
final ParticipantEnglober englober = ent.getValue();
if (englober == null) {
pending = null;
continue;
}
assert englober != null;
if (pending != null && englober == pending.getParticipantEnglober()) {
2022-01-04 17:21:17 +00:00
pending.addParticipant(ent.getKey());
2011-02-14 11:56:34 +00:00
continue;
}
2022-01-04 17:21:17 +00:00
pending = Doll.createPuma(englober, ent.getKey(), getSkinParam(), skin, stringBounder,
2019-07-14 20:09:26 +00:00
skinParam.getCurrentStyleBuilder());
2011-02-14 11:56:34 +00:00
result.add(pending);
}
return Collections.unmodifiableList(result);
}
2010-11-15 20:35:36 +00:00
public double getOffsetForEnglobers(StringBounder stringBounder) {
double result = 0;
2022-01-04 17:21:17 +00:00
for (Doll englober : getExistingParticipantEnglober(stringBounder)) {
final Component comp = skin.createComponent(null, ComponentType.ENGLOBER, null, skinParam,
englober.getParticipantEnglober().getTitle());
2010-11-15 20:35:36 +00:00
final double height = comp.getPreferredHeight(stringBounder);
if (height > result)
2010-11-15 20:35:36 +00:00
result = height;
2010-11-15 20:35:36 +00:00
}
return result;
}
static private final int MARGIN_FOR_ENGLOBERS = 4;
static private final int MARGIN_FOR_ENGLOBERS1 = 2;
public double getTailHeight(StringBounder stringBounder, boolean showTail) {
final double marginForEnglobers = getExistingParticipantEnglober(stringBounder).size() > 0
? MARGIN_FOR_ENGLOBERS
2016-08-25 20:45:37 +00:00
: 0;
2010-11-15 20:35:36 +00:00
if (showTail == false)
2010-11-15 20:35:36 +00:00
return 1 + marginForEnglobers;
2010-11-15 20:35:36 +00:00
double r = 0;
for (LivingParticipantBox livingParticipantBox : participants.values()) {
final double y = livingParticipantBox.getParticipantBox().getTailHeight(stringBounder);
r = Math.max(r, y);
}
return r + marginForEnglobers;
}
2011-02-14 11:56:34 +00:00
public void addParticipant(Participant p, ParticipantEnglober participantEnglober) {
participants.put(p, null);
participantEnglobers2.put(p, participantEnglober);
}
public void setLivingParticipantBox(Participant p, LivingParticipantBox box) {
if (participants.containsKey(p) == false)
2011-02-14 11:56:34 +00:00
throw new IllegalArgumentException();
2010-11-15 20:35:36 +00:00
participants.put(p, box);
}
public void addEvent(Event event, GraphicalElement object) {
if (events.keySet().contains(event) == false)
2010-11-15 20:35:36 +00:00
eventsList.add(event);
2010-11-15 20:35:36 +00:00
events.put(event, object);
}
public void addEvent(Newpage newpage, GraphicalNewpage object, Event justBefore) {
final int idx = eventsList.indexOf(justBefore);
if (idx == -1)
2010-11-15 20:35:36 +00:00
throw new IllegalArgumentException();
2010-11-15 20:35:36 +00:00
eventsList.add(idx, newpage);
events.put(newpage, object);
assert events.size() == eventsList.size();
}
2022-09-12 20:08:34 +00:00
void setDimension(XDimension2D dim) {
if (dimension != null)
2010-11-15 20:35:36 +00:00
throw new IllegalStateException();
2010-11-15 20:35:36 +00:00
this.dimension = dim;
}
2022-09-12 20:08:34 +00:00
public XDimension2D getDimension() {
2010-11-15 20:35:36 +00:00
return dimension;
}
2018-11-26 18:46:22 +00:00
TextBlock asTextBlock(final double delta, final double width, final Page page, final boolean showTail) {
2023-02-26 18:51:17 +00:00
return new AbstractTextBlock() {
2018-11-26 18:46:22 +00:00
public void drawU(UGraphic ug) {
drawU22(ug, delta, width, page, showTail);
}
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
2018-11-26 18:46:22 +00:00
final double height = page.getHeight();
2022-09-12 20:08:34 +00:00
return new XDimension2D(width, height);
2018-11-26 18:46:22 +00:00
}
};
}
2015-04-07 18:18:37 +00:00
void drawU22(final UGraphic ug, final double delta, double width, Page page, boolean showTail) {
final double height = page.getHeight();
final UGraphic ugTranslated = clipAndTranslate2(delta, width, page, ug);
final SimpleContext2D context = new SimpleContext2D(true);
this.drawDolls(ug, height - MARGIN_FOR_ENGLOBERS1, context);
2015-04-07 18:18:37 +00:00
this.drawPlaygroundU(ugTranslated, context);
this.drawLineU22(ug, showTail, page);
this.drawHeadTailU(ug, page, showTail ? height - getTailHeight(ug.getStringBounder(), true) : 0);
this.drawPlaygroundU(ugTranslated, new SimpleContext2D(false));
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
private UGraphic clipAndTranslate2(final double delta, double width, Page p, UGraphic ug) {
ug = ug.apply(new UClip(0, p.getBodyRelativePosition(), width, p.getBodyHeight() + 1));
2018-11-26 18:46:22 +00:00
ug = ug.apply(getTranslate4(delta));
return ug;
}
private UTranslate getTranslate4(final double delta) {
if (delta > 0)
2020-03-18 10:50:02 +00:00
return UTranslate.dy(-delta);
2018-11-26 18:46:22 +00:00
return new UTranslate();
2010-11-15 20:35:36 +00:00
}
2015-04-07 18:18:37 +00:00
private void drawLineU22(UGraphic ug, boolean showTail, Page page) {
2016-09-29 19:51:18 +00:00
// http://plantuml.sourceforge.net/qa/?qa=4826/lifelines-broken-for-txt-seq-diagrams-when-create-is-used
2023-02-06 21:04:53 +00:00
// ::comment when CORE
2016-09-29 19:51:18 +00:00
final boolean isTxt = ug instanceof UGraphicTxt;
// ::done
2023-02-06 21:04:53 +00:00
// ::uncomment when CORE
// final boolean isTxt = false;
// ::done
2015-04-07 18:18:37 +00:00
for (LivingParticipantBox box : getAllLivingParticipantBox()) {
final double create = box.getCreate();
final double startMin = page.getBodyRelativePosition() - box.magicMargin(ug.getStringBounder());
final double endMax = startMin + page.getBodyHeight() + 2 * box.magicMargin(ug.getStringBounder());
double start = startMin;
if (create > 0) {
if (create > page.getNewpage2())
2015-04-07 18:18:37 +00:00
continue;
2015-04-07 18:18:37 +00:00
if (create >= page.getNewpage1() && create < page.getNewpage2()) {
if (isTxt)
2016-09-29 19:51:18 +00:00
start = (int) create;
else
2016-09-29 19:51:18 +00:00
start += create - page.getNewpage1() + 2 * box.magicMargin(ug.getStringBounder());
2015-04-07 18:18:37 +00:00
}
}
final double myDelta = page.getNewpage1() - page.getHeaderHeight();
2016-06-19 14:16:41 +00:00
box.drawLineU22(ug, start, endMax, showTail, myDelta);
2015-04-07 18:18:37 +00:00
}
}
2010-11-15 20:35:36 +00:00
private void drawHeadTailU(UGraphic ug, Page page, double positionTail) {
2011-04-19 16:50:40 +00:00
for (Map.Entry<Participant, LivingParticipantBox> ent : participants.entrySet()) {
final Participant p = ent.getKey();
final LivingParticipantBox box = ent.getValue();
2010-11-15 20:35:36 +00:00
final double create = box.getCreate();
boolean showHead = true;
if (create > 0) {
if (create > page.getNewpage2())
2010-11-15 20:35:36 +00:00
continue;
if (create >= page.getNewpage1() && create < page.getNewpage2())
2010-11-15 20:35:36 +00:00
showHead = false;
2010-11-15 20:35:36 +00:00
}
2011-04-19 16:50:40 +00:00
final Url url = p.getUrl();
if (url != null)
2013-12-10 19:36:50 +00:00
ug.startUrl(url);
2010-11-15 20:35:36 +00:00
box.getParticipantBox().drawHeadTailU(ug, topStartingY, showHead, positionTail);
if (url != null)
2020-05-17 21:15:50 +00:00
ug.closeUrl();
2010-11-15 20:35:36 +00:00
}
}
private double getMaxX() {
return dimension.getWidth();
}
private double getMaxY() {
return dimension.getHeight();
}
2015-04-07 18:18:37 +00:00
private void drawPlaygroundU(UGraphic ug, Context2D context) {
for (Participant p : getAllParticipants())
2010-11-15 20:35:36 +00:00
drawLifeLineU(ug, p);
for (GraphicalElement element : getAllGraphicalElements())
2010-11-15 20:35:36 +00:00
element.drawU(ug, getMaxX(), context);
2010-11-15 20:35:36 +00:00
}
private void drawDolls(UGraphic ug, double height, Context2D context) {
2022-01-04 17:21:17 +00:00
for (Doll doll : getExistingParticipantEnglober(ug.getStringBounder())) {
double x1 = getX1(doll);
final double x2 = getX2(ug.getStringBounder(), doll);
2010-11-15 20:35:36 +00:00
final Component comp = getEngloberComponent(doll);
2010-11-15 20:35:36 +00:00
final double width = x2 - x1;
final double preferedWidth = getEngloberPreferedWidth(ug.getStringBounder(), doll);
2010-11-15 20:35:36 +00:00
if (preferedWidth > width) {
2011-04-19 16:50:40 +00:00
// if (englober.getFirst2() == englober.getLast2()) {
x1 -= (preferedWidth - width) / 2;
// }
2022-09-12 20:08:34 +00:00
final XDimension2D dim = new XDimension2D(preferedWidth, height);
2013-12-10 19:36:50 +00:00
comp.drawU(ug.apply(new UTranslate(x1, 1)), new Area(dim), context);
2010-11-15 20:35:36 +00:00
} else {
2022-09-12 20:08:34 +00:00
final XDimension2D dim = new XDimension2D(width, height);
2013-12-10 19:36:50 +00:00
comp.drawU(ug.apply(new UTranslate(x1, 1)), new Area(dim), context);
2010-11-15 20:35:36 +00:00
}
}
}
2022-01-04 17:21:17 +00:00
public double getEngloberPreferedWidth(StringBounder stringBounder, Doll doll) {
return getEngloberComponent(doll).getPreferredWidth(stringBounder);
2010-11-15 20:35:36 +00:00
}
2022-01-04 17:21:17 +00:00
private Component getEngloberComponent(Doll doll) {
final ParticipantEnglober participantEnglober = doll.getParticipantEnglober();
final ISkinParam s = participantEnglober.getBoxColor() == null ? skinParam
: new SkinParamBackcolored(skinParam, participantEnglober.getBoxColor());
return skin.createComponent(doll.getUsedStyles(), ComponentType.ENGLOBER, null, s,
2019-07-14 20:09:26 +00:00
participantEnglober.getTitle());
2010-11-15 20:35:36 +00:00
}
2022-01-04 17:21:17 +00:00
public double getX1(Doll doll) {
final Participant first = doll.getFirst2TOBEPRIVATE();
2010-11-15 20:35:36 +00:00
final ParticipantBox firstBox = participants.get(first).getParticipantBox();
return firstBox.getStartingX() + 1;
}
2022-01-04 17:21:17 +00:00
public double getX2(StringBounder stringBounder, Doll doll) {
final Participant last = doll.getLast2TOBEPRIVATE();
2010-11-15 20:35:36 +00:00
final ParticipantBox lastBox = participants.get(last).getParticipantBox();
return lastBox.getMaxX(stringBounder) - 1;
}
private void drawLifeLineU(UGraphic ug, Participant p) {
final LifeLine line = getLivingParticipantBox(p).getLifeLine();
line.finish(getMaxY());
line.drawU(ug, getSkin(), skinParam);
}
2022-01-04 17:21:17 +00:00
private Doll getParticipantEnglober(Participant p, StringBounder stringBounder) {
for (Doll pe : getExistingParticipantEnglober(stringBounder))
if (pe.contains(p))
2010-11-15 20:35:36 +00:00
return pe;
2010-11-15 20:35:36 +00:00
return null;
}
public void setTopStartingY(double topStartingY) {
this.topStartingY = topStartingY;
}
2011-08-08 17:48:29 +00:00
Participant getFirst(Collection<Participant> someParticipants) {
2021-05-14 08:42:57 +00:00
final List<Participant> list = new ArrayList<>(participants.keySet());
2011-08-08 17:48:29 +00:00
int min = -1;
for (Participant p : someParticipants) {
final int n = list.indexOf(p);
assert n != -1;
if (min == -1 || min > n)
2011-08-08 17:48:29 +00:00
min = n;
2011-08-08 17:48:29 +00:00
}
return list.get(min);
}
Participant getLast(Collection<Participant> someParticipants) {
2021-05-14 08:42:57 +00:00
final List<Participant> list = new ArrayList<>(participants.keySet());
2011-08-08 17:48:29 +00:00
int max = -1;
for (Participant p : someParticipants) {
final int n = list.indexOf(p);
assert n != -1;
if (max == -1 || max < n)
2011-08-08 17:48:29 +00:00
max = n;
2011-08-08 17:48:29 +00:00
}
return list.get(max);
}
2016-06-19 14:16:41 +00:00
public double getArrowThickness() {
final UStroke result = skinParam.getThickness(LineParam.sequenceArrow, null);
if (result == null)
2016-06-19 14:16:41 +00:00
return 1;
2016-06-19 14:16:41 +00:00
return result.getThickness();
}
2010-11-15 20:35:36 +00:00
}