|
1 | 1 | package the.bytecode.club.bytecodeviewer.gui.resourcelist; |
2 | 2 |
|
| 3 | +import javax.swing.tree.TreeNode; |
| 4 | +import javax.swing.tree.TreePath; |
| 5 | +import java.awt.*; |
3 | 6 | import java.awt.event.KeyAdapter; |
4 | 7 | import java.awt.event.KeyEvent; |
5 | 8 | import java.util.Enumeration; |
6 | | -import javax.swing.tree.TreeNode; |
7 | | -import javax.swing.tree.TreePath; |
8 | | -import the.bytecode.club.bytecodeviewer.BytecodeViewer; |
9 | 9 |
|
10 | 10 | /*************************************************************************** |
11 | 11 | * Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite * |
|
29 | 29 | * @author Konloch |
30 | 30 | * @since 6/22/2021 |
31 | 31 | */ |
32 | | -public class SearchKeyAdapter extends KeyAdapter |
33 | | -{ |
34 | | - private final ResourceListPane resourceListPane; |
35 | | - |
36 | | - public SearchKeyAdapter(ResourceListPane resourceListPane) {this.resourceListPane = resourceListPane;} |
37 | | - |
38 | | - @Override |
39 | | - public void keyPressed(final KeyEvent ke) |
40 | | - { |
41 | | - //only trigger on enter |
42 | | - if (ke.getKeyCode() != KeyEvent.VK_ENTER) |
43 | | - return; |
44 | | - |
45 | | - final String qt = resourceListPane.quickSearch.getText(); |
46 | | - resourceListPane.quickSearch.setText(""); |
47 | | - |
48 | | - if (qt.isEmpty()) //NOPE |
49 | | - return; |
50 | | - |
51 | | - String[] path; |
52 | | - int found = 0; |
53 | | - |
54 | | - if (qt.contains(".")) |
55 | | - { |
56 | | - path = qt.split("\\."); |
57 | | - } |
58 | | - else |
59 | | - { |
60 | | - path = new String[]{qt}; |
61 | | - } |
62 | | - |
63 | | - ResourceTreeNode curNode = resourceListPane.treeRoot; |
64 | | - if (resourceListPane.exact.isSelected()) |
65 | | - { |
66 | | - pathLoop: |
67 | | - for (int i = 0; i < path.length; i++) |
68 | | - { |
69 | | - final String pathName = path[i]; |
70 | | - final boolean isLast = i == path.length - 1; |
71 | | - |
72 | | - for (int c = 0; c < curNode.getChildCount(); c++) |
73 | | - { |
74 | | - final ResourceTreeNode child = (ResourceTreeNode) curNode.getChildAt(c); |
75 | | - System.out.println(pathName + ":" + child.getUserObject()); |
76 | | - |
77 | | - if (child.getUserObject().equals(pathName)) |
78 | | - { |
79 | | - curNode = child; |
80 | | - if (isLast) |
81 | | - { |
82 | | - System.out.println("Found! " + curNode); |
83 | | - found++; |
84 | | - final TreePath pathn = new TreePath(curNode.getPath()); |
85 | | - resourceListPane.tree.setSelectionPath(pathn); |
86 | | - resourceListPane.tree.makeVisible(pathn); |
87 | | - resourceListPane.tree.scrollPathToVisible(pathn); |
88 | | - resourceListPane.openPath(pathn); //auto open |
89 | | - break pathLoop; |
90 | | - } |
91 | | - continue pathLoop; |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - System.out.println("Could not find " + pathName); |
96 | | - break; |
97 | | - } |
98 | | - } |
99 | | - else |
100 | | - { |
101 | | - @SuppressWarnings("unchecked") |
102 | | - Enumeration<TreeNode> enums = curNode.depthFirstEnumeration(); |
103 | | - while (enums != null && enums.hasMoreElements()) |
104 | | - { |
105 | | - ResourceTreeNode node = (ResourceTreeNode) enums.nextElement(); |
106 | | - if (node.isLeaf()) |
107 | | - { |
108 | | - if (((String) (node.getUserObject())).toLowerCase().contains(path[path.length - 1].toLowerCase())) |
109 | | - { |
110 | | - TreeNode[] pathArray = node.getPath(); |
111 | | - int k = 0; |
112 | | - StringBuilder fullPath = new StringBuilder(); |
113 | | - while (pathArray != null |
114 | | - && k < pathArray.length) |
115 | | - { |
116 | | - ResourceTreeNode n = (ResourceTreeNode) pathArray[k]; |
117 | | - String s = (String) (n.getUserObject()); |
118 | | - fullPath.append(s); |
119 | | - if (k++ != pathArray.length - 1) |
120 | | - { |
121 | | - fullPath.append("."); |
122 | | - } |
123 | | - } |
124 | | - String fullPathString = fullPath.toString(); |
125 | | - if (fullPathString.toLowerCase().contains(qt.toLowerCase())) |
126 | | - { |
127 | | - System.out.println("Found! " + node); |
128 | | - found++; |
129 | | - if (found >= 30) |
130 | | - { //TODO probably make this a setting, no real reason it's 30 |
131 | | - BytecodeViewer.showMessage("Uh oh, there could be more results but you've" |
132 | | - + " triggered the 30 classes at once limit. Try refining your search."); |
133 | | - return; |
134 | | - } |
135 | | - final TreePath pathn = new TreePath(node.getPath()); |
136 | | - resourceListPane.tree.setSelectionPath(pathn.getParentPath()); |
137 | | - resourceListPane.tree.setSelectionPath(pathn); |
138 | | - resourceListPane.tree.makeVisible(pathn); |
139 | | - resourceListPane.tree.scrollPathToVisible(pathn); |
140 | | - } |
141 | | - } |
142 | | - } |
143 | | - } |
144 | | - } |
145 | | - } |
| 32 | +public class SearchKeyAdapter extends KeyAdapter { |
| 33 | + private final ResourceListPane resourceListPane; |
| 34 | + |
| 35 | + public SearchKeyAdapter(ResourceListPane resourceListPane) { |
| 36 | + this.resourceListPane = resourceListPane; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void keyPressed(final KeyEvent ke) { |
| 41 | + //only trigger on enter |
| 42 | + if (ke.getKeyCode() != KeyEvent.VK_ENTER) |
| 43 | + return; |
| 44 | + |
| 45 | + final String qt = resourceListPane.quickSearch.getText(); |
| 46 | + |
| 47 | + if (qt.trim().isEmpty()) //NOPE |
| 48 | + return; |
| 49 | + |
| 50 | + String[] path; |
| 51 | + if (qt.contains(".")) { |
| 52 | + path = qt.split("\\."); |
| 53 | + } else { |
| 54 | + path = new String[]{qt}; |
| 55 | + } |
| 56 | + |
| 57 | + ResourceTreeNode curNode = resourceListPane.treeRoot; |
| 58 | + boolean caseSensitive = resourceListPane.caseSensitive.isSelected(); |
| 59 | + |
| 60 | + boolean success = false; |
| 61 | + if (resourceListPane.exact.isSelected()) { |
| 62 | + pathLoop: |
| 63 | + for (int i = 0; i < path.length; i++) { |
| 64 | + final String pathName = path[i]; |
| 65 | + final boolean isLast = i == path.length - 1; |
| 66 | + |
| 67 | + for (int c = 0; c < curNode.getChildCount(); c++) { |
| 68 | + final ResourceTreeNode child = (ResourceTreeNode) curNode.getChildAt(c); |
| 69 | + Object userObject = child.getUserObject(); |
| 70 | + if (caseSensitive ? userObject.equals(pathName) : userObject.toString().equalsIgnoreCase(pathName)) { |
| 71 | + curNode = child; |
| 72 | + if (isLast) { |
| 73 | + final TreePath pathn = new TreePath(curNode.getPath()); |
| 74 | + resourceListPane.tree.setSelectionPath(pathn); |
| 75 | + resourceListPane.tree.makeVisible(pathn); |
| 76 | + resourceListPane.tree.scrollPathToVisible(pathn); |
| 77 | + resourceListPane.openPath(pathn); //auto open |
| 78 | + success = true; |
| 79 | + break pathLoop; |
| 80 | + } |
| 81 | + continue pathLoop; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + System.out.println("Could not find " + pathName); |
| 86 | + break; |
| 87 | + } |
| 88 | + } else { |
| 89 | + @SuppressWarnings("unchecked") |
| 90 | + Enumeration<TreeNode> enums = curNode.depthFirstEnumeration(); |
| 91 | + while (enums != null && enums.hasMoreElements()) { |
| 92 | + ResourceTreeNode node = (ResourceTreeNode) enums.nextElement(); |
| 93 | + if (node.isLeaf()) { |
| 94 | + String userObject = (String) (node.getUserObject()); |
| 95 | + String lastElem = path[path.length - 1]; |
| 96 | + |
| 97 | + if (caseSensitive ? userObject.contains(lastElem) : userObject.toLowerCase().contains(lastElem.toLowerCase())) { |
| 98 | + TreeNode[] pathArray = node.getPath(); |
| 99 | + int k = 0; |
| 100 | + StringBuilder fullPath = new StringBuilder(); |
| 101 | + while (pathArray != null |
| 102 | + && k < pathArray.length) { |
| 103 | + ResourceTreeNode n = (ResourceTreeNode) pathArray[k]; |
| 104 | + String s = (String) (n.getUserObject()); |
| 105 | + fullPath.append(s); |
| 106 | + if (k++ != pathArray.length - 1) { |
| 107 | + fullPath.append("."); |
| 108 | + } |
| 109 | + } |
| 110 | + String fullPathString = fullPath.toString(); |
| 111 | + |
| 112 | + if (caseSensitive ? fullPathString.contains(qt) : fullPathString.toLowerCase().contains(qt.toLowerCase())) { |
| 113 | + final TreePath pathn = new TreePath(node.getPath()); |
| 114 | + resourceListPane.tree.setSelectionPath(pathn.getParentPath()); |
| 115 | + resourceListPane.tree.setSelectionPath(pathn); |
| 116 | + resourceListPane.tree.makeVisible(pathn); |
| 117 | + resourceListPane.tree.scrollPathToVisible(pathn); |
| 118 | + success = true; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + if (!success) { |
| 128 | + Toolkit.getDefaultToolkit().beep(); |
| 129 | + } |
| 130 | + } |
146 | 131 | } |
0 commit comments