1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-05-31 23:50:49 +00:00
plantuml/src/net/sourceforge/plantuml/cucadiagram/MethodsOrFieldsArea.java

251 lines
8.2 KiB
Java
Raw Normal View History

2010-11-25 21:12:39 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2016-01-09 12:15:40 +00:00
* (C) Copyright 2009-2017, Arnaud Roques
2010-11-25 21:12:39 +00:00
*
* Project Info: http://plantuml.sourceforge.net
*
* 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.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* Original Author: Arnaud Roques
*
2013-12-10 19:36:50 +00:00
* Revision $Revision: 4749 $
2010-11-25 21:12:39 +00:00
*
*/
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;
import net.sourceforge.plantuml.Dimension2DDouble;
import net.sourceforge.plantuml.FontParam;
import net.sourceforge.plantuml.ISkinParam;
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;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.graphic.HtmlColor;
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;
2010-11-25 21:12:39 +00:00
import net.sourceforge.plantuml.skin.VisibilityModifier;
import net.sourceforge.plantuml.skin.rose.Rose;
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;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.ugraphic.UFont;
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;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.utils.CharHidder;
2010-11-25 21:12:39 +00:00
2015-06-07 10:23:10 +00:00
public class MethodsOrFieldsArea extends AbstractTextBlock implements TextBlockWidth, TextBlock {
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;
2011-08-08 17:48:29 +00:00
private final HtmlColor color;
2015-04-07 18:18:37 +00:00
private final HtmlColor hyperlinkColor;
private final boolean useUnderlineForHyperlink;
2010-11-25 21:12:39 +00:00
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;
public MethodsOrFieldsArea(List<Member> members, FontParam fontParam, ISkinParam skinParam) {
this(members, fontParam, skinParam, HorizontalAlignment.LEFT);
}
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,
HorizontalAlignment align) {
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.color = rose.getFontColor(skinParam, fontParam);
2015-04-07 18:18:37 +00:00
this.hyperlinkColor = skinParam.getHyperlinkColor();
this.useUnderlineForHyperlink = skinParam.useUnderlineForHyperlink();
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);
}
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);
}
2015-09-28 20:42:17 +00:00
FontConfiguration config = new FontConfiguration(skinParam, fontParam, null);
2013-12-10 19:36:50 +00:00
if (m.isAbstract()) {
config = config.italic();
}
if (m.isStatic()) {
config = config.underline();
}
2015-07-11 09:32:49 +00:00
TextBlock bloc = Display.getWithNewlines(s).create(config, align, skinParam, CreoleMode.SIMPLE_LINE);
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) {
ug.closeAction();
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
public Rectangle2D getInnerPosition(String member, StringBounder stringBounder) {
return bloc.getInnerPosition(member, stringBounder);
}
2010-11-25 21:12:39 +00:00
2011-09-08 10:42:27 +00:00
}
2010-11-25 21:12:39 +00:00
private TextBlock getUBlock(final VisibilityModifier modifier) {
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
}
public Dimension2D calculateDimension(StringBounder stringBounder) {
return new Dimension2DDouble(1, 1);
}
};
}
2013-12-10 19:36:50 +00:00
final HtmlColor back = modifier.getBackground() == null ? null : rose.getHtmlColor(skinParam,
modifier.getBackground());
2011-08-08 17:48:29 +00:00
final HtmlColor fore = rose.getHtmlColor(skinParam, modifier.getForeground());
2010-11-25 21:12:39 +00:00
2011-09-08 10:42:27 +00:00
final TextBlock uBlock = modifier.getUBlock(skinParam.classAttributeIconSize(), fore, back);
2010-11-25 21:12:39 +00:00
return uBlock;
}
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
public Rectangle2D getInnerPosition(String member, StringBounder stringBounder) {
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()) {
2015-06-07 10:23:10 +00:00
group = new ULayoutGroup(new PlacementStrategyVisibility(stringBounder,
2013-12-10 19:36:50 +00:00
skinParam.getCircledCharacterRadius() + 3));
for (Member att : members) {
final TextBlock bloc = createTextBlock(att);
final VisibilityModifier modifier = att.getVisibilityModifier();
group.add(getUBlock(modifier));
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 {
throw new IllegalStateException();
}
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
}