1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-22 18:05:04 +00:00
plantuml/src/net/sourceforge/plantuml/activitydiagram3/ftile/Swimlanes.java

347 lines
12 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2016-01-09 12:15:40 +00:00
* (C) Copyright 2009-2017, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2013-12-10 19:36:50 +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:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2013-12-10 19:36:50 +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.activitydiagram3.ftile;
import java.awt.geom.Dimension2D;
import java.util.ArrayList;
import java.util.List;
2016-01-09 12:15:40 +00:00
import net.sourceforge.plantuml.ColorParam;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.FontParam;
import net.sourceforge.plantuml.ISkinParam;
2016-01-09 12:15:40 +00:00
import net.sourceforge.plantuml.LineParam;
import net.sourceforge.plantuml.Pragma;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.activitydiagram3.Instruction;
import net.sourceforge.plantuml.activitydiagram3.InstructionList;
import net.sourceforge.plantuml.activitydiagram3.LinkRendering;
import net.sourceforge.plantuml.activitydiagram3.ftile.vcompact.FtileFactoryDelegatorAddNote;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.activitydiagram3.ftile.vcompact.FtileFactoryDelegatorAddUrl;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.activitydiagram3.ftile.vcompact.FtileFactoryDelegatorAssembly;
import net.sourceforge.plantuml.activitydiagram3.ftile.vcompact.FtileFactoryDelegatorCreateGroup;
2017-02-01 18:55:51 +00:00
import net.sourceforge.plantuml.activitydiagram3.ftile.vcompact.FtileFactoryDelegatorCreateParallel;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.activitydiagram3.ftile.vcompact.FtileFactoryDelegatorIf;
import net.sourceforge.plantuml.activitydiagram3.ftile.vcompact.FtileFactoryDelegatorRepeat;
import net.sourceforge.plantuml.activitydiagram3.ftile.vcompact.FtileFactoryDelegatorWhile;
import net.sourceforge.plantuml.activitydiagram3.ftile.vcompact.UGraphicInterceptorOneSwimlane;
import net.sourceforge.plantuml.activitydiagram3.ftile.vcompact.VCompactFactory;
import net.sourceforge.plantuml.cucadiagram.Display;
2015-06-07 10:23:10 +00:00
import net.sourceforge.plantuml.graphic.AbstractTextBlock;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
import net.sourceforge.plantuml.graphic.HtmlColor;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
2018-05-06 19:59:19 +00:00
import net.sourceforge.plantuml.graphic.TextBlockUtils;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.UGraphicDelegator;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.graphic.color.ColorType;
2016-01-09 12:15:40 +00:00
import net.sourceforge.plantuml.skin.rose.Rose;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.svek.UGraphicForSnake;
import net.sourceforge.plantuml.ugraphic.LimitFinder;
import net.sourceforge.plantuml.ugraphic.MinMax;
import net.sourceforge.plantuml.ugraphic.UChange;
import net.sourceforge.plantuml.ugraphic.UChangeBackColor;
import net.sourceforge.plantuml.ugraphic.UChangeColor;
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.ULine;
import net.sourceforge.plantuml.ugraphic.URectangle;
import net.sourceforge.plantuml.ugraphic.UShape;
import net.sourceforge.plantuml.ugraphic.UStroke;
import net.sourceforge.plantuml.ugraphic.UTranslate;
2018-07-27 21:56:46 +00:00
import net.sourceforge.plantuml.ugraphic.comp.SlotSet;
2016-12-01 20:29:25 +00:00
import net.sourceforge.plantuml.utils.MathUtils;
2013-12-10 19:36:50 +00:00
2015-06-07 10:23:10 +00:00
public class Swimlanes extends AbstractTextBlock implements TextBlock {
2013-12-10 19:36:50 +00:00
private final ISkinParam skinParam;;
2016-01-09 12:15:40 +00:00
private final Pragma pragma;
2013-12-10 19:36:50 +00:00
2015-04-07 18:18:37 +00:00
private final List<Swimlane> swimlanes = new ArrayList<Swimlane>();
2013-12-10 19:36:50 +00:00
private Swimlane currentSwimlane = null;
private final Instruction root = new InstructionList();
private Instruction currentInstruction = root;
2016-05-11 21:31:47 +00:00
private LinkRendering nextLinkRenderer = LinkRendering.none();
2013-12-10 19:36:50 +00:00
2016-01-09 12:15:40 +00:00
public Swimlanes(ISkinParam skinParam, Pragma pragma) {
2013-12-10 19:36:50 +00:00
this.skinParam = skinParam;
2016-01-09 12:15:40 +00:00
this.pragma = pragma;
2015-05-31 18:56:03 +00:00
}
private FontConfiguration getFontConfiguration() {
2016-01-09 12:15:40 +00:00
return new FontConfiguration(skinParam, FontParam.SWIMLANE_TITLE, null);
2013-12-10 19:36:50 +00:00
}
2016-08-25 20:45:37 +00:00
private FtileFactory getFtileFactory(StringBounder stringBounder) {
FtileFactory factory = new VCompactFactory(skinParam, stringBounder);
2016-06-19 14:16:41 +00:00
factory = new FtileFactoryDelegatorAddUrl(factory);
factory = new FtileFactoryDelegatorAssembly(factory);
factory = new FtileFactoryDelegatorIf(factory, pragma);
factory = new FtileFactoryDelegatorWhile(factory);
factory = new FtileFactoryDelegatorRepeat(factory);
2017-01-21 22:25:28 +00:00
factory = new FtileFactoryDelegatorCreateParallel(factory);
2017-11-20 16:10:36 +00:00
// factory = new FtileFactoryDelegatorCreateParallelAddingMargin(new
// FtileFactoryDelegatorCreateParallel1(factory));
2016-06-19 14:16:41 +00:00
factory = new FtileFactoryDelegatorAddNote(factory);
factory = new FtileFactoryDelegatorCreateGroup(factory);
2013-12-10 19:36:50 +00:00
return factory;
}
public void swimlane(String name, HtmlColor color, Display label) {
currentSwimlane = getOrCreate(name);
if (color != null) {
2015-09-28 20:42:17 +00:00
currentSwimlane.setSpecificColorTOBEREMOVED(ColorType.BACK, color);
2013-12-10 19:36:50 +00:00
}
2015-07-11 09:32:49 +00:00
if (Display.isNull(label) == false) {
2013-12-10 19:36:50 +00:00
currentSwimlane.setDisplay(label);
}
}
private Swimlane getOrCreate(String name) {
2015-04-07 18:18:37 +00:00
for (Swimlane s : swimlanes) {
2013-12-10 19:36:50 +00:00
if (s.getName().equals(name)) {
return s;
}
}
final Swimlane result = new Swimlane(name);
2015-04-07 18:18:37 +00:00
swimlanes.add(result);
2013-12-10 19:36:50 +00:00
return result;
}
class Cross extends UGraphicDelegator {
private Cross(UGraphic ug) {
super(ug);
}
@Override
public void draw(UShape shape) {
if (shape instanceof Ftile) {
final Ftile tile = (Ftile) shape;
2015-04-07 18:18:37 +00:00
tile.drawU(this);
2013-12-10 19:36:50 +00:00
} else if (shape instanceof Connection) {
final Connection connection = (Connection) shape;
final Ftile tile1 = connection.getFtile1();
final Ftile tile2 = connection.getFtile2();
if (tile1 == null || tile2 == null) {
return;
}
2015-04-07 18:18:37 +00:00
if (tile1.getSwimlaneOut() != tile2.getSwimlaneIn()) {
2013-12-10 19:36:50 +00:00
final ConnectionCross connectionCross = new ConnectionCross(connection);
connectionCross.drawU(getUg());
}
}
}
public UGraphic apply(UChange change) {
return new Cross(getUg().apply(change));
}
}
2016-05-11 21:31:47 +00:00
static private final double separationMargin = 10;
2017-11-20 16:10:36 +00:00
private TextBlock full;
2013-12-10 19:36:50 +00:00
public void drawU(UGraphic ug) {
2017-11-20 16:10:36 +00:00
if (full == null) {
final FtileFactory factory = getFtileFactory(ug.getStringBounder());
full = root.createFtile(factory);
if (swimlanes.size() <= 1) {
// BUG42
full = new TextBlockInterceptorUDrawable(full);
}
}
2015-04-07 18:18:37 +00:00
2013-12-10 19:36:50 +00:00
ug = new UGraphicForSnake(ug);
2015-04-07 18:18:37 +00:00
if (swimlanes.size() <= 1) {
2013-12-10 19:36:50 +00:00
full.drawU(ug);
ug.flushUg();
return;
}
2017-01-21 22:25:28 +00:00
drawWhenSwimlanes(ug, full);
2015-04-07 18:18:37 +00:00
}
static private void printDebug(UGraphic ug, SlotSet slot, HtmlColor col, TextBlock full) {
slot.drawDebugX(ug.apply(new UChangeColor(col)).apply(new UChangeBackColor(col)),
full.calculateDimension(ug.getStringBounder()).getHeight());
}
private void drawWhenSwimlanes(UGraphic ug, TextBlock full) {
2013-12-10 19:36:50 +00:00
final StringBounder stringBounder = ug.getStringBounder();
final Dimension2D dimensionFull = full.calculateDimension(stringBounder);
final UTranslate titleHeightTranslate = getTitleHeightTranslate(stringBounder);
computeSize(ug, full);
double x2 = 0;
2015-04-07 18:18:37 +00:00
for (Swimlane swimlane : swimlanes) {
2015-09-28 20:42:17 +00:00
final HtmlColor back = swimlane.getColors(skinParam).getColor(ColorType.BACK);
if (back != null) {
final UGraphic background = ug.apply(new UChangeBackColor(back)).apply(new UChangeColor(back))
.apply(new UTranslate(x2, 0));
2018-07-27 21:56:46 +00:00
background.draw(new URectangle(swimlane.getActualWidth(), dimensionFull.getHeight()
2013-12-10 19:36:50 +00:00
+ titleHeightTranslate.getDy()));
}
2018-07-27 21:56:46 +00:00
final TextBlock swTitle = getTitle(swimlane);
2017-01-21 22:25:28 +00:00
final double titleWidth = swTitle.calculateDimension(stringBounder).getWidth();
2018-07-27 21:56:46 +00:00
final double posTitle = x2 + (swimlane.getActualWidth() - titleWidth) / 2;
2017-01-21 22:25:28 +00:00
swTitle.drawU(ug.apply(new UTranslate(posTitle, 0)));
2013-12-10 19:36:50 +00:00
drawSeparation(ug.apply(new UTranslate(x2, 0)), dimensionFull.getHeight() + titleHeightTranslate.getDy());
full.drawU(new UGraphicInterceptorOneSwimlane(ug, swimlane).apply(swimlane.getTranslate()).apply(
titleHeightTranslate));
2018-07-27 21:56:46 +00:00
x2 += swimlane.getActualWidth();
2013-12-10 19:36:50 +00:00
}
drawSeparation(ug.apply(new UTranslate(x2, 0)), dimensionFull.getHeight() + titleHeightTranslate.getDy());
2018-06-12 20:50:45 +00:00
2013-12-10 19:36:50 +00:00
final Cross cross = new Cross(ug.apply(titleHeightTranslate));
full.drawU(cross);
cross.flushUg();
}
2016-12-01 20:29:25 +00:00
private void computeDrawingWidths(UGraphic ug, TextBlock full) {
final StringBounder stringBounder = ug.getStringBounder();
for (Swimlane swimlane : swimlanes) {
final LimitFinder limitFinder = new LimitFinder(stringBounder, false);
final UGraphicInterceptorOneSwimlane interceptor = new UGraphicInterceptorOneSwimlane(new UGraphicForSnake(
limitFinder), swimlane);
full.drawU(interceptor);
interceptor.flushUg();
final MinMax minMax = limitFinder.getMinMax();
swimlane.setMinMax(minMax);
}
}
2013-12-10 19:36:50 +00:00
private void computeSize(UGraphic ug, TextBlock full) {
2016-12-01 20:29:25 +00:00
computeDrawingWidths(ug, full);
double x1 = 0;
final StringBounder stringBounder = ug.getStringBounder();
double swimlaneWidth = skinParam.swimlaneWidth();
if (swimlaneWidth == -1) {
for (Swimlane swimlane : swimlanes) {
final MinMax minMax = swimlane.getMinMax();
swimlaneWidth = Math.max(swimlaneWidth, minMax.getWidth());
}
}
for (Swimlane swimlane : swimlanes) {
final MinMax minMax = swimlane.getMinMax();
final double drawingWidth = minMax.getWidth() + 2 * separationMargin;
2018-07-27 21:56:46 +00:00
final TextBlock swTitle = getTitle(swimlane);
2016-12-01 20:29:25 +00:00
final double titleWidth = swTitle.calculateDimension(stringBounder).getWidth();
final double totalWidth = MathUtils.max(swimlaneWidth, drawingWidth, titleWidth + 2 * separationMargin);
final UTranslate translate = new UTranslate(x1 - minMax.getMinX() + separationMargin
+ (totalWidth - drawingWidth) / 2.0, 0);
swimlane.setTranslateAndWidth(translate, totalWidth);
x1 += totalWidth;
}
}
2013-12-10 19:36:50 +00:00
private UTranslate getTitleHeightTranslate(final StringBounder stringBounder) {
double titlesHeight = 0;
2015-04-07 18:18:37 +00:00
for (Swimlane swimlane : swimlanes) {
2018-07-27 21:56:46 +00:00
final TextBlock swTitle = getTitle(swimlane);
2013-12-10 19:36:50 +00:00
titlesHeight = Math.max(titlesHeight, swTitle.calculateDimension(stringBounder).getHeight());
}
final UTranslate titleHeightTranslate = new UTranslate(0, titlesHeight);
return titleHeightTranslate;
}
2018-07-27 21:56:46 +00:00
private TextBlock getTitle(Swimlane swimlane) {
return swimlane.getDisplay().create(getFontConfiguration(), HorizontalAlignment.LEFT, skinParam,
skinParam.wrapWidth());
}
2013-12-10 19:36:50 +00:00
private void drawSeparation(UGraphic ug, double height) {
2016-03-06 16:47:34 +00:00
HtmlColor color = skinParam.getHtmlColor(ColorParam.swimlaneBorder, null, false);
if (color == null) {
color = ColorParam.swimlaneBorder.getDefaultValue();
}
2016-01-09 12:15:40 +00:00
final UStroke thickness = Rose.getStroke(skinParam, LineParam.swimlaneBorder, 2);
ug.apply(thickness).apply(new UChangeColor(color)).draw(new ULine(0, height));
2013-12-10 19:36:50 +00:00
}
public Dimension2D calculateDimension(StringBounder stringBounder) {
2017-11-20 16:10:36 +00:00
return getMinMax(stringBounder).getDimension();
2013-12-10 19:36:50 +00:00
}
public Instruction getCurrent() {
return currentInstruction;
}
public void setCurrent(Instruction current) {
this.currentInstruction = current;
}
public LinkRendering nextLinkRenderer() {
return nextLinkRenderer;
}
public void setNextLinkRenderer(LinkRendering link) {
2016-05-11 21:31:47 +00:00
if (link == null) {
throw new IllegalArgumentException();
}
2013-12-10 19:36:50 +00:00
this.nextLinkRenderer = link;
}
public Swimlane getCurrentSwimlane() {
return currentSwimlane;
}
2018-06-12 20:50:45 +00:00
2018-05-06 19:59:19 +00:00
private MinMax cachedMinMax;
@Override
public MinMax getMinMax(StringBounder stringBounder) {
if (cachedMinMax == null) {
cachedMinMax = TextBlockUtils.getMinMax(this, stringBounder);
}
return cachedMinMax;
}
2013-12-10 19:36:50 +00:00
}