-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e600348
commit e2468f4
Showing
4 changed files
with
177 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package gui.webdiff; | ||
|
||
public class TreeNodeInfo { | ||
private final String name; | ||
private final String fullPath; | ||
private int id = -1; // NonLeafNode has id = -1; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getFullPath() { | ||
return fullPath; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public TreeNodeInfo(String name, String fullPath) { | ||
this.name = name; | ||
this.fullPath = fullPath; | ||
} | ||
|
||
public TreeNodeInfo(String name, String fullPath, int id) { | ||
this.name = name; | ||
this.fullPath = fullPath; | ||
this.id = id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package gui.webdiff; | ||
|
||
import org.refactoringminer.astDiff.actions.ASTDiff; | ||
|
||
import javax.swing.tree.DefaultMutableTreeNode; | ||
import javax.swing.tree.TreeNode; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/* Created by pourya on 2024-04-16*/ | ||
public class TreeViewGenerator { | ||
private final DefaultMutableTreeNode root = new DefaultMutableTreeNode(new TreeNodeInfo("", "")); | ||
private final DefaultMutableTreeNode compressedTree = new DefaultMutableTreeNode(new TreeNodeInfo("", "")); | ||
private final List<ASTDiff> diffs; | ||
|
||
public DefaultMutableTreeNode getCompressedTree() { | ||
return compressedTree; | ||
} | ||
|
||
public TreeViewGenerator(Map<String, String> modifiedFilesName, List<ASTDiff> diffs){ | ||
this.diffs = diffs; | ||
for(Map.Entry<String, String> pair : modifiedFilesName.entrySet()) { | ||
String fileName = pair.getValue(); | ||
String[] tokens = fileName.split("/"); | ||
int counter = 1; | ||
for(String token : tokens) { | ||
String pathToNode = concatWithSlash(tokens, counter); | ||
DefaultMutableTreeNode parent = findNode(pathToNode); | ||
TreeNodeInfo parentNodeInfo = (TreeNodeInfo) parent.getUserObject(); | ||
if(!parentNodeInfo.getName().equals(token)) { | ||
TreeNodeInfo nodeInfo = new TreeNodeInfo(token, pathToNode); | ||
DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(nodeInfo); | ||
parent.add(newChild); | ||
} | ||
counter++; | ||
} | ||
} | ||
compressNode(compressedTree, root); | ||
} | ||
private static String concatWithSlash(String[] tokens, int numberOfTokensToConcat) { | ||
StringBuilder sb = new StringBuilder(); | ||
int index = 0; | ||
for(String token : tokens) { | ||
if(index < numberOfTokensToConcat) { | ||
sb.append(token); | ||
} | ||
if(index < numberOfTokensToConcat - 1) { | ||
sb.append("/"); | ||
} | ||
index++; | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private DefaultMutableTreeNode findNode(String pathToNode) { | ||
String[] tokens = pathToNode.split("/"); | ||
Enumeration<TreeNode> enumeration = root.children(); | ||
int index = 0; | ||
DefaultMutableTreeNode lastNode = null; | ||
while(enumeration.hasMoreElements() && index < tokens.length) { | ||
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)enumeration.nextElement(); | ||
TreeNodeInfo treeNodeInfo = (TreeNodeInfo) treeNode.getUserObject(); | ||
if(treeNodeInfo.getName().equals(tokens[index])) { | ||
lastNode = treeNode; | ||
index++; | ||
enumeration = treeNode.children(); | ||
} | ||
} | ||
return lastNode != null ? lastNode : root; | ||
} | ||
|
||
private void compressNode(DefaultMutableTreeNode newNode, DefaultMutableTreeNode oldNode) { | ||
Enumeration<TreeNode> enumeration = oldNode.children(); | ||
int childCount = oldNode.getChildCount(); | ||
if(childCount == 1) { | ||
while(enumeration.hasMoreElements()) { | ||
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)enumeration.nextElement(); | ||
setIdAccordingly(treeNode); | ||
TreeNodeInfo treeNodeInfo = (TreeNodeInfo) treeNode.getUserObject(); | ||
String nodeName = treeNodeInfo.getName(); | ||
if(!nodeName.endsWith(".java")) { | ||
if(oldNode.isRoot()) { | ||
TreeNodeInfo newNodeInfo = new TreeNodeInfo(nodeName, nodeName); | ||
newNode.setUserObject(newNodeInfo); | ||
} | ||
else { | ||
TreeNodeInfo newNodeInfo = (TreeNodeInfo) newNode.getUserObject(); | ||
TreeNodeInfo updatedNodeInfo = new TreeNodeInfo(newNodeInfo.getName() + "/" + nodeName, newNodeInfo.getFullPath() + "/" + nodeName); | ||
newNode.setUserObject(updatedNodeInfo); | ||
} | ||
compressNode(newNode, treeNode); | ||
} | ||
else { | ||
// this node is a leaf | ||
newNode.add(treeNode); | ||
} | ||
} | ||
} | ||
else { | ||
while(enumeration.hasMoreElements()) { | ||
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)enumeration.nextElement(); | ||
setIdAccordingly(treeNode); | ||
DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(treeNode.getUserObject()); | ||
newNode.add(newChild); | ||
compressNode(newChild, treeNode); | ||
} | ||
} | ||
} | ||
|
||
private void setIdAccordingly(DefaultMutableTreeNode treeNode) { | ||
if (!treeNode.isLeaf()) return; | ||
TreeNodeInfo nodeInfo = (TreeNodeInfo) treeNode.getUserObject(); | ||
for (int i = 0; i < diffs.size(); i++) { | ||
if (diffs.get(i).getDstPath().equals(nodeInfo.getFullPath())) { | ||
nodeInfo.setId(i); | ||
break; | ||
} | ||
} | ||
} | ||
} |