1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-05-28 22:20:49 +00:00
plantuml/src/net/sourceforge/plantuml/cucadiagram/Link.java

574 lines
15 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2019-01-16 18:34:41 +00:00
* (C) Copyright 2009-2020, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2010-11-15 20:35:36 +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-15 20:35:36 +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-15 20:35:36 +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
*
*
*/
package net.sourceforge.plantuml.cucadiagram;
2010-11-25 21:12:39 +00:00
import java.awt.geom.Dimension2D;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Hideable;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ISkinSimple;
import net.sourceforge.plantuml.OptionFlags;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Removeable;
2019-06-26 19:24:49 +00:00
import net.sourceforge.plantuml.UmlDiagramType;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.Url;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.command.Position;
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-03-20 21:40:07 +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-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.graphic.USymbolInterface;
2015-09-28 20:42:17 +00:00
import net.sourceforge.plantuml.graphic.color.Colors;
2018-10-21 19:44:14 +00:00
import net.sourceforge.plantuml.skin.VisibilityModifier;
2019-07-14 20:09:26 +00:00
import net.sourceforge.plantuml.style.StyleBuilder;
2016-07-04 19:06:50 +00:00
import net.sourceforge.plantuml.svek.Bibliotekon;
2019-08-26 17:07:21 +00:00
import net.sourceforge.plantuml.ugraphic.UComment;
2011-08-08 17:48:29 +00:00
import net.sourceforge.plantuml.ugraphic.UFont;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.utils.UniqueSequence;
2010-11-15 20:35:36 +00:00
2018-06-25 19:05:58 +00:00
public class Link extends WithLinkType implements Hideable, Removeable {
2010-11-15 20:35:36 +00:00
2019-07-14 20:09:26 +00:00
public final StyleBuilder getStyleBuilder() {
return styleBuilder;
}
2010-11-15 20:35:36 +00:00
final private IEntity cl1;
final private IEntity cl2;
2016-07-04 19:06:50 +00:00
private String port1;
private String port2;
2013-12-10 19:36:50 +00:00
final private Display label;
2010-11-15 20:35:36 +00:00
2011-01-09 19:00:05 +00:00
private int length;
2010-11-15 20:35:36 +00:00
final private String qualifier1;
final private String qualifier2;
final private String uid = "LNK" + UniqueSequence.getValue();
2013-12-10 19:36:50 +00:00
private Display note;
2011-08-08 17:48:29 +00:00
private Position notePosition;
2015-09-28 20:42:17 +00:00
private Colors noteColors;
2016-09-29 19:51:18 +00:00
private NoteLinkStrategy noteLinkStrategy;
2011-08-08 17:48:29 +00:00
2010-11-15 20:35:36 +00:00
private boolean invis = false;
2011-01-05 18:23:06 +00:00
private double weight = 1.0;
2010-11-15 20:35:36 +00:00
private final String labeldistance;
private final String labelangle;
2011-04-19 16:50:40 +00:00
private boolean constraint = true;
2011-08-08 17:48:29 +00:00
private boolean inverted = false;
2013-12-10 19:36:50 +00:00
private LinkArrow linkArrow = LinkArrow.NONE;
2011-08-08 17:48:29 +00:00
2013-12-10 19:36:50 +00:00
private boolean opale;
private boolean horizontalSolitary;
2015-04-07 18:18:37 +00:00
private String sametail;
2018-10-21 19:44:14 +00:00
private VisibilityModifier visibilityModifier;
2019-07-14 20:09:26 +00:00
private final StyleBuilder styleBuilder;
2011-03-20 21:40:07 +00:00
2013-12-10 19:36:50 +00:00
private Url url;
2019-08-26 17:07:21 +00:00
public String idCommentForSvg() {
if (type.looksLikeRevertedForSvg()) {
final String comment = getEntity1().getCode().getFullName() + "<-" + getEntity2().getCode().getFullName();
return comment;
}
if (type.looksLikeNoDecorAtAllSvg()) {
final String comment = getEntity1().getCode().getFullName() + "-" + getEntity2().getCode().getFullName();
return comment;
}
final String comment = getEntity1().getCode().getFullName() + "->" + getEntity2().getCode().getFullName();
return comment;
}
public UComment commentForSvg() {
if (type.looksLikeRevertedForSvg()) {
return new UComment("reverse link " + getEntity1().getCode().getFullName() + " to "
+ getEntity2().getCode().getFullName());
}
return new UComment("link " + getEntity1().getCode().getFullName() + " to "
+ getEntity2().getCode().getFullName());
}
2019-07-14 20:09:26 +00:00
public Link(IEntity cl1, IEntity cl2, LinkType type, Display label, int length, StyleBuilder styleBuilder) {
this(cl1, cl2, type, label, length, null, null, null, null, null, styleBuilder);
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
public Link(IEntity cl1, IEntity cl2, LinkType type, Display label, int length, String qualifier1,
2019-07-14 20:09:26 +00:00
String qualifier2, String labeldistance, String labelangle, StyleBuilder styleBuilder) {
this(cl1, cl2, type, label, length, qualifier1, qualifier2, labeldistance, labelangle, null, styleBuilder);
2011-03-20 21:40:07 +00:00
}
2013-12-10 19:36:50 +00:00
public Link(IEntity cl1, IEntity cl2, LinkType type, Display label, int length, String qualifier1,
2019-07-14 20:09:26 +00:00
String qualifier2, String labeldistance, String labelangle, HtmlColor specificColor,
StyleBuilder styleBuilder) {
2010-11-15 20:35:36 +00:00
if (length < 1) {
throw new IllegalArgumentException();
}
2013-12-10 19:36:50 +00:00
if (cl1 == null) {
throw new IllegalArgumentException();
}
if (cl2 == null) {
2010-11-15 20:35:36 +00:00
throw new IllegalArgumentException();
}
2013-12-10 19:36:50 +00:00
2019-07-14 20:09:26 +00:00
this.styleBuilder = styleBuilder;
2010-11-15 20:35:36 +00:00
this.cl1 = cl1;
this.cl2 = cl2;
this.type = type;
2015-07-11 09:32:49 +00:00
if (Display.isNull(label)) {
this.label = Display.NULL;
2015-04-07 18:18:37 +00:00
} else {
2016-11-04 21:39:29 +00:00
this.label = label.manageGuillemet();
2018-10-21 19:44:14 +00:00
if (VisibilityModifier.isVisibilityCharacter(label.get(0))) {
visibilityModifier = VisibilityModifier.getVisibilityModifier(label.get(0), false);
}
2013-12-10 19:36:50 +00:00
}
2010-11-15 20:35:36 +00:00
this.length = length;
this.qualifier1 = qualifier1;
this.qualifier2 = qualifier2;
this.labeldistance = labeldistance;
this.labelangle = labelangle;
2018-06-25 19:05:58 +00:00
this.setSpecificColor(specificColor);
2011-04-19 16:50:40 +00:00
if (qualifier1 != null) {
2013-12-10 19:36:50 +00:00
((ILeaf) cl1).setNearDecoration(true);
2011-04-19 16:50:40 +00:00
}
if (qualifier2 != null) {
2013-12-10 19:36:50 +00:00
((ILeaf) cl2).setNearDecoration(true);
2011-04-19 16:50:40 +00:00
}
2015-07-11 09:32:49 +00:00
// if (type.getDecor2() == LinkDecor.EXTENDS) {
// setSametail(cl1.getUid());
// }
2015-04-07 18:18:37 +00:00
}
2018-05-01 17:26:04 +00:00
// private static boolean doWeHaveToRemoveUrlAtStart(Display label) {
// if (label.size() == 0) {
// return false;
// }
// final String s = label.get(0).toString();
// if (s.matches("^\\[\\[\\S+\\]\\].+$")) {
// return true;
// }
// return false;
// }
2010-11-15 20:35:36 +00:00
public Link getInv() {
2011-08-08 17:48:29 +00:00
// if (getLength() == 1) {
// final int x = cl1.getXposition();
// cl2.setXposition(x-1);
// }
2013-12-10 19:36:50 +00:00
final Link result = new Link(cl2, cl1, getType().getInversed(), label, length, qualifier2, qualifier1,
2019-07-14 20:09:26 +00:00
labeldistance, labelangle, getSpecificColor(), styleBuilder);
2018-04-06 20:36:30 +00:00
result.inverted = !this.inverted;
2016-07-04 19:06:50 +00:00
result.port1 = this.port2;
result.port2 = this.port1;
2018-04-06 20:36:30 +00:00
result.url = this.url;
2011-08-08 17:48:29 +00:00
return result;
2011-03-20 21:40:07 +00:00
}
2018-06-25 19:05:58 +00:00
@Override
2015-04-07 18:18:37 +00:00
public void goNorank() {
setConstraint(false);
}
2010-11-15 20:35:36 +00:00
public String getLabeldistance() {
// Default in dot 1.0
return labeldistance;
}
public String getLabelangle() {
// Default in dot -25
return labelangle;
}
public String getUid() {
return uid;
}
public final boolean isInvis() {
2013-12-10 19:36:50 +00:00
if (type.isInvisible()) {
return true;
}
2010-11-15 20:35:36 +00:00
return invis;
}
public final void setInvis(boolean invis) {
this.invis = invis;
}
public boolean isBetween(IEntity cl1, IEntity cl2) {
if (cl1.equals(this.cl1) && cl2.equals(this.cl2)) {
return true;
}
if (cl1.equals(this.cl2) && cl2.equals(this.cl1)) {
return true;
}
return false;
}
@Override
public String toString() {
2015-04-07 18:18:37 +00:00
return super.toString() + " {" + length + "} " + cl1 + "-->" + cl2;
2010-11-15 20:35:36 +00:00
}
public IEntity getEntity1() {
return cl1;
}
public IEntity getEntity2() {
return cl2;
}
2016-07-04 19:06:50 +00:00
public EntityPort getEntityPort1(Bibliotekon bibliotekon) {
return new EntityPort(bibliotekon.getShapeUid((ILeaf) cl1), port1);
}
public EntityPort getEntityPort2(Bibliotekon bibliotekon) {
return new EntityPort(bibliotekon.getShapeUid((ILeaf) cl2), port2);
}
2018-06-25 19:05:58 +00:00
@Override
2010-11-15 20:35:36 +00:00
public LinkType getType() {
2011-09-08 10:42:27 +00:00
if (opale) {
return new LinkType(LinkDecor.NONE, LinkDecor.NONE);
}
2015-04-07 18:18:37 +00:00
if (getSametail() != null) {
return new LinkType(LinkDecor.NONE, LinkDecor.NONE);
}
LinkType result = type;
if (OptionFlags.USE_INTERFACE_EYE1) {
if (isLollipopInterfaceEye(cl1)) {
type = type.withLollipopInterfaceEye1();
}
if (isLollipopInterfaceEye(cl2)) {
type = type.withLollipopInterfaceEye2();
}
}
return result;
}
2016-01-30 12:20:07 +00:00
private boolean isReallyGroup(IEntity ent) {
if (ent.isGroup() == false) {
return false;
}
final IGroup group = (IGroup) ent;
return group.getChildren().size() + group.getLeafsDirect().size() > 0;
}
public LinkType getTypePatchCluster() {
LinkType result = getType();
if (isReallyGroup(getEntity1())) {
result = result.withoutDecors2();
}
if (isReallyGroup(getEntity2())) {
result = result.withoutDecors1();
}
return result;
}
2015-04-07 18:18:37 +00:00
private LinkType getTypeSpecialForPrinting() {
if (opale) {
return new LinkType(LinkDecor.NONE, LinkDecor.NONE);
}
LinkType result = type;
if (OptionFlags.USE_INTERFACE_EYE1) {
if (isLollipopInterfaceEye(cl1)) {
type = type.withLollipopInterfaceEye1();
}
if (isLollipopInterfaceEye(cl2)) {
type = type.withLollipopInterfaceEye2();
}
}
return result;
}
private boolean isLollipopInterfaceEye(IEntity ent) {
return ent.getUSymbol() instanceof USymbolInterface;
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
public Display getLabel() {
2010-11-15 20:35:36 +00:00
return label;
}
public int getLength() {
return length;
}
2011-01-09 19:00:05 +00:00
public final void setLength(int length) {
this.length = length;
}
2010-11-15 20:35:36 +00:00
public String getQualifier1() {
return qualifier1;
}
public String getQualifier2() {
return qualifier2;
}
2011-01-05 18:23:06 +00:00
public final double getWeight() {
2010-11-15 20:35:36 +00:00
return weight;
}
2011-01-05 18:23:06 +00:00
public final void setWeight(double weight) {
2010-11-15 20:35:36 +00:00
this.weight = weight;
}
2013-12-10 19:36:50 +00:00
public final Display getNote() {
2010-11-15 20:35:36 +00:00
return note;
}
2016-09-29 19:51:18 +00:00
public final NoteLinkStrategy getNoteLinkStrategy() {
return noteLinkStrategy;
}
2015-09-28 20:42:17 +00:00
public final Colors getNoteColors() {
return noteColors;
2013-12-10 19:36:50 +00:00
}
2011-08-08 17:48:29 +00:00
public final Position getNotePosition() {
return notePosition;
}
2015-09-28 20:42:17 +00:00
public final void addNote(Display note, Position position, Colors colors) {
2010-11-15 20:35:36 +00:00
this.note = note;
2011-08-08 17:48:29 +00:00
this.notePosition = position;
2015-09-28 20:42:17 +00:00
this.noteColors = colors;
2016-09-29 19:51:18 +00:00
this.noteLinkStrategy = NoteLinkStrategy.NORMAL;
2011-08-08 17:48:29 +00:00
}
2016-09-29 19:51:18 +00:00
public final void addNoteFrom(Link other, NoteLinkStrategy strategy) {
this.note = other.note;
this.notePosition = other.notePosition;
this.noteColors = other.noteColors;
this.noteLinkStrategy = strategy;
}
2010-11-15 20:35:36 +00:00
2013-12-10 19:36:50 +00:00
public boolean isAutoLinkOfAGroup() {
if (getEntity1().isGroup() == false) {
2010-11-15 20:35:36 +00:00
return false;
}
2013-12-10 19:36:50 +00:00
if (getEntity2().isGroup() == false) {
2010-11-15 20:35:36 +00:00
return false;
}
2013-12-10 19:36:50 +00:00
if (getEntity1() == getEntity2()) {
2010-11-15 20:35:36 +00:00
return true;
}
return false;
}
2013-12-10 19:36:50 +00:00
public boolean containsType(LeafType type) {
2017-04-05 17:37:42 +00:00
if (getEntity1().getLeafType() == type || getEntity2().getLeafType() == type) {
2010-11-15 20:35:36 +00:00
return true;
}
return false;
}
2013-12-10 19:36:50 +00:00
public boolean contains(IEntity entity) {
if (getEntity1() == entity || getEntity2() == entity) {
2010-11-15 20:35:36 +00:00
return true;
}
return false;
}
2013-12-10 19:36:50 +00:00
public IEntity getOther(IEntity entity) {
if (getEntity1() == entity) {
return getEntity2();
2010-11-15 20:35:36 +00:00
}
2013-12-10 19:36:50 +00:00
if (getEntity2() == entity) {
return getEntity1();
}
throw new IllegalArgumentException();
2010-11-15 20:35:36 +00:00
}
2015-04-07 18:18:37 +00:00
public double getMarginDecors1(StringBounder stringBounder, UFont fontQualif, ISkinSimple spriteContainer) {
2013-12-10 19:36:50 +00:00
final double q = getQualifierMargin(stringBounder, fontQualif, qualifier1, spriteContainer);
2011-09-08 10:42:27 +00:00
final LinkDecor decor = getType().getDecor1();
2013-12-10 19:36:50 +00:00
return decor.getMargin() + q;
2010-11-25 21:12:39 +00:00
}
2015-04-07 18:18:37 +00:00
public double getMarginDecors2(StringBounder stringBounder, UFont fontQualif, ISkinSimple spriteContainer) {
2013-12-10 19:36:50 +00:00
final double q = getQualifierMargin(stringBounder, fontQualif, qualifier2, spriteContainer);
2011-09-08 10:42:27 +00:00
final LinkDecor decor = getType().getDecor2();
2013-12-10 19:36:50 +00:00
return decor.getMargin() + q;
2010-11-25 21:12:39 +00:00
}
2013-12-10 19:36:50 +00:00
private double getQualifierMargin(StringBounder stringBounder, UFont fontQualif, String qualif,
2015-04-07 18:18:37 +00:00
ISkinSimple spriteContainer) {
2010-11-25 21:12:39 +00:00
if (qualif != null) {
2015-09-28 20:42:17 +00:00
final TextBlock b = Display.create(qualif).create(FontConfiguration.blackBlueTrue(fontQualif),
2015-07-11 09:32:49 +00:00
HorizontalAlignment.LEFT, spriteContainer);
2010-11-25 21:12:39 +00:00
final Dimension2D dim = b.calculateDimension(stringBounder);
return Math.max(dim.getWidth(), dim.getHeight());
}
return 0;
}
2011-04-19 16:50:40 +00:00
public final boolean isConstraint() {
return constraint;
}
public final void setConstraint(boolean constraint) {
this.constraint = constraint;
}
2011-09-08 10:42:27 +00:00
public void setOpale(boolean opale) {
this.opale = opale;
}
2013-12-10 19:36:50 +00:00
public final void setHorizontalSolitary(boolean horizontalSolitary) {
this.horizontalSolitary = horizontalSolitary;
}
public final boolean isHorizontalSolitary() {
return horizontalSolitary;
}
public final LinkArrow getLinkArrow() {
if (inverted) {
return linkArrow.reverse();
}
return linkArrow;
}
public final void setLinkArrow(LinkArrow linkArrow) {
this.linkArrow = linkArrow;
}
public final boolean isInverted() {
return inverted;
}
public boolean hasEntryPoint() {
return (getEntity1().isGroup() == false && ((ILeaf) getEntity1()).getEntityPosition() != EntityPosition.NORMAL)
|| (getEntity2().isGroup() == false && ((ILeaf) getEntity2()).getEntityPosition() != EntityPosition.NORMAL);
}
public boolean hasTwoEntryPointsSameContainer() {
return getEntity1().isGroup() == false && getEntity2().isGroup() == false
&& ((ILeaf) getEntity1()).getEntityPosition() != EntityPosition.NORMAL
&& ((ILeaf) getEntity2()).getEntityPosition() != EntityPosition.NORMAL
&& getEntity1().getParentContainer() == getEntity2().getParentContainer();
}
public Url getUrl() {
return url;
}
public void setUrl(Url url) {
this.url = url;
}
public boolean isHidden() {
return hidden || cl1.isHidden() || cl2.isHidden();
}
public boolean sameConnections(Link other) {
if (this.cl1 == other.cl1 && this.cl2 == other.cl2) {
return true;
2011-09-08 10:42:27 +00:00
}
2013-12-10 19:36:50 +00:00
if (this.cl1 == other.cl2 && this.cl2 == other.cl1) {
return true;
}
return false;
2011-09-08 10:42:27 +00:00
}
2015-04-07 18:18:37 +00:00
public boolean doesTouch(Link other) {
if (this.cl1 == other.cl1) {
return true;
}
if (this.cl1 == other.cl2) {
return true;
}
if (this.cl2 == other.cl1) {
return true;
}
if (this.cl2 == other.cl2) {
return true;
}
return false;
}
public boolean isAutolink() {
return cl1 == cl2;
}
2013-12-10 19:36:50 +00:00
public boolean isRemoved() {
return cl1.isRemoved() || cl2.isRemoved();
}
public boolean hasUrl() {
2015-07-11 09:32:49 +00:00
if (Display.isNull(label) == false && label.hasUrl()) {
2013-12-10 19:36:50 +00:00
return true;
}
return getUrl() != null;
}
2015-04-07 18:18:37 +00:00
public String getSametail() {
return sametail;
}
public void setSametail(String sametail) {
this.sametail = sametail;
}
2013-12-10 19:36:50 +00:00
2016-07-04 19:06:50 +00:00
public void setPortMembers(String port1, String port2) {
this.port1 = port1;
this.port2 = port2;
if (port1 != null) {
2016-12-14 21:01:03 +00:00
((ILeaf) cl1).addPortShortName(port1);
2016-07-04 19:06:50 +00:00
}
if (port2 != null) {
2016-12-14 21:01:03 +00:00
((ILeaf) cl2).addPortShortName(port2);
2016-07-04 19:06:50 +00:00
}
}
2018-10-21 19:44:14 +00:00
public final VisibilityModifier getVisibilityModifier() {
return visibilityModifier;
}
2019-06-26 19:24:49 +00:00
private UmlDiagramType umlType;
public void setUmlDiagramType(UmlDiagramType type) {
this.umlType = type;
}
public UmlDiagramType getUmlDiagramType() {
return umlType;
}
2010-11-15 20:35:36 +00:00
}