Skip to content

Commit

Permalink
Attempt to fix file chooser dialog on MacOSX (winder#2441)
Browse files Browse the repository at this point in the history
  • Loading branch information
breiler authored Jan 28, 2024
1 parent 061670e commit 1690248
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2015-2021 Will Winder
Copyright 2015-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -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;

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


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Will Winder
Copyright 2021-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
Expand All @@ -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.
Expand All @@ -45,7 +49,19 @@ public void registerFileFilter(FileFilter fileFilter) {
fileFilters.add(fileFilter);
}

public Collection<FileFilter> 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()));
}
}

0 comments on commit 1690248

Please sign in to comment.