1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-01 08:00:48 +00:00
plantuml/src/net/sourceforge/plantuml/cucadiagram/BodyEnhanced1.java

254 lines
7.9 KiB
Java
Raw Normal View History

2020-12-14 18:31:06 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2020-12-14 18:31:06 +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.cucadiagram;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
2021-02-02 10:12:15 +00:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2020-12-14 18:31:06 +00:00
import net.sourceforge.plantuml.ISkinParam;
2022-12-17 11:10:05 +00:00
import net.sourceforge.plantuml.StringUtils;
2020-12-14 18:31:06 +00:00
import net.sourceforge.plantuml.Url;
2022-09-12 20:08:34 +00:00
import net.sourceforge.plantuml.awt.geom.XRectangle2D;
import net.sourceforge.plantuml.baraye.EntityImp;
2020-12-14 18:31:06 +00:00
import net.sourceforge.plantuml.creole.CreoleMode;
import net.sourceforge.plantuml.creole.Parser;
import net.sourceforge.plantuml.creole.legacy.CreoleParser;
import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
import net.sourceforge.plantuml.graphic.InnerStrategy;
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.graphic.TextBlockUtils;
import net.sourceforge.plantuml.graphic.TextBlockVertical2;
2022-10-18 20:57:44 +00:00
import net.sourceforge.plantuml.style.PName;
2020-12-14 18:31:06 +00:00
import net.sourceforge.plantuml.style.Style;
import net.sourceforge.plantuml.svek.Ports;
import net.sourceforge.plantuml.svek.WithPorts;
public class BodyEnhanced1 extends BodyEnhancedAbstract implements TextBlock, WithPorts {
private final Display rawBody2;
2022-05-21 09:41:00 +00:00
2020-12-14 18:31:06 +00:00
private final ISkinParam skinParam;
private final boolean lineFirst;
2021-05-14 08:42:57 +00:00
private final List<Url> urls = new ArrayList<>();
2022-05-21 09:41:00 +00:00
private final EntityImp entity;
2020-12-14 18:31:06 +00:00
private final boolean inEllipse;
private final Style style;
BodyEnhanced1(HorizontalAlignment align, List<CharSequence> rawBody, ISkinParam skinParam, EntityImp entity,
2022-05-21 09:41:00 +00:00
Style style) {
2022-10-18 20:57:44 +00:00
super(align, style.getFontConfiguration(skinParam.getIHtmlColorSet(), entity.getColors()), style);
2020-12-14 18:31:06 +00:00
this.style = style;
this.rawBody2 = Display.create(rawBody);
2022-05-21 09:41:00 +00:00
2020-12-14 18:31:06 +00:00
this.skinParam = skinParam;
this.lineFirst = true;
this.entity = entity;
this.inEllipse = false;
}
BodyEnhanced1(HorizontalAlignment align, Display display, ISkinParam skinParam, EntityImp entity, Style style) {
2022-10-18 20:57:44 +00:00
super(align, style.getFontConfiguration(skinParam.getIHtmlColorSet(), entity.getColors()), style);
2020-12-14 18:31:06 +00:00
this.style = style;
this.entity = entity;
2022-05-21 09:41:00 +00:00
2020-12-14 18:31:06 +00:00
this.skinParam = skinParam;
this.lineFirst = false;
2022-05-21 09:41:00 +00:00
final LeafType leafType = entity.getLeafType();
this.inEllipse = leafType == LeafType.USECASE || leafType == LeafType.USECASE_BUSINESS;
2020-12-14 18:31:06 +00:00
2022-02-12 17:27:51 +00:00
if (inEllipse && display.size() > 0 && isBlockSeparator(display.get(0).toString()))
2020-12-14 18:31:06 +00:00
display = display.add("");
2022-02-12 17:27:51 +00:00
2020-12-14 18:31:06 +00:00
this.rawBody2 = display;
}
@Override
protected double getMarginX() {
return 6;
}
private static boolean isTreeOrTable(String s) {
2021-02-02 10:12:15 +00:00
s = StringUtils.trinNoTrace(s);
2020-12-14 18:31:06 +00:00
return Parser.isTreeStart(s) || CreoleParser.isTableLine(s);
}
@Override
2021-12-14 21:38:22 +00:00
final protected TextBlock getArea(StringBounder stringBounder) {
2022-02-12 17:27:51 +00:00
if (area != null)
2020-12-14 18:31:06 +00:00
return area;
2022-02-12 17:27:51 +00:00
2020-12-14 18:31:06 +00:00
urls.clear();
2021-05-14 08:42:57 +00:00
final List<TextBlock> blocks = new ArrayList<>();
2020-12-14 18:31:06 +00:00
char separator = lineFirst ? '_' : 0;
TextBlock title = null;
2022-05-31 16:31:10 +00:00
Display display = null;
2020-12-14 18:31:06 +00:00
for (ListIterator<CharSequence> it = rawBody2.iterator(); it.hasNext();) {
final CharSequence cs = it.next();
2022-11-04 17:36:03 +00:00
// if (cs instanceof EmbeddedDiagram) {
// if (display == null)
// display = Display.empty();
// if (display.size() > 0 || separator != 0) {
// blocks.add(buildTextBlock(display, separator, title, stringBounder));
// separator = 0;
// title = null;
// display = null;
// }
// blocks.add(TextBlockUtils.withMargin(((EmbeddedDiagram) cs).asDraw(), 2, 2));
// } else {
final String s = cs.toString();
if (isBlockSeparator(s)) {
2022-05-31 16:31:10 +00:00
if (display == null)
display = Display.empty();
2022-11-04 17:36:03 +00:00
blocks.add(buildTextBlock(display, separator, title, stringBounder));
separator = s.charAt(0);
title = getTitle(s, skinParam);
display = null;
} else if (isTreeOrTable(s)) {
final boolean isTable = CreoleParser.isTableLine(s);
if (display == null)
display = Display.empty();
blocks.add(buildTextBlock(display, separator, title, stringBounder));
separator = 0;
title = null;
display = null;
final List<CharSequence> allTree = buildTreeOrTable(s, it);
final FontConfiguration fontConfiguration = style.getFontConfiguration(skinParam.getIHtmlColorSet());
TextBlock bloc = Display.create(allTree).create7(fontConfiguration, align, skinParam, CreoleMode.FULL);
if (isTable)
bloc = TextBlockUtils.withMargin(bloc, 10, 10, 0, 5);
blocks.add(bloc);
2020-12-14 18:31:06 +00:00
} else {
2022-11-04 17:36:03 +00:00
if (display == null)
display = Display.empty();
display = display.add(cs);
if (cs instanceof Member && ((Member) cs).getUrl() != null)
urls.add(((Member) cs).getUrl());
2020-12-14 18:31:06 +00:00
}
2022-11-04 17:36:03 +00:00
// }
2020-12-14 18:31:06 +00:00
}
2022-05-31 16:31:10 +00:00
if (display == null)
display = Display.empty();
if (inEllipse && display.size() == 0)
2020-12-14 18:31:06 +00:00
display = display.add("");
2022-05-31 16:31:10 +00:00
2022-10-18 20:57:44 +00:00
blocks.add(buildTextBlock(display, separator, title, stringBounder));
2020-12-14 18:31:06 +00:00
2022-02-12 17:27:51 +00:00
if (blocks.size() == 1)
2020-12-14 18:31:06 +00:00
this.area = blocks.get(0);
2022-02-12 17:27:51 +00:00
else
2020-12-14 18:31:06 +00:00
this.area = new TextBlockVertical2(blocks, align);
2022-10-18 20:57:44 +00:00
final double minClassWidth = style.value(PName.MinimumWidth).asDouble();
if (minClassWidth > 0)
this.area = TextBlockUtils.withMinWidth(this.area, minClassWidth, align);
2020-12-14 18:31:06 +00:00
return area;
}
2022-10-18 20:57:44 +00:00
private TextBlock buildTextBlock(Display display, char separator, TextBlock title, StringBounder stringBounder) {
// TextBlock result = display.create9(titleConfig, align, skinParam,
// LineBreakStrategy.NONE);
TextBlock result = new MethodsOrFieldsArea(display, skinParam, align, entity, style);
result = decorate(result, separator, title, stringBounder);
return result;
}
2020-12-14 18:31:06 +00:00
private static List<CharSequence> buildTreeOrTable(String init, ListIterator<CharSequence> it) {
2021-05-14 08:42:57 +00:00
final List<CharSequence> result = new ArrayList<>();
2021-02-02 10:12:15 +00:00
final Pattern p = Pattern.compile("^(\\s+)");
final Matcher m = p.matcher(init);
String start = "";
2022-02-12 17:27:51 +00:00
if (m.find())
2021-02-02 10:12:15 +00:00
start = m.group(1);
2022-02-12 17:27:51 +00:00
2021-02-02 10:12:15 +00:00
result.add(purge(init, start));
2020-12-14 18:31:06 +00:00
while (it.hasNext()) {
2021-02-02 10:12:15 +00:00
String s = it.next().toString();
if (isTreeOrTable(s)) {
result.add(purge(s, start));
2020-12-14 18:31:06 +00:00
} else {
it.previous();
return result;
}
}
return result;
}
2021-02-02 10:12:15 +00:00
private static String purge(String s, String start) {
2022-02-12 17:27:51 +00:00
if (s.startsWith(start))
2021-02-02 10:12:15 +00:00
return s.substring(start.length());
2022-02-12 17:27:51 +00:00
2021-02-02 10:12:15 +00:00
return s;
}
2021-12-14 21:38:22 +00:00
@Override
2020-12-14 18:31:06 +00:00
public Ports getPorts(StringBounder stringBounder) {
final TextBlock area = getArea(stringBounder);
2022-02-12 17:27:51 +00:00
if (area instanceof WithPorts)
2020-12-14 18:31:06 +00:00
return ((WithPorts) area).getPorts(stringBounder);
2022-02-12 17:27:51 +00:00
2020-12-14 18:31:06 +00:00
return new Ports();
}
public List<Url> getUrls() {
return Collections.unmodifiableList(urls);
}
2022-09-12 20:08:34 +00:00
public XRectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
2020-12-14 18:31:06 +00:00
return getArea(stringBounder).getInnerPosition(member, stringBounder, strategy);
}
}