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

Fixed the file dialog for Linux and started migrating other dialogs #2447

Merged
merged 1 commit into from
Jan 30, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2024 Will Winder

This file is part of Universal Gcode Sender (UGS).

UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.universalgcodesender.uielements;

import com.willwinder.universalgcodesender.uielements.helpers.FilenameFilterAdapter;
import org.apache.commons.lang3.StringUtils;

import javax.swing.filechooser.FileFilter;
import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import java.util.Optional;

/**
* A generic file dialog base
*/
public class FileDialogBase extends FileDialog {

public FileDialogBase(String directory) {
super((Frame) null);

File directoryFile = new File(directory);
if (directoryFile.isFile()) {
directory = directoryFile.getParent();
}
setDirectory(directory);
}

@Override
public void setVisible(boolean visible) {
super.setVisible(visible);
setModal(true);
requestFocus();
}

/**
* Returns the selected file or an empty optional
*
* @return the selected file
*/
public Optional<File> getSelectedFile() {
String file = getFile();
if (StringUtils.isEmpty(file)) {
return Optional.empty();
}

return Optional.of(new File(getDirectory() + File.separatorChar + getFile()));
}

/**
* Sets a file filter
*
* @param fileFilter the file filter
*/
public void setFileFilter(FileFilter fileFilter) {
setFilenameFilter(new FilenameFilterAdapter(fileFilter));
}

/**
* Sets the selected file
*
* @param file the selected file
*/
public void setSelectedFile(File file) {
setDirectory(file.getParent());
setFile(file.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2024 Will Winder

This file is part of Universal Gcode Sender (UGS).

UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.universalgcodesender.uielements;

import java.awt.FileDialog;

public class FileOpenDialog extends FileDialogBase {
public FileOpenDialog(String directory) {
super(directory);
setMode(FileDialog.LOAD);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2024 Will Winder

This file is part of Universal Gcode Sender (UGS).

UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.universalgcodesender.uielements;

import java.awt.FileDialog;

public class FileSaveDialog extends FileDialogBase {
public FileSaveDialog(String directory) {
super(directory);
setMode(FileDialog.SAVE);
}

public FileSaveDialog() {
this("");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018 Will Winder
Copyright 2018-2024 Will Winder

This file is part of Universal Gcode Sender (UGS).

Expand All @@ -19,30 +19,26 @@ This file is part of Universal Gcode Sender (UGS).

package com.willwinder.universalgcodesender.uielements.components;

import com.willwinder.universalgcodesender.uielements.FileOpenDialog;
import org.apache.commons.lang3.StringUtils;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import java.io.File;

/**
* FileFilter which is limited to firmware settings files.
*/
public class FirmwareSettingsFileTypeFilter extends FileFilter {
public static JFileChooser getSettingsFileChooser() {
FirmwareSettingsFileTypeFilter filter = new FirmwareSettingsFileTypeFilter();

// Setup file browser with the last path used.
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setFileHidingEnabled(true);
fileChooser.addChoosableFileFilter(filter);
fileChooser.setAcceptAllFileFilterUsed(true);
fileChooser.setFileFilter(filter);

return fileChooser;
private static FileOpenDialog fileOpenDialog;

public static FileOpenDialog getSettingsFileChooser() {
if (fileOpenDialog == null) {
fileOpenDialog = new FileOpenDialog("");
fileOpenDialog.setFileFilter(new FirmwareSettingsFileTypeFilter());
}
return fileOpenDialog;
}

@Override
public boolean accept(File f) {
if (f.isDirectory()) {
Expand All @@ -51,7 +47,7 @@ public boolean accept(File f) {

return StringUtils.endsWith(f.getName(), ".settings");
}

@Override
public String getDescription() {
return "Firmware settings";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener;
import com.willwinder.universalgcodesender.i18n.Localization;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.uielements.FileOpenDialog;
import com.willwinder.universalgcodesender.uielements.FileSaveDialog;
import com.willwinder.universalgcodesender.uielements.components.FirmwareSettingsFileTypeFilter;
import com.willwinder.universalgcodesender.utils.SettingsComparator;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
Expand All @@ -57,9 +58,9 @@ public class FirmwareSettingsDialog extends JDialog implements IFirmwareSettings
private static final int COL_INDEX_SETTING = 0;
private static final Logger logger = Logger.getLogger(FirmwareSettingsDialog.class.getName());

private final IFirmwareSettings firmwareSettingsManager;
private final transient IFirmwareSettings firmwareSettingsManager;
private final FirmwareSettingsTableModel firmwareSettingsTableModel;
private final BackendAPI backend;
private final transient BackendAPI backend;

private JButton closeButton;
private JButton saveButton;
Expand Down Expand Up @@ -169,22 +170,20 @@ private void initComponents() {
}

private void importButtonActionPerformed() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FirmwareSettingsFileTypeFilter());
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
FirmwareSettingUtils.importSettings(fileChooser.getSelectedFile(), backend.getController().getFirmwareSettings());
}
FileOpenDialog fileChooser = FirmwareSettingsFileTypeFilter.getSettingsFileChooser();
fileChooser.setVisible(true);
fileChooser.getSelectedFile().ifPresent(file -> FirmwareSettingUtils.importSettings(file, backend.getController().getFirmwareSettings()));
}

private void exportButtonActionPerformed() {
JFileChooser fileChooser = new JFileChooser();
FileSaveDialog fileSaveDialog = new FileSaveDialog();
fileSaveDialog.setFileFilter(new FirmwareSettingsFileTypeFilter());

String date = LocalDate.now().format(DateTimeFormatter.ISO_DATE);
fileChooser.setSelectedFile(new File("firmware_" + date + ".settings"));
fileChooser.setFileFilter(new FirmwareSettingsFileTypeFilter());
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
FirmwareSettingUtils.exportSettings(file, backend.getController());
}
fileSaveDialog.setSelectedFile(new File("firmware_" + date + ".settings"));
fileSaveDialog.setVisible(true);

fileSaveDialog.getSelectedFile().ifPresent(file -> FirmwareSettingUtils.exportSettings(file, backend.getController()));
}

private void closeButtonActionPerformed() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2024 Will Winder

This file is part of Universal Gcode Sender (UGS).

UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.universalgcodesender.uielements.helpers;

import org.apache.commons.io.filefilter.IOFileFilter;

import javax.swing.filechooser.FileFilter;
import java.io.File;

/**
* A file filter adapter to be used together with apache commons
*
* @author Joacim Breiler
*/
public class FilenameFilterAdapter implements IOFileFilter {

private final FileFilter fileFilter;

public FilenameFilterAdapter(FileFilter fileFilter) {
this.fileFilter = fileFilter;
}

@Override
public boolean accept(File file) {
return fileFilter.accept(file);
}

@Override
public boolean accept(File dir, String name) {
return fileFilter.accept(new File(dir.getAbsolutePath() + File.separatorChar + name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.universalgcodesender.firmware.FirmwareSettingsFile;
import com.willwinder.universalgcodesender.i18n.Localization;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.uielements.FileOpenDialog;
import com.willwinder.universalgcodesender.uielements.components.FirmwareSettingsFileTypeFilter;
import com.willwinder.universalgcodesender.uielements.components.RoundedPanel;
import com.willwinder.universalgcodesender.uielements.helpers.ThemeColors;
Expand All @@ -33,8 +34,6 @@ This file is part of Universal Gcode Sender (UGS).
import org.openide.util.ImageUtilities;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
Expand Down Expand Up @@ -108,15 +107,15 @@ private void initComponents() {
labelNameValue = new JLabel("");
labelNameValue.setFont(labelNameValue.getFont().deriveFont(Font.BOLD, 16));

labelFirmware = new JLabel(Localization.getString("platform.plugin.setupwizard.import-settings.firmware"), JLabel.RIGHT);
labelFirmware = new JLabel(Localization.getString("platform.plugin.setupwizard.import-settings.firmware"), SwingConstants.RIGHT);
labelFirmware.setFont(labelFirmware.getFont().deriveFont(Font.BOLD));
labelFirmwareValue = new JLabel("");

labelCreatedBy = new JLabel(Localization.getString("platform.plugin.setupwizard.import-settings.created-by"), JLabel.RIGHT);
labelCreatedBy = new JLabel(Localization.getString("platform.plugin.setupwizard.import-settings.created-by"), SwingConstants.RIGHT);
labelCreatedBy.setFont(labelCreatedBy.getFont().deriveFont(Font.BOLD));
labelCreatedByValue = new JLabel("");

labelDate = new JLabel(Localization.getString("platform.plugin.setupwizard.import-settings.created-date"), JLabel.RIGHT);
labelDate = new JLabel(Localization.getString("platform.plugin.setupwizard.import-settings.created-date"), SwingConstants.RIGHT);
labelDate.setFont(labelDate.getFont().deriveFont(Font.BOLD));
labelDateValue = new JLabel("");

Expand All @@ -129,11 +128,9 @@ private void initComponents() {

buttonOpen = new JButton(Localization.getString("platform.plugin.setupwizard.import-settings.open-settings"));
buttonOpen.addActionListener(event -> {
JFileChooser fileChooser = FirmwareSettingsFileTypeFilter.getSettingsFileChooser();
int returnVal = fileChooser.showOpenDialog(new JFrame());
if (returnVal == JFileChooser.APPROVE_OPTION) {
loadSettingsFile(fileChooser.getSelectedFile());
}
FileOpenDialog fileChooser = FirmwareSettingsFileTypeFilter.getSettingsFileChooser();
fileChooser.setVisible(true);
fileChooser.getSelectedFile().ifPresent(this::loadSettingsFile);
});

buttonImport = new JButton(Localization.getString("platform.plugin.setupwizard.import-settings.import"));
Expand Down
Loading
Loading