Skip to content

Commit

Permalink
Add support for capturing arguments during return and showing multipl…
Browse files Browse the repository at this point in the history
…e argument values on separate lines.
  • Loading branch information
FeldrinH committed Sep 24, 2023
1 parent 4e84790 commit caaca0b
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Soovi korral on võimalik seadistada mõningaid parameetreid, mis muudavad seda

* `Dendrologist.setUIScale(uiScale)` seab kordaja millega korrutatakse kõikide graafiliste elementide mõõtmed. See on kasulik ennekõike kui vaikesuurusega tekst on lugemiseks liiga väike.
* `Dendrologist.setShowMethodNames(showMethodNames)` lülitab sisse või välja meetodite nimede kuvamise rekursioonipuus (vaikimis on see sees). See on kasulik, et vähendada visuaalset müra, kui meetodi nimi on pikk või väljakutseid on palju.

* `Dendrologist.setArgumentCapture(duringCall, duringReturn)` võimaldab seadistada millal argumentide väärtused talletatakse. Vaikimisi talletatakse argumentide väärtused nii väljakutsel kui ka tagastusel. Väärtused tagastusel kuvatakse ainult siis kui need erinevad väärtustest väljakutsel.

### Kasutamine koos siluriga (IntelliJ IDEA)

Expand Down
23 changes: 20 additions & 3 deletions src/main/java/ee/ut/dendroloj/CallTreeLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


class CallTreeLayout {
Expand Down Expand Up @@ -115,15 +116,31 @@ private static LayoutResult updateGraph(CallTreeNode current, boolean hideNewEle
Node node = graph.getNode(nodeId);
if (node == null) {
node = graph.addNode(nodeId);
node.setAttribute("label", current.argumentString());
if (Dendrologist.captureArgsDuringCall) {
node.setAttribute("label", current.argumentString());
}
if (hideNewElements) {
node.setAttribute("ui.hide");
}
newElements.add(node);
}

if (current.hasReturned() && current.getThrown() != null) {
node.setAttribute("ui.class", "error");
if (current.hasReturned() && !node.hasAttribute("_returned")) {
node.setAttribute("_returned", true);

if (current.getThrown() != null) {
node.setAttribute("ui.class", "error");
}

if (Dendrologist.captureArgsDuringReturn) {
String oldArgumentString = (String) node.getAttribute("label");
String newArgumentString = current.argumentString();
if (oldArgumentString == null || oldArgumentString.isEmpty()) {
node.setAttribute("label", newArgumentString);
} else if (!newArgumentString.equals(oldArgumentString)) {
node.setAttribute("label", oldArgumentString + "\n" + newArgumentString);
}
}
}

if (parent != null) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/ee/ut/dendroloj/Dendrologist.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ee.ut.dendroloj;

import java.awt.*;
import java.nio.file.DirectoryNotEmptyException;

public class Dendrologist {

Expand All @@ -13,6 +14,8 @@ public class Dendrologist {

// Runtime settings
protected static boolean showMethodNames = true;
protected static boolean captureArgsDuringCall = true;
protected static boolean captureArgsDuringReturn = true;

private Dendrologist() {
}
Expand All @@ -35,6 +38,17 @@ public static void setShowMethodNames(boolean showMethodNames) {
Dendrologist.showMethodNames = showMethodNames;
}

/**
* Sets when to capture argument values.
*
* @param duringCall Capture argument values during call. Default: {@code true}
* @param duringReturn Capture argument values during return. Default: {@code true}
*/
public static void setArgumentCapture(boolean duringCall, boolean duringReturn) {
Dendrologist.captureArgsDuringCall = duringCall;
Dendrologist.captureArgsDuringReturn = duringReturn;
}

/**
* Wakes up dendrologist.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
* This file is part of GraphStream <http://graphstream-project.org>.
*
* GraphStream is a library whose purpose is to handle static or dynamic
* graph, create them from scratch, file or any source and display them.
*
* This program is free software distributed under the terms of two licenses, the
* CeCILL-C license that fits European law, and the GNU Lesser General Public
* License. You can use, modify and/ or redistribute the software under the terms
* of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
* URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is 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 Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
*/

/**
* @author Antoine Dutot <[email protected]>
* @author Guilhelm Savin <[email protected]>
* @author Hicham Brahimi <[email protected]>
*/

package org.graphstream.ui.swing.renderer.shape.swing;

import org.graphstream.ui.swing.Backend;

import java.awt.*;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Objects;

@SuppressWarnings("unused")
class SwingTextBox extends TextBox {

Font font;
Color textColor;
Color bgColor;
boolean rounded;
double padx;
double pady;

TextLayout[] text;
double width;
double height;

public SwingTextBox(Font font, Color textColor, Color bgColor, boolean rounded, double padx, double pady) {
this.font = font;
this.textColor = textColor;
this.bgColor = bgColor;
this.rounded = rounded;
this.padx = padx;
this.pady = pady;

this.text = null;
this.textData = null;
this.width = 0.0;
this.height = 0.0;
}


/**
* Changes the text and compute its bounds. This method tries to avoid recomputing bounds
* if the text does not really changed.
*/
public void setText(String text, Backend backend) {
// DENDROLOJ EDIT:
// Add support for rendering multiple lines of text.

if (text == null || text.isBlank()) {
this.textData = null;
this.text = null;
this.width = 0.0;
this.height = 0.0;
return;
}
if (Objects.equals(text, textData)) {
return;
}

// As the text is not rendered using the default affine transform, but using
// the identity transform, and as the FontRenderContext uses the current
// transform, we use a predefined default font render context initialized
// with an identity transform here.

// Note: There must be at least one line because zero lines would mean that the text was blank, which was checked before.
String[] lines = text.split("\n");
this.textData = text;
this.text = new TextLayout[lines.length];
this.width = 0.0;
this.height = 0.0;
for (int i = 0; i < lines.length; i++) {
TextLayout line = new TextLayout(lines[i], font, TextBox.defaultFontRenderContext);
this.text[i] = line;
this.width = Math.max(this.width, line.getBounds().getWidth());
this.height += line.getAscent() + line.getDescent();
}
}

@Override
public String getText() {
return textData;
}

@Override
public double getWidth() {
return width;
}

@Override
public double getHeight() {
return height;
}

@Override
public double getDescent() {
if (text != null)
return text[0].getDescent();
else
return 0;
}

@Override
public double getAscent() {
if (text != null)
return text[0].getAscent();
else
return 0;
}

@Override
public void render(Backend backend, double xLeft, double yBottom) {
if (text != null) {
Graphics2D g = backend.graphics2D();

if (bgColor != null) {
double a = getAscent();
double h = getHeight();

g.setColor(bgColor);
if (rounded) {
g.fill(new RoundRectangle2D.Double(xLeft - padx, yBottom - (a + pady), getWidth() + 1 + (padx + padx), h + (pady + pady), 6, 6));
} else {
g.fill(new Rectangle2D.Double(xLeft - padx, yBottom - (a + pady), getWidth() + 1 + (padx + padx), h + (pady + pady)));
}
}

// DENDROLOJ EDIT:
// Add support for rendering multiple lines of text.
g.setColor(textColor);
yBottom -= getAscent();
for (TextLayout line : text) {
yBottom += line.getAscent();
line.draw(g, (float) xLeft, (float) yBottom);
yBottom += line.getDescent();
}
}
}

}
23 changes: 21 additions & 2 deletions src/test/java/Katsed.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,26 @@ static void unbalancedTree(int w, int h) {
}
}

@Grow
public static int abi2(int[] a, int i, int n) {
//i - nii palju on massiivis elemente olemas
//keerukus Teeta(3^n)
if (n == 0)
return 1;
// i elementi on paigas
a[i] = 0;
int summa = abi2(a, i + 1, n - 1);
a[i] = 1;
summa += abi2(a, i + 1, n - 1);
a[i] = 2;
summa += abi2(a, i + 1, n - 1);
return summa;
}

public static void main(String[] args) {
Dendrologist.setUIScale(1.5);
Dendrologist.setShowMethodNames(false);
Dendrologist.setArgumentCapture(true, true);

Dendrologist.wakeUp();

Expand All @@ -83,7 +100,9 @@ public static void main(String[] args) {
// fib3(6);

// fib(16);
fib(8);
pööraJupid(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
// fib(8);
// pööraJupid(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});

abi2(new int[3], 0, 3);
}
}

0 comments on commit caaca0b

Please sign in to comment.