plantuml/src/net/sourceforge/plantuml/cucadiagram/MethodsOrFieldsArea.java

384 lines
12 KiB
Java
Raw Normal View History

2010-11-25 21:12:39 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2010-11-25 21:12:39 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2010-11-25 21:12:39 +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
*
2010-11-25 21:12:39 +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
2013-12-10 19:36:50 +00:00
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
2010-11-25 21:12:39 +00:00
* 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
*
*
*/
2013-12-10 19:36:50 +00:00
package net.sourceforge.plantuml.cucadiagram;
2010-11-25 21:12:39 +00:00
2022-06-29 16:45:19 +00:00
import java.util.ArrayList;
2021-12-14 21:38:22 +00:00
import java.util.Collection;
2022-06-29 16:45:19 +00:00
import java.util.Collections;
import java.util.Comparator;
2022-11-04 17:36:03 +00:00
import java.util.Iterator;
2022-06-29 16:45:19 +00:00
import java.util.List;
2010-11-25 21:12:39 +00:00
2021-07-25 10:41:36 +00:00
import net.sourceforge.plantuml.EmbeddedDiagram;
2010-11-25 21:12:39 +00:00
import net.sourceforge.plantuml.ISkinParam;
2022-12-17 11:10:05 +00:00
import net.sourceforge.plantuml.StringUtils;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Url;
2022-09-12 20:08:34 +00:00
import net.sourceforge.plantuml.awt.geom.XDimension2D;
import net.sourceforge.plantuml.awt.geom.XRectangle2D;
2022-11-07 19:27:11 +00:00
import net.sourceforge.plantuml.baraye.ILeaf;
2015-07-11 09:32:49 +00:00
import net.sourceforge.plantuml.creole.CreoleMode;
2015-06-07 10:23:10 +00:00
import net.sourceforge.plantuml.graphic.AbstractTextBlock;
2011-01-23 19:36:52 +00:00
import net.sourceforge.plantuml.graphic.FontConfiguration;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
2017-04-05 17:37:42 +00:00
import net.sourceforge.plantuml.graphic.InnerStrategy;
2010-11-25 21:12:39 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
2015-06-07 10:23:10 +00:00
import net.sourceforge.plantuml.graphic.TextBlockLineBefore;
2010-11-25 21:12:39 +00:00
import net.sourceforge.plantuml.graphic.TextBlockUtils;
2016-11-18 21:12:09 +00:00
import net.sourceforge.plantuml.graphic.TextBlockWithUrl;
2010-11-25 21:12:39 +00:00
import net.sourceforge.plantuml.skin.VisibilityModifier;
2022-02-01 20:21:45 +00:00
import net.sourceforge.plantuml.style.PName;
import net.sourceforge.plantuml.style.Style;
2016-07-04 19:06:50 +00:00
import net.sourceforge.plantuml.svek.Ports;
import net.sourceforge.plantuml.svek.WithPorts;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.PlacementStrategy;
2010-11-25 21:12:39 +00:00
import net.sourceforge.plantuml.ugraphic.PlacementStrategyVisibility;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.PlacementStrategyY1Y2Center;
2010-11-25 21:12:39 +00:00
import net.sourceforge.plantuml.ugraphic.PlacementStrategyY1Y2Left;
import net.sourceforge.plantuml.ugraphic.PlacementStrategyY1Y2Right;
2010-11-25 21:12:39 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.ULayoutGroup;
2022-11-04 17:36:03 +00:00
import net.sourceforge.plantuml.ugraphic.UTranslate;
2020-03-18 10:50:02 +00:00
import net.sourceforge.plantuml.ugraphic.color.HColor;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.utils.CharHidder;
2010-11-25 21:12:39 +00:00
2020-12-14 18:31:06 +00:00
public class MethodsOrFieldsArea extends AbstractTextBlock implements TextBlock, WithPorts {
2015-06-07 10:23:10 +00:00
public TextBlock asBlockMemberImpl() {
2022-02-12 17:27:51 +00:00
return new TextBlockLineBefore(style.value(PName.LineThickness).asDouble(),
TextBlockUtils.withMargin(this, 6, 4));
2015-06-07 10:23:10 +00:00
}
2010-11-25 21:12:39 +00:00
private final ISkinParam skinParam;
2022-04-10 19:24:55 +00:00
2020-12-14 18:31:06 +00:00
private final Display members;
2013-12-10 19:36:50 +00:00
private final HorizontalAlignment align;
2022-11-04 17:36:03 +00:00
private final List<EmbeddedDiagram> embeddeds = new ArrayList<>();
2022-05-21 09:41:00 +00:00
2016-12-14 21:01:03 +00:00
private final ILeaf leaf;
2020-12-01 21:39:27 +00:00
private final Style style;
2013-12-10 19:36:50 +00:00
2022-05-21 09:41:00 +00:00
public MethodsOrFieldsArea(Display members, ISkinParam skinParam, ILeaf leaf, Style style) {
this(members, skinParam, HorizontalAlignment.LEFT, leaf, style);
2013-12-10 19:36:50 +00:00
}
2010-11-25 21:12:39 +00:00
2022-05-21 09:41:00 +00:00
public MethodsOrFieldsArea(Display members, ISkinParam skinParam, HorizontalAlignment align, ILeaf leaf,
Style style) {
2020-12-01 21:39:27 +00:00
this.style = style;
2016-12-14 21:01:03 +00:00
this.leaf = leaf;
2022-05-21 09:41:00 +00:00
2013-12-10 19:36:50 +00:00
this.align = align;
2010-11-25 21:12:39 +00:00
this.skinParam = skinParam;
2022-11-04 17:36:03 +00:00
final List<CharSequence> result = new ArrayList<>();
final Iterator<CharSequence> it = members.iterator();
while (it.hasNext()) {
final CharSequence cs = it.next();
final String type = EmbeddedDiagram.getEmbeddedType(StringUtils.trinNoTrace(cs));
if (type != null)
embeddeds.add(EmbeddedDiagram.createAndSkip(type, it, skinParam));
else
result.add(cs);
}
this.members = Display.create(result);
2010-11-25 21:12:39 +00:00
}
private boolean hasSmallIcon() {
2022-05-21 09:41:00 +00:00
if (skinParam.classAttributeIconSize() == 0)
2011-09-08 10:42:27 +00:00
return false;
2022-05-21 09:41:00 +00:00
2020-12-14 18:31:06 +00:00
for (CharSequence cs : members) {
if (cs instanceof Member == false)
continue;
final Member m = (Member) cs;
2021-12-14 21:38:22 +00:00
if (m.getVisibilityModifier() != null)
2010-11-25 21:12:39 +00:00
return true;
2021-12-14 21:38:22 +00:00
2010-11-25 21:12:39 +00:00
}
return false;
}
2022-11-04 17:36:03 +00:00
@Override
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
2022-11-04 17:36:03 +00:00
final XDimension2D dim1 = calculateDimensionOnlyMembers(stringBounder);
double x = dim1.getWidth();
double y = dim1.getHeight();
for (EmbeddedDiagram embedded : embeddeds) {
final XDimension2D dim = embedded.calculateDimension(stringBounder);
x = Math.max(dim.getWidth(), x);
y += dim.getHeight();
}
return new XDimension2D(x, y);
}
private XDimension2D calculateDimensionOnlyMembers(StringBounder stringBounder) {
2013-12-10 19:36:50 +00:00
double smallIcon = 0;
2022-05-21 09:41:00 +00:00
if (hasSmallIcon())
2013-12-10 19:36:50 +00:00
smallIcon = skinParam.getCircledCharacterRadius() + 3;
2022-05-21 09:41:00 +00:00
2010-11-25 21:12:39 +00:00
double x = 0;
double y = 0;
2020-12-14 18:31:06 +00:00
for (CharSequence cs : members) {
final TextBlock bloc = createTextBlock(cs);
2022-09-12 20:08:34 +00:00
final XDimension2D dim = bloc.calculateDimension(stringBounder);
2010-11-25 21:12:39 +00:00
x = Math.max(dim.getWidth(), x);
2013-12-10 19:36:50 +00:00
y += dim.getHeight();
2010-11-25 21:12:39 +00:00
}
2013-12-10 19:36:50 +00:00
x += smallIcon;
2022-11-04 17:36:03 +00:00
2022-09-12 20:08:34 +00:00
return new XDimension2D(x, y);
2010-11-25 21:12:39 +00:00
}
2022-06-29 16:45:19 +00:00
private Collection<String> sortBySize(Collection<String> all) {
final List<String> result = new ArrayList<String>(all);
Collections.sort(result, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
final int diff = s2.length() - s1.length();
if (diff != 0)
return diff;
return s1.compareTo(s2);
}
});
return result;
}
2021-12-14 21:38:22 +00:00
@Override
2016-07-04 19:06:50 +00:00
public Ports getPorts(StringBounder stringBounder) {
2022-06-29 16:45:19 +00:00
final Ports ports = new Ports();
2016-07-04 19:06:50 +00:00
double y = 0;
2021-12-14 21:38:22 +00:00
2022-06-29 16:45:19 +00:00
final Collection<String> shortNames = sortBySize(leaf.getPortShortNames());
2020-12-14 18:31:06 +00:00
for (CharSequence cs : members) {
final TextBlock bloc = createTextBlock(cs);
2022-09-12 20:08:34 +00:00
final XDimension2D dim = bloc.calculateDimension(stringBounder);
2022-06-29 16:45:19 +00:00
final Elected elected = getElected(convert(cs), shortNames);
if (elected != null)
ports.add(elected.getShortName(), elected.getScore(), y, dim.getHeight());
2021-12-14 21:38:22 +00:00
2016-07-04 19:06:50 +00:00
y += dim.getHeight();
}
2022-06-29 16:45:19 +00:00
return ports;
2016-07-04 19:06:50 +00:00
}
2021-12-14 21:38:22 +00:00
private String convert(CharSequence cs) {
if (cs instanceof Member)
return ((Member) cs).getDisplay(false);
return cs.toString();
}
2022-06-29 16:45:19 +00:00
public Elected getElected(String cs, Collection<String> shortNames) {
for (String shortName : shortNames) {
final int score = getScore(cs, shortName);
2021-12-14 21:38:22 +00:00
if (score > 0)
return new Elected(shortName, score);
}
return null;
}
2022-06-29 16:45:19 +00:00
private int getScore(String cs, String shortName) {
2021-12-14 21:38:22 +00:00
if (cs.matches(".*\\b" + shortName + "\\b.*"))
return 100;
if (cs.contains(shortName))
return 50;
return 0;
}
2020-12-14 18:31:06 +00:00
private TextBlock createTextBlock(CharSequence cs) {
2022-05-27 14:18:47 +00:00
FontConfiguration config = FontConfiguration.create(skinParam, style, leaf.getColors());
2020-12-14 18:31:06 +00:00
if (cs instanceof Member) {
final Member m = (Member) cs;
final boolean withVisibilityChar = skinParam.classAttributeIconSize() == 0;
String s = m.getDisplay(withVisibilityChar);
2021-12-14 21:38:22 +00:00
if (withVisibilityChar && s.startsWith("#"))
2020-12-14 18:31:06 +00:00
s = CharHidder.addTileAtBegin(s);
2021-12-14 21:38:22 +00:00
if (m.isAbstract())
2020-12-14 18:31:06 +00:00
config = config.italic();
2021-12-14 21:38:22 +00:00
if (m.isStatic())
2020-12-14 18:31:06 +00:00
config = config.underline();
TextBlock bloc = Display.getWithNewlines(s).create8(config, align, skinParam, CreoleMode.SIMPLE_LINE,
2023-01-09 19:13:37 +00:00
style.wrapWidth());
2020-12-14 18:31:06 +00:00
bloc = TextBlockUtils.fullInnerPosition(bloc, m.getDisplay(false));
return new TextBlockTracer(m, bloc);
2013-12-10 19:36:50 +00:00
}
2022-11-04 17:36:03 +00:00
// if (cs instanceof EmbeddedDiagram)
// return ((EmbeddedDiagram) cs).asDraw(skinParam);
2021-07-25 10:41:36 +00:00
return Display.getWithNewlines(cs.toString()).create8(config, align, skinParam, CreoleMode.SIMPLE_LINE,
2023-01-09 19:13:37 +00:00
style.wrapWidth());
2020-12-14 18:31:06 +00:00
2010-11-25 21:12:39 +00:00
}
2015-06-07 10:23:10 +00:00
static class TextBlockTracer extends AbstractTextBlock implements TextBlock {
2010-11-25 21:12:39 +00:00
2013-12-10 19:36:50 +00:00
private final TextBlock bloc;
private final Url url;
public TextBlockTracer(Member m, TextBlock bloc) {
this.bloc = bloc;
this.url = m.getUrl();
}
public void drawU(UGraphic ug) {
2021-12-14 21:38:22 +00:00
if (url != null)
2013-12-10 19:36:50 +00:00
ug.startUrl(url);
2021-12-14 21:38:22 +00:00
2013-12-10 19:36:50 +00:00
bloc.drawU(ug);
2021-12-14 21:38:22 +00:00
if (url != null)
2020-05-17 21:15:50 +00:00
ug.closeUrl();
2021-12-14 21:38:22 +00:00
2010-11-25 21:12:39 +00:00
}
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
final XDimension2D dim = bloc.calculateDimension(stringBounder);
2013-12-10 19:36:50 +00:00
return dim;
}
2015-09-28 20:42:17 +00:00
2015-06-07 10:23:10 +00:00
@Override
2022-09-12 20:08:34 +00:00
public XRectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
2017-04-05 17:37:42 +00:00
return bloc.getInnerPosition(member, stringBounder, strategy);
2015-06-07 10:23:10 +00:00
}
2010-11-25 21:12:39 +00:00
2011-09-08 10:42:27 +00:00
}
2016-11-18 21:12:09 +00:00
private TextBlock getUBlock(final VisibilityModifier modifier, Url url) {
2010-11-25 21:12:39 +00:00
if (modifier == null) {
2015-06-07 10:23:10 +00:00
return new AbstractTextBlock() {
2010-11-25 21:12:39 +00:00
2013-12-10 19:36:50 +00:00
public void drawU(UGraphic ug) {
2010-11-25 21:12:39 +00:00
}
2019-03-01 22:16:29 +00:00
@Override
2022-09-12 20:08:34 +00:00
public XRectangle2D getInnerPosition(String member, StringBounder stringBounder,
InnerStrategy strategy) {
2019-03-01 22:16:29 +00:00
return null;
}
2010-11-25 21:12:39 +00:00
2022-09-12 20:08:34 +00:00
public XDimension2D calculateDimension(StringBounder stringBounder) {
return new XDimension2D(1, 1);
2010-11-25 21:12:39 +00:00
}
};
}
2022-04-10 19:24:55 +00:00
final Style style = modifier.getStyleSignature().getMergedStyle(skinParam.getCurrentStyleBuilder());
2022-09-18 17:08:06 +00:00
final HColor borderColor = style.value(PName.LineColor).asColor(skinParam.getIHtmlColorSet());
2022-04-10 19:24:55 +00:00
final boolean isField = modifier.isField();
final HColor backColor = isField ? null
2022-09-18 17:08:06 +00:00
: style.value(PName.BackGroundColor).asColor(skinParam.getIHtmlColorSet());
2010-11-25 21:12:39 +00:00
2022-02-01 20:21:45 +00:00
final TextBlock uBlock = modifier.getUBlock(skinParam.classAttributeIconSize(), borderColor, backColor,
url != null);
2016-11-18 21:12:09 +00:00
return TextBlockWithUrl.withUrl(uBlock, url);
2010-11-25 21:12:39 +00:00
}
2015-05-31 18:56:03 +00:00
public boolean contains(String member) {
2020-12-14 18:31:06 +00:00
for (CharSequence cs : members) {
final Member att = (Member) cs;
2021-12-14 21:38:22 +00:00
if (att.getDisplay(false).startsWith(member))
2015-05-31 18:56:03 +00:00
return true;
2021-12-14 21:38:22 +00:00
2015-05-31 18:56:03 +00:00
}
return false;
}
2015-06-07 10:23:10 +00:00
@Override
2022-09-12 20:08:34 +00:00
public XRectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
2015-06-07 10:23:10 +00:00
final ULayoutGroup group = getLayout(stringBounder);
2022-09-12 20:08:34 +00:00
final XDimension2D dim = calculateDimension(stringBounder);
2015-06-07 10:23:10 +00:00
return group.getInnerPosition(member, dim.getWidth(), dim.getHeight(), stringBounder);
}
private ULayoutGroup getLayout(final StringBounder stringBounder) {
2013-12-10 19:36:50 +00:00
final ULayoutGroup group;
if (hasSmallIcon()) {
group = new ULayoutGroup(
new PlacementStrategyVisibility(stringBounder, skinParam.getCircledCharacterRadius() + 3));
2020-12-14 18:31:06 +00:00
for (CharSequence cs : members) {
2021-08-30 17:13:54 +00:00
final TextBlock bloc = createTextBlock(cs);
2022-11-04 17:36:03 +00:00
// if (cs instanceof EmbeddedDiagram) {
// group.add(getUBlock(null, null));
// } else {
final Member att = (Member) cs;
final VisibilityModifier modifier = att.getVisibilityModifier();
group.add(getUBlock(modifier, att.getUrl()));
// }
2013-12-10 19:36:50 +00:00
group.add(bloc);
}
} else {
final PlacementStrategy placementStrategy;
2021-12-14 21:38:22 +00:00
if (align == HorizontalAlignment.LEFT)
2015-06-07 10:23:10 +00:00
placementStrategy = new PlacementStrategyY1Y2Left(stringBounder);
2021-12-14 21:38:22 +00:00
else if (align == HorizontalAlignment.CENTER)
2015-06-07 10:23:10 +00:00
placementStrategy = new PlacementStrategyY1Y2Center(stringBounder);
2021-12-14 21:38:22 +00:00
else
placementStrategy = new PlacementStrategyY1Y2Right(stringBounder);
2021-12-14 21:38:22 +00:00
2013-12-10 19:36:50 +00:00
group = new ULayoutGroup(placementStrategy);
2020-12-14 18:31:06 +00:00
for (CharSequence cs : members) {
final TextBlock bloc = createTextBlock(cs);
2013-12-10 19:36:50 +00:00
group.add(bloc);
}
}
2015-06-07 10:23:10 +00:00
return group;
}
public void drawU(UGraphic ug) {
2022-11-04 17:36:03 +00:00
final StringBounder stringBounder = ug.getStringBounder();
final ULayoutGroup group = getLayout(stringBounder);
final XDimension2D dim = calculateDimensionOnlyMembers(stringBounder);
2015-06-07 10:23:10 +00:00
group.drawU(ug, dim.getWidth(), dim.getHeight());
2022-11-04 17:36:03 +00:00
ug = ug.apply(UTranslate.dy(dim.getHeight()));
for (EmbeddedDiagram embedded : embeddeds) {
embedded.drawU(ug);
ug = ug.apply(UTranslate.dy(embedded.calculateDimension(stringBounder).getHeight()));
}
2013-12-10 19:36:50 +00:00
}
2015-05-31 18:56:03 +00:00
2010-11-25 21:12:39 +00:00
}