Skip to content

Commit

Permalink
Added pressing 'm' shows method call links
Browse files Browse the repository at this point in the history
  • Loading branch information
adamldavis committed Jun 2, 2012
1 parent 99f6dcc commit 57df736
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 6 deletions.
65 changes: 64 additions & 1 deletion src/com/adamldavis/z/Z.java
Expand Up @@ -6,6 +6,7 @@
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
Expand All @@ -26,6 +27,8 @@
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.SwingUtilities;

Expand All @@ -35,6 +38,7 @@
import com.adamldavis.swing.Swutil;
import com.adamldavis.z.SmoothAnimator.AnimationType;
import com.adamldavis.z.ZNode.ZNodeType;
import com.adamldavis.z.ZNodeLink.LinkType;
import com.adamldavis.z.api.APIFactory;
import com.adamldavis.z.api.Editor;
import com.adamldavis.z.editor.ZCodeEditor;
Expand Down Expand Up @@ -117,6 +121,8 @@ public void actionPerformed(ActionEvent e) {

private Point mouseLocation;

private final List<ZNodeLink> links = new ArrayList<ZNodeLink>();

public Z() {
display.addMouseWheelListener(new MouseWheelListener() {

Expand Down Expand Up @@ -225,6 +231,32 @@ public void mouseMoved(MouseEvent e) {
mouseLocation = e.getPoint();
}
});
display.addKeyListener(new KeyAdapter() {

@Override
public void keyReleased(KeyEvent e) {
links.clear();
}

@Override
public void keyPressed(KeyEvent e) {
if (!links.isEmpty())
return;
switch (e.getKeyChar()) {
case 'm':
if (selectedNode.getNodeType() == ZNodeType.CLASS) {
addMethodLinks();
}
case 'p':
// TODO: add polymorphic links
case 'i':
case 'r':
// TODO: add import/require links
case 'h':
// TODO: add has_a links
}
}
});
zfactory = new ZFactory(Z.class.getResourceAsStream("z.properties"));
loadSettings();
timer.schedule(new TimerTask() {
Expand Down Expand Up @@ -389,7 +421,8 @@ private ZNodePositioner makeNodePositioner() {
public ZFactory zfactory;

public void run() {
if (aniCount.incrementAndGet() >= 100) {
if ((state == State.ANIMATING && aniCount.incrementAndGet() >= 100)
|| (state == State.SELECTING && aniCount.addAndGet(2) >= 100)) {
if (state == State.ANIMATING) {
state = State.NORMAL;
} else if (state == State.SELECTING) {
Expand Down Expand Up @@ -685,4 +718,34 @@ public Point getMouseLocation() {
return mouseLocation;
}

private void addMethodLinks() {
log.info("addMethodLinks()");
Map<String, ZNode> nodeMap = new HashMap<String, ZNode>();
Pattern patt = Pattern.compile("(\\w+)\\(");

for (ZNode method : selectedNode.getSubmodules()) {
final Matcher matcher = patt.matcher(method.getName());
if (matcher.find()) {
final String name = matcher.group(1);
nodeMap.put(name, method);
}
}
log.info("nodeMap={}", nodeMap);
for (ZNode method : selectedNode.getSubmodules()) {
for (ZNode call : method.getSubmodules()) {
// TODO fix
if (nodeMap.containsKey(call.getName())) {
links.add(new ZNodeLink(method,
nodeMap.get(call.getName()), LinkType.METHOD_CALL));
}
}
}
// just add 1 so there's at least 1
links.add(new ZNodeLink(selectedNode, selectedNode, LinkType.HAS_A));
}

public List<ZNodeLink> getLinks() {
return links;
}

}
41 changes: 41 additions & 0 deletions src/com/adamldavis/z/ZNodeLink.java
@@ -0,0 +1,41 @@
/** Copyright 2012, Adam L. Davis, all rights reserved. */
package com.adamldavis.z;

/**
* Represents a link between two nodes such as a method-call.
*
* @author Adam L. Davis
*
*/
public class ZNodeLink {

public enum LinkType {
METHOD_CALL, EXTENDS, HAS_A, REQUIRES
}

private final ZNode node1;

private final ZNode node2;

private final LinkType linkType;

public ZNodeLink(ZNode node1, ZNode node2, LinkType linkType) {
super();
this.node1 = node1;
this.node2 = node2;
this.linkType = linkType;
}

public ZNode getNode1() {
return node1;
}

public ZNode getNode2() {
return node2;
}

public LinkType getLinkType() {
return linkType;
}

}
22 changes: 19 additions & 3 deletions src/com/adamldavis/z/gui/swing/ZDisplay.java
Expand Up @@ -6,9 +6,11 @@
import static java.awt.Color.WHITE;
import static java.awt.Color.YELLOW;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
Expand All @@ -27,6 +29,7 @@
import com.adamldavis.z.Z;
import com.adamldavis.z.Z.State;
import com.adamldavis.z.ZNode;
import com.adamldavis.z.ZNodeLink;

/**
* @author Adam L. Davis
Expand Down Expand Up @@ -76,7 +79,7 @@ protected void paintBuffered(Graphics2D g2d) {
z.getScale(), YELLOW.darker());

for (ZNode node : zNodes) {
if (node == z.getSelectedNode()) {
if (node == z.getSelectedNode() && z.getLinks().isEmpty()) {
selNodePainter.paint(node);
} else if (node == z.getHoveredNode()) {
hoverPainter.paint(node);
Expand All @@ -90,14 +93,27 @@ protected void paintBuffered(Graphics2D g2d) {
final int s = (int) z.getScale() * 80;
g2d.drawOval(point2.x - s / 2, point2.y - s / 2, s, s);
}
g2d.setFont(g2d.getFont().deriveFont(20f));
if (z.getHoverText() != null) {
g2d.setColor(WHITE);
g2d.setFont(g2d.getFont().deriveFont(20f));
final Point point = z.getMouseLocation();
final int length = z.getHoverText().length();
g2d.setColor(WHITE);
g2d.drawString(z.getHoverText(), point.x
- (length * 10 * point.x / width), point.y);
}
g2d.setColor(LIGHT_GRAY);
g2d.drawRect(8, 30, 40, 20);
g2d.drawString("Back", 8, 42);
g2d.setStroke(new BasicStroke(2.0f));
for (ZNodeLink link : z.getLinks()) {
g2d.setPaint(new GradientPaint(link.getNode1().getLocation(),
YELLOW.darker(), link.getNode2().getLocation(), Color.BLUE
.darker()));
g2d.drawLine((int) link.getNode1().getLocation().getX(), (int) link
.getNode1().getLocation().getY(), (int) link.getNode2()
.getLocation().getX(), (int) link.getNode2().getLocation()
.getY());
}
}

public Dimension getDimension() {
Expand Down
8 changes: 6 additions & 2 deletions src/com/adamldavis/z/java/JavaLanguageParser.java
Expand Up @@ -363,12 +363,13 @@ public Collection<ZNode> loadImports(File file) {
@Override
public void loadMethodHierarchy(ZNode node) {
// TODO make this better!
final Pattern methodPattern = Pattern.compile("(\\w+\\.\\w+)\\(");
final Pattern methodPattern = Pattern.compile("([\\.\\w]+)\\(");
final Set<String> methods = new HashSet<String>();
boolean inMethod = false;

for (String line : node.getCodeLines()) {
Matcher matcher = methodPattern.matcher(line);
if (matcher.find()) {
if (inMethod && matcher.find()) {
String name = matcher.group(1);
if (!methods.contains(name)) {
methods.add(name);
Expand All @@ -377,6 +378,9 @@ public void loadMethodHierarchy(ZNode node) {
.getParentFile()));
}
}
if (hasOpenBracket(line)) {
inMethod = true;
}
}
}

Expand Down

0 comments on commit 57df736

Please sign in to comment.