1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-07 10:50:53 +00:00
plantuml/src/net/sourceforge/plantuml/cucadiagram/Link.java

318 lines
8.2 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009, Arnaud Roques
*
* 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
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* 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
*
2011-03-20 21:40:07 +00:00
* Revision $Revision: 6152 $
2010-11-15 20:35:36 +00:00
*
*/
package net.sourceforge.plantuml.cucadiagram;
2010-11-25 21:12:39 +00:00
import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Dimension2D;
import java.util.Arrays;
2010-11-15 20:35:36 +00:00
import net.sourceforge.plantuml.UniqueSequence;
import net.sourceforge.plantuml.cucadiagram.dot.DrawFile;
2011-01-23 19:36:52 +00:00
import net.sourceforge.plantuml.graphic.FontConfiguration;
2010-11-25 21:12:39 +00:00
import net.sourceforge.plantuml.graphic.HorizontalAlignement;
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;
import net.sourceforge.plantuml.graphic.TextBlockUtils;
2010-11-15 20:35:36 +00:00
public class Link implements Imaged {
final private IEntity cl1;
final private IEntity cl2;
final private LinkType type;
final private String label;
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();
private DrawFile imageFile;
private String note;
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;
2010-11-15 20:35:36 +00:00
public Link(IEntity cl1, IEntity cl2, LinkType type, String 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
}
public Link(IEntity cl1, IEntity cl2, LinkType type, String label, int length, String qualifier1,
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);
}
public Link(IEntity cl1, IEntity cl2, LinkType type, String label, int length, String qualifier1,
String qualifier2, String labeldistance, String labelangle, HtmlColor specificColor) {
2010-11-15 20:35:36 +00:00
if (length < 1) {
throw new IllegalArgumentException();
}
if (cl1 == null || cl2 == null) {
throw new IllegalArgumentException();
}
this.cl1 = cl1;
this.cl2 = cl2;
this.type = type;
this.label = label;
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;
2010-11-15 20:35:36 +00:00
}
public Link getInv() {
2011-03-20 21:40:07 +00:00
return new Link(cl2, cl1, type.getInv(), label, length, qualifier2, qualifier1, labeldistance, labelangle,
specificColor);
}
public Link getDashed() {
return new Link(cl1, cl2, type.getDashed(), label, length, qualifier1, qualifier2, labeldistance, labelangle,
specificColor);
}
public Link getDotted() {
return new Link(cl1, cl2, type.getDotted(), label, length, qualifier1, qualifier2, labeldistance, labelangle,
specificColor);
}
public Link getBold() {
return new Link(cl1, cl2, type.getBold(), label, length, qualifier1, qualifier2, labeldistance, labelangle,
specificColor);
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() {
return invis;
}
public final void setInvis(boolean invis) {
this.invis = invis;
}
private static IEntity muteProxy(IEntity ent, Group g, IEntity proxy) {
if (ent.getParent() == g) {
return proxy;
}
return ent;
}
public Link mute(Group g, Entity proxy) {
if (cl1.getParent() == g && cl1.getType() != EntityType.GROUP && cl2.getParent() == g
&& cl2.getType() != EntityType.GROUP) {
return null;
}
final IEntity ent1 = muteProxy(cl1, g, proxy);
final IEntity ent2 = muteProxy(cl2, g, proxy);
if (this.cl1 == ent1 && this.cl2 == ent2) {
return this;
}
return new Link(ent1, ent2, type, label, length, qualifier1, qualifier2, labeldistance, labelangle);
}
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() {
return super.toString() + " " + cl1 + "-->" + cl2;
}
public IEntity getEntity1() {
return cl1;
}
public IEntity getEntity2() {
return cl2;
}
public LinkType getType() {
return type;
}
public String getLabel() {
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;
}
public final String getNote() {
return note;
}
public final void setNote(String note) {
this.note = note;
}
public DrawFile getImageFile() {
return imageFile;
}
public void setImageFile(DrawFile imageFile) {
this.imageFile = imageFile;
}
public boolean isAutolink(Group g) {
if (getEntity1() == g.getEntityCluster() && getEntity2() == g.getEntityCluster()) {
return true;
}
return false;
}
public boolean isToEdgeLink(Group g) {
if (getEntity1().getParent() != g || getEntity2().getParent() != g) {
return false;
}
assert getEntity1().getParent() == g && getEntity2().getParent() == g;
if (isAutolink(g)) {
return false;
}
if (getEntity2().getType() == EntityType.GROUP) {
assert getEntity1().getType() != EntityType.GROUP;
return true;
}
return false;
}
public boolean isFromEdgeLink(Group g) {
if (getEntity1().getParent() != g || getEntity2().getParent() != g) {
return false;
}
assert getEntity1().getParent() == g && getEntity2().getParent() == g;
if (isAutolink(g)) {
return false;
}
if (getEntity1().getType() == EntityType.GROUP) {
assert getEntity2().getType() != EntityType.GROUP;
return true;
}
return false;
}
public boolean containsType(EntityType type) {
if (getEntity1().getType() == type || getEntity2().getType() == type) {
return true;
}
return false;
}
public boolean contains(IEntity entity) {
if (getEntity1() == entity || getEntity2() == entity) {
return true;
}
return false;
}
2010-11-25 21:12:39 +00:00
public double getMarginDecors1(StringBounder stringBounder, Font fontQualif) {
final double q = getQualifierMargin(stringBounder, fontQualif, qualifier1);
final LinkDecor decor = type.getDecor1();
return decor.getSize() + q;
}
public double getMarginDecors2(StringBounder stringBounder, Font fontQualif) {
final double q = getQualifierMargin(stringBounder, fontQualif, qualifier2);
final LinkDecor decor = type.getDecor2();
return decor.getSize() + q;
}
private double getQualifierMargin(StringBounder stringBounder, Font fontQualif, String qualif) {
if (qualif != null) {
2011-01-23 19:36:52 +00:00
final TextBlock b = TextBlockUtils.create(Arrays.asList(qualif), new FontConfiguration(fontQualif,
Color.BLACK), HorizontalAlignement.LEFT);
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) {
this.specificColor = HtmlColor.getColorIfValid(s);
}
2010-11-15 20:35:36 +00:00
}