summaryrefslogtreecommitdiff
path: root/src/net/sourceforge/plantuml/cucadiagram/Display.java
diff options
context:
space:
mode:
authorAndrew Shadura <andrew.shadura@collabora.co.uk>2016-03-02 13:54:28 +0100
committerAndrew Shadura <andrew.shadura@collabora.co.uk>2016-03-02 13:54:28 +0100
commitc8193f238ed8e884ba793479204846db8354d1c0 (patch)
treed3a8307484a37cb81b21f29b16dd76935277846b /src/net/sourceforge/plantuml/cucadiagram/Display.java
parent26b872bb194ec7622997914bba53309a94b64d20 (diff)
Imported Upstream version 8036
Diffstat (limited to 'src/net/sourceforge/plantuml/cucadiagram/Display.java')
-rw-r--r--src/net/sourceforge/plantuml/cucadiagram/Display.java269
1 files changed, 213 insertions, 56 deletions
diff --git a/src/net/sourceforge/plantuml/cucadiagram/Display.java b/src/net/sourceforge/plantuml/cucadiagram/Display.java
index 4fe93ca..b6bac58 100644
--- a/src/net/sourceforge/plantuml/cucadiagram/Display.java
+++ b/src/net/sourceforge/plantuml/cucadiagram/Display.java
@@ -2,9 +2,9 @@
* PlantUML : a free UML diagram generator
* ========================================================================
*
- * (C) Copyright 2009-2014, Arnaud Roques
+ * (C) Copyright 2009-2017, Arnaud Roques
*
- * Project Info: http://plantuml.sourceforge.net
+ * Project Info: http://plantuml.com
*
* This file is part of PlantUML.
*
@@ -36,35 +36,74 @@ package net.sourceforge.plantuml.cucadiagram;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import net.sourceforge.plantuml.CharSequence2;
+import net.sourceforge.plantuml.CharSequence2Impl;
import net.sourceforge.plantuml.EmbededDiagram;
+import net.sourceforge.plantuml.ISkinSimple;
+import net.sourceforge.plantuml.LineLocationImpl;
+import net.sourceforge.plantuml.SpriteContainer;
+import net.sourceforge.plantuml.StringUtils;
import net.sourceforge.plantuml.Url;
import net.sourceforge.plantuml.UrlBuilder;
import net.sourceforge.plantuml.UrlBuilder.ModeUrl;
+import net.sourceforge.plantuml.creole.CreoleMode;
+import net.sourceforge.plantuml.creole.CreoleParser;
+import net.sourceforge.plantuml.creole.Sheet;
+import net.sourceforge.plantuml.creole.SheetBlock1;
+import net.sourceforge.plantuml.creole.SheetBlock2;
+import net.sourceforge.plantuml.graphic.CircledCharacter;
+import net.sourceforge.plantuml.graphic.FontConfiguration;
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
+import net.sourceforge.plantuml.graphic.HtmlColor;
+import net.sourceforge.plantuml.graphic.TextBlock;
+import net.sourceforge.plantuml.graphic.TextBlockSimple;
+import net.sourceforge.plantuml.graphic.TextBlockSpotted;
+import net.sourceforge.plantuml.graphic.TextBlockUtils;
+import net.sourceforge.plantuml.graphic.VerticalAlignment;
+import net.sourceforge.plantuml.sequencediagram.MessageNumber;
+import net.sourceforge.plantuml.ugraphic.UFont;
+import net.sourceforge.plantuml.ugraphic.UStroke;
public class Display implements Iterable<CharSequence> {
- private final List<CharSequence> display = new ArrayList<CharSequence>();
+ private final List<CharSequence> display;
private final HorizontalAlignment naturalHorizontalAlignment;
+ private final boolean isNull;
+ private final CreoleMode defaultCreoleMode;
+
+ // public void setDefaultCreoleMode(CreoleMode defaultCreoleMode) {
+ // this.defaultCreoleMode = defaultCreoleMode;
+ // }
+
+ public Display removeUrlHiddenNewLineUrl() {
+ final String full = UrlBuilder.purgeUrl(asStringWithHiddenNewLine());
+ return new Display(StringUtils.splitHiddenNewLine(full), this.naturalHorizontalAlignment, this.isNull,
+ this.defaultCreoleMode);
+ }
+
+ public final static Display NULL = new Display(null, null, true, CreoleMode.FULL);
public boolean isWhite() {
return display.size() == 0 || (display.size() == 1 && display.get(0).toString().matches("\\s*"));
}
public static Display empty() {
- return new Display((HorizontalAlignment) null);
+ return new Display((HorizontalAlignment) null, false, CreoleMode.FULL);
}
public static Display create(CharSequence... s) {
return create(Arrays.asList(s));
}
- public static Display create(List<? extends CharSequence> other) {
- return new Display(other, null);
+ public static Display create(Collection<? extends CharSequence> other) {
+ return new Display(other, null, false, CreoleMode.FULL);
}
public static Display getWithNewlines(Code s) {
@@ -73,7 +112,8 @@ public class Display implements Iterable<CharSequence> {
public static Display getWithNewlines(String s) {
if (s == null) {
- return null;
+ // Thread.dumpStack();
+ return NULL;
}
final List<String> result = new ArrayList<String>();
final StringBuilder current = new StringBuilder();
@@ -99,39 +139,48 @@ public class Display implements Iterable<CharSequence> {
current.append(c);
current.append(c2);
}
+ } else if (c == StringUtils.hiddenNewLine()) {
+ result.add(current.toString());
+ current.setLength(0);
} else {
current.append(c);
}
}
result.add(current.toString());
- return new Display(result, naturalHorizontalAlignment);
+ return new Display(result, naturalHorizontalAlignment, false, CreoleMode.FULL);
}
- private Display(Display other) {
- this(other.naturalHorizontalAlignment);
+ private Display(Display other, CreoleMode mode) {
+ this(other.naturalHorizontalAlignment, other.isNull, mode);
this.display.addAll(other.display);
}
- private Display(HorizontalAlignment naturalHorizontalAlignment) {
- this.naturalHorizontalAlignment = naturalHorizontalAlignment;
+ private Display(HorizontalAlignment naturalHorizontalAlignment, boolean isNull, CreoleMode defaultCreoleMode) {
+ this.defaultCreoleMode = defaultCreoleMode;
+ this.isNull = isNull;
+ this.display = isNull ? null : new ArrayList<CharSequence>();
+ this.naturalHorizontalAlignment = isNull ? null : naturalHorizontalAlignment;
}
- private Display(List<? extends CharSequence> other, HorizontalAlignment naturalHorizontalAlignment) {
- this(naturalHorizontalAlignment);
- this.display.addAll(manageEmbededDiagrams2(other));
+ private Display(Collection<? extends CharSequence> other, HorizontalAlignment naturalHorizontalAlignment,
+ boolean isNull, CreoleMode defaultCreoleMode) {
+ this(naturalHorizontalAlignment, isNull, defaultCreoleMode);
+ if (isNull == false) {
+ this.display.addAll(manageEmbededDiagrams2(other));
+ }
}
- private static List<CharSequence> manageEmbededDiagrams2(final List<? extends CharSequence> strings) {
+ private static List<CharSequence> manageEmbededDiagrams2(final Collection<? extends CharSequence> strings) {
final List<CharSequence> result = new ArrayList<CharSequence>();
final Iterator<? extends CharSequence> it = strings.iterator();
while (it.hasNext()) {
CharSequence s = it.next();
- if (s != null && s.toString().trim().equals("{{")) {
+ if (s != null && StringUtils.trin(s.toString()).equals("{{")) {
final List<CharSequence> other = new ArrayList<CharSequence>();
other.add("@startuml");
while (it.hasNext()) {
final CharSequence s2 = it.next();
- if (s2 != null && s2.toString().trim().equals("}}")) {
+ if (s2 != null && StringUtils.trin(s2.toString()).equals("}}")) {
break;
}
other.add(s2);
@@ -149,11 +198,32 @@ public class Display implements Iterable<CharSequence> {
for (CharSequence line : display) {
result.add("<u>" + line);
}
- return new Display(result, this.naturalHorizontalAlignment);
+ return new Display(result, this.naturalHorizontalAlignment, this.isNull, this.defaultCreoleMode);
+ }
+
+ public Display withCreoleMode(CreoleMode mode) {
+ if (isNull) {
+ throw new IllegalArgumentException();
+ }
+ return new Display(this, mode);
+ }
+
+ public String asStringWithHiddenNewLine() {
+ final StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < display.size(); i++) {
+ sb.append(display.get(i));
+ if (i < display.size() - 1) {
+ sb.append(StringUtils.hiddenNewLine());
+ }
+ }
+ return sb.toString();
}
@Override
public String toString() {
+ if (isNull) {
+ return "NULL";
+ }
return display.toString();
}
@@ -168,55 +238,27 @@ public class Display implements Iterable<CharSequence> {
}
public Display addAll(Display other) {
- final Display result = new Display(this);
+ final Display result = new Display(this, this.defaultCreoleMode);
result.display.addAll(other.display);
return result;
}
public Display addFirst(CharSequence s) {
- final Display result = new Display(this);
+ final Display result = new Display(this, this.defaultCreoleMode);
result.display.add(0, s);
return result;
}
public Display add(CharSequence s) {
- final Display result = new Display(this);
+ final Display result = new Display(this, this.defaultCreoleMode);
result.display.add(s);
return result;
}
- private boolean firstColumnRemovable() {
- boolean allEmpty = true;
- for (CharSequence s : this) {
- if (s.length() == 0) {
- continue;
- }
- allEmpty = false;
- final char c = s.charAt(0);
- if (c != ' ' && c != '\t') {
- return false;
- }
- }
- return allEmpty == false;
- }
-
- public Display removeEmptyColumns() {
- if (firstColumnRemovable() == false) {
- return this;
- }
- final Display result = new Display(this);
- do {
- for (int i = 0; i < result.size(); i++) {
- final CharSequence s = result.get(i);
- if (s.length() > 0) {
- result.display.set(i, s.toString().substring(1));
- }
- }
- } while (result.firstColumnRemovable());
- return result;
- }
-
public int size() {
+ if (isNull) {
+ return 0;
+ }
return display.size();
}
@@ -229,26 +271,37 @@ public class Display implements Iterable<CharSequence> {
}
public Display subList(int i, int size) {
- return new Display(display.subList(i, size), this.naturalHorizontalAlignment);
+ return new Display(display.subList(i, size), this.naturalHorizontalAlignment, this.isNull,
+ this.defaultCreoleMode);
}
public List<? extends CharSequence> as() {
return Collections.unmodifiableList(display);
}
+ public List<CharSequence2> as2() {
+ final List<CharSequence2> result = new ArrayList<CharSequence2>();
+ LineLocationImpl location = new LineLocationImpl("inner", null);
+ for (CharSequence cs : display) {
+ location = location.oneLineRead();
+ result.add(new CharSequence2Impl(cs, location));
+ }
+ return Collections.unmodifiableList(result);
+ }
+
public Url initUrl() {
if (this.size() == 0) {
return null;
}
final UrlBuilder urlBuilder = new UrlBuilder(null, ModeUrl.AT_START);
- return urlBuilder.getUrl(this.get(0).toString().trim());
+ return urlBuilder.getUrl(StringUtils.trin(this.get(0).toString()));
}
public Display removeUrl(Url url) {
if (url == null) {
return this;
}
- final Display result = new Display(this.naturalHorizontalAlignment);
+ final Display result = new Display(this.naturalHorizontalAlignment, this.isNull, this.defaultCreoleMode);
result.display.add(UrlBuilder.purgeUrl(this.get(0).toString()));
result.display.addAll(this.subList(1, this.size()).display);
return result;
@@ -268,4 +321,108 @@ public class Display implements Iterable<CharSequence> {
return naturalHorizontalAlignment;
}
+ public List<Display> splitMultiline(Pattern separator) {
+ final List<Display> result = new ArrayList<Display>();
+ Display pending = new Display(this.naturalHorizontalAlignment, this.isNull, this.defaultCreoleMode);
+ result.add(pending);
+ for (CharSequence line : display) {
+ final Matcher m = separator.matcher(line);
+ if (m.find()) {
+ final CharSequence s1 = line.subSequence(0, m.start());
+ pending.display.add(s1);
+ final CharSequence s2 = line.subSequence(m.end(), line.length());
+ pending = new Display(this.naturalHorizontalAlignment, this.isNull, this.defaultCreoleMode);
+ result.add(pending);
+ pending.display.add(s2);
+ } else {
+ pending.display.add(line);
+ }
+ }
+ return Collections.unmodifiableList(result);
+ }
+
+ // ------
+
+ public static boolean isNull(Display display) {
+ // if (display == null) {
+ // throw new IllegalArgumentException();
+ // }
+ return display == null || display.isNull;
+ }
+
+ public TextBlock create(FontConfiguration fontConfiguration, HorizontalAlignment horizontalAlignment,
+ ISkinSimple spriteContainer) {
+ return create(fontConfiguration, horizontalAlignment, spriteContainer, CreoleMode.FULL);
+ }
+
+ public TextBlock createWithNiceCreoleMode(FontConfiguration fontConfiguration,
+ HorizontalAlignment horizontalAlignment, ISkinSimple spriteContainer) {
+ return create(fontConfiguration, horizontalAlignment, spriteContainer, defaultCreoleMode);
+ }
+
+ public TextBlock create(FontConfiguration fontConfiguration, HorizontalAlignment horizontalAlignment,
+ ISkinSimple spriteContainer, CreoleMode modeSimpleLine) {
+ return create(fontConfiguration, horizontalAlignment, spriteContainer, 0, modeSimpleLine, null, null);
+ }
+
+ public TextBlock create(FontConfiguration fontConfiguration, HorizontalAlignment horizontalAlignment,
+ ISkinSimple spriteContainer, double maxMessageSize, CreoleMode modeSimpleLine, UFont fontForStereotype,
+ HtmlColor htmlColorForStereotype) {
+ if (getNaturalHorizontalAlignment() != null) {
+ horizontalAlignment = getNaturalHorizontalAlignment();
+ }
+ if (size() > 0) {
+ if (get(0) instanceof Stereotype) {
+ return createStereotype(fontConfiguration, horizontalAlignment, spriteContainer, 0, fontForStereotype,
+ htmlColorForStereotype);
+ }
+ if (get(size() - 1) instanceof Stereotype) {
+ return createStereotype(fontConfiguration, horizontalAlignment, spriteContainer, size() - 1,
+ fontForStereotype, htmlColorForStereotype);
+ }
+ if (get(0) instanceof MessageNumber) {
+ return createMessageNumber(fontConfiguration, horizontalAlignment, spriteContainer, maxMessageSize);
+ }
+ }
+
+ return getCreole(fontConfiguration, horizontalAlignment, spriteContainer, maxMessageSize, modeSimpleLine);
+ }
+
+ private TextBlock getCreole(FontConfiguration fontConfiguration, HorizontalAlignment horizontalAlignment,
+ ISkinSimple spriteContainer, double maxMessageSize, CreoleMode modeSimpleLine) {
+ final Sheet sheet = new CreoleParser(fontConfiguration, horizontalAlignment, spriteContainer, modeSimpleLine)
+ .createSheet(this);
+ final SheetBlock1 sheetBlock1 = new SheetBlock1(sheet, maxMessageSize, spriteContainer == null ? 0
+ : spriteContainer.getPadding());
+ return new SheetBlock2(sheetBlock1, sheetBlock1, new UStroke(1.5));
+ }
+
+ private TextBlock createMessageNumber(FontConfiguration fontConfiguration, HorizontalAlignment horizontalAlignment,
+ ISkinSimple spriteContainer, double maxMessageSize) {
+ TextBlock tb1 = subList(0, 1).getCreole(fontConfiguration, horizontalAlignment, spriteContainer,
+ maxMessageSize, CreoleMode.FULL);
+ tb1 = TextBlockUtils.withMargin(tb1, 0, 4, 0, 0);
+ final TextBlock tb2 = subList(1, size()).getCreole(fontConfiguration, horizontalAlignment, spriteContainer,
+ maxMessageSize, CreoleMode.FULL);
+ return TextBlockUtils.mergeLR(tb1, tb2, VerticalAlignment.CENTER);
+
+ }
+
+ private TextBlock createStereotype(FontConfiguration fontConfiguration, HorizontalAlignment horizontalAlignment,
+ SpriteContainer spriteContainer, int position, UFont fontForStereotype, HtmlColor htmlColorForStereotype) {
+ final Stereotype stereotype = (Stereotype) get(position);
+ if (stereotype.isSpotted()) {
+ final CircledCharacter circledCharacter = new CircledCharacter(stereotype.getCharacter(),
+ stereotype.getRadius(), stereotype.getCircledFont(), stereotype.getHtmlColor(), null,
+ fontConfiguration.getColor());
+ if (stereotype.getLabel(false) == null) {
+ return new TextBlockSpotted(circledCharacter, this.subList(1, this.size()), fontConfiguration,
+ horizontalAlignment, spriteContainer);
+ }
+ return new TextBlockSpotted(circledCharacter, this, fontConfiguration, horizontalAlignment, spriteContainer);
+ }
+ return new TextBlockSimple(this, fontConfiguration, horizontalAlignment, spriteContainer, 0, fontForStereotype,
+ htmlColorForStereotype);
+ }
+
}