1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-09-27 14:39:02 +00:00
plantuml/src/net/sourceforge/plantuml/timingdiagram/Ribbon.java

261 lines
8.8 KiB
Java
Raw Normal View History

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
*
*/
package net.sourceforge.plantuml.timingdiagram;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
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;
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.graphic.HtmlColor;
2017-02-01 18:55:51 +00:00
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;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.graphic.color.ColorType;
import net.sourceforge.plantuml.graphic.color.Colors;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UTranslate;
public class Ribbon implements TimeDrawing {
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 double delta = 12;
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;
2017-02-01 18:55:51 +00:00
2018-01-04 22:32:45 +00:00
public Ribbon(TimingRuler ruler, ISkinParam skinParam, List<TimingNote> notes) {
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;
2017-02-01 18:55:51 +00:00
}
public IntricatedPoint getTimeProjection(StringBounder stringBounder, TimeTick tick) {
final double x = ruler.getPosInPixel(tick);
2017-02-15 21:34:36 +00:00
final double y = delta * 0.5 + getHeightForConstraints();
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));
}
}
return new IntricatedPoint(new Point2D.Double(x, y - delta), new Point2D.Double(x, y + delta));
}
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);
}
public void drawU(UGraphic ug) {
2018-01-04 22:32:45 +00:00
final StringBounder stringBounder = ug.getStringBounder();
final double halfDelta = delta / 2;
drawNotes(ug.apply(new UTranslate(0, -delta)), Position.TOP);
2017-02-15 21:34:36 +00:00
2018-01-04 22:32:45 +00:00
final double ribbonHeight = getRibbonHeight();
UGraphic ugDown = ug.apply(new UTranslate(0, getHeightForConstraints()
+ getHeightForNotes(stringBounder, Position.TOP)));
final TextBlock initial;
2017-02-15 21:34:36 +00:00
if (initialState == null) {
2018-01-04 22:32:45 +00:00
initial = null;
2017-02-15 21:34:36 +00:00
} else {
2018-01-04 22:32:45 +00:00
initial = createTextBlock(initialState);
2018-03-09 21:37:34 +00:00
if (changes.size() > 0) {
final double a = getPosInPixel(changes.get(0));
drawPentaA(ugDown.apply(new UTranslate(-getInitialWidth(stringBounder), -halfDelta)),
getInitialWidth(stringBounder) + a, changes.get(0));
}
2017-02-15 21:34:36 +00:00
}
2017-02-01 18:55:51 +00:00
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;
2017-04-05 17:37:42 +00:00
if (changes.get(i).isCompletelyHidden() == false) {
2018-01-04 22:32:45 +00:00
drawHexa(ugDown.apply(new UTranslate(a, -halfDelta)), b - a, changes.get(i));
2017-04-05 17:37:42 +00:00
}
}
if (changes.size() >= 1) {
final ChangeState last = changes.get(changes.size() - 1);
final double a = getPosInPixel(last);
if (last.isCompletelyHidden() == false) {
2018-01-04 22:32:45 +00:00
drawPentaB(ugDown.apply(new UTranslate(a, -halfDelta)), ruler.getWidth() - a, last);
2017-04-05 17:37:42 +00:00
}
2017-02-01 18:55:51 +00:00
}
2018-01-04 22:32:45 +00:00
ugDown = ugDown.apply(new UTranslate(0, halfDelta));
2017-02-15 21:34:36 +00:00
2018-01-04 22:32:45 +00:00
if (initial != null) {
final Dimension2D dimInital = initial.calculateDimension(stringBounder);
initial.drawU(ugDown.apply(new UTranslate(-getDelta() - dimInital.getWidth(), -dimInital.getHeight() / 2)));
2017-02-15 21:34:36 +00:00
}
2017-03-12 17:22:02 +00:00
for (int i = 0; i < changes.size(); i++) {
final ChangeState change = changes.get(i);
2017-02-01 18:55:51 +00:00
final double x = ruler.getPosInPixel(change.getWhen());
2017-04-05 17:37:42 +00:00
if (change.isBlank() == false && change.isCompletelyHidden() == false) {
2018-01-04 22:32:45 +00:00
final TextBlock state = createTextBlock(change.getState());
2017-03-12 17:22:02 +00:00
final Dimension2D dim = state.calculateDimension(stringBounder);
final double xtext;
if (i == changes.size() - 1) {
xtext = x + getDelta();
} else {
final double x2 = ruler.getPosInPixel(changes.get(i + 1).getWhen());
xtext = (x + x2) / 2 - dim.getWidth() / 2;
}
state.drawU(ugDown.apply(new UTranslate(xtext, -dim.getHeight() / 2)));
}
2017-02-15 21:34:36 +00:00
final String commentString = change.getComment();
if (commentString != null) {
2018-01-04 22:32:45 +00:00
final TextBlock comment = createTextBlock(commentString);
2017-02-15 21:34:36 +00:00
final Dimension2D dimComment = comment.calculateDimension(stringBounder);
comment.drawU(ugDown.apply(new UTranslate(x + getDelta(), -delta - dimComment.getHeight())));
}
}
for (TimeConstraint constraint : constraints) {
2018-01-04 22:32:45 +00:00
constraint.drawU(ug.apply(new UTranslate(0, getHeightForConstraints() / 2)), ruler, skinParam);
2017-02-01 18:55:51 +00:00
}
2018-01-04 22:32:45 +00:00
drawNotes(
ug.apply(new UTranslate(0, getHeightForConstraints() + getHeightForNotes(stringBounder, Position.TOP)
+ ribbonHeight)), Position.BOTTOM);
}
private void drawNotes(UGraphic ug, final Position position) {
for (TimingNote note : notes) {
if (note.getPosition() == position) {
final double x = ruler.getPosInPixel(note.getWhen());
note.drawU(ug.apply(new UTranslate(x, 0)));
}
}
2017-02-01 18:55:51 +00:00
}
2017-02-15 21:34:36 +00:00
private double getInitialWidth(final StringBounder stringBounder) {
2018-01-04 22:32:45 +00:00
return createTextBlock(initialState).calculateDimension(stringBounder).getWidth() + getRibbonHeight();
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);
}
2018-01-04 22:32:45 +00:00
private double getRibbonHeight() {
return 2 * delta;
}
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();
final HtmlColor back = initialColors.getColor(ColorType.BACK);
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);
}
private double getHeightForConstraints() {
if (constraints.size() == 0) {
return 0;
2017-02-01 18:55:51 +00:00
}
2017-02-15 21:34:36 +00:00
return 30;
2017-02-01 18:55:51 +00:00
}
2018-01-04 22:32:45 +00:00
public double getHeight(StringBounder stringBounder) {
return 3 * delta + getHeightForConstraints() + getHeightForNotes(stringBounder, Position.TOP)
+ getHeightForNotes(stringBounder, Position.BOTTOM);
}
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
}
public double getDelta() {
return delta;
}
2017-02-15 21:34:36 +00:00
public TextBlock getWidthHeader(StringBounder stringBounder) {
if (initialState != null) {
2018-01-04 22:32:45 +00:00
return TextBlockUtils.empty(getInitialWidth(stringBounder), getRibbonHeight());
2017-02-15 21:34:36 +00:00
}
return TextBlockUtils.empty(0, 0);
}
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);
}
2017-02-01 18:55:51 +00:00
}