2017-02-01 18:55:51 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
2019-01-16 18:34:41 +00:00
|
|
|
* (C) Copyright 2009-2020, Arnaud Roques
|
2017-02-01 18:55:51 +00:00
|
|
|
*
|
|
|
|
* Project Info: http://plantuml.com
|
|
|
|
*
|
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
|
|
|
|
*
|
2017-02-01 18:55:51 +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
|
|
|
|
*
|
|
|
|
*/
|
2020-03-18 10:50:02 +00:00
|
|
|
package net.sourceforge.plantuml.timingdiagram.graphic;
|
2017-02-01 18:55:51 +00:00
|
|
|
|
|
|
|
import java.awt.geom.Dimension2D;
|
|
|
|
import java.awt.geom.Point2D;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
import net.sourceforge.plantuml.Dimension2DDouble;
|
2017-02-01 18:55:51 +00:00
|
|
|
import net.sourceforge.plantuml.FontParam;
|
|
|
|
import net.sourceforge.plantuml.ISkinParam;
|
2018-01-04 22:32:45 +00:00
|
|
|
import net.sourceforge.plantuml.command.Position;
|
2017-02-01 18:55:51 +00:00
|
|
|
import net.sourceforge.plantuml.cucadiagram.Display;
|
2020-03-18 10:50:02 +00:00
|
|
|
import net.sourceforge.plantuml.graphic.AbstractTextBlock;
|
2017-02-01 18:55:51 +00:00
|
|
|
import net.sourceforge.plantuml.graphic.FontConfiguration;
|
|
|
|
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
|
|
|
|
import net.sourceforge.plantuml.graphic.StringBounder;
|
2017-04-05 17:37:42 +00:00
|
|
|
import net.sourceforge.plantuml.graphic.SymbolContext;
|
2017-02-01 18:55:51 +00:00
|
|
|
import net.sourceforge.plantuml.graphic.TextBlock;
|
2017-02-15 21:34:36 +00:00
|
|
|
import net.sourceforge.plantuml.graphic.TextBlockUtils;
|
2020-03-18 10:50:02 +00:00
|
|
|
import net.sourceforge.plantuml.graphic.UDrawable;
|
2017-04-05 17:37:42 +00:00
|
|
|
import net.sourceforge.plantuml.graphic.color.ColorType;
|
|
|
|
import net.sourceforge.plantuml.graphic.color.Colors;
|
2020-03-18 10:50:02 +00:00
|
|
|
import net.sourceforge.plantuml.timingdiagram.ChangeState;
|
|
|
|
import net.sourceforge.plantuml.timingdiagram.TimeConstraint;
|
|
|
|
import net.sourceforge.plantuml.timingdiagram.TimeTick;
|
|
|
|
import net.sourceforge.plantuml.timingdiagram.TimingNote;
|
|
|
|
import net.sourceforge.plantuml.timingdiagram.TimingRuler;
|
2017-02-01 18:55:51 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.UGraphic;
|
2019-03-01 22:16:29 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.ULine;
|
2017-02-01 18:55:51 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.UTranslate;
|
2020-03-18 10:50:02 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.color.HColor;
|
2017-02-01 18:55:51 +00:00
|
|
|
|
2020-04-26 18:31:41 +00:00
|
|
|
public class Ribbon implements PDrawing {
|
2017-02-01 18:55:51 +00:00
|
|
|
|
|
|
|
private final List<ChangeState> changes = new ArrayList<ChangeState>();
|
2017-02-15 21:34:36 +00:00
|
|
|
private final List<TimeConstraint> constraints = new ArrayList<TimeConstraint>();
|
2017-02-01 18:55:51 +00:00
|
|
|
|
|
|
|
private final ISkinParam skinParam;
|
|
|
|
private final TimingRuler ruler;
|
2017-02-15 21:34:36 +00:00
|
|
|
private String initialState;
|
2017-04-05 17:37:42 +00:00
|
|
|
private Colors initialColors;
|
2018-01-04 22:32:45 +00:00
|
|
|
private final List<TimingNote> notes;
|
2020-04-26 18:31:41 +00:00
|
|
|
private final boolean compact;
|
|
|
|
private final TextBlock title;
|
2020-05-07 14:12:08 +00:00
|
|
|
private final int suggestedHeight;
|
2017-02-01 18:55:51 +00:00
|
|
|
|
2020-05-07 14:12:08 +00:00
|
|
|
public Ribbon(TimingRuler ruler, ISkinParam skinParam, List<TimingNote> notes, boolean compact, TextBlock title,
|
|
|
|
int suggestedHeight) {
|
|
|
|
this.suggestedHeight = suggestedHeight == 0 ? 24 : suggestedHeight;
|
2020-04-26 18:31:41 +00:00
|
|
|
this.compact = compact;
|
2017-02-01 18:55:51 +00:00
|
|
|
this.ruler = ruler;
|
|
|
|
this.skinParam = skinParam;
|
2018-01-04 22:32:45 +00:00
|
|
|
this.notes = notes;
|
2020-04-26 18:31:41 +00:00
|
|
|
this.title = title;
|
2017-02-01 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public IntricatedPoint getTimeProjection(StringBounder stringBounder, TimeTick tick) {
|
|
|
|
final double x = ruler.getPosInPixel(tick);
|
2020-03-18 10:50:02 +00:00
|
|
|
final double y = getHeightForConstraints(stringBounder) + getRibbonHeight() / 2;
|
2017-02-01 18:55:51 +00:00
|
|
|
for (ChangeState change : changes) {
|
|
|
|
if (change.getWhen().compareTo(tick) == 0) {
|
|
|
|
return new IntricatedPoint(new Point2D.Double(x, y), new Point2D.Double(x, y));
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 10:50:02 +00:00
|
|
|
return new IntricatedPoint(new Point2D.Double(x, y - getRibbonHeight() / 2),
|
|
|
|
new Point2D.Double(x, y + getRibbonHeight() / 2));
|
2017-02-01 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addChange(ChangeState change) {
|
|
|
|
this.changes.add(change);
|
|
|
|
}
|
|
|
|
|
|
|
|
private double getPosInPixel(ChangeState change) {
|
|
|
|
return ruler.getPosInPixel(change.getWhen());
|
|
|
|
}
|
|
|
|
|
|
|
|
private FontConfiguration getFontConfiguration() {
|
2018-11-26 18:46:22 +00:00
|
|
|
return new FontConfiguration(skinParam, FontParam.TIMING, null);
|
2017-02-01 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 22:32:45 +00:00
|
|
|
private TextBlock createTextBlock(String value) {
|
2017-02-15 21:34:36 +00:00
|
|
|
final Display display = Display.getWithNewlines(value);
|
2017-02-01 18:55:51 +00:00
|
|
|
return display.create(getFontConfiguration(), HorizontalAlignment.LEFT, skinParam);
|
|
|
|
}
|
|
|
|
|
2020-04-26 18:31:41 +00:00
|
|
|
public TextBlock getPart1(double fullAvailableWidth) {
|
2020-03-18 10:50:02 +00:00
|
|
|
return new AbstractTextBlock() {
|
|
|
|
public void drawU(UGraphic ug) {
|
2020-04-26 18:31:41 +00:00
|
|
|
if (compact) {
|
|
|
|
final double titleHeight = title.calculateDimension(ug.getStringBounder()).getHeight();
|
|
|
|
final double dy = (getRibbonHeight() - titleHeight) / 2;
|
|
|
|
title.drawU(ug.apply(UTranslate.dy(dy)));
|
|
|
|
}
|
2017-04-05 17:37:42 +00:00
|
|
|
}
|
2017-02-15 21:34:36 +00:00
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
public Dimension2D calculateDimension(StringBounder stringBounder) {
|
2020-04-26 18:31:41 +00:00
|
|
|
double width = getInitialWidth(stringBounder);
|
|
|
|
if (compact) {
|
|
|
|
width += title.calculateDimension(stringBounder).getWidth() + 10;
|
|
|
|
}
|
|
|
|
return new Dimension2DDouble(width, getRibbonHeight());
|
2017-03-12 17:22:02 +00:00
|
|
|
}
|
2020-03-18 10:50:02 +00:00
|
|
|
};
|
|
|
|
}
|
2018-01-04 22:32:45 +00:00
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
public UDrawable getPart2() {
|
|
|
|
return new UDrawable() {
|
|
|
|
public void drawU(UGraphic ug) {
|
|
|
|
drawPart2(ug);
|
|
|
|
}
|
|
|
|
};
|
2018-01-04 22:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void drawNotes(UGraphic ug, final Position position) {
|
|
|
|
for (TimingNote note : notes) {
|
|
|
|
if (note.getPosition() == position) {
|
|
|
|
final double x = ruler.getPosInPixel(note.getWhen());
|
2020-03-18 10:50:02 +00:00
|
|
|
note.drawU(ug.apply(UTranslate.dx(x)));
|
2018-01-04 22:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-01 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 21:34:36 +00:00
|
|
|
private double getInitialWidth(final StringBounder stringBounder) {
|
2020-04-26 18:31:41 +00:00
|
|
|
if (initialState == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return createTextBlock(initialState).calculateDimension(stringBounder).getWidth() + 24;
|
2017-02-15 21:34:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 17:22:02 +00:00
|
|
|
private void drawHexa(UGraphic ug, double len, ChangeState change) {
|
2018-01-04 22:32:45 +00:00
|
|
|
final HexaShape shape = HexaShape.create(len, getRibbonHeight(), change.getContext());
|
2017-02-15 21:34:36 +00:00
|
|
|
shape.drawU(ug);
|
|
|
|
}
|
|
|
|
|
2019-03-01 22:16:29 +00:00
|
|
|
private void drawFlat(UGraphic ug, double len, ChangeState change) {
|
2020-03-18 10:50:02 +00:00
|
|
|
final ULine line = ULine.hline(len);
|
|
|
|
change.getContext().apply(ug).apply(UTranslate.dy(getRibbonHeight() / 2)).draw(line);
|
2019-03-01 22:16:29 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 22:32:45 +00:00
|
|
|
private double getRibbonHeight() {
|
2020-05-07 14:12:08 +00:00
|
|
|
return suggestedHeight;
|
2018-01-04 22:32:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 17:22:02 +00:00
|
|
|
private void drawPentaB(UGraphic ug, double len, ChangeState change) {
|
2018-01-04 22:32:45 +00:00
|
|
|
final PentaBShape shape = PentaBShape.create(len, getRibbonHeight(), change.getContext());
|
2017-02-15 21:34:36 +00:00
|
|
|
shape.drawU(ug);
|
2017-02-01 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 17:22:02 +00:00
|
|
|
private void drawPentaA(UGraphic ug, double len, ChangeState change) {
|
2017-04-05 17:37:42 +00:00
|
|
|
SymbolContext context = change.getContext();
|
2020-03-18 10:50:02 +00:00
|
|
|
final HColor back = initialColors.getColor(ColorType.BACK);
|
2017-04-05 17:37:42 +00:00
|
|
|
if (back != null) {
|
|
|
|
context = context.withBackColor(back);
|
|
|
|
}
|
2018-01-04 22:32:45 +00:00
|
|
|
final PentaAShape shape = PentaAShape.create(len, getRibbonHeight(), context);
|
2017-02-15 21:34:36 +00:00
|
|
|
shape.drawU(ug);
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
private double getHeightForConstraints(StringBounder stringBounder) {
|
|
|
|
return TimeConstraint.getHeightForConstraints(stringBounder, constraints);
|
2018-01-04 22:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private double getHeightForNotes(StringBounder stringBounder, Position position) {
|
|
|
|
double height = 0;
|
|
|
|
for (TimingNote note : notes) {
|
|
|
|
if (note.getPosition() == position) {
|
|
|
|
height = Math.max(height, note.getHeight(stringBounder));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return height;
|
2017-02-01 18:55:51 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
private double getMarginX() {
|
|
|
|
return 12;
|
2017-02-15 21:34:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 17:37:42 +00:00
|
|
|
public void setInitialState(String initialState, Colors initialColors) {
|
2017-02-15 21:34:36 +00:00
|
|
|
this.initialState = initialState;
|
2017-04-05 17:37:42 +00:00
|
|
|
this.initialColors = initialColors;
|
2017-02-15 21:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addConstraint(TimeConstraint constraint) {
|
|
|
|
this.constraints.add(constraint);
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
public double getFullHeight(StringBounder stringBounder) {
|
|
|
|
return getHeightForConstraints(stringBounder) + getHeightForTopComment(stringBounder)
|
|
|
|
+ getHeightForNotes(stringBounder, Position.TOP) + getRibbonHeight()
|
|
|
|
+ getHeightForNotes(stringBounder, Position.BOTTOM) + getBottomMargin();
|
|
|
|
}
|
|
|
|
|
|
|
|
private double getBottomMargin() {
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void drawPart2(UGraphic ug) {
|
|
|
|
final StringBounder stringBounder = ug.getStringBounder();
|
|
|
|
|
|
|
|
final UGraphic ugRibbon = ug.apply(UTranslate.dy(getHeightForConstraints(stringBounder)
|
|
|
|
+ getHeightForTopComment(stringBounder) + getHeightForNotes(stringBounder, Position.TOP)));
|
|
|
|
|
|
|
|
drawBeforeZeroState(ugRibbon);
|
|
|
|
drawBeforeZeroStateLabel(ugRibbon.apply(UTranslate.dy(getRibbonHeight() / 2)));
|
|
|
|
drawStates(ugRibbon);
|
|
|
|
drawStatesLabels(ugRibbon.apply(UTranslate.dy(getRibbonHeight() / 2)));
|
|
|
|
|
|
|
|
drawConstraints(ug.apply(UTranslate.dy(getHeightForConstraints(stringBounder) / 2)));
|
|
|
|
|
|
|
|
drawNotes(ug, Position.TOP);
|
|
|
|
drawNotes(ug.apply(UTranslate.dy(getHeightForConstraints(stringBounder) + getRibbonHeight()
|
|
|
|
+ getHeightForNotes(stringBounder, Position.TOP))), Position.BOTTOM);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void drawBeforeZeroState(UGraphic ug) {
|
|
|
|
if (initialState != null && changes.size() > 0) {
|
|
|
|
final StringBounder stringBounder = ug.getStringBounder();
|
|
|
|
final double a = getPosInPixel(changes.get(0));
|
|
|
|
drawPentaA(ug.apply(UTranslate.dx(-getInitialWidth(stringBounder))), getInitialWidth(stringBounder) + a,
|
|
|
|
changes.get(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void drawBeforeZeroStateLabel(final UGraphic ug) {
|
|
|
|
final StringBounder stringBounder = ug.getStringBounder();
|
|
|
|
if (initialState != null) {
|
|
|
|
final TextBlock initial = createTextBlock(initialState);
|
|
|
|
final Dimension2D dimInital = initial.calculateDimension(stringBounder);
|
|
|
|
initial.drawU(ug.apply(new UTranslate(-getMarginX() - dimInital.getWidth(), -dimInital.getHeight() / 2)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void drawStates(UGraphic ug) {
|
|
|
|
for (int i = 0; i < changes.size() - 1; i++) {
|
|
|
|
final double a = getPosInPixel(changes.get(i));
|
|
|
|
final double b = getPosInPixel(changes.get(i + 1));
|
|
|
|
assert b > a;
|
|
|
|
if (changes.get(i).isFlat()) {
|
|
|
|
drawFlat(ug.apply(UTranslate.dx(a)), b - a, changes.get(i));
|
|
|
|
} else if (changes.get(i).isCompletelyHidden() == false) {
|
|
|
|
drawHexa(ug.apply(UTranslate.dx(a)), b - a, changes.get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changes.size() >= 1) {
|
|
|
|
final ChangeState last = changes.get(changes.size() - 1);
|
|
|
|
final double a = getPosInPixel(last);
|
|
|
|
if (last.isFlat()) {
|
|
|
|
drawFlat(ug.apply(UTranslate.dx(a)), ruler.getWidth() - a, last);
|
|
|
|
} else if (last.isCompletelyHidden() == false) {
|
|
|
|
drawPentaB(ug.apply(UTranslate.dx(a)), ruler.getWidth() - a, last);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void drawStatesLabels(UGraphic ug) {
|
|
|
|
final StringBounder stringBounder = ug.getStringBounder();
|
|
|
|
for (int i = 0; i < changes.size(); i++) {
|
|
|
|
final ChangeState change = changes.get(i);
|
|
|
|
final double x = ruler.getPosInPixel(change.getWhen());
|
|
|
|
if (change.isBlank() == false && change.isCompletelyHidden() == false && change.isFlat() == false) {
|
|
|
|
final TextBlock state = createTextBlock(change.getState());
|
|
|
|
final Dimension2D dim = state.calculateDimension(stringBounder);
|
|
|
|
final double xtext;
|
|
|
|
if (i == changes.size() - 1) {
|
|
|
|
xtext = x + getMarginX();
|
|
|
|
} else {
|
|
|
|
final double x2 = ruler.getPosInPixel(changes.get(i + 1).getWhen());
|
|
|
|
xtext = (x + x2) / 2 - dim.getWidth() / 2;
|
|
|
|
}
|
|
|
|
state.drawU(ug.apply(new UTranslate(xtext, -dim.getHeight() / 2)));
|
|
|
|
}
|
|
|
|
final TextBlock commentTopBlock = getCommentTopBlock(change);
|
|
|
|
final Dimension2D dimComment = commentTopBlock.calculateDimension(stringBounder);
|
|
|
|
commentTopBlock
|
|
|
|
.drawU(ug.apply(new UTranslate(x + getMarginX(), -getRibbonHeight() / 2 - dimComment.getHeight())));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private TextBlock getCommentTopBlock(final ChangeState change) {
|
|
|
|
if (change.getComment() == null) {
|
|
|
|
return TextBlockUtils.empty(0, 0);
|
|
|
|
}
|
|
|
|
return createTextBlock(change.getComment());
|
|
|
|
}
|
|
|
|
|
|
|
|
private double getHeightForTopComment(StringBounder stringBounder) {
|
|
|
|
double result = 0;
|
|
|
|
for (ChangeState change : changes) {
|
|
|
|
result = Math.max(result, getCommentTopBlock(change).calculateDimension(stringBounder).getHeight());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void drawConstraints(final UGraphic ug) {
|
|
|
|
for (TimeConstraint constraint : constraints) {
|
|
|
|
constraint.drawU(ug, ruler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 18:55:51 +00:00
|
|
|
}
|