mirror of
https://github.com/dmolony/DiskBrowser.git
synced 2024-11-29 11:49:29 +00:00
auto expand selected path
This commit is contained in:
parent
f5cdeb61cb
commit
ca44da4c94
@ -30,18 +30,22 @@ import javax.swing.tree.TreeSelectionModel;
|
|||||||
import com.bytezone.diskbrowser.gui.RedoHandler.RedoData;
|
import com.bytezone.diskbrowser.gui.RedoHandler.RedoData;
|
||||||
import com.bytezone.diskbrowser.gui.TreeBuilder.FileNode;
|
import com.bytezone.diskbrowser.gui.TreeBuilder.FileNode;
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------------//
|
||||||
abstract class AbstractTab extends JPanel implements Tab
|
abstract class AbstractTab extends JPanel implements Tab
|
||||||
|
// -----------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
private final static Cursor handCursor = new Cursor (Cursor.HAND_CURSOR);
|
private final static Cursor handCursor = new Cursor (Cursor.HAND_CURSOR);
|
||||||
private final List<MouseAdapter> adapters = new ArrayList<MouseAdapter> ();
|
private final List<MouseAdapter> adapters = new ArrayList<MouseAdapter> ();
|
||||||
private Font font;
|
private Font font;
|
||||||
private final JScrollPane scrollpane;
|
private final JScrollPane scrollPane;
|
||||||
final DiskAndFileSelector eventHandler;
|
final DiskAndFileSelector eventHandler;
|
||||||
final RedoHandler redoHandler;
|
final RedoHandler redoHandler;
|
||||||
final RedoData redoData;
|
final RedoData redoData;
|
||||||
protected JTree tree;
|
protected JTree tree;
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
public AbstractTab (RedoHandler redoHandler, DiskAndFileSelector selector, Font font)
|
public AbstractTab (RedoHandler redoHandler, DiskAndFileSelector selector, Font font)
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
super (new BorderLayout ());
|
super (new BorderLayout ());
|
||||||
|
|
||||||
@ -50,17 +54,20 @@ abstract class AbstractTab extends JPanel implements Tab
|
|||||||
this.redoHandler = redoHandler;
|
this.redoHandler = redoHandler;
|
||||||
this.redoData = redoHandler.createData ();
|
this.redoData = redoHandler.createData ();
|
||||||
|
|
||||||
scrollpane =
|
scrollPane =
|
||||||
new JScrollPane (null, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_NEVER);
|
new JScrollPane (null, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_NEVER);
|
||||||
scrollpane.setBorder (null);
|
scrollPane.setBorder (null);
|
||||||
add (scrollpane, BorderLayout.CENTER);
|
// scrollPane.setBorder (BorderFactory.createEmptyBorder ());
|
||||||
|
add (scrollPane, BorderLayout.CENTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
protected void setTree (JTree tree)
|
protected void setTree (JTree tree)
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
this.tree = tree;
|
this.tree = tree;
|
||||||
tree.setFont (font);
|
tree.setFont (font);
|
||||||
scrollpane.setViewportView (tree);
|
scrollPane.setViewportView (tree);
|
||||||
TreeSelectionModel tsm = tree.getSelectionModel ();
|
TreeSelectionModel tsm = tree.getSelectionModel ();
|
||||||
tsm.setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION);
|
tsm.setSelectionMode (TreeSelectionModel.SINGLE_TREE_SELECTION);
|
||||||
|
|
||||||
@ -70,38 +77,50 @@ abstract class AbstractTab extends JPanel implements Tab
|
|||||||
addTreeMouseListener (new CursorHandler ());
|
addTreeMouseListener (new CursorHandler ());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
protected void setTreeFont (Font font)
|
protected void setTreeFont (Font font)
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
tree.setFont (font);
|
tree.setFont (font);
|
||||||
this.font = font;
|
this.font = font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
public void addTreeMouseListener (MouseAdapter adapter)
|
public void addTreeMouseListener (MouseAdapter adapter)
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
tree.addMouseListener (adapter);
|
tree.addMouseListener (adapter);
|
||||||
adapters.add (adapter);
|
adapters.add (adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
private void restoreAdapters ()
|
private void restoreAdapters ()
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
for (MouseAdapter ma : adapters)
|
for (MouseAdapter ma : adapters)
|
||||||
tree.addMouseListener (ma);
|
tree.addMouseListener (ma);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
protected Object getSelectedObject ()
|
protected Object getSelectedObject ()
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
DefaultMutableTreeNode node =
|
DefaultMutableTreeNode node =
|
||||||
(DefaultMutableTreeNode) tree.getLastSelectedPathComponent ();
|
(DefaultMutableTreeNode) tree.getLastSelectedPathComponent ();
|
||||||
return node == null ? null : node.getUserObject ();
|
return node == null ? null : node.getUserObject ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
@Override
|
@Override
|
||||||
public DefaultMutableTreeNode getRootNode ()
|
public DefaultMutableTreeNode getRootNode ()
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
return (DefaultMutableTreeNode) tree.getModel ().getRoot ();
|
return (DefaultMutableTreeNode) tree.getModel ().getRoot ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
protected DefaultMutableTreeNode findNode (int nodeNo)
|
protected DefaultMutableTreeNode findNode (int nodeNo)
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
DefaultMutableTreeNode rootNode = getRootNode ();
|
DefaultMutableTreeNode rootNode = getRootNode ();
|
||||||
Enumeration<TreeNode> children = rootNode.breadthFirstEnumeration ();
|
Enumeration<TreeNode> children = rootNode.breadthFirstEnumeration ();
|
||||||
@ -112,7 +131,9 @@ abstract class AbstractTab extends JPanel implements Tab
|
|||||||
return selectNode;
|
return selectNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
protected DefaultMutableTreeNode findFirstLeafNode ()
|
protected DefaultMutableTreeNode findFirstLeafNode ()
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
DefaultMutableTreeNode rootNode = getRootNode ();
|
DefaultMutableTreeNode rootNode = getRootNode ();
|
||||||
Enumeration<TreeNode> children = rootNode.depthFirstEnumeration ();
|
Enumeration<TreeNode> children = rootNode.depthFirstEnumeration ();
|
||||||
@ -131,21 +152,28 @@ abstract class AbstractTab extends JPanel implements Tab
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Trigger the TreeSelectionListener set by the real Tab (if the value is different)
|
// Trigger the TreeSelectionListener set by the real Tab (if the value is different)
|
||||||
protected void showNode (DefaultMutableTreeNode showNode)
|
// ---------------------------------------------------------------------------------//
|
||||||
|
protected void showNode (DefaultMutableTreeNode treeNode)
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
assert showNode != null;
|
assert treeNode != null;
|
||||||
TreePath tp = getPathToNode (showNode);
|
TreePath tp = getPathToNode (treeNode);
|
||||||
if (tp == null)
|
if (tp == null)
|
||||||
{
|
{
|
||||||
System.out.println ("Not found: " + showNode);
|
System.out.println ("Not found: " + treeNode);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tree.requestFocusInWindow ();
|
||||||
tree.setSelectionPath (tp);
|
tree.setSelectionPath (tp);
|
||||||
tree.scrollPathToVisible (tp);
|
tree.scrollPathToVisible (tp);
|
||||||
tree.requestFocusInWindow ();
|
if (!treeNode.isLeaf ())
|
||||||
|
tree.expandPath (tp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
protected TreePath getPathToNode (DefaultMutableTreeNode selectNode)
|
protected TreePath getPathToNode (DefaultMutableTreeNode selectNode)
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel ();
|
DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel ();
|
||||||
TreeNode[] nodes = treeModel.getPathToRoot (selectNode);
|
TreeNode[] nodes = treeModel.getPathToRoot (selectNode);
|
||||||
@ -154,7 +182,9 @@ abstract class AbstractTab extends JPanel implements Tab
|
|||||||
return new TreePath (nodes);
|
return new TreePath (nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
private class CursorHandler extends MouseAdapter
|
private class CursorHandler extends MouseAdapter
|
||||||
|
// ---------------------------------------------------------------------------------//
|
||||||
{
|
{
|
||||||
private Cursor oldCursor;
|
private Cursor oldCursor;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user