Skip to content

Commit d96aa2b

Browse files
committed
Add Looping Search For Non-Exact
1 parent 85bc27a commit d96aa2b

1 file changed

Lines changed: 136 additions & 97 deletions

File tree

src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/SearchKeyAdapter.java

Lines changed: 136 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.awt.*;
66
import java.awt.event.KeyAdapter;
77
import java.awt.event.KeyEvent;
8-
import java.util.Arrays;
98
import java.util.Enumeration;
109

1110
/***************************************************************************
@@ -30,100 +29,140 @@
3029
* @author Konloch
3130
* @since 6/22/2021
3231
*/
33-
public class SearchKeyAdapter extends KeyAdapter {
34-
private final ResourceListPane resourceListPane;
35-
36-
public SearchKeyAdapter(ResourceListPane resourceListPane) {
37-
this.resourceListPane = resourceListPane;
38-
}
39-
40-
@Override
41-
public void keyPressed(final KeyEvent ke) {
42-
//only trigger on enter
43-
if (ke.getKeyCode() != KeyEvent.VK_ENTER)
44-
return;
45-
46-
String qt = resourceListPane.quickSearch.getText();
47-
48-
if (qt.trim().isEmpty()) //NOPE
49-
return;
50-
51-
String[] path;
52-
53-
path = qt.split("[\\./]+"); // split at dot or slash
54-
qt = qt.replace('/', '.');
55-
56-
ResourceTreeNode curNode = resourceListPane.treeRoot;
57-
boolean caseSensitive = resourceListPane.caseSensitive.isSelected();
58-
59-
boolean success = false;
60-
if (resourceListPane.exact.isSelected()) {
61-
pathLoop:
62-
for (int i = 0; i < path.length; i++) {
63-
final String pathName = path[i];
64-
final boolean isLast = i == path.length - 1;
65-
66-
for (int c = 0; c < curNode.getChildCount(); c++) {
67-
final ResourceTreeNode child = (ResourceTreeNode) curNode.getChildAt(c);
68-
Object userObject = child.getUserObject();
69-
if (caseSensitive ? userObject.toString().equals(pathName) : userObject.toString().equalsIgnoreCase(pathName)) {
70-
curNode = child;
71-
if (isLast) {
72-
final TreePath pathn = new TreePath(curNode.getPath());
73-
resourceListPane.tree.setSelectionPath(pathn);
74-
resourceListPane.tree.makeVisible(pathn);
75-
resourceListPane.tree.scrollPathToVisible(pathn);
76-
resourceListPane.openPath(pathn); //auto open
77-
success = true;
78-
break pathLoop;
79-
}
80-
continue pathLoop;
81-
}
82-
}
83-
System.out.println("Could not find " + pathName);
84-
break;
85-
}
86-
} else {
87-
@SuppressWarnings("unchecked")
88-
Enumeration<TreeNode> enums = curNode.depthFirstEnumeration();
89-
while (enums != null && enums.hasMoreElements()) {
90-
ResourceTreeNode node = (ResourceTreeNode) enums.nextElement();
91-
if (node.isLeaf()) {
92-
String userObject = (String) (node.getUserObject());
93-
String lastElem = path[path.length - 1];
94-
95-
if (caseSensitive ? userObject.contains(lastElem) : userObject.toLowerCase().contains(lastElem.toLowerCase())) {
96-
TreeNode[] pathArray = node.getPath();
97-
int k = 0;
98-
StringBuilder fullPath = new StringBuilder();
99-
while (pathArray != null
100-
&& k < pathArray.length) {
101-
ResourceTreeNode n = (ResourceTreeNode) pathArray[k];
102-
String s = (String) (n.getUserObject());
103-
fullPath.append(s);
104-
if (k++ != pathArray.length - 1) {
105-
fullPath.append(".");
106-
}
107-
}
108-
String fullPathString = fullPath.toString();
109-
110-
if (caseSensitive ? fullPathString.contains(qt) : fullPathString.toLowerCase().contains(qt.toLowerCase())) {
111-
final TreePath pathn = new TreePath(node.getPath());
112-
resourceListPane.tree.setSelectionPath(pathn.getParentPath());
113-
resourceListPane.tree.setSelectionPath(pathn);
114-
resourceListPane.tree.makeVisible(pathn);
115-
resourceListPane.tree.scrollPathToVisible(pathn);
116-
success = true;
117-
break;
118-
}
119-
}
120-
}
121-
}
122-
123-
}
124-
125-
if (!success) {
126-
Toolkit.getDefaultToolkit().beep();
127-
}
128-
}
32+
public class SearchKeyAdapter extends KeyAdapter
33+
{
34+
private final ResourceListPane resourceListPane;
35+
private int iteratePast;
36+
private String pastQT;
37+
38+
public SearchKeyAdapter(ResourceListPane resourceListPane)
39+
{
40+
this.resourceListPane = resourceListPane;
41+
}
42+
43+
@Override
44+
public void keyPressed(final KeyEvent ke)
45+
{
46+
//only trigger on enter
47+
if (ke.getKeyCode() != KeyEvent.VK_ENTER)
48+
return;
49+
50+
String qt = resourceListPane.quickSearch.getText();
51+
52+
if (qt.trim().isEmpty()) //NOPE
53+
return;
54+
55+
if (pastQT == null || !pastQT.equals(qt))
56+
{
57+
iteratePast = 0;
58+
pastQT = qt;
59+
}
60+
61+
String[] path;
62+
63+
path = qt.split("[\\./]+"); // split at dot or slash
64+
qt = qt.replace('/', '.');
65+
66+
ResourceTreeNode curNode = resourceListPane.treeRoot;
67+
boolean caseSensitive = resourceListPane.caseSensitive.isSelected();
68+
69+
boolean success = false;
70+
if (resourceListPane.exact.isSelected())
71+
{
72+
pathLoop:
73+
for (int i = 0; i < path.length; i++)
74+
{
75+
final String pathName = path[i];
76+
final boolean isLast = i == path.length - 1;
77+
78+
for (int c = 0; c < curNode.getChildCount(); c++)
79+
{
80+
final ResourceTreeNode child = (ResourceTreeNode) curNode.getChildAt(c);
81+
Object userObject = child.getUserObject();
82+
if (caseSensitive ? userObject.toString().equals(pathName) : userObject.toString().equalsIgnoreCase(pathName))
83+
{
84+
curNode = child;
85+
if (isLast)
86+
{
87+
final TreePath pathn = new TreePath(curNode.getPath());
88+
resourceListPane.tree.setSelectionPath(pathn);
89+
resourceListPane.tree.makeVisible(pathn);
90+
resourceListPane.tree.scrollPathToVisible(pathn);
91+
resourceListPane.openPath(pathn); //auto open
92+
success = true;
93+
break pathLoop;
94+
}
95+
continue pathLoop;
96+
}
97+
}
98+
System.out.println("Could not find " + pathName);
99+
break;
100+
}
101+
}
102+
else
103+
{
104+
int iteratations = 0;
105+
TreePath loopFallBack = null;
106+
TreePath pathOpen = null;
107+
108+
@SuppressWarnings("unchecked") Enumeration<TreeNode> enums = curNode.depthFirstEnumeration();
109+
while (enums != null && enums.hasMoreElements())
110+
{
111+
ResourceTreeNode node = (ResourceTreeNode) enums.nextElement();
112+
if (node.isLeaf())
113+
{
114+
String userObject = (String) (node.getUserObject());
115+
String lastElem = path[path.length - 1];
116+
117+
if (caseSensitive ? userObject.contains(lastElem) : userObject.toLowerCase().contains(lastElem.toLowerCase()))
118+
{
119+
TreeNode[] pathArray = node.getPath();
120+
int k = 0;
121+
StringBuilder fullPath = new StringBuilder();
122+
while (pathArray != null && k < pathArray.length)
123+
{
124+
ResourceTreeNode n = (ResourceTreeNode) pathArray[k];
125+
String s = (String) (n.getUserObject());
126+
fullPath.append(s);
127+
if (k++ != pathArray.length - 1)
128+
fullPath.append(".");
129+
}
130+
131+
String fullPathString = fullPath.toString();
132+
133+
if (caseSensitive ? fullPathString.contains(qt) : fullPathString.toLowerCase().contains(qt.toLowerCase()))
134+
{
135+
if (loopFallBack == null)
136+
loopFallBack = new TreePath(node.getPath());
137+
138+
if (iteratations++ < iteratePast)
139+
continue;
140+
141+
pathOpen = new TreePath(node.getPath());
142+
break;
143+
}
144+
}
145+
}
146+
}
147+
148+
if (pathOpen == null && loopFallBack != null)
149+
{
150+
iteratePast = 0;
151+
pathOpen = loopFallBack;
152+
}
153+
154+
if (pathOpen != null)
155+
{
156+
resourceListPane.tree.setSelectionPath(pathOpen.getParentPath());
157+
resourceListPane.tree.setSelectionPath(pathOpen);
158+
resourceListPane.tree.makeVisible(pathOpen);
159+
resourceListPane.tree.scrollPathToVisible(pathOpen);
160+
iteratePast++;
161+
success = true;
162+
}
163+
}
164+
165+
if (!success)
166+
Toolkit.getDefaultToolkit().beep();
167+
}
129168
}

0 commit comments

Comments
 (0)