Skip to content

Commit

Permalink
Replace JFileChooser with native FileDialog to avoid permission issue…
Browse files Browse the repository at this point in the history
… with MacOS
  • Loading branch information
gabrielandrade2 committed Feb 22, 2023
1 parent 5032772 commit 67f47d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
33 changes: 15 additions & 18 deletions src/sociocom/fuzzyannotation/ui/FileSelectionUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;

public class FileSelectionUI {
Expand All @@ -43,7 +42,7 @@ public FileSelectionUI() {
JLabel title = new JLabel("Fuzzy Annotation Tool");
title.setFont(title.getFont().deriveFont(Font.BOLD, 20f));
titlePanel.add(title);
JLabel version = new JLabel("v0.2");
JLabel version = new JLabel("v0.2.1");
titlePanel.add(version);

JPanel panel = new JPanel();
Expand Down Expand Up @@ -135,20 +134,18 @@ private void openFile(ActionEvent e) {
Main.openWindow(WindowType.valueOf(command), file, autoSaveCheckBox.isSelected());
}

private static Path fileChooser() {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
FileFilter filter = new FileNameExtensionFilter("Text files", "txt", "xml");
jfc.addChoosableFileFilter(filter);
jfc.setFileFilter(filter);
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.setAcceptAllFileFilterUsed(false);
int returnValue = jfc.showOpenDialog(null);

if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
System.out.println(selectedFile.getAbsolutePath());
return selectedFile.toPath();
private Path fileChooser() {
FileDialog fd = new FileDialog(frame, "Choose a file", FileDialog.LOAD);
fd.setDirectory(FileSystemView.getFileSystemView().getHomeDirectory().toString());
fd.setFile("*.xml;*.txt");
fd.setFilenameFilter(
(File dir, String name) -> name.endsWith(".xml") || name.endsWith(".txt"));
fd.setVisible(true);
String directory = fd.getDirectory();
String file = fd.getFile();
if (directory == null || file == null) {
return null;
}
return null;
return Paths.get(directory).resolve(file);
}
}
25 changes: 11 additions & 14 deletions src/sociocom/fuzzyannotation/ui/annotation/BaseAnnotationUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -22,7 +23,6 @@
import java.util.prefs.Preferences;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
Expand All @@ -33,7 +33,6 @@
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
import javax.swing.text.Highlighter;
import javax.swing.text.NumberFormatter;
Expand Down Expand Up @@ -332,18 +331,16 @@ protected void updateAnnotationSpan(int offset) {
}

private void saveAction(ActionEvent e) {
JFileChooser jfc = new JFileChooser(
FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setDialogTitle("Specify a file to save");
jfc.setSelectedFile(new File("annotation.xml"));
jfc.setAcceptAllFileFilterUsed(false);
jfc.setFileFilter(new FileNameExtensionFilter("XML file", "xml"));
int returnValue = jfc.showSaveDialog(frame);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
System.out.println(selectedFile.getAbsolutePath());
save(selectedFile.getAbsolutePath(), true);
FileDialog fd = new FileDialog(frame, "Specify a file to save", FileDialog.SAVE);
fd.setDirectory(FileSystemView.getFileSystemView().getHomeDirectory().toString());
fd.setFile("annotation.xml");
fd.setVisible(true);
String directory = fd.getDirectory();
String file = fd.getFile();
if (directory == null || file == null) {
return;
}
save(Paths.get(directory).resolve(file).toAbsolutePath().toString(), true);
}

private void save(String path, boolean showMessage) {
Expand Down

0 comments on commit 67f47d9

Please sign in to comment.