Skip to content

Commit

Permalink
introduce Kotlin (rewrite Colors class with Kotlin)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chentai-Kao committed May 8, 2019
1 parent d4c1101 commit 423628b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 70 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.4.1'
id "org.jetbrains.kotlin.jvm" version "1.3.31"
}

group 'com.jetbrains'
Expand All @@ -21,6 +22,7 @@ dependencies {

apply plugin: 'idea'
apply plugin: 'org.jetbrains.intellij'
apply plugin: 'kotlin'

intellij {
version '2019.1' // Intellij version to build against
Expand Down
73 changes: 44 additions & 29 deletions src/main/java/Canvas.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import callgraph.Colors;
import com.google.common.collect.ImmutableMap;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
Expand Down Expand Up @@ -34,11 +35,23 @@ class Canvas extends JPanel {
private final float regularLineWidth = 1.0f;
private final Stroke solidLineStroke = new BasicStroke(regularLineWidth);
private final Map<String, Color> methodAccessColorMap = ImmutableMap.<String, Color>builder()
.put(PsiModifier.PUBLIC, Colors.green)
.put(PsiModifier.PROTECTED, Colors.cyan)
.put(PsiModifier.PACKAGE_LOCAL, Colors.lightOrange)
.put(PsiModifier.PRIVATE, Colors.red)
.put(PsiModifier.PUBLIC, Colors.GREEN.getColor())
.put(PsiModifier.PROTECTED, Colors.CYAN.getColor())
.put(PsiModifier.PACKAGE_LOCAL, Colors.LIGHT_ORANGE.getColor())
.put(PsiModifier.PRIVATE, Colors.RED.getColor())
.build();
private List<Color> heatMapColors = Arrays.asList(
Colors.DEEP_BLUE.getColor(),
Colors.BLUE.getColor(),
Colors.LIGHT_BLUE.getColor(),
Colors.CYAN.getColor(),
Colors.GREEN.getColor(),
Colors.LIGHT_GREEN.getColor(),
Colors.YELLOW.getColor(),
Colors.LIGHT_ORANGE.getColor(),
Colors.ORANGE.getColor(),
Colors.RED.getColor()
);

Canvas(@NotNull CallGraphToolWindow callGraphToolWindow, @NotNull Graph graph) {
super();
Expand All @@ -58,7 +71,7 @@ public void paintComponent(Graphics graphics) {
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

// fill the background for entire canvas
graphics2D.setColor(Colors.backgroundColor);
graphics2D.setColor(Colors.BACKGROUND_COLOR.getColor());
graphics2D.fillRect(0, 0, this.getWidth(), this.getHeight());

// draw un-highlighted and highlighted self loops
Expand All @@ -70,7 +83,7 @@ public void paintComponent(Graphics graphics) {
this.visibleEdges.stream()
.filter(edge -> edge.getSourceNode() != edge.getTargetNode() &&
!isNodeHighlighted(edge.getSourceNode()) && !isNodeHighlighted(edge.getTargetNode()))
.forEach(edge -> drawNonLoopEdge(graphics2D, edge, Colors.unHighlightedColor));
.forEach(edge -> drawNonLoopEdge(graphics2D, edge, Colors.UN_HIGHLIGHTED_COLOR.getColor()));

// draw upstream/downstream edges
Set<Node> highlightedNodes = this.visibleNodes.stream()
Expand All @@ -82,8 +95,8 @@ public void paintComponent(Graphics graphics) {
Set<Edge> downstreamEdges = highlightedNodes.stream()
.flatMap(node -> node.getOutEdges().values().stream())
.collect(Collectors.toSet());
upstreamEdges.forEach(edge -> drawNonLoopEdge(graphics2D, edge, Colors.upstreamColor));
downstreamEdges.forEach(edge -> drawNonLoopEdge(graphics2D, edge, Colors.downstreamColor));
upstreamEdges.forEach(edge -> drawNonLoopEdge(graphics2D, edge, Colors.UPSTREAM_COLOR.getColor()));
downstreamEdges.forEach(edge -> drawNonLoopEdge(graphics2D, edge, Colors.UPSTREAM_COLOR.getColor()));

// draw un-highlighted labels
Set<Node> upstreamNodes = upstreamEdges.stream().map(Edge::getSourceNode).collect(Collectors.toSet());
Expand All @@ -93,26 +106,26 @@ public void paintComponent(Graphics graphics) {
!isNodeHighlighted(node) && !upstreamNodes.contains(node) && !downstreamNodes.contains(node)
)
.collect(Collectors.toSet());
unHighlightedNodes.forEach(node -> drawNodeLabels(graphics2D, node, Colors.neutralColor, false));
unHighlightedNodes.forEach(node -> drawNodeLabels(graphics2D, node, Colors.NEUTRAL_COLOR.getColor(), false));

// draw un-highlighted nodes (upstream/downstream nodes are excluded)
this.nodeShapesMap = new HashMap<>();
unHighlightedNodes.stream()
.filter(node -> !upstreamNodes.contains(node) && !downstreamNodes.contains(node))
.forEach(node -> drawNode(graphics2D, node, Colors.unHighlightedColor));
.forEach(node -> drawNode(graphics2D, node, Colors.UN_HIGHLIGHTED_COLOR.getColor()));

// draw upstream/downstream label and nodes
upstreamNodes.forEach(node -> drawNodeLabels(graphics2D, node, Colors.upstreamColor, false));
downstreamNodes.forEach(node -> drawNodeLabels(graphics2D, node, Colors.downstreamColor, false));
upstreamNodes.forEach(node -> drawNode(graphics2D, node, Colors.upstreamColor));
downstreamNodes.forEach(node -> drawNode(graphics2D, node, Colors.downstreamColor));
upstreamNodes.forEach(node -> drawNodeLabels(graphics2D, node, Colors.UPSTREAM_COLOR.getColor(), false));
downstreamNodes.forEach(node -> drawNodeLabels(graphics2D, node, Colors.DOWNSTREAM_COLOR.getColor(), false));
upstreamNodes.forEach(node -> drawNode(graphics2D, node, Colors.UPSTREAM_COLOR.getColor()));
downstreamNodes.forEach(node -> drawNode(graphics2D, node, Colors.DOWNSTREAM_COLOR.getColor()));

// draw highlighted node and label
this.visibleNodes.stream()
.filter(this::isNodeHighlighted)
.forEach(node -> {
drawNode(graphics2D, node, Colors.highlightedColor);
drawNodeLabels(graphics2D, node, Colors.highlightedColor, true);
drawNode(graphics2D, node, Colors.HIGHLIGHTED_COLOR.getColor());
drawNodeLabels(graphics2D, node, Colors.HIGHLIGHTED_COLOR.getColor(), true);
});
}

Expand Down Expand Up @@ -265,15 +278,15 @@ private Color getNodeBackgroundColor(@NotNull Node node) {
.filter(psiModifierList::hasModifierProperty)
.findFirst()
.map(this.methodAccessColorMap::get)
.orElse(Colors.backgroundColor);
.orElse(Colors.BACKGROUND_COLOR.getColor());
} else if (this.callGraphToolWindow.isNodeColorByClassName()) {
PsiClass psiClass = node.getMethod().getContainingClass();
if (psiClass != null) {
int hashIndex = psiClass.hashCode() % Colors.heatMapColors.size();
return Colors.heatMapColors.get(hashIndex);
int hashIndex = psiClass.hashCode() % this.heatMapColors.size();
return this.heatMapColors.get(hashIndex);
}
}
return Colors.backgroundColor;
return Colors.BACKGROUND_COLOR.getColor();
}

@NotNull
Expand All @@ -289,12 +302,12 @@ private List<AbstractMap.SimpleEntry<String, Color>> createNodeLabels(
// package name
if (this.callGraphToolWindow.isRenderFunctionPackageName(isNodeHovered)) {
String packageName = Utils.getMethodPackageName(node.getMethod());
labels.add(new AbstractMap.SimpleEntry<>(packageName, Colors.unHighlightedColor));
labels.add(new AbstractMap.SimpleEntry<>(packageName, Colors.UN_HIGHLIGHTED_COLOR.getColor()));
}
// file path
if (this.callGraphToolWindow.isRenderFunctionFilePath(isNodeHovered)) {
String filePath = Utils.getMethodFilePath(node.getMethod());
labels.add(new AbstractMap.SimpleEntry<>(filePath, Colors.unHighlightedColor));
labels.add(new AbstractMap.SimpleEntry<>(filePath, Colors.UN_HIGHLIGHTED_COLOR.getColor()));
}
return labels;
}
Expand Down Expand Up @@ -334,7 +347,7 @@ private void drawNodeLabels(
(float) boundingBoxLowerLeft.getY()
);
Color textBackgroundColor = this.callGraphToolWindow.isQueried(node.getMethod().getName()) ?
Colors.highlightedBackgroundColor : Colors.backgroundColor;
Colors.HIGHLIGHTED_BACKGROUND_COLOR.getColor() : Colors.BACKGROUND_COLOR.getColor();
graphics2D.setColor(textBackgroundColor);
graphics2D.fillRect(
(int) boundingBoxUpperLeft.getX() + 1,
Expand All @@ -344,10 +357,10 @@ private void drawNodeLabels(
);
// draw border if the node is hovered
if (isNodeHovered) {
drawLine(graphics2D, boundingBoxLowerLeft, boundingBoxUpperLeft, Colors.unHighlightedColor);
drawLine(graphics2D, boundingBoxUpperLeft, boundingBoxUpperRight, Colors.unHighlightedColor);
drawLine(graphics2D, boundingBoxUpperRight, boundingBoxLowerRight, Colors.unHighlightedColor);
drawLine(graphics2D, boundingBoxLowerRight, boundingBoxLowerLeft, Colors.unHighlightedColor);
drawLine(graphics2D, boundingBoxLowerLeft, boundingBoxUpperLeft, Colors.UN_HIGHLIGHTED_COLOR.getColor());
drawLine(graphics2D, boundingBoxUpperLeft, boundingBoxUpperRight, Colors.UN_HIGHLIGHTED_COLOR.getColor());
drawLine(graphics2D, boundingBoxUpperRight, boundingBoxLowerRight, Colors.UN_HIGHLIGHTED_COLOR.getColor());
drawLine(graphics2D, boundingBoxLowerRight, boundingBoxLowerLeft, Colors.UN_HIGHLIGHTED_COLOR.getColor());
}
// draw text
IntStream.range(0, labels.size())
Expand Down Expand Up @@ -443,8 +456,10 @@ private void drawSelfLoop(@NotNull Graphics2D graphics2D, @NotNull Point2D nodeC
);
Shape strokedUpstreamHalfShape = this.solidLineStroke.createStrokedShape(upstreamHalfArc);
Shape strokedDownstreamHalfShape = this.solidLineStroke.createStrokedShape(downstreamHalfArc);
Color upstreamHalfLoopColor = isHighlighted ? Colors.upstreamColor : Colors.unHighlightedColor;
Color downstreamHalfLoopColor = isHighlighted ? Colors.downstreamColor : Colors.unHighlightedColor;
Color upstreamHalfLoopColor = isHighlighted ?
Colors.UPSTREAM_COLOR.getColor() : Colors.UN_HIGHLIGHTED_COLOR.getColor();
Color downstreamHalfLoopColor = isHighlighted ?
Colors.DOWNSTREAM_COLOR.getColor() : Colors.UN_HIGHLIGHTED_COLOR.getColor();
graphics2D.setColor(upstreamHalfLoopColor);
graphics2D.draw(strokedUpstreamHalfShape);
graphics2D.setColor(downstreamHalfLoopColor);
Expand Down
41 changes: 0 additions & 41 deletions src/main/java/Colors.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/kotlin/callgraph/Colors.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package callgraph

import com.intellij.ui.JBColor
import java.awt.Color

enum class Colors(val color: JBColor) {
BACKGROUND_COLOR(JBColor(Color(0xFDFEFF), Color(0x292B2D))),
UN_HIGHLIGHTED_COLOR(JBColor(Color(0xC6C8CA), Color(0x585A5C))),
NEUTRAL_COLOR(JBColor(Color(0x626466), Color(0x949698))),
HIGHLIGHTED_COLOR(JBColor(Color(0x4285F4), Color(0x589DEF))),
HIGHLIGHTED_BACKGROUND_COLOR(JBColor(Color(0xFFFF00), Color(0xFFFF00))),
UPSTREAM_COLOR(JBColor(Color(0xFBBC05), Color(0xBE9117))),
DOWNSTREAM_COLOR(JBColor(Color(0x34A853), Color(0x538863))),

DEEP_BLUE(JBColor(Color(0x0000FF), Color(0x0000FF))),
BLUE(JBColor(Color(0x0088FF), Color(0x0088FF))),
LIGHT_BLUE(JBColor(Color(0x00FFFF), Color(0x00FFFF))),
CYAN(JBColor(Color(0x00FF88), Color(0x00FF88))),
GREEN(JBColor(Color(0x00FF00), Color(0x00FF00))),
LIGHT_GREEN(JBColor(Color(0x88FF00), Color(0x88FF00))),
YELLOW(JBColor(Color(0xFFFF00), Color(0xFFFF00))),
LIGHT_ORANGE(JBColor(Color(0xFFAA00), Color(0xFFAA00))),
ORANGE(JBColor(Color(0xFF6600), Color(0xFF6600))),
RED(JBColor(Color(0xFF0000), Color(0xFF0000)))
}

0 comments on commit 423628b

Please sign in to comment.