diff --git a/gui/build.gradle.kts b/gui/build.gradle.kts index 18f7a39..8319ca5 100644 --- a/gui/build.gradle.kts +++ b/gui/build.gradle.kts @@ -1,5 +1,6 @@ plugins { id("com.github.johnrengelman.shadow") + id("org.openjfx.javafxplugin") version "0.0.14" } dependencies { @@ -24,4 +25,8 @@ tasks.build { artifacts { add("archives", tasks.shadowJar) +} + +javafx { + modules = listOf("javafx.base", "javafx.graphics", "javafx.controls") } \ No newline at end of file diff --git a/gui/src/main/java/cz/speedy11/mcrpx/gui/Main.java b/gui/src/main/java/cz/speedy11/mcrpx/gui/Main.java index 82777ab..e1cea40 100644 --- a/gui/src/main/java/cz/speedy11/mcrpx/gui/Main.java +++ b/gui/src/main/java/cz/speedy11/mcrpx/gui/Main.java @@ -26,6 +26,7 @@ import cz.speedy11.mcrpx.gui.component.ExtractorPanel; import cz.speedy11.mcrpx.gui.util.ImageUtil; +import javafx.application.Platform; import javax.swing.*; import java.awt.*; @@ -49,6 +50,9 @@ public static void main(String[] args) { } catch (Exception ignored) { } + // Initialize toolkit + Platform.startup(() -> {}); + JFrame frame = new JFrame(); frame.setContentPane(new ExtractorPanel()); frame.setTitle("MCRPX (" + (Main.class.getPackage().getSpecificationVersion()) + ")"); diff --git a/gui/src/main/java/cz/speedy11/mcrpx/gui/component/DirectorySelectPanel.java b/gui/src/main/java/cz/speedy11/mcrpx/gui/component/DirectorySelectPanel.java new file mode 100644 index 0000000..4f7e451 --- /dev/null +++ b/gui/src/main/java/cz/speedy11/mcrpx/gui/component/DirectorySelectPanel.java @@ -0,0 +1,85 @@ +/* + * This file is part of MCRPX, licensed under the MIT License. + * + * Copyright (c) Michal Spišak (Speedy11CZ) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package cz.speedy11.mcrpx.gui.component; + + +import javafx.application.Platform; +import javafx.stage.DirectoryChooser; +import javafx.stage.FileChooser; + +import javax.swing.*; +import java.io.File; + +import static cz.speedy11.mcrpx.gui.util.ComponentUtil.componentAlignedToCenter; + +/** + * Panel used for selecting file or directory and displaying it. + * + * @author Michal Spišak (Speedy11CZ) + * @since 1.1.0 + */ +public class DirectorySelectPanel extends JPanel { + + private File file; + + /** + * Creates new panel with directory selector. + * + * @param text Text displayed before file selector + */ + public DirectorySelectPanel(String text) { + super(); + + this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); + + JTextField filePath = new JTextField(); + filePath.setEditable(false); + + JButton button = new JButton("..."); + + DirectoryChooser directoryChooser = new DirectoryChooser(); + button.addActionListener(e -> { + Platform.runLater(() -> { + file = directoryChooser.showDialog(null); + if (file != null) { + filePath.setText(file.getAbsolutePath()); + } + }); + }); + + this.add(componentAlignedToCenter(new JLabel(text))); + this.add(componentAlignedToCenter(filePath)); + this.add(componentAlignedToCenter(button)); + } + + /** + * Returns selected file. + * + * @return Selected file + */ + public File getFile() { + return file; + } +} diff --git a/gui/src/main/java/cz/speedy11/mcrpx/gui/component/ExtractorPanel.java b/gui/src/main/java/cz/speedy11/mcrpx/gui/component/ExtractorPanel.java index 794338d..2ca5dac 100644 --- a/gui/src/main/java/cz/speedy11/mcrpx/gui/component/ExtractorPanel.java +++ b/gui/src/main/java/cz/speedy11/mcrpx/gui/component/ExtractorPanel.java @@ -47,8 +47,8 @@ public ExtractorPanel() { this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); this.setBorder(BorderFactory.createEmptyBorder(Main.PANEL_MARGIN, Main.PANEL_MARGIN, Main.PANEL_MARGIN, Main.PANEL_MARGIN)); - FileSelectPanel inputFileSelectPanel = new FileSelectPanel("Input File: ", false); - FileSelectPanel outputDirectorySelectPanel = new FileSelectPanel("Output Directory: ", true); + FileSelectPanel inputFileSelectPanel = new FileSelectPanel("Input File: "); + DirectorySelectPanel outputDirectorySelectPanel = new DirectorySelectPanel("Output Directory: "); JButton btnExtract = new JButton("Extract"); btnExtract.addActionListener(e -> { @@ -121,4 +121,4 @@ public ExtractorPanel() { add(Box.createRigidArea(new Dimension(0, Main.COMPONENT_MARGIN))); add(componentAlignedToCenter(controlPanel)); } -} +} \ No newline at end of file diff --git a/gui/src/main/java/cz/speedy11/mcrpx/gui/component/FileSelectPanel.java b/gui/src/main/java/cz/speedy11/mcrpx/gui/component/FileSelectPanel.java index 646846c..c797109 100644 --- a/gui/src/main/java/cz/speedy11/mcrpx/gui/component/FileSelectPanel.java +++ b/gui/src/main/java/cz/speedy11/mcrpx/gui/component/FileSelectPanel.java @@ -24,9 +24,21 @@ package cz.speedy11.mcrpx.gui.component; + +import com.sun.javafx.util.Utils; +import cz.speedy11.mcrpx.gui.Main; +import javafx.application.Application; +import javafx.application.Platform; +import javafx.stage.FileChooser; +import javafx.stage.Stage; + import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; +import javax.swing.filechooser.FileSystemView; +import javax.swing.plaf.FileChooserUI; +import java.awt.*; import java.io.File; +import java.lang.annotation.Native; import static cz.speedy11.mcrpx.gui.util.ComponentUtil.componentAlignedToCenter; @@ -40,48 +52,48 @@ public class FileSelectPanel extends JPanel { private File file; + /** + * Returns selected file. + * + * @return Selected file + */ + public File getFile() { + return file; + } + /** * Creates new panel with file selector. * * @param text Text displayed before file selector - * @param directory If true, only directories can be selected */ - public FileSelectPanel(String text, boolean directory) { + public FileSelectPanel(String text) { super(); + this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); JTextField filePath = new JTextField(); filePath.setEditable(false); JButton button = new JButton("..."); + + FileChooser fileChooser = new FileChooser(); button.addActionListener(e -> { - JFileChooser fileChooser = new JFileChooser(); - fileChooser.setMultiSelectionEnabled(false); - fileChooser.setFileHidingEnabled(true); - fileChooser.setFileSelectionMode(directory ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_ONLY); - if (!directory) { - fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Minecraft Assets File (*.zip;*.jar)", "zip", "jar")); - fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Zip File Format (*.zip)", "zip", "jar")); - fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Jar File (*.jar)", "jar")); - fileChooser.setAcceptAllFileFilterUsed(true); - } - if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { - file = fileChooser.getSelectedFile(); - filePath.setText(file.getAbsolutePath()); - } + Platform.runLater(() -> { + fileChooser.getExtensionFilters().addAll( + new FileChooser.ExtensionFilter("Minecraft Assets File (*.zip;*.jar)", "*.zip", "*.jar"), + new FileChooser.ExtensionFilter("Zip File Format (*.zip)", "*.zip"), + new FileChooser.ExtensionFilter("Jar File (*.jar)", "*.jar") + ); + fileChooser.setSelectedExtensionFilter(fileChooser.getExtensionFilters().get(0)); + file = fileChooser.showOpenDialog(null); + if (file != null) { + filePath.setText(file.getAbsolutePath()); + } + }); }); this.add(componentAlignedToCenter(new JLabel(text))); this.add(componentAlignedToCenter(filePath)); this.add(componentAlignedToCenter(button)); } - - /** - * Returns selected file. - * - * @return Selected file - */ - public File getFile() { - return file; - } }