diff --git a/ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/OpenAction.java b/ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/OpenAction.java index 2e1220a90d..b1c0d91922 100644 --- a/ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/OpenAction.java +++ b/ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/OpenAction.java @@ -1,5 +1,5 @@ /* - Copyright 2015-2021 Will Winder + Copyright 2015-2024 Will Winder This file is part of Universal Gcode Sender (UGS). @@ -19,25 +19,20 @@ This file is part of Universal Gcode Sender (UGS). package com.willwinder.ugs.nbp.core.actions; import com.willwinder.ugs.nbp.core.services.FileFilterService; -import com.willwinder.ugs.nbp.lib.EditorUtils; import com.willwinder.ugs.nbp.lib.lookup.CentralLookup; import com.willwinder.ugs.nbp.lib.services.LocalizingService; import com.willwinder.universalgcodesender.model.BackendAPI; -import com.willwinder.universalgcodesender.utils.GUIHelpers; +import org.apache.commons.lang3.StringUtils; import org.openide.awt.ActionID; import org.openide.awt.ActionReference; import org.openide.awt.ActionReferences; import org.openide.awt.ActionRegistration; -import org.openide.cookies.OpenCookie; -import org.openide.filesystems.FileUtil; -import org.openide.loaders.DataObject; -import org.openide.loaders.DataObjectNotFoundException; import org.openide.util.ImageUtilities; import org.openide.util.Lookup; import javax.swing.AbstractAction; -import javax.swing.JFileChooser; -import javax.swing.JFrame; +import java.awt.FileDialog; +import java.awt.Frame; import java.awt.event.ActionEvent; import java.io.File; @@ -64,7 +59,7 @@ public final class OpenAction extends AbstractAction { public static final String ICON_BASE = "resources/icons/open.svg"; private final transient FileFilterService fileFilterService; private final transient BackendAPI backend; - private final JFileChooser fileChooser; + private final FileDialog fileChooser; public OpenAction() { this(CentralLookup.getDefault().lookup(BackendAPI.class).getSettings().getLastOpenedFilename()); @@ -90,11 +85,10 @@ public boolean isEnabled() { @Override public void actionPerformed(ActionEvent e) { // Fetches all available file formats that UGS can open - fileFilterService.getFileFilters().forEach(fileChooser::addChoosableFileFilter); - - int returnVal = fileChooser.showOpenDialog(new JFrame()); - if (returnVal == JFileChooser.APPROVE_OPTION) { - File selectedFile = fileChooser.getSelectedFile(); + fileChooser.setFilenameFilter(fileFilterService.getFilenameFilters()); + fileChooser.setVisible(true); + if (StringUtils.isNotEmpty(fileChooser.getFile())) { + File selectedFile = new File(fileChooser.getDirectory() + File.separatorChar + fileChooser.getFile()); openFile(selectedFile); } } @@ -104,14 +98,11 @@ public void openFile(File selectedFile) { action.actionPerformed(null); } - private JFileChooser createFileChooser(String directory) { - JFileChooser chooser = new JFileChooser(directory); - chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); - chooser.setFileHidingEnabled(true); - chooser.setDialogType(JFileChooser.OPEN_DIALOG); - chooser.setAcceptAllFileFilterUsed(true); - return chooser; + private FileDialog createFileChooser(String directory) { + FileDialog fileDialog = new FileDialog((Frame)null); + fileDialog.setDirectory(directory); + fileDialog.setMode(FileDialog.LOAD); + fileDialog.setMultipleMode(false); + return fileDialog; } - - } diff --git a/ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/services/FileFilterService.java b/ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/services/FileFilterService.java index 0f1d415049..2f3a286d1e 100644 --- a/ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/services/FileFilterService.java +++ b/ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/services/FileFilterService.java @@ -1,5 +1,5 @@ /* - Copyright 2021 Will Winder + Copyright 2021-2024 Will Winder This file is part of Universal Gcode Sender (UGS). @@ -19,12 +19,16 @@ This file is part of Universal Gcode Sender (UGS). package com.willwinder.ugs.nbp.core.services; import com.willwinder.universalgcodesender.uielements.components.GcodeFileTypeFilter; +import org.apache.commons.io.filefilter.IOFileFilter; +import org.apache.commons.io.filefilter.OrFileFilter; import org.openide.util.lookup.ServiceProvider; import javax.swing.filechooser.FileFilter; -import java.util.Collection; +import java.io.File; +import java.io.FilenameFilter; import java.util.HashSet; import java.util.Set; +import java.util.stream.Collectors; /** * A file filter service that keeps track of all file types that we are allowed to open. @@ -45,7 +49,19 @@ public void registerFileFilter(FileFilter fileFilter) { fileFilters.add(fileFilter); } - public Collection getFileFilters() { - return fileFilters; + public FilenameFilter getFilenameFilters() { + return new OrFileFilter(fileFilters.stream() + .map(fileFilter -> new IOFileFilter() { + @Override + public boolean accept(File file) { + return fileFilter.accept(file); + } + + @Override + public boolean accept(File directory, String filename) { + return fileFilter.accept(new File(directory.getAbsolutePath() + File.separatorChar + filename)); + } + } ) + .collect(Collectors.toList())); } }