Skip to content

Commit

Permalink
Migrate to Swing for GUI. Add configuration options for UI scale and …
Browse files Browse the repository at this point in the history
…showing method names.
  • Loading branch information
FeldrinH committed Sep 12, 2023
1 parent b20ea28 commit c123676
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 85 deletions.
12 changes: 1 addition & 11 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import groovy.lang.Closure
plugins {
id("com.github.johnrengelman.shadow") version "8.1.1"
id("java")
id("org.openjfx.javafxplugin") version "0.1.0"
id("com.palantir.git-version") version "3.0.0"
}

Expand All @@ -19,22 +18,13 @@ dependencies {
implementation("net.bytebuddy:byte-buddy:1.14.4")
implementation("net.bytebuddy:byte-buddy-agent:1.14.4")
implementation("org.graphstream:gs-core:2.0")
implementation("org.graphstream:gs-ui-javafx:2.0")
implementation("org.graphstream:gs-ui-swing:2.0")
implementation("net.java.dev.jna:jna:5.12.1")
implementation("net.java.dev.jna:jna-platform:5.12.1")
implementation("org.openjfx:javafx-graphics:13.0.2:win")
implementation("org.openjfx:javafx-graphics:13.0.2:linux")
implementation("org.openjfx:javafx-graphics:13.0.2:mac")
testImplementation(platform("org.junit:junit-bom:5.9.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
}

javafx {
version = "13"
modules("javafx.controls", "javafx.fxml")
setPlatform("windows") // Dependencies are manually included for each platform. The platform is set explicitly here to ensure reproducible builds.
}

tasks.test {
useJUnitPlatform()
}
Expand Down
34 changes: 26 additions & 8 deletions src/main/java/ee/ut/dendroloj/Dendrologist.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
package ee.ut.dendroloj;

public class Dendrologist {

private static boolean awake = false;

protected static boolean showMethodNames = true;

private Dendrologist() {
}

/**
* Wakes up dendrologist with default configuration.
*/
public static void wakeUp() {
wakeUp(true);
init(1.0, true);
}

public static void wakeUp(boolean visualize) {
init(visualize);
/**
* Wakes up dendrologist with custom configuration.
*
* @param uiScale sets scaling multiplier for UI elements (text and nodes). default: 1.0
* @param showMethodNames if enabled shows method name for each method call. default: true
*/
public static void wakeUp(double uiScale, boolean showMethodNames) {
init(uiScale, showMethodNames);
}

private static void init(boolean visualize) {
private static void init(double uiScale, boolean showMethodNames) {
if (awake) return;

Dendrologist.showMethodNames = showMethodNames;

initTracing();
if (visualize)
initGraphics();
initGraphics(uiScale);

awake = true;
}

Expand All @@ -24,8 +42,8 @@ private static void initTracing() {
AgentTracer.init();
}

private static void initGraphics() {
JavaFXGUI.init();
private static void initGraphics(double uiScale) {
GraphGUI.init(uiScale);
}
}

47 changes: 47 additions & 0 deletions src/main/java/ee/ut/dendroloj/GraphGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ee.ut.dendroloj;

import org.graphstream.graph.Graph;

import org.graphstream.graph.implementations.SingleGraph;

class GraphGUI {
public static void init(double uiScale) {
System.setProperty("org.graphstream.ui", "swing");

Graph graph = new SingleGraph("DendroloJ");
graph.setAttribute("ui.quality");
graph.setAttribute("ui.antialias");
graph.setAttribute("ui.stylesheet", String.format("""
edge {
size: %fpx;
text-size: %f;
text-alignment: center;
text-background-mode: plain;
text-background-color: rgba(255, 255, 255, 180);
text-padding: %f;
text-offset: 5, 0;
}
edge .returned {
fill-color: gray;
}
node {
size: %fpx;
text-size: %f;
text-alignment: at-right;
text-background-mode: plain;
text-background-color: rgba(255, 255, 255, 180);
text-padding: %f;
text-offset: 5, 0;
}
node .error {
fill-color: #fa4c29;
}
""", Math.sqrt(uiScale), uiScale * 12, uiScale + 1, Math.sqrt(uiScale) * 10, uiScale * 12, uiScale + 1));

SimpleTreeLayout.setGraph(graph);
graph.display(false);
}
}
34 changes: 0 additions & 34 deletions src/main/java/ee/ut/dendroloj/JavaFXGUI.java

This file was deleted.

12 changes: 5 additions & 7 deletions src/main/java/ee/ut/dendroloj/SimpleTreeLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ class SimpleTreeLayout {

protected static Graph graph = null;

protected static void setGraph(Graph graph) {
public static void setGraph(Graph graph) {
SimpleTreeLayout.graph = graph;
Viewer v = graph.display();
v.disableAutoLayout();
}

private record NodeMetaWrapper(CallTreeNode node, List<NodeMetaWrapper> children, double reservedWidth) {
}

protected static void updateGraph(MetaTreeNode root) {
public static void updateGraph(MetaTreeNode root) {
root.childStream()
.map(c -> wrap((CallTreeNode) c))
.forEach(meta -> updateGraph(meta, 0, 0, Math.max(1.0, meta.reservedWidth / 20), null));
}

private record NodeMetaWrapper(CallTreeNode node, List<NodeMetaWrapper> children, double reservedWidth) {
}

private static NodeMetaWrapper wrap(CallTreeNode node) {
List<NodeMetaWrapper> children = node.childStream().map(SimpleTreeLayout::wrap).toList();
double sum = children.stream().mapToDouble(c -> c.reservedWidth).sum();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ee/ut/dendroloj/TreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Stream<CallTreeNode> childStream() {
public String argumentString() {
if (callArguments.length == 0) return "";
String args = Arrays.deepToString(callArguments);
return '(' + args.substring(1, args.length() - 1) + ')';
return (Dendrologist.showMethodNames ? signature : "") + '(' + args.substring(1, args.length() - 1) + ')';
}
}

Expand Down
23 changes: 0 additions & 23 deletions src/main/resources/graphStyle.css

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/java/Katsed.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ private static void juhuslikHargnemine(int n) {
public static void main(String[] args) {
Dendrologist.wakeUp();

fib(10);
fib(8);
}
}

0 comments on commit c123676

Please sign in to comment.