Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show multiple relations in Graph view #125

Merged
merged 1 commit into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package com.neueda.jetbrains.plugin.graphdb.visualization.renderers;

import com.neueda.jetbrains.plugin.graphdb.platform.ShouldNeverHappenException;
import com.neueda.jetbrains.plugin.graphdb.visualization.util.IntersectionUtil;
import com.neueda.jetbrains.plugin.graphdb.visualization.util.RenderingUtil;
import prefuse.Constants;
import prefuse.render.EdgeRenderer;
import prefuse.visual.EdgeItem;
import prefuse.visual.VisualItem;
import prefuse.visual.tuple.TableNodeItem;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.util.List;
import java.util.Optional;
import java.util.Spliterator;

import static com.neueda.jetbrains.plugin.graphdb.visualization.constants.VisualizationParameters.EDGE_THICKNESS;
import static com.neueda.jetbrains.plugin.graphdb.visualization.constants.VisualizationParameters.NODE_DIAMETER;
import static java.lang.Math.*;
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;

public class CustomEdgeRenderer extends EdgeRenderer {

private static final double RADIUS = (NODE_DIAMETER + EDGE_THICKNESS) / 2;
private static final double RIGHT_ANGLE_IN_RADS = 1.5708;

public CustomEdgeRenderer(int edgeTypeLine) {
super(edgeTypeLine);
Expand All @@ -30,59 +36,89 @@ protected Shape getRawShape(VisualItem item) {
VisualItem item1 = edge.getSourceItem();
VisualItem item2 = edge.getTargetItem();

getAlignedPoint(m_tmpPoints[0], item1.getBounds(), m_xAlign1, m_yAlign1);
getAlignedPoint(m_tmpPoints[1], item2.getBounds(), m_xAlign2, m_yAlign2);
m_curWidth = (float) (m_width * getLineWidth(item));
boolean isDirected = m_edgeArrow != Constants.EDGE_ARROW_NONE;
boolean isLoopNode = item1 == item2;
double shift = getRelationShift(edge, item1, item2);

// TODO decide on the angle here for loop arrow
double angle = 0.261799 * 3;
double x1 = item1.getBounds().getCenterX();
double y1 = item1.getBounds().getCenterY();
double x2 = item2.getBounds().getCenterX();
double y2 = item2.getBounds().getCenterY();

boolean isLoopNode = item1 == item2;
if (!isLoopNode && edge.isDirected() && m_edgeArrow != Constants.EDGE_ARROW_NONE) {
boolean forward = (m_edgeArrow == Constants.EDGE_ARROW_FORWARD);
Point2D start = m_tmpPoints[forward ? 0 : 1];
Point2D end = m_tmpPoints[forward ? 1 : 0];
if (isLoopNode) {
double angle = 0.261799 * 3 * shift;

VisualItem dest = forward ? edge.getTargetItem() : edge.getSourceItem();
Point2D center = new Point2D.Double(dest.getBounds().getCenterX(), dest.getBounds().getCenterY());
List<Point2D> intersections = IntersectionUtil.getCircleLineIntersectionPoint(start, end, center, dest.getBounds().getWidth() / 2);
getAlignedPoint(m_tmpPoints[0], item1.getBounds(), m_xAlign1, m_yAlign1);
getAlignedPoint(m_tmpPoints[1], item2.getBounds(), m_xAlign2, m_yAlign2);
m_curWidth = (float) (m_width * getLineWidth(item));

if (intersections.size() == 0) {
throw new ShouldNeverHappenException("Andrew Naydyonock", "edge always intersect a node");
if (isDirected) {
double x = m_tmpPoints[0].getX();
double y = m_tmpPoints[0].getY();

Point.Double[] arrowPoints = RenderingUtil.arrow(angle, RADIUS, x, y, m_arrowHeight + 8);
AffineTransform at = getArrowTrans(arrowPoints[0], arrowPoints[1], m_curWidth);
m_curArrow = at.createTransformedShape(m_arrowHead);
}

end = intersections.get(0);
return RenderingUtil.loopArrow(angle, RADIUS, x1, y1, m_arrowHeight);
} else {
double angle = RIGHT_ANGLE_IN_RADS - atan2(y2 - y1, x2 - x1);

AffineTransform at = getArrowTrans(start, end, m_curWidth);
m_curArrow = at.createTransformedShape(m_arrowHead);
double shiftInRad = toRadians(shift * 20);

Point2D lineEnd = m_tmpPoints[forward ? 1 : 0];
lineEnd.setLocation(0, -m_arrowHeight);
at.transform(lineEnd, lineEnd);
} else if (isLoopNode && edge.isDirected() && m_edgeArrow != Constants.EDGE_ARROW_NONE) {
double x = m_tmpPoints[0].getX();
double y = m_tmpPoints[0].getY();
double lineX1 = x1 + RADIUS * sin(angle + shiftInRad);
double lineY1 = y1 + RADIUS * cos(angle + shiftInRad);

Point.Double[] arrowPoints = RenderingUtil.arrow(angle, RADIUS, x, y, m_arrowHeight + 8);
AffineTransform at = getArrowTrans(arrowPoints[0], arrowPoints[1], m_curWidth);
m_curArrow = at.createTransformedShape(m_arrowHead);
} else {
m_curArrow = null;
}
double arrowX = x2 - RADIUS * sin(angle - shiftInRad);
double arrowY = y2 - RADIUS * cos(angle - shiftInRad);

Shape shape;
double n1x = m_tmpPoints[0].getX();
double n1y = m_tmpPoints[0].getY();
double n2x = m_tmpPoints[1].getX();
double n2y = m_tmpPoints[1].getY();
if (isLoopNode) {
shape = RenderingUtil.loopArrow(angle, RADIUS, n1x, n1y, m_arrowHeight);
} else {
m_line.setLine(n1x, n1y, n2x, n2y);
shape = m_line;
if (isDirected) {
AffineTransform at = getArrowTrans(new Point2D.Double(lineX1, lineY1), new Point2D.Double(arrowX, arrowY), m_curWidth);
m_curArrow = at.createTransformedShape(m_arrowHead);

double lineX2 = arrowX - m_arrowWidth * sin(angle);
double lineY2 = arrowY - m_arrowWidth * cos(angle);

m_line.setLine(lineX1, lineY1, lineX2, lineY2);
} else {
m_line.setLine(lineX1, lineY1, arrowX, arrowY);
}

return m_line;
}
}

return shape;
private double getRelationShift(EdgeItem edge, VisualItem node1, VisualItem node2) {
boolean reverse = node1.getRow() > node2.getRow();
VisualItem node = reverse ? node1 : node2;

return cast(node, TableNodeItem.class)
.map(n -> {
Spliterator<?> iterator = spliteratorUnknownSize(n.edges(), 0);
List<EdgeItem> edges = stream(iterator, false)
.map(e -> cast(e, EdgeItem.class))
.filter(Optional::isPresent)
.map(Optional::get)
.filter(e ->
(e.getSourceItem() == node1 && e.getTargetItem() == node2) ||
(e.getSourceItem() == node2 && e.getTargetItem() == node1)
)
.collect(toList());

if (edges.size() > 1) {
int relNumber = edges.indexOf(edge);
double relPos = relNumber - edges.size() / 2;
if (edges.size() % 2 == 0) {
relPos = relPos + 0.5;
}

return reverse ? relPos : relPos * -1;
} else {
return 0d;
}
})
.orElse(0d);
}

@Override
Expand All @@ -97,4 +133,13 @@ public boolean locatePoint(Point2D p, VisualItem item) {
|| (m_curArrow != null && m_curArrow.contains(p.getX(), p.getY()));
}
}

@SuppressWarnings("unchecked")
private <T> Optional<T> cast(Object o, Class<T> clazz) {
if (clazz.isInstance(o)) {
return Optional.of((T) o);
} else {
return Optional.empty();
}
}
}
Loading