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

214 lines
7.7 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;
2020-04-26 18:31:41 +00:00
import java.awt.geom.Point2D;
2020-06-21 20:31:45 +00:00
import java.util.ArrayList;
import java.util.List;
2019-03-01 22:16:29 +00:00
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.Dimension2DDouble;
2022-03-29 21:13:46 +00:00
import net.sourceforge.plantuml.FontParam;
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.ISkinParam;
2022-03-29 21:13:46 +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;
2022-03-29 21:13:46 +00:00
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
2019-03-29 22:14:07 +00:00
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;
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.IntricatedPoint;
2019-03-01 22:16:29 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.ULine;
import net.sourceforge.plantuml.ugraphic.UTranslate;
2020-03-18 10:50:02 +00:00
public class PlayerBinary extends Player {
2019-03-01 22:16:29 +00:00
2022-03-29 21:13:46 +00:00
private static final String LOW_STRING = "0";
private static final String HIGH_STRING = "1";
2021-05-14 08:42:57 +00:00
private final List<TimeConstraint> constraints = new ArrayList<>();
2022-03-29 21:13:46 +00:00
private final SortedMap<TimeTick, ChangeState> values = new TreeMap<>();
private ChangeState initialState;
2019-03-01 22:16:29 +00:00
2022-05-04 17:52:00 +00:00
public PlayerBinary(String code, ISkinParam skinParam, TimingRuler ruler, boolean compact, Stereotype stereotype) {
super(code, skinParam, ruler, compact, stereotype);
this.suggestedHeight = 30;
2019-03-01 22:16:29 +00:00
}
2020-06-21 20:31:45 +00:00
private double getHeightForConstraints(StringBounder stringBounder) {
return TimeConstraint.getHeightForConstraints(stringBounder, constraints);
}
2020-03-18 10:50:02 +00:00
public double getFullHeight(StringBounder stringBounder) {
2020-06-21 20:31:45 +00:00
return getHeightForConstraints(stringBounder) + suggestedHeight;
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() {
return StyleSignatureBasic.of(SName.root, SName.element, SName.timingDiagram, SName.binary).withTOBECHANGED(stereotype);
2019-03-01 22:16:29 +00:00
}
public IntricatedPoint getTimeProjection(StringBounder stringBounder, TimeTick tick) {
2020-04-26 18:31:41 +00:00
final double x = ruler.getPosInPixel(tick);
2022-03-29 21:13:46 +00:00
return new IntricatedPoint(new Point2D.Double(x, getYpos(stringBounder, HIGH_STRING)),
new Point2D.Double(x, getYpos(stringBounder, HIGH_STRING)));
2019-03-01 22:16:29 +00:00
}
public void addNote(TimeTick now, Display note, Position position) {
throw new UnsupportedOperationException();
}
public void defineState(String stateCode, String label) {
throw new UnsupportedOperationException();
}
public void setState(TimeTick now, String comment, Colors color, String... states) {
2022-03-29 21:13:46 +00:00
final ChangeState cs = new ChangeState(now, comment, color, convert(states[0]));
if (now == null)
this.initialState = cs;
else
this.values.put(now, cs);
2019-03-01 22:16:29 +00:00
}
2022-03-29 21:13:46 +00:00
private String[] convert(String value) {
if ("1".equals(value) || "high".equalsIgnoreCase(value))
return new String[] { HIGH_STRING };
return new String[] { LOW_STRING };
2019-03-01 22:16:29 +00:00
}
public void createConstraint(TimeTick tick1, TimeTick tick2, String message) {
2020-06-21 20:31:45 +00:00
this.constraints.add(new TimeConstraint(tick1, tick2, message, skinParam));
2019-03-01 22:16:29 +00:00
}
private final double ymargin = 8;
2022-03-29 21:13:46 +00:00
private double getYpos(StringBounder stringBounder, String state) {
if (state.equalsIgnoreCase(LOW_STRING))
return getYlow(stringBounder);
return getYhigh(stringBounder);
2020-06-21 20:31:45 +00:00
}
private double getYlow(StringBounder stringBounder) {
return getFullHeight(stringBounder) - ymargin;
}
private double getYhigh(StringBounder stringBounder) {
return ymargin + getHeightForConstraints(stringBounder);
2019-03-01 22:16:29 +00:00
}
2020-04-26 18:31:41 +00:00
public TextBlock getPart1(double fullAvailableWidth, double specialVSpace) {
2020-03-18 10:50:02 +00:00
return new AbstractTextBlock() {
public void drawU(UGraphic ug) {
final StringBounder stringBounder = ug.getStringBounder();
final TextBlock title = getTitle();
final Dimension2D dim = title.calculateDimension(stringBounder);
final double y = (getFullHeight(stringBounder) - dim.getHeight()) / 2;
title.drawU(ug.apply(UTranslate.dy(y)));
2019-03-01 22:16:29 +00:00
}
2020-03-18 10:50:02 +00:00
public Dimension2D calculateDimension(StringBounder stringBounder) {
final Dimension2D dim = getTitle().calculateDimension(stringBounder);
return Dimension2DDouble.delta(dim, 5, 0);
}
};
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 = getContext().apply(ug);
double lastx = 0;
2022-03-29 21:13:46 +00:00
String lastValue = initialState == null ? LOW_STRING : initialState.getState();
2020-06-21 20:31:45 +00:00
final StringBounder stringBounder = ug.getStringBounder();
final ULine vline = ULine.vline(getYlow(stringBounder) - getYhigh(stringBounder));
2022-03-29 21:13:46 +00:00
for (Map.Entry<TimeTick, ChangeState> ent : values.entrySet()) {
final ChangeState value = ent.getValue();
2020-03-18 10:50:02 +00:00
final double x = ruler.getPosInPixel(ent.getKey());
2022-03-29 21:13:46 +00:00
2020-06-21 20:31:45 +00:00
ug.apply(new UTranslate(lastx, getYpos(stringBounder, lastValue))).draw(ULine.hline(x - lastx));
2022-03-29 21:13:46 +00:00
if (lastValue.equalsIgnoreCase(value.getState()) == false)
2020-06-21 20:31:45 +00:00
ug.apply(new UTranslate(x, getYhigh(stringBounder))).draw(vline);
2022-03-29 21:13:46 +00:00
if (value.getComment() != null) {
final TextBlock label = getTextBlock(value.getComment());
// final Dimension2D dim = label.calculateDimension(ug.getStringBounder());
label.drawU(ug.apply(new UTranslate(x + 2, getYhigh(stringBounder))));
2020-03-18 10:50:02 +00:00
}
2022-03-29 21:13:46 +00:00
2020-03-18 10:50:02 +00:00
lastx = x;
2022-03-29 21:13:46 +00:00
lastValue = value.getState();
2020-03-18 10:50:02 +00:00
}
2020-06-21 20:31:45 +00:00
ug.apply(new UTranslate(lastx, getYpos(stringBounder, lastValue)))
.draw(ULine.hline(ruler.getWidth() - lastx));
drawConstraints(ug.apply(UTranslate.dy(getHeightForConstraints(ug.getStringBounder()))));
2020-03-18 10:50:02 +00:00
}
};
2019-03-01 22:16:29 +00:00
}
2022-03-29 21:13:46 +00:00
final protected FontConfiguration getCommentFontConfiguration() {
if (UseStyle.useBetaStyle() == false)
return FontConfiguration.create(skinParam, FontParam.TIMING, null);
return FontConfiguration.create(skinParam,
getStyleSignature().getMergedStyle(skinParam.getCurrentStyleBuilder()));
}
private TextBlock getTextBlock(String value) {
final Display display = Display.getWithNewlines(value);
return display.create(getCommentFontConfiguration(), HorizontalAlignment.LEFT, skinParam);
}
2020-06-21 20:31:45 +00:00
private void drawConstraints(final UGraphic ug) {
for (TimeConstraint constraint : constraints) {
constraint.drawU(ug, ruler);
}
}
2019-03-01 22:16:29 +00:00
}