From 5603f466b3737fadd0255cae7c95bdd28b45aa17 Mon Sep 17 00:00:00 2001 From: Konloch Date: Fri, 25 Jun 2021 21:10:32 -0700 Subject: [PATCH] Code Cleanup --- .../club/bytecodeviewer/Constants.java | 1 + .../bytecodeviewer/gui/MainViewerGUI.java | 244 ++++++------------ .../gui/components/FileChooser.java | 59 +++++ .../bytecodeviewer/plugin/PluginManager.java | 2 +- .../util/NewlineOutputStream.java | 5 +- 5 files changed, 144 insertions(+), 167 deletions(-) create mode 100644 src/main/java/the/bytecode/club/bytecodeviewer/gui/components/FileChooser.java diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/Constants.java b/src/main/java/the/bytecode/club/bytecodeviewer/Constants.java index f9bcbc30..c1e4052e 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/Constants.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/Constants.java @@ -40,6 +40,7 @@ public class Constants public static final String libsDirectory = getBCVDirectory() + fs + "libs" + fs; public static String krakatauWorkingDirectory = getBCVDirectory() + fs + "krakatau_" + krakatauVersion; public static String enjarifyWorkingDirectory = getBCVDirectory() + fs + "enjarify_" + enjarifyVersion; + public static final String[] SUPPORTED_FILE_EXTENSIONS = new String[]{"jar", "zip", "class", "apk", "dex", "war", "jsp"}; public static List recentPlugins; public static List recentFiles; diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/gui/MainViewerGUI.java b/src/main/java/the/bytecode/club/bytecodeviewer/gui/MainViewerGUI.java index da672166..15b44ed3 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/gui/MainViewerGUI.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/MainViewerGUI.java @@ -10,11 +10,9 @@ import javax.swing.*; import javax.swing.filechooser.FileFilter; import org.objectweb.asm.tree.ClassNode; -import the.bytecode.club.bytecodeviewer.BytecodeViewer; -import the.bytecode.club.bytecodeviewer.Configuration; -import the.bytecode.club.bytecodeviewer.Resources; -import the.bytecode.club.bytecodeviewer.Settings; +import the.bytecode.club.bytecodeviewer.*; import the.bytecode.club.bytecodeviewer.api.ExceptionUI; +import the.bytecode.club.bytecodeviewer.gui.components.FileChooser; import the.bytecode.club.bytecodeviewer.gui.components.VisibleComponent; import the.bytecode.club.bytecodeviewer.gui.components.AboutWindow; import the.bytecode.club.bytecodeviewer.gui.components.RunOptions; @@ -740,52 +738,20 @@ public class MainViewerGUI extends JFrame { return null; } - public void selectFile() + public void compileOnNewThread() { - final JFileChooser fc = new JFileChooser(); + Thread t = new Thread(() -> BytecodeViewer.compile(true)); + t.start(); + } - try { - File f = new File(Configuration.lastDirectory); - if (f.exists()) - fc.setSelectedFile(f); - } catch (Exception ignored) { - + public void runResources() + { + if (BytecodeViewer.getLoadedClasses().isEmpty()) { + BytecodeViewer.showMessage("First open a class, jar, zip, apk or dex file."); + return; } - - fc.setDialogTitle("Select File or Folder to open in BCV"); - fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); - fc.setAcceptAllFileFilterUsed(true); - fc.setFileFilter(new FileFilter() { - @Override - public boolean accept(File f) { - if (f.isDirectory()) - return true; - - String extension = MiscUtils.extension(f.getAbsolutePath()); - return extension.equals("jar") || extension.equals("zip") - || extension.equals("class") || extension.equals("apk") - || extension.equals("dex") || extension.equals("war") || extension.equals("jsp"); - - } - @Override - public String getDescription() { - return "APKs, DEX, Class Files or Zip/Jar/War Archives"; - } - }); - - int returnVal = fc.showOpenDialog(BytecodeViewer.viewer); - - if (returnVal == JFileChooser.APPROVE_OPTION) { - Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath(); - try { - BytecodeViewer.viewer.updateBusyStatus(true); - BytecodeViewer.openFiles(new File[]{fc.getSelectedFile()}, true); - BytecodeViewer.viewer.updateBusyStatus(false); - } catch (Exception e1) { - new ExceptionUI(e1); - } - } + new RunOptions().setVisible(true); } public void reloadResources() @@ -826,48 +792,34 @@ public class MainViewerGUI extends JFrame { } } - public void compileOnNewThread() + public void selectFile() { - Thread t = new Thread(() -> BytecodeViewer.compile(true)); - t.start(); - } - - public void runResources() - { - if (BytecodeViewer.getLoadedClasses().isEmpty()) { - BytecodeViewer.showMessage("First open a class, jar, zip, apk or dex file."); - return; - } + final JFileChooser fc = new FileChooser(new File(Configuration.lastDirectory), + "Select File or Folder to open in BCV", + "APKs, DEX, Class Files or Zip/Jar/War Archives", + Constants.SUPPORTED_FILE_EXTENSIONS); - new RunOptions().setVisible(true); - } - - public void showForeignLibraryWarning() - { - if (!deleteForeignOutdatedLibs.isSelected()) { - BytecodeViewer.showMessage("WARNING: With this being toggled off outdated libraries will NOT be " - + "removed. It's also a security issue. ONLY TURN IT OFF IF YOU KNOW WHAT YOU'RE DOING."); + int returnVal = fc.showOpenDialog(BytecodeViewer.viewer); + if (returnVal == JFileChooser.APPROVE_OPTION) + { + Configuration.lastDirectory = fc.getSelectedFile().getAbsolutePath(); + try { + BytecodeViewer.viewer.updateBusyStatus(true); + BytecodeViewer.openFiles(new File[]{fc.getSelectedFile()}, true); + BytecodeViewer.viewer.updateBusyStatus(false); + } catch (Exception e1) { + new ExceptionUI(e1); + } } - Configuration.deleteForeignLibraries = deleteForeignOutdatedLibs.isSelected(); } public void selectPythonC() { - JFileChooser fc = new JFileChooser(); - fc.setFileFilter(new FileFilter() { - @Override - public boolean accept(File f) { - return true; - } - - @Override - public String getDescription() { - return "Python (Or PyPy for speed) 2.7 Executable"; - } - }); - fc.setFileHidingEnabled(false); - fc.setAcceptAllFileFilterUsed(false); + final JFileChooser fc = new FileChooser(new File(Configuration.lastDirectory), + "Select Python 2.7 Executable", + "Python (Or PyPy for speed) 2.7 Executable", + "everything"); + int returnVal = fc.showOpenDialog(BytecodeViewer.viewer); - if (returnVal == JFileChooser.APPROVE_OPTION) try { Configuration.python = fc.getSelectedFile().getAbsolutePath(); @@ -877,22 +829,12 @@ public class MainViewerGUI extends JFrame { } public void selectJavac() { - JFileChooser fc = new JFileChooser(); - fc.setFileFilter(new FileFilter() { - @Override - public boolean accept(File f) { - return true; - } - - @Override - public String getDescription() { - return "Javac Executable (Requires JDK 'C:/programfiles/Java/JDK_xx/bin/javac.exe)"; - } - }); - fc.setFileHidingEnabled(false); - fc.setAcceptAllFileFilterUsed(false); + final JFileChooser fc = new FileChooser(new File(Configuration.lastDirectory), + "Select Javac Executable", + "Javac Executable (Requires JDK 'C:/programfiles/Java/JDK_xx/bin/javac.exe)", + "everything"); + int returnVal = fc.showOpenDialog(BytecodeViewer.viewer); - if (returnVal == JFileChooser.APPROVE_OPTION) try { Configuration.javac = fc.getSelectedFile().getAbsolutePath(); @@ -902,22 +844,12 @@ public class MainViewerGUI extends JFrame { } public void selectJava() { - JFileChooser fc = new JFileChooser(); - fc.setFileFilter(new FileFilter() { - @Override - public boolean accept(File f) { - return true; - } - - @Override - public String getDescription() { - return "Java Executable (Inside Of JRE/JDK 'C:/programfiles/Java/JDK_xx/bin/java.exe')"; - } - }); - fc.setFileHidingEnabled(false); - fc.setAcceptAllFileFilterUsed(false); + final JFileChooser fc = new FileChooser(new File(Configuration.lastDirectory), + "Select Java Executable", + "Java Executable (Inside Of JRE/JDK 'C:/programfiles/Java/JDK_xx/bin/java.exe')", + "everything"); + int returnVal = fc.showOpenDialog(BytecodeViewer.viewer); - if (returnVal == JFileChooser.APPROVE_OPTION) try { Configuration.java = fc.getSelectedFile().getAbsolutePath(); @@ -927,22 +859,12 @@ public class MainViewerGUI extends JFrame { } public void selectPythonC3() { - JFileChooser fc = new JFileChooser(); - fc.setFileFilter(new FileFilter() { - @Override - public boolean accept(File f) { - return true; - } - - @Override - public String getDescription() { - return "Python (Or PyPy for speed) 3.x Executable"; - } - }); - fc.setFileHidingEnabled(false); - fc.setAcceptAllFileFilterUsed(false); + final JFileChooser fc = new FileChooser(new File(Configuration.lastDirectory), + "Select Python 3.x Executable", + "Python (Or PyPy for speed) 3.x Executable", + "everything"); + int returnVal = fc.showOpenDialog(BytecodeViewer.viewer); - if (returnVal == JFileChooser.APPROVE_OPTION) try { Configuration.python3 = fc.getSelectedFile().getAbsolutePath(); @@ -952,23 +874,16 @@ public class MainViewerGUI extends JFrame { } public void selectOpenalLibraryFolder() { - JFileChooser fc = new JFileChooser(); - fc.setFileFilter(new FileFilter() { - @Override - public boolean accept(File f) { - return f.isDirectory(); - } - - @Override - public String getDescription() { - return "Optional Library Folder"; - } - }); + final JFileChooser fc = new FileChooser(new File(Configuration.lastDirectory), + "Select Library Folder", + "Optional Library Folder", + "everything"); + fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fc.setFileHidingEnabled(false); fc.setAcceptAllFileFilterUsed(false); + int returnVal = fc.showOpenDialog(BytecodeViewer.viewer); - if (returnVal == JFileChooser.APPROVE_OPTION) try { Configuration.library = fc.getSelectedFile().getAbsolutePath(); @@ -978,22 +893,12 @@ public class MainViewerGUI extends JFrame { } public void selectJRERTLibrary() { - JFileChooser fc = new JFileChooser(); - fc.setFileFilter(new FileFilter() { - @Override - public boolean accept(File f) { - return true; - } - - @Override - public String getDescription() { - return "JRE RT Library"; - } - }); - fc.setFileHidingEnabled(false); - fc.setAcceptAllFileFilterUsed(false); + final JFileChooser fc = new FileChooser(new File(Configuration.lastDirectory), + "Select JRE RT Jar", + "JRE RT Library", + "everything"); + int returnVal = fc.showOpenDialog(BytecodeViewer.viewer); - if (returnVal == JFileChooser.APPROVE_OPTION) try { Configuration.rt = fc.getSelectedFile().getAbsolutePath(); @@ -1004,12 +909,13 @@ public class MainViewerGUI extends JFrame { public void openExternalPlugin() { - JFileChooser fc = new JFileChooser(); + JFileChooser fc = new FileChooser(new File(Configuration.lastDirectory), + "Select External Plugin", + "External Plugin", + "everything"); fc.setFileFilter(PluginManager.fileFilter()); - fc.setFileHidingEnabled(false); - fc.setAcceptAllFileFilterUsed(false); + int returnVal = fc.showOpenDialog(BytecodeViewer.viewer); - if (returnVal == JFileChooser.APPROVE_OPTION) try { BytecodeViewer.viewer.updateBusyStatus(true); @@ -1022,12 +928,12 @@ public class MainViewerGUI extends JFrame { public void askBeforeExiting() { - JOptionPane pane = new JOptionPane( - "Are you sure you want to exit?"); + JOptionPane pane = new JOptionPane("Are you sure you want to exit?"); + Object[] options = new String[]{"Yes", "No"}; + pane.setOptions(options); - JDialog dialog = pane.createDialog(BytecodeViewer.viewer, - "Bytecode Viewer - Exit"); + JDialog dialog = pane.createDialog(BytecodeViewer.viewer, "Bytecode Viewer - Exit"); dialog.setVisible(true); Object obj = pane.getValue(); int result = -1; @@ -1035,9 +941,21 @@ public class MainViewerGUI extends JFrame { if (options[k].equals(obj)) result = k; - if (result == 0) { + if (result == 0) + { Configuration.canExit = true; System.exit(0); } } + + public void showForeignLibraryWarning() + { + if (!deleteForeignOutdatedLibs.isSelected()) + { + BytecodeViewer.showMessage("WARNING: With this being toggled off outdated libraries will NOT be " + + "removed. It's also a security issue. ONLY TURN IT OFF IF YOU KNOW WHAT YOU'RE DOING."); + } + + Configuration.deleteForeignLibraries = deleteForeignOutdatedLibs.isSelected(); + } } \ No newline at end of file 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 new file mode 100644 index 00000000..b6d603a9 --- /dev/null +++ b/src/main/java/the/bytecode/club/bytecodeviewer/gui/components/FileChooser.java @@ -0,0 +1,59 @@ +package the.bytecode.club.bytecodeviewer.gui.components; + +import the.bytecode.club.bytecodeviewer.Configuration; +import the.bytecode.club.bytecodeviewer.util.MiscUtils; + +import javax.swing.*; +import javax.swing.filechooser.FileFilter; +import java.io.File; +import java.util.HashSet; + +/** + * @author Konloch + * @since 6/25/2021 + */ +public class FileChooser extends JFileChooser +{ + private final File filePath; + private final String title; + private final String description; + private final String[] extensions; + private final HashSet extensionSet = new HashSet<>(); + + public FileChooser(File filePath, String title, String description, String... extensions) + { + this.filePath = filePath; + this.title = title; + this.description = description; + this.extensions = extensions; + + try { + if (filePath.exists()) + setSelectedFile(filePath); + } catch (Exception ignored) { } + + setDialogTitle(title); + setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); + setFileHidingEnabled(false); + setAcceptAllFileFilterUsed(false); + setFileFilter(new FileFilter() + { + @Override + public boolean accept(File f) + { + if (f.isDirectory()) + return true; + + if(extensions[0].equals("everything")) + return true; + + return extensionSet.contains(MiscUtils.extension(f.getAbsolutePath())); + } + + @Override + public String getDescription() { + return description; + } + }); + } +} diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginManager.java b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginManager.java index ae57d02b..531c09a9 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginManager.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/plugin/PluginManager.java @@ -30,7 +30,7 @@ import the.bytecode.club.bytecodeviewer.util.MiscUtils; /** * Supports loading of groovy, python or ruby scripts. - *

+ * * Only allows one plugin to be running at once. * * @author Konloch diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/NewlineOutputStream.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/NewlineOutputStream.java index 23ef898b..bca4eb4e 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/util/NewlineOutputStream.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/NewlineOutputStream.java @@ -25,9 +25,8 @@ import org.jetbrains.annotations.NotNull; ***************************************************************************/ /** - * Convert the various newline conventions to the local platform's - * newline convention.

- *

+ * Convert the various newline conventions to the local platform's newline convention. + * * This stream can be used with the Message.writeTo method to * generate a message that uses the local plaform's line terminator * for the purpose of (e.g.) saving the message to a local file.