Skip to content

Commit d7a8284

Browse files
committed
Add refresh button
Simpify when I add a folder outside of the program and want it to show up. I could also add a filewatch onto the folder itself and do this automatically, but a the moment I don't want to deal with the various events I'd likely need to keep track of, as we'd probably want to be smart and update the tree wherever the event was for things like renames (though the naive approach would be to just refresh the entire tree, which is non-performant but simple)
1 parent 5f504e3 commit d7a8284

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/main/java/com/peetseater/filesorter/trees/AbstractFileTreePanel.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
public abstract class AbstractFileTreePanel extends JPanel implements ActionListener, FileVisitor<Path> {
2727

2828
JButton browseButton = new JButton("Browse");
29+
JButton refreshButton = new JButton("Refresh");
30+
transient Path loadedPath = null;
2931
DefaultMutableTreeNode rootNode;
3032
JTree jTree;
3133
String browseText;
@@ -48,13 +50,21 @@ protected AbstractFileTreePanel(String browseText, boolean onlyIncludeDirectorie
4850
jTree.setEditable(false);
4951
jTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
5052
jTree.setShowsRootHandles(true);
53+
54+
5155

5256

5357
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
54-
add(browseButton);
58+
JPanel buttonPanel = new JPanel();
59+
BoxLayout sideBySideButtons = new BoxLayout(buttonPanel, BoxLayout.X_AXIS);
60+
61+
buttonPanel.add(browseButton);
62+
buttonPanel.add(refreshButton);
63+
add(buttonPanel);
5564
add(scrollPane);
5665

5766
browseButton.addActionListener(this);
67+
refreshButton.addActionListener(this);
5868
}
5969

6070
DefaultMutableTreeNode currentParent = rootNode;
@@ -77,9 +87,14 @@ public void loadPath(Path rootPath) {
7787
@Override
7888
public void actionPerformed(ActionEvent e) {
7989
AppLogger.info("FileTreePanel with text \"" + browseText + "\" button clicked " + this.rootNode);
80-
if (jFileChooser.showOpenDialog(this.getParent()) == JFileChooser.APPROVE_OPTION) {
81-
File file = jFileChooser.getSelectedFile();
82-
this.loadPath(file.toPath());
90+
if (e.getActionCommand().equals("Refresh")) {
91+
this.loadPath(this.loadedPath);
92+
} else if (e.getActionCommand().equals(this.browseText)) {
93+
if (jFileChooser.showOpenDialog(this.getParent()) == JFileChooser.APPROVE_OPTION) {
94+
File file = jFileChooser.getSelectedFile();
95+
this.loadPath(file.toPath());
96+
this.loadedPath = file.toPath();
97+
}
8398
}
8499
}
85100

0 commit comments

Comments
 (0)