Skip to content

Commit

Permalink
Support hyperlinks, issue #24
Browse files Browse the repository at this point in the history
  • Loading branch information
hallvard committed Apr 4, 2017
1 parent 4c2cf8a commit 1db77d6
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IWorkbenchPage;

import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.FileSystem;
import net.sourceforge.plantuml.OptionFlags;
import net.sourceforge.plantuml.SourceStringReader;
Expand Down Expand Up @@ -46,6 +51,11 @@ public class Diagram {
*/
private ImageData imageData;

/**
* Layout data for diagram elements
*/
private Collection<LinkData> links = null;

/**
* Create a diagram
*
Expand Down Expand Up @@ -84,7 +94,8 @@ public ImageData getImage(IPath path) throws IOException {
}
OptionFlags.getInstance().setQuiet(true);

imageData = getImage(textDiagram, imageNumber);
links = new ArrayList<LinkData>();
imageData = getImage(textDiagram, imageNumber, links);
}
return getImageData();
}
Expand All @@ -109,10 +120,12 @@ private static void setGraphvizPath() {
* @return ImageData
*/
public static ImageData getImage(String textDiagram) {
return getImage(textDiagram, 0);
return getImage(textDiagram, 0, null);
}

private static ImageData getImage(String textDiagram, int imageNum) {
private static FileFormatOption layoutFormatOption = new FileFormatOption(FileFormat.PNG);

private static ImageData getImage(String textDiagram, int imageNum, Collection<LinkData> links) {
setGraphvizPath();

ImageData imageData = null;
Expand All @@ -122,6 +135,12 @@ private static ImageData getImage(String textDiagram, int imageNum) {
// image generation.
SourceStringReader reader = new SourceStringReader(textDiagram);
String desc = reader.generateImage(os, imageNum);
if (links != null) {
String cMapData = reader.getCMapData(0, layoutFormatOption);
if (cMapData != null) {
parseImageMapString(cMapData, links);
}
}
os.flush();
os.close();

Expand All @@ -136,6 +155,75 @@ private static ImageData getImage(String textDiagram, int imageNum) {
return imageData;
}

private static void parseImageMapString(String cMapData, Collection<LinkData> links) {
String[] areaElements = cMapData.split(Pattern.quote("<area "));
for (String areaElement : areaElements) {
// int pos = areaElement.indexOf('>');
// if (pos >= 0) {
// areaElement = areaElement.substring(0, pos);
// if (areaElement.endsWith("/")) {
// areaElement = areaElement.substring(0, areaElement.length() - 1);
// }
// }
LinkData link = new LinkData();
link.href = getAttributeValue(areaElement, "href");
link.title = getAttributeValue(areaElement, "title");
link.altText = getAttributeValue(areaElement, "alt");
String coords = getAttributeValue(areaElement, "coords");
if (coords != null) {
String[] ints = coords.split(",");
if (ints.length == 4) {
try {
int x1 = Integer.valueOf(ints[0]), y1 = Integer.valueOf(ints[1]), x2 = Integer.valueOf(ints[2]), y2 = Integer.valueOf(ints[3]);
link.rect = new Rectangle(x1, y1, x2 - x1, y2 - y1);
} catch (NumberFormatException e) {
}
}
}
links.add(link);
}
}
/*
@startuml
actor Bob [[http://plantuml.com/sequence]]
actor "This is [[http://plantuml.com/sequence Alice]] actor" as Alice
Bob -> Alice [[http://plantuml.com/start]] : hello
note left [[http://plantuml.com/start]]
a note with a link
end note
Alice -> Bob : hello with [[http://plantuml.com/start{Tooltip for message} some link]]
note right [[http://plantuml.com/start]] : another note
note left of Bob
You can use [[http://plantuml.com/start links in notes]] also.
end note
@enduml
<map id="plantuml_map" name="plantuml_map">
<area shape="rect" id="id1" href="http://plantuml.com/sequence" title="http://plantuml.com/sequence" alt="" coords="375,221,408,238"/>
<area shape="rect" id="id2" href="http://plantuml.com/sequence" title="http://plantuml.com/sequence" alt="" coords="375,68,408,85"/>
<area shape="rect" id="id3" href="http://plantuml.com/start" title="Tooltip for message" alt="" coords="321,143,383,158"/>
<area shape="rect" id="id4" href="http://plantuml.com/start" title="http://plantuml.com/start" alt="" coords="94,183,180,199"/>
<area shape="rect" id="id5" href="http://plantuml.com/start" title="http://plantuml.com/start" alt="" coords="245,104,388,126"/>
<area shape="rect" id="id6" href="http://plantuml.com/start" title="http://plantuml.com/start" alt="" coords="99,101,229,126"/>
<area shape="rect" id="id7" href="http://plantuml.com/start" title="http://plantuml.com/start" alt="" coords="239,140,496,165"/>
<area shape="rect" id="id8" href="http://plantuml.com/sequence" title="http://plantuml.com/sequence" alt="" coords="222,5,248,257"/>
</map>
*/

private static String getAttributeValue(String element, String attributeName) {
String prefix = attributeName + "=\"";
int start = element.indexOf(prefix);
if (start >= 0) {
start += prefix.length();
String suffix = "\"";
int end = element.indexOf(suffix, start);
if (end > start) {
return element.substring(start, end);
}
}
return null;
}

public String extractTextDiagram(String diagramText) {
textDiagram = diagramText;
if (textDiagram == null) {
Expand All @@ -160,6 +248,10 @@ public ImageData getImageData() {
return imageData;
}

public Collection<LinkData> getLinks() {
return links;
}

/**
* {@link String}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.sourceforge.plantuml.eclipse.utils;

import org.eclipse.swt.graphics.Rectangle;

public class LinkData {

public String href, title, altText;
public Rectangle rect;
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ protected IStatus run(IProgressMonitor monitor) {
public void run() {
if (! canvas.isDisposed()) {
canvas.loadImage(imageData);
canvas.setLinks(diagram.getLinks());
lastTextDiagram = diagram.getTextDiagram();
lastImageNumber = diagram.getImageNumber();
}
Expand Down
Loading

0 comments on commit 1db77d6

Please sign in to comment.