use mustache.java to make the node label generation more readable.

This commit is contained in:
millmanorama 2018-02-12 13:34:57 +01:00
parent cce4e918cd
commit eddab91e43
2 changed files with 52 additions and 23 deletions

View File

@ -0,0 +1,10 @@
<div style="font-size:{{size}}px;">
{{#pinned}}
<img height={{size}} width={{size}} src={{MARKER_PIN_URL}}>
{{/pinned}}
{{#locked}}
<img height={{size}} width={{size}} src={{LOCK_URL}}>
{{/locked}}
<img height={{size}} width={{size}} src={{iconFileName}}>
{{accountName}}
</div>

View File

@ -18,6 +18,8 @@
*/
package org.sleuthkit.autopsy.communications;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
@ -27,6 +29,9 @@ import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxCellState;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
@ -51,6 +56,16 @@ final class mxGraphImpl extends mxGraph {
private static final Logger logger = Logger.getLogger(mxGraphImpl.class.getName());
private static final URL MARKER_PIN_URL = mxGraphImpl.class.getResource("/org/sleuthkit/autopsy/communications/images/marker--pin.png");
private static final URL LOCK_URL = mxGraphImpl.class.getResource("/org/sleuthkit/autopsy/communications/images/lock_large_locked.png");
/**
* mustache.java template
*/
private final static Mustache labelMustache;
static {
// String labelTemplatePath = "/org/sleuthkit/autopsy/communications/Vertex_Label_template.html";
InputStream templateStream = mxGraphImpl.class.getResourceAsStream("/org/sleuthkit/autopsy/communications/Vertex_Label_template.html");
labelMustache = new DefaultMustacheFactory().compile(new InputStreamReader(templateStream), "Vertex_Label");
}
static final private mxStylesheet mxStylesheet = new mxStylesheet();
private final HashSet<AccountDeviceInstanceKey> pinnedAccountDevices = new HashSet<>();
@ -132,24 +147,27 @@ final class mxGraphImpl extends mxGraph {
@Override
public String convertValueToString(Object cell) {
final StringWriter stringWriter = new StringWriter();
HashMap<String, Object> scopes = new HashMap<>();
Object value = getModel().getValue(cell);
if (value instanceof AccountDeviceInstanceKey) {
final AccountDeviceInstanceKey adiKey = (AccountDeviceInstanceKey) value;
final String accountName = adiKey.getAccountDeviceInstance().getAccount().getTypeSpecificID();
final double size = Math.round(Math.log(adiKey.getMessageCount()) + 5);
scopes.put("accountName", adiKey.getAccountDeviceInstance().getAccount().getTypeSpecificID());
scopes.put("size", Math.round(Math.log(adiKey.getMessageCount()) + 5));
String iconFileName = Utils.getIconFileName(adiKey.getAccountDeviceInstance().getAccount().getAccountType());
String label = "<img height=" + size + " width=" + size + " src="
+ mxGraphImpl.class.getResource("/org/sleuthkit/autopsy/communications/images/" + iconFileName)
+ ">" + accountName;
if (pinnedAccountDevices.contains(adiKey)) {
label = "<img height=" + size + " width=" + size + " src=" + MARKER_PIN_URL + ">" + label;
}
if (lockedVertices.contains((mxCell) cell)) {
label += "<img height=" + size + " width=" + size + " src=" + LOCK_URL + ">";
}
return "<div style=\"font-size:" + size + "px;\" >" + label + "</div>";
scopes.put("iconFileName", mxGraphImpl.class
.getResource("/org/sleuthkit/autopsy/communications/images/"
+ Utils.getIconFileName(adiKey.getAccountDeviceInstance().getAccount().getAccountType())));
scopes.put("pinned", pinnedAccountDevices.contains(adiKey));
scopes.put("MARKER_PIN_URL", MARKER_PIN_URL);
scopes.put("locked", lockedVertices.contains((mxCell) cell));
scopes.put("LOCK_URL", LOCK_URL);
labelMustache.execute(stringWriter, scopes);
return stringWriter.toString();
} else {
return "";
}
@ -253,6 +271,7 @@ final class mxGraphImpl extends mxGraph {
boolean isVertexLocked(mxCell vertex) {
return lockedVertices.contains(vertex);
}
private class SwingWorkerImpl extends SwingWorker<Void, Void> {