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

498 lines
12 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2015-04-07 18:18:37 +00:00
* (C) Copyright 2009-2014, Arnaud Roques
2010-11-15 20:35:36 +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-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.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* Original Author: Arnaud Roques
*
2015-04-07 18:18:37 +00:00
* Revision $Revision: 15543 $
2010-11-15 20:35:36 +00:00
*
*/
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;
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;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.graphic.HtmlColorSet;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.HtmlColorUtils;
2010-11-25 21:12:39 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
import net.sourceforge.plantuml.graphic.TextBlockUtils;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.graphic.USymbolInterface;
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
2013-12-10 19:36:50 +00:00
public class Link implements Hideable, Removeable {
2010-11-15 20:35:36 +00:00
final private IEntity cl1;
final private IEntity cl2;
2013-12-10 19:36:50 +00:00
private LinkType type;
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;
2013-12-10 19:36:50 +00:00
private HtmlColor noteColor;
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-03-20 21:40:07 +00:00
private HtmlColor specificColor;
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;
2011-03-20 21:40:07 +00:00
2013-12-10 19:36:50 +00:00
private Url url;
public Link(IEntity cl1, IEntity cl2, LinkType type, Display label, int length) {
2011-03-20 21:40:07 +00:00
this(cl1, cl2, type, label, length, null, null, null, null, null);
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,
2010-11-15 20:35:36 +00:00
String qualifier2, String labeldistance, String labelangle) {
2011-03-20 21:40:07 +00:00
this(cl1, cl2, type, label, length, qualifier1, qualifier2, labeldistance, labelangle, null);
}
2013-12-10 19:36:50 +00:00
public Link(IEntity cl1, IEntity cl2, LinkType type, Display label, int length, String qualifier1,
2011-03-20 21:40:07 +00:00
String qualifier2, String labeldistance, String labelangle, HtmlColor specificColor) {
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
2010-11-15 20:35:36 +00:00
this.cl1 = cl1;
this.cl2 = cl2;
this.type = type;
2013-12-10 19:36:50 +00:00
if (label == null) {
this.label = null;
2015-04-07 18:18:37 +00:00
} else if (doWeHaveToRemoveUrlAtStart(label)) {
2013-12-10 19:36:50 +00:00
this.url = label.initUrl();
this.label = label.removeUrl(url);
2015-04-07 18:18:37 +00:00
} else {
this.label = label;
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;
2011-03-20 21:40:07 +00:00
this.specificColor = 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-04-07 18:18:37 +00:00
// if (type.getDecor2() == LinkDecor.EXTENDS) {
// setSametail(cl1.getUid());
// }
}
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,
2011-09-08 10:42:27 +00:00
labeldistance, labelangle, specificColor);
2011-08-08 17:48:29 +00:00
result.inverted = true;
return result;
2011-03-20 21:40:07 +00:00
}
2013-12-10 19:36:50 +00:00
public void goDashed() {
type = type.getDashed();
2011-03-20 21:40:07 +00:00
}
2013-12-10 19:36:50 +00:00
public void goDotted() {
type = type.getDotted();
2011-03-20 21:40:07 +00:00
}
2013-12-10 19:36:50 +00:00
private boolean hidden = false;
public void goHidden() {
this.hidden = true;
}
2015-04-07 18:18:37 +00:00
public void goNorank() {
setConstraint(false);
}
2013-12-10 19:36:50 +00:00
public void goBold() {
type = type.getBold();
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;
}
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;
}
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;
}
2013-12-10 19:36:50 +00:00
public final HtmlColor getNoteColor() {
return noteColor;
}
2011-08-08 17:48:29 +00:00
public final Position getNotePosition() {
return notePosition;
}
2013-12-10 19:36:50 +00:00
public final void addNote(Display note, Position position, HtmlColor noteColor) {
2010-11-15 20:35:36 +00:00
this.note = note;
2011-08-08 17:48:29 +00:00
this.notePosition = position;
2013-12-10 19:36:50 +00:00
this.noteColor = noteColor;
2011-08-08 17:48:29 +00:00
}
2013-12-10 19:36:50 +00:00
public final void addNote(String n, Position position, HtmlColor noteColor) {
this.note = Display.getWithNewlines(n);
2011-08-08 17:48:29 +00:00
this.notePosition = position;
2013-12-10 19:36:50 +00:00
this.noteColor = noteColor;
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) {
if (getEntity1().getEntityType() == type || getEntity2().getEntityType() == 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-04-07 18:18:37 +00:00
final TextBlock b = TextBlockUtils.create(Display.create(qualif), new FontConfiguration(fontQualif,
HtmlColorUtils.BLACK, HtmlColorUtils.BLUE, true), 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-03-20 21:40:07 +00:00
public HtmlColor getSpecificColor() {
return specificColor;
}
public void setSpecificColor(String s) {
2015-04-07 18:18:37 +00:00
this.specificColor = HtmlColorSet.getInstance().getColorIfValid(s);
2011-03-20 21:40:07 +00:00
}
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() {
if (label != null && label.hasUrl()) {
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
2010-11-15 20:35:36 +00:00
}