Skip to content

Commit 0427f2b

Browse files
committed
Fix navigator tree selection highlighting
1 parent d19737f commit 0427f2b

File tree

1 file changed

+64
-103
lines changed

1 file changed

+64
-103
lines changed

src/main/java/the/bytecode/club/jda/gui/FileNavigationPane.java

Lines changed: 64 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class FileNavigationPane extends JDAWindow implements FileDrop.Listener
3434
JButton open = new JButton("+");
3535
JButton close = new JButton("-");
3636

37-
MyTreeNode treeRoot = new MyTreeNode("Loaded Files:");
38-
MyTree tree = new MyTree(treeRoot);
37+
FileNode treeRoot = new FileNode("Loaded Files:");
38+
FileTree tree = new FileTree(treeRoot);
3939
final JTextField quickSearch = new JTextField(quickSearchText);
4040

4141
public transient KeyAdapter search = new KeyAdapter()
@@ -45,7 +45,6 @@ public void keyPressed(final KeyEvent ke)
4545
{
4646
if (ke.getKeyCode() == KeyEvent.VK_ENTER)
4747
{
48-
4948
final String qt = quickSearch.getText();
5049
quickSearch.setText("");
5150

@@ -70,77 +69,46 @@ public void keyPressed(final KeyEvent ke)
7069
path = new String[] { qt };
7170
}
7271

73-
MyTreeNode curNode = treeRoot;
72+
FileNode curNode = treeRoot;
7473
if (exact.isSelected())
7574
{
76-
pathLoop:
77-
for (int i = 0; i < path.length; i++)
78-
{
79-
final String pathName = path[i];
80-
final boolean isLast = i == path.length - 1;
81-
82-
for (int c = 0; c < curNode.getChildCount(); c++)
83-
{
84-
final MyTreeNode child = (MyTreeNode) curNode.getChildAt(c);
85-
System.out.println(pathName + ":" + child.getUserObject());
86-
87-
if (child.getUserObject().equals(pathName))
88-
{
89-
curNode = child;
90-
if (isLast)
91-
{
92-
final TreePath pathn = new TreePath(curNode.getPath());
93-
tree.setSelectionPath(pathn);
94-
tree.makeVisible(pathn);
95-
tree.scrollPathToVisible(pathn);
96-
openPath(pathn); //auto open
97-
System.out.println("Found! " + curNode);
98-
break pathLoop;
99-
}
100-
continue pathLoop;
101-
}
102-
}
103-
104-
System.out.println("Could not find " + pathName);
105-
break;
106-
}
75+
// TODO
10776
}
10877
else
10978
{
79+
@SuppressWarnings("unchecked")
80+
Enumeration<FileNode> enums = curNode.depthFirstEnumeration();
81+
while (enums != null && enums.hasMoreElements())
11082
{
111-
@SuppressWarnings("unchecked") Enumeration<MyTreeNode> enums = curNode.depthFirstEnumeration();
112-
while (enums != null && enums.hasMoreElements())
113-
{
11483

115-
MyTreeNode node = enums.nextElement();
116-
if (node.isLeaf())
84+
FileNode node = enums.nextElement();
85+
if (node.isLeaf())
86+
{
87+
if (((String) (node.getUserObject())).contains(path[path.length - 1]))
11788
{
118-
if (((String) (node.getUserObject())).contains(path[path.length - 1]))
89+
TreeNode pathArray[] = node.getPath();
90+
int k = 0;
91+
StringBuilder fullPath = new StringBuilder();
92+
while (pathArray != null && k < pathArray.length)
11993
{
120-
TreeNode pathArray[] = node.getPath();
121-
int k = 0;
122-
StringBuilder fullPath = new StringBuilder();
123-
while (pathArray != null && k < pathArray.length)
94+
FileNode n = (FileNode) pathArray[k];
95+
String s = (String) (n.getUserObject());
96+
fullPath.append(s);
97+
if (k++ != pathArray.length - 1)
12498
{
125-
MyTreeNode n = (MyTreeNode) pathArray[k];
126-
String s = (String) (n.getUserObject());
127-
fullPath.append(s);
128-
if (k++ != pathArray.length - 1)
129-
{
130-
fullPath.append(".");
131-
}
132-
}
133-
String fullPathString = fullPath.toString();
134-
if (fullPathString != null && fullPathString.contains(qt))
135-
{
136-
System.out.println("Found! " + node);
137-
final TreePath pathn = new TreePath(node.getPath());
138-
tree.setSelectionPath(pathn.getParentPath());
139-
tree.setSelectionPath(pathn);
140-
tree.makeVisible(pathn);
141-
tree.scrollPathToVisible(pathn);
99+
fullPath.append(".");
142100
}
143101
}
102+
String fullPathString = fullPath.toString();
103+
if (!fullPathString.equals("null") && fullPathString.contains(qt))
104+
{
105+
System.out.println("Found! " + node);
106+
final TreePath pathn = new TreePath(node.getPath());
107+
tree.setSelectionPath(pathn.getParentPath());
108+
tree.setSelectionPath(pathn);
109+
tree.makeVisible(pathn);
110+
tree.scrollPathToVisible(pathn);
111+
}
144112
}
145113
}
146114
}
@@ -201,9 +169,9 @@ public void keyReleased(KeyEvent arg0)
201169
{
202170
if (arg0.getKeyCode() == KeyEvent.VK_ENTER)
203171
{
204-
if (arg0.getSource() instanceof MyTree)
172+
if (arg0.getSource() instanceof FileTree)
205173
{
206-
MyTree tree = (MyTree) arg0.getSource();
174+
FileTree tree = (FileTree) arg0.getSource();
207175
openPath(tree.getSelectionPath());
208176
}
209177
}
@@ -245,6 +213,7 @@ public void focusLost(final FocusEvent arg0)
245213
p2.setLayout(new BorderLayout());
246214
p2.add(quickSearch, BorderLayout.NORTH);
247215
JPanel p3 = new JPanel(new BorderLayout());
216+
exact.setEnabled(false);
248217
p3.add(exact, BorderLayout.WEST);
249218
JPanel p4 = new JPanel(new BorderLayout());
250219
p4.add(open, BorderLayout.EAST);
@@ -307,7 +276,7 @@ public void updateTree()
307276
treeRoot.removeAllChildren();
308277
for (FileContainer container : JDA.files)
309278
{
310-
MyTreeNode root = new MyTreeNode(container.name);
279+
FileNode root = new FileNode(container.name);
311280
treeRoot.add(root);
312281
ImageRenderer renderer = new ImageRenderer();
313282
tree.setCellRenderer(renderer);
@@ -321,25 +290,25 @@ public void updateTree()
321290
final String[] spl = name.split("/");
322291
if (spl.length < 2)
323292
{
324-
root.add(new MyTreeNode(name));
293+
root.add(new FileNode(name));
325294
}
326295
else
327296
{
328-
MyTreeNode parent = root;
297+
FileNode parent = root;
329298
for (final String s : spl)
330299
{
331-
MyTreeNode child = null;
300+
FileNode child = null;
332301
for (int i = 0; i < parent.getChildCount(); i++)
333302
{
334-
if (((MyTreeNode) parent.getChildAt(i)).getUserObject().equals(s))
303+
if (((FileNode) parent.getChildAt(i)).getUserObject().equals(s))
335304
{
336-
child = (MyTreeNode) parent.getChildAt(i);
305+
child = (FileNode) parent.getChildAt(i);
337306
break;
338307
}
339308
}
340309
if (child == null)
341310
{
342-
child = new MyTreeNode(s);
311+
child = new FileNode(s);
343312
parent.add(child);
344313
}
345314
parent = child;
@@ -387,12 +356,12 @@ private void expandAll(final JTree tree, final TreePath parent, final boolean ex
387356
}
388357
}
389358

390-
public class MyTree extends JTree
359+
public class FileTree extends JTree
391360
{
392361
private static final long serialVersionUID = -2355167326094772096L;
393362
DefaultMutableTreeNode treeRoot;
394363

395-
public MyTree(final DefaultMutableTreeNode treeRoot)
364+
public FileTree(final DefaultMutableTreeNode treeRoot)
396365
{
397366
super(treeRoot);
398367
this.treeRoot = treeRoot;
@@ -426,12 +395,12 @@ public void paint(final Graphics g)
426395
}
427396
}
428397

429-
public class MyTreeNode extends DefaultMutableTreeNode
398+
public class FileNode extends DefaultMutableTreeNode
430399
{
431400

432401
private static final long serialVersionUID = -8817777566176729571L;
433402

434-
public MyTreeNode(final Object o)
403+
public FileNode(final Object o)
435404
{
436405
super(o);
437406
}
@@ -448,10 +417,10 @@ public void sort()
448417
}
449418

450419
@SuppressWarnings("unchecked")
451-
private void recursiveSort(final MyTreeNode node)
420+
private void recursiveSort(final FileNode node)
452421
{
453422
Collections.sort(node.children, nodeComparator);
454-
for (MyTreeNode nextNode : (Iterable<MyTreeNode>) node.children)
423+
for (FileNode nextNode : (Iterable<FileNode>) node.children)
455424
{
456425
if (nextNode.getChildCount() > 0)
457426
{
@@ -460,29 +429,22 @@ private void recursiveSort(final MyTreeNode node)
460429
}
461430
}
462431

463-
protected Comparator<MyTreeNode> nodeComparator = new Comparator<MyTreeNode>()
432+
protected Comparator<FileNode> nodeComparator = (a, b) ->
464433
{
465-
@Override
466-
public int compare(final MyTreeNode o1, final MyTreeNode o2)
467-
{
468-
// To make sure nodes with children are always on top
469-
final int firstOffset = o1.getChildCount() > 0 ? -1000 : 0;
470-
final int secondOffset = o2.getChildCount() > 0 ? 1000 : 0;
471-
return o1.toString().compareToIgnoreCase(o2.toString()) + firstOffset + secondOffset;
472-
}
473-
474-
@Override
475-
public boolean equals(final Object obj)
476-
{
477-
return false;
478-
}
479-
480-
@Override
481-
public int hashCode()
482-
{
483-
final int hash = 7;
484-
return hash;
485-
}
434+
// Ensure nodes with children are always on top
435+
final boolean aEmpty = a.getChildCount() > 0;
436+
final boolean bEmpty = b.getChildCount() > 0;
437+
if (aEmpty && !bEmpty)
438+
return -1;
439+
else if (!aEmpty && bEmpty)
440+
return 1;
441+
442+
// Try insensitive first, but if they are the same insensitively do it case sensitively
443+
int compare = a.toString().compareToIgnoreCase(b.toString());
444+
if (compare != 0)
445+
return compare;
446+
else
447+
return a.toString().compareTo(b.toString());
486448
};
487449
}
488450

@@ -567,15 +529,14 @@ public void openPath(TreePath path)
567529
*/
568530
public class ImageRenderer extends DefaultTreeCellRenderer
569531
{
570-
571532
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
572533
{ //called every time there is a pane update, I.E. whenever you expand a folder
573534

574-
Component ret = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
535+
Component ret = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
575536

576-
if (value != null && value instanceof FileNavigationPane.MyTreeNode)
537+
if (value != null && value instanceof FileNode)
577538
{
578-
FileNavigationPane.MyTreeNode node = (FileNavigationPane.MyTreeNode) value;
539+
FileNode node = (FileNode) value;
579540
String name = node.toString();
580541

581542
if (name.endsWith(".jar"))

0 commit comments

Comments
 (0)