From 94f983f005af2341449e0626b093534130833dc6 Mon Sep 17 00:00:00 2001 From: hajdam Date: Sat, 11 Sep 2021 14:24:57 +0200 Subject: [PATCH] Delete operation --- .../gui/components/FileChooser.java | 9 ++---- .../gui/contextmenu/ContextMenu.java | 4 +-- .../resourcelist/{Remove.java => Delete.java} | 8 ++--- .../gui/resourcelist/ResourceListPane.java | 31 +++++++++++++++++++ .../translation/TranslatedStrings.java | 2 +- .../club/bytecodeviewer/util/DialogUtils.java | 4 +-- src/main/resources/translations/english.json | 2 +- 7 files changed, 44 insertions(+), 16 deletions(-) rename src/main/java/the/bytecode/club/bytecodeviewer/gui/contextmenu/resourcelist/{Remove.java => Delete.java} (90%) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/FileChooser.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/FileChooser.java index c2e4b39b..5ce9464c 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/FileChooser.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/FileChooser.java @@ -43,20 +43,17 @@ public class FileChooser extends JFileChooser { Set extensionSet = new HashSet<>(Arrays.asList(extensions)); + setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); try { - if(file.isDirectory()) - setCurrentDirectory(file); - else - setSelectedFile(file); + setSelectedFile(file); } catch (Exception ignored) { } setDialogTitle(title); - setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); setFileHidingEnabled(false); setAcceptAllFileFilterUsed(false); if(!skipFileFilter) { - setFileFilter(new FileFilter() + addChoosableFileFilter(new FileFilter() { @Override public boolean accept(File f) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/contextmenu/ContextMenu.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/contextmenu/ContextMenu.java index 3f3fe6c8..10a690a1 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/contextmenu/ContextMenu.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/contextmenu/ContextMenu.java @@ -11,7 +11,7 @@ import the.bytecode.club.bytecodeviewer.gui.contextmenu.resourcelist.New; import the.bytecode.club.bytecodeviewer.gui.contextmenu.resourcelist.Open; import the.bytecode.club.bytecodeviewer.gui.contextmenu.resourcelist.QuickEdit; import the.bytecode.club.bytecodeviewer.gui.contextmenu.resourcelist.QuickOpen; -import the.bytecode.club.bytecodeviewer.gui.contextmenu.resourcelist.Remove; +import the.bytecode.club.bytecodeviewer.gui.contextmenu.resourcelist.Delete; import the.bytecode.club.bytecodeviewer.gui.resourcelist.ResourceTree; import the.bytecode.club.bytecodeviewer.searching.LDCSearchTreeNodeResult; @@ -45,7 +45,7 @@ public class ContextMenu static { //resource list - addContext(new Remove()); //TODO rename to delete and add support for resources & whole parent nodes (directories) + addContext(new Delete()); addContext(new New()); addContext(new Open()); addContext(new QuickOpen()); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/contextmenu/resourcelist/Remove.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/contextmenu/resourcelist/Delete.java similarity index 90% rename from src/main/java/the/bytecode/club/bytecodeviewer/gui/contextmenu/resourcelist/Remove.java rename to src/main/java/the/bytecode/club/bytecodeviewer/gui/contextmenu/resourcelist/Delete.java index 1dce6332..62bc35ac 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/contextmenu/resourcelist/Remove.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/contextmenu/resourcelist/Delete.java @@ -29,18 +29,18 @@ import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings; * @author Konloch * @since 7/26/2021 */ -public class Remove extends ContextMenuItem +public class Delete extends ContextMenuItem { - public Remove() + public Delete() { super(ContextMenuType.CONTAINER, ((tree, selPath, result, menu) -> { - menu.add(new AbstractAction(TranslatedStrings.REMOVE.toString()) + menu.add(new AbstractAction(TranslatedStrings.DELETE.toString()) { @Override public void actionPerformed(ActionEvent e) { - BytecodeViewer.viewer.resourcePane.expandAll(tree, selPath, false); + BytecodeViewer.viewer.resourcePane.removeNode(tree, selPath); } }); })); diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListPane.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListPane.java index 574b1dca..d092e8d9 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListPane.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/ResourceListPane.java @@ -19,6 +19,7 @@ import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JTree; +import javax.swing.tree.MutableTreeNode; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; import me.konloch.kontainer.io.DiskWriter; @@ -261,6 +262,36 @@ public class ResourceListPane extends TranslatedVisibleComponent implements File tree.collapsePath(parent); } } + + @SuppressWarnings("rawtypes") + public void removeNode(final JTree tree, final TreePath nodePath) { + MutableTreeNode node = findNodeByPath(nodePath); + if (node == null) + return; + + node.removeFromParent(); + tree.repaint(); + tree.updateUI(); + } + + @SuppressWarnings("rawtypes") + private MutableTreeNode findNodeByPath(TreePath path) { + MutableTreeNode node = treeRoot; + for (int pathStep = 1; pathStep < path.getPathCount(); pathStep++) { + TreeNode pathNode = (TreeNode) path.getPathComponent(pathStep); + int childIndex = node.getIndex(pathNode); + if (childIndex < 0) { + return null; + } + node = (MutableTreeNode) node.getChildAt(childIndex); + + if (node == null) { + return null; + } + } + + return node; + } public void resetWorkspace() { diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/translation/TranslatedStrings.java b/src/main/java/the/bytecode/club/bytecodeviewer/translation/TranslatedStrings.java index da7aaddd..52a4ad1e 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/translation/TranslatedStrings.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/translation/TranslatedStrings.java @@ -62,7 +62,7 @@ public enum TranslatedStrings OPEN_UNSTYLED, QUICK_OPEN, - REMOVE, + DELETE, NEW, EXPAND, COLLAPSE, diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/DialogUtils.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/DialogUtils.java index 7292cbc7..0784169b 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/DialogUtils.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/DialogUtils.java @@ -99,9 +99,9 @@ public class DialogUtils extensions); if(filter != null) - fc.setFileFilter(filter); + fc.addChoosableFileFilter(filter); else - fc.setFileFilter(new FileFilter() + fc.addChoosableFileFilter(new FileFilter() { @Override public boolean accept(File f) diff --git a/src/main/resources/translations/english.json b/src/main/resources/translations/english.json index 52733c72..cf7aa961 100644 --- a/src/main/resources/translations/english.json +++ b/src/main/resources/translations/english.json @@ -7,7 +7,7 @@ "OPEN": "Open...", "OPEN_UNSTYLED": "Open", "QUICK_OPEN": "Quick Open", - "REMOVE": "Remove", + "DELETE": "Delete", "NEW": "New", "EXPAND": "Expand", "COLLAPSE": "Collapse",