Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use native FileChooser instead of JFileChooser #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions gui/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("com.github.johnrengelman.shadow")
id("org.openjfx.javafxplugin") version "0.0.14"
}

dependencies {
Expand All @@ -24,4 +25,8 @@ tasks.build {

artifacts {
add("archives", tasks.shadowJar)
}

javafx {
modules = listOf("javafx.base", "javafx.graphics", "javafx.controls")
}
4 changes: 4 additions & 0 deletions gui/src/main/java/cz/speedy11/mcrpx/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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()) + ")");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* This file is part of MCRPX, licensed under the MIT License.
*
* Copyright (c) Michal Spišak (Speedy11CZ) <[email protected]>
*
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down Expand Up @@ -121,4 +121,4 @@ public ExtractorPanel() {
add(Box.createRigidArea(new Dimension(0, Main.COMPONENT_MARGIN)));
add(componentAlignedToCenter(controlPanel));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}