plantuml/src/net/sourceforge/plantuml/timingdiagram/PlayerRobustConcise.java

224 lines
7.6 KiB
Java
Raw Normal View History

2019-03-01 22:16:29 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2019-03-01 22:16:29 +00:00
*
* Project Info: http://plantuml.com
*
* 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
*
* 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.timingdiagram;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import net.sourceforge.plantuml.ISkinParam;
2022-02-10 18:16:18 +00:00
import net.sourceforge.plantuml.UseStyle;
2022-05-04 17:52:00 +00:00
import net.sourceforge.plantuml.awt.geom.Dimension2D;
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.command.Position;
import net.sourceforge.plantuml.cucadiagram.Display;
2022-05-04 17:52:00 +00:00
import net.sourceforge.plantuml.cucadiagram.Stereotype;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.graphic.AbstractTextBlock;
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.graphic.UDrawable;
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.graphic.color.Colors;
2022-02-10 18:16:18 +00:00
import net.sourceforge.plantuml.style.SName;
import net.sourceforge.plantuml.style.Style;
2022-05-04 17:52:00 +00:00
import net.sourceforge.plantuml.style.StyleSignature;
2022-03-01 18:11:51 +00:00
import net.sourceforge.plantuml.style.StyleSignatureBasic;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.timingdiagram.graphic.Histogram;
import net.sourceforge.plantuml.timingdiagram.graphic.IntricatedPoint;
2020-04-26 18:31:41 +00:00
import net.sourceforge.plantuml.timingdiagram.graphic.PDrawing;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.timingdiagram.graphic.PlayerFrame;
import net.sourceforge.plantuml.timingdiagram.graphic.Ribbon;
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UTranslate;
2020-03-18 10:50:02 +00:00
public final class PlayerRobustConcise extends Player {
2019-03-01 22:16:29 +00:00
2021-05-14 08:42:57 +00:00
private final Set<ChangeState> changes = new TreeSet<>();
private final List<TimeConstraint> constraints = new ArrayList<>();
private final List<TimingNote> notes = new ArrayList<>();
2020-03-18 10:50:02 +00:00
private final Map<String, String> statesLabel = new LinkedHashMap<String, String>();
private final TimingStyle type;
2019-03-01 22:16:29 +00:00
2020-03-18 10:50:02 +00:00
private String initialState;
2020-04-26 18:31:41 +00:00
private PDrawing cached;
2020-03-18 10:50:02 +00:00
private Colors initialColors;
2019-03-01 22:16:29 +00:00
2022-05-04 17:52:00 +00:00
public PlayerRobustConcise(TimingStyle type, String full, ISkinParam skinParam, TimingRuler ruler, boolean compact,
Stereotype stereotype) {
super(full, skinParam, ruler, compact, stereotype);
2020-03-18 10:50:02 +00:00
this.type = type;
this.suggestedHeight = 0;
2020-03-18 10:50:02 +00:00
}
2019-03-01 22:16:29 +00:00
2022-02-10 18:16:18 +00:00
@Override
2022-05-04 17:52:00 +00:00
protected StyleSignature getStyleSignature() {
2022-02-10 18:16:18 +00:00
if (type == TimingStyle.CONCISE)
2022-05-04 17:52:00 +00:00
return StyleSignatureBasic.of(SName.root, SName.element, SName.timingDiagram, SName.concise).withTOBECHANGED(stereotype);
2022-02-10 18:16:18 +00:00
if (type == TimingStyle.ROBUST)
2022-05-04 17:52:00 +00:00
return StyleSignatureBasic.of(SName.root, SName.element, SName.timingDiagram, SName.robust).withTOBECHANGED(stereotype);
2022-02-10 18:16:18 +00:00
throw new IllegalStateException();
}
2020-04-26 18:31:41 +00:00
private PDrawing buildPDrawing() {
2022-02-10 18:16:18 +00:00
final Style style = getStyleSignature().getMergedStyle(skinParam.getCurrentStyleBuilder());
if (type == TimingStyle.CONCISE)
return new Ribbon(ruler, skinParam, notes, isCompact(), getTitle(), suggestedHeight, style);
2020-03-18 10:50:02 +00:00
if (type == TimingStyle.ROBUST) {
2022-03-01 18:11:51 +00:00
final Style style0 = StyleSignatureBasic.of(SName.root, SName.element, SName.timingDiagram)
2022-02-10 18:16:18 +00:00
.getMergedStyle(skinParam.getCurrentStyleBuilder());
return new Histogram(ruler, skinParam, statesLabel.values(), isCompact(), getTitle(), suggestedHeight,
style, style0);
2020-03-18 10:50:02 +00:00
}
2022-02-10 18:16:18 +00:00
2020-03-18 10:50:02 +00:00
throw new IllegalStateException();
2019-03-01 22:16:29 +00:00
}
2020-04-26 18:31:41 +00:00
public final TextBlock getPart1(final double fullAvailableWidth, final double specialVSpace) {
2020-03-18 10:50:02 +00:00
return new AbstractTextBlock() {
public void drawU(UGraphic ug) {
2022-02-10 18:16:18 +00:00
if (isCompact() == false)
new PlayerFrame(getTitle(), skinParam).drawFrameTitle(ug);
2020-04-26 18:31:41 +00:00
ug = ug.apply(getTranslateForTimeDrawing(ug.getStringBounder())).apply(UTranslate.dy(specialVSpace));
getTimeDrawing().getPart1(fullAvailableWidth).drawU(ug);
2020-03-18 10:50:02 +00:00
}
public Dimension2D calculateDimension(StringBounder stringBounder) {
2020-04-26 18:31:41 +00:00
return getTimeDrawing().getPart1(fullAvailableWidth).calculateDimension(stringBounder);
2020-03-18 10:50:02 +00:00
}
};
2019-03-01 22:16:29 +00:00
}
2020-03-18 10:50:02 +00:00
public UDrawable getPart2() {
return new UDrawable() {
public void drawU(UGraphic ug) {
ug = ug.apply(getTranslateForTimeDrawing(ug.getStringBounder()));
getTimeDrawing().getPart2().drawU(ug);
}
};
2019-03-01 22:16:29 +00:00
}
private UTranslate getTranslateForTimeDrawing(StringBounder stringBounder) {
2020-03-18 10:50:02 +00:00
return UTranslate.dy(getTitleHeight(stringBounder));
2019-03-01 22:16:29 +00:00
}
2020-03-18 10:50:02 +00:00
public final double getFullHeight(StringBounder stringBounder) {
return getTitleHeight(stringBounder) + getZoneHeight(stringBounder);
}
2019-03-01 22:16:29 +00:00
2020-03-18 10:50:02 +00:00
private double getTitleHeight(StringBounder stringBounder) {
2022-02-10 18:16:18 +00:00
if (isCompact())
2020-04-26 18:31:41 +00:00
return 6;
2022-02-10 18:16:18 +00:00
2020-03-18 10:50:02 +00:00
return getTitle().calculateDimension(stringBounder).getHeight() + 6;
}
2020-04-26 18:31:41 +00:00
private PDrawing getTimeDrawing() {
2022-02-10 18:16:18 +00:00
if (cached == null)
2019-03-01 22:16:29 +00:00
cached = computeTimeDrawing();
2022-02-10 18:16:18 +00:00
2019-03-01 22:16:29 +00:00
return cached;
}
2020-04-26 18:31:41 +00:00
private PDrawing computeTimeDrawing() {
final PDrawing result = buildPDrawing();
2019-03-01 22:16:29 +00:00
result.setInitialState(initialState, initialColors);
2022-02-10 18:16:18 +00:00
for (ChangeState change : changes)
2019-03-01 22:16:29 +00:00
result.addChange(change);
2022-02-10 18:16:18 +00:00
for (TimeConstraint constraint : constraints)
2019-03-01 22:16:29 +00:00
result.addConstraint(constraint);
2022-02-10 18:16:18 +00:00
2019-03-01 22:16:29 +00:00
return result;
}
private double getZoneHeight(StringBounder stringBounder) {
2020-03-18 10:50:02 +00:00
return getTimeDrawing().getFullHeight(stringBounder);
2019-03-01 22:16:29 +00:00
}
2020-03-18 10:50:02 +00:00
public final void setState(TimeTick now, String comment, Colors color, String... states) {
2022-02-10 18:16:18 +00:00
for (int i = 0; i < states.length; i++)
2019-03-01 22:16:29 +00:00
states[i] = decodeState(states[i]);
2022-02-10 18:16:18 +00:00
2019-03-01 22:16:29 +00:00
if (now == null) {
this.initialState = states[0];
this.initialColors = color;
} else {
this.changes.add(new ChangeState(now, comment, color, states));
}
}
private String decodeState(String code) {
final String label = statesLabel.get(code);
2022-02-10 18:16:18 +00:00
if (label == null)
2019-03-01 22:16:29 +00:00
return code;
2022-02-10 18:16:18 +00:00
2019-03-01 22:16:29 +00:00
return label;
}
2020-03-18 10:50:02 +00:00
public final IntricatedPoint getTimeProjection(StringBounder stringBounder, TimeTick tick) {
2019-03-01 22:16:29 +00:00
final IntricatedPoint point = getTimeDrawing().getTimeProjection(stringBounder, tick);
2022-02-10 18:16:18 +00:00
if (point == null)
2019-03-01 22:16:29 +00:00
return null;
2022-02-10 18:16:18 +00:00
2019-03-01 22:16:29 +00:00
final UTranslate translation = getTranslateForTimeDrawing(stringBounder);
return point.translated(translation);
}
2020-03-18 10:50:02 +00:00
public final void createConstraint(TimeTick tick1, TimeTick tick2, String message) {
this.constraints.add(new TimeConstraint(tick1, tick2, message, skinParam));
2019-03-01 22:16:29 +00:00
}
2020-03-18 10:50:02 +00:00
public final void addNote(TimeTick now, Display note, Position position) {
2022-02-10 18:16:18 +00:00
Style style = null;
if (UseStyle.useBetaStyle()) {
2022-03-01 18:11:51 +00:00
final StyleSignatureBasic signature = StyleSignatureBasic.of(SName.root, SName.element, SName.timingDiagram,
2022-02-10 18:16:18 +00:00
SName.note);
style = signature.getMergedStyle(skinParam.getCurrentStyleBuilder());
}
this.notes.add(new TimingNote(now, this, note, position, skinParam, style));
2019-03-01 22:16:29 +00:00
}
2020-03-18 10:50:02 +00:00
public final void defineState(String stateCode, String label) {
2019-03-01 22:16:29 +00:00
statesLabel.put(stateCode, label);
}
}