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/MethodsOrFieldsArea.java

298 lines
9.9 KiB
Java
Raw Normal View History

2010-11-25 21:12:39 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, 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
import java.awt.geom.Dimension2D;
2015-05-31 18:56:03 +00:00
import java.awt.geom.Rectangle2D;
2010-11-25 21:12:39 +00:00
import java.util.ArrayList;
import java.util.List;
2016-12-14 21:01:03 +00:00
import java.util.Map;
2010-11-25 21:12:39 +00:00
import net.sourceforge.plantuml.Dimension2DDouble;
import net.sourceforge.plantuml.FontParam;
import net.sourceforge.plantuml.ISkinParam;
import net.sourceforge.plantuml.SkinParam;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Url;
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;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.TextBlockWidth;
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;
import net.sourceforge.plantuml.skin.rose.Rose;
import net.sourceforge.plantuml.style.SName;
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.UGraphic;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.ULayoutGroup;
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
2016-07-04 19:06:50 +00:00
public class MethodsOrFieldsArea extends AbstractTextBlock implements TextBlockWidth, TextBlock, WithPorts {
2015-06-07 10:23:10 +00:00
public TextBlock asBlockMemberImpl() {
return new TextBlockLineBefore(TextBlockUtils.withMargin(this, 6, 4));
}
2010-11-25 21:12:39 +00:00
2015-09-28 20:42:17 +00:00
private final FontParam fontParam;
2010-11-25 21:12:39 +00:00
private final ISkinParam skinParam;
private final Rose rose = new Rose();
2013-12-10 19:36:50 +00:00
private final List<Member> members = new ArrayList<Member>();
private final HorizontalAlignment align;
2016-03-06 16:47:34 +00:00
private final Stereotype stereotype;
2016-12-14 21:01:03 +00:00
private final ILeaf leaf;
2020-06-21 20:31:45 +00:00
private final SName diagramType;
2013-12-10 19:36:50 +00:00
2016-12-14 21:01:03 +00:00
public MethodsOrFieldsArea(List<Member> members, FontParam fontParam, ISkinParam skinParam, Stereotype stereotype,
2020-06-21 20:31:45 +00:00
ILeaf leaf, SName diagramType) {
this(members, fontParam, skinParam, HorizontalAlignment.LEFT, stereotype, leaf, diagramType);
2013-12-10 19:36:50 +00:00
}
2010-11-25 21:12:39 +00:00
2013-12-10 19:36:50 +00:00
public MethodsOrFieldsArea(List<Member> members, FontParam fontParam, ISkinParam skinParam,
2020-06-21 20:31:45 +00:00
HorizontalAlignment align, Stereotype stereotype, ILeaf leaf, SName diagramType) {
this.diagramType = diagramType;
2016-12-14 21:01:03 +00:00
this.leaf = leaf;
2016-03-06 16:47:34 +00:00
this.stereotype = stereotype;
2013-12-10 19:36:50 +00:00
this.align = align;
2010-11-25 21:12:39 +00:00
this.skinParam = skinParam;
2015-09-28 20:42:17 +00:00
this.fontParam = fontParam;
2013-12-10 19:36:50 +00:00
this.members.addAll(members);
2010-11-25 21:12:39 +00:00
}
private boolean hasSmallIcon() {
2011-09-08 10:42:27 +00:00
if (skinParam.classAttributeIconSize() == 0) {
return false;
}
2010-11-25 21:12:39 +00:00
for (Member m : members) {
if (m.getVisibilityModifier() != null) {
return true;
}
}
return false;
}
public Dimension2D calculateDimension(StringBounder stringBounder) {
2013-12-10 19:36:50 +00:00
double smallIcon = 0;
if (hasSmallIcon()) {
smallIcon = skinParam.getCircledCharacterRadius() + 3;
}
2010-11-25 21:12:39 +00:00
double x = 0;
double y = 0;
for (Member m : members) {
2013-12-10 19:36:50 +00:00
final TextBlock bloc = createTextBlock(m);
2010-11-25 21:12:39 +00:00
final Dimension2D dim = bloc.calculateDimension(stringBounder);
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;
2010-11-25 21:12:39 +00:00
return new Dimension2DDouble(x, y);
}
2016-07-04 19:06:50 +00:00
public Ports getPorts(StringBounder stringBounder) {
final Ports result = new Ports();
double y = 0;
2016-12-14 21:01:03 +00:00
final Election election = new Election();
for (Member m : members) {
2017-04-05 17:37:42 +00:00
election.addCandidate(m.getDisplay(false), m);
2016-12-14 21:01:03 +00:00
}
final Map<Member, String> memberWithPort = election.getAllElected(leaf.getPortShortNames());
2016-07-04 19:06:50 +00:00
for (Member m : members) {
final TextBlock bloc = createTextBlock(m);
final Dimension2D dim = bloc.calculateDimension(stringBounder);
2016-12-14 21:01:03 +00:00
final String port = memberWithPort.get(m);
if (port != null) {
result.add(port, y, dim.getHeight());
}
2016-07-04 19:06:50 +00:00
y += dim.getHeight();
}
return result;
}
2013-12-10 19:36:50 +00:00
private TextBlock createTextBlock(Member m) {
final boolean withVisibilityChar = skinParam.classAttributeIconSize() == 0;
2015-04-07 18:18:37 +00:00
String s = m.getDisplay(withVisibilityChar);
if (withVisibilityChar && s.startsWith("#")) {
s = CharHidder.addTileAtBegin(s);
}
FontConfiguration config;
if (SkinParam.USE_STYLES()) {
2020-06-21 20:31:45 +00:00
// final Style style = StyleSignature.of(SName.root, SName.element, SName.componentDiagram, SName.component)
// .getMergedStyle(skinParam.getCurrentStyleBuilder());
final Style style = fontParam.getStyleDefinition(diagramType)
.getMergedStyle(skinParam.getCurrentStyleBuilder());
2020-11-21 17:33:24 +00:00
config = new FontConfiguration(skinParam, style);
} else {
config = new FontConfiguration(skinParam, fontParam, stereotype);
}
2013-12-10 19:36:50 +00:00
if (m.isAbstract()) {
config = config.italic();
}
if (m.isStatic()) {
config = config.underline();
}
2020-04-05 15:13:04 +00:00
TextBlock bloc = Display.getWithNewlines(s).create8(config, align, skinParam, CreoleMode.SIMPLE_LINE,
2018-03-09 21:37:34 +00:00
skinParam.wrapWidth());
2015-06-07 10:23:10 +00:00
bloc = TextBlockUtils.fullInnerPosition(bloc, m.getDisplay(false));
2013-12-10 19:36:50 +00:00
return new TextBlockTracer(m, bloc);
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) {
if (url != null) {
ug.startUrl(url);
2010-11-25 21:12:39 +00:00
}
2013-12-10 19:36:50 +00:00
bloc.drawU(ug);
if (url != null) {
2020-05-17 21:15:50 +00:00
ug.closeUrl();
2010-11-25 21:12:39 +00:00
}
}
2013-12-10 19:36:50 +00:00
public Dimension2D calculateDimension(StringBounder stringBounder) {
final Dimension2D dim = bloc.calculateDimension(stringBounder);
return dim;
}
2015-09-28 20:42:17 +00:00
2015-06-07 10:23:10 +00:00
@Override
2017-04-05 17:37:42 +00:00
public Rectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
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
public Rectangle2D 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
public Dimension2D calculateDimension(StringBounder stringBounder) {
return new Dimension2DDouble(1, 1);
}
};
}
final HColor back = modifier.getBackground() == null ? null
: rose.getHtmlColor(skinParam, modifier.getBackground());
2020-03-18 10:50:02 +00:00
final HColor fore = rose.getHtmlColor(skinParam, modifier.getForeground());
2010-11-25 21:12:39 +00:00
2016-11-18 21:12:09 +00:00
final TextBlock uBlock = modifier.getUBlock(skinParam.classAttributeIconSize(), fore, back, url != null);
return TextBlockWithUrl.withUrl(uBlock, url);
2010-11-25 21:12:39 +00:00
}
2013-12-10 19:36:50 +00:00
public TextBlock asTextBlock(final double widthToUse) {
2015-06-07 10:23:10 +00:00
return this;
2015-05-31 18:56:03 +00:00
}
public boolean contains(String member) {
for (Member att : members) {
if (att.getDisplay(false).startsWith(member)) {
return true;
}
}
return false;
}
2015-06-07 10:23:10 +00:00
@Override
2017-04-05 17:37:42 +00:00
public Rectangle2D getInnerPosition(String member, StringBounder stringBounder, InnerStrategy strategy) {
2015-06-07 10:23:10 +00:00
final ULayoutGroup group = getLayout(stringBounder);
final Dimension2D dim = calculateDimension(stringBounder);
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));
2013-12-10 19:36:50 +00:00
for (Member att : members) {
final TextBlock bloc = createTextBlock(att);
final VisibilityModifier modifier = att.getVisibilityModifier();
2016-11-18 21:12:09 +00:00
group.add(getUBlock(modifier, att.getUrl()));
2013-12-10 19:36:50 +00:00
group.add(bloc);
}
} else {
final PlacementStrategy placementStrategy;
if (align == HorizontalAlignment.LEFT) {
2015-06-07 10:23:10 +00:00
placementStrategy = new PlacementStrategyY1Y2Left(stringBounder);
2013-12-10 19:36:50 +00:00
} else if (align == HorizontalAlignment.CENTER) {
2015-06-07 10:23:10 +00:00
placementStrategy = new PlacementStrategyY1Y2Center(stringBounder);
2013-12-10 19:36:50 +00:00
} else {
2018-10-21 19:44:14 +00:00
placementStrategy = new PlacementStrategyY1Y2Left(stringBounder);
2013-12-10 19:36:50 +00:00
}
group = new ULayoutGroup(placementStrategy);
for (Member att : members) {
final TextBlock bloc = createTextBlock(att);
group.add(bloc);
}
}
2015-06-07 10:23:10 +00:00
return group;
}
public void drawU(UGraphic ug) {
final ULayoutGroup group = getLayout(ug.getStringBounder());
final Dimension2D dim = calculateDimension(ug.getStringBounder());
group.drawU(ug, dim.getWidth(), dim.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
}